FarmUserSummaryController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <?php
  2. namespace App\Module\Game\AdminControllers;
  3. use App\Module\Farm\Enums\GROWTH_STAGE;
  4. use App\Module\Farm\Models\FarmCrop;
  5. use App\Module\Farm\Models\FarmGodBuff;
  6. use App\Module\Farm\Models\FarmHouseConfig;
  7. use App\Module\Farm\Models\FarmLand;
  8. use App\Module\Farm\Models\FarmLandType;
  9. use App\Module\Farm\Models\FarmSeed;
  10. use App\Module\Farm\Models\FarmUser;
  11. use App\Module\Farm\Services\BuffService;
  12. use App\Module\Farm\Services\FarmService;
  13. use App\Module\Farm\Services\HouseService;
  14. use App\Module\Farm\Services\LandService;
  15. use App\Module\Fund\Models\FundModel;
  16. use App\Module\Fund\Services\AccountService;
  17. use App\Module\GameItems\Enums\ITEM_TYPE;
  18. use App\Module\GameItems\Models\Item;
  19. use App\Module\GameItems\Models\ItemUser;
  20. use App\Module\GameItems\Services\ItemService;
  21. use App\Module\User\Models\User;
  22. use Dcat\Admin\Grid;
  23. use Dcat\Admin\Layout\Content;
  24. use Dcat\Admin\Layout\Row;
  25. use Dcat\Admin\Widgets\Card;
  26. use Dcat\Admin\Widgets\Table;
  27. use Dcat\Admin\Widgets\Box;
  28. use Dcat\Admin\Widgets\Alert;
  29. use Illuminate\Support\Facades\DB;
  30. use Spatie\RouteAttributes\Attributes\Get;
  31. use Spatie\RouteAttributes\Attributes\Resource;
  32. use UCore\DcatAdmin\AdminController;
  33. /**
  34. * 农场用户信息汇总控制器
  35. *
  36. * 用于展示用户的农场信息汇总,包括房屋、土地、作物、物品和代币信息
  37. */
  38. #[Resource('farm-user-summary', names: 'dcat.admin.farm-user-summary')]
  39. class FarmUserSummaryController extends AdminController
  40. {
  41. /**
  42. * 页面标题
  43. *
  44. * @var string
  45. */
  46. protected $title = '农场用户信息汇总';
  47. /**
  48. * 用户列表页面
  49. *
  50. * @param Content $content
  51. * @return Content
  52. */
  53. public function index(Content $content)
  54. {
  55. return $content
  56. ->title($this->title)
  57. ->description('查看用户的农场信息汇总')
  58. ->body($this->grid());
  59. }
  60. /**
  61. * 查看指定用户的农场信息汇总
  62. *
  63. * @param int $userId 用户ID
  64. * @param Content $content
  65. * @return Content
  66. */
  67. #[Get('farm-user-summary/{userId}', name: 'dcat.admin.farm-user-summary.show')]
  68. public function show($userId, Content $content)
  69. {
  70. // 查找用户
  71. $user = User::find($userId);
  72. if (!$user) {
  73. admin_error('错误', '用户不存在');
  74. return redirect()->route('dcat.admin.farm-user-summary');
  75. }
  76. return $content
  77. ->title($this->title)
  78. ->description("用户 {$user->username}(ID: {$user->id})的农场信息汇总")
  79. ->body(function (Row $row) use ($user) {
  80. // 第一行:用户基本信息和房屋信息
  81. $row->column(6, $this->userInfoCard($user));
  82. $row->column(6, $this->houseInfoCard($user->id));
  83. // 第二行:土地信息和作物信息
  84. $row->column(12, $this->landInfoCard($user->id));
  85. // 第三行:物品信息
  86. $row->column(12, $this->itemInfoCard($user->id));
  87. // 第四行:代币信息
  88. $row->column(12, $this->fundInfoCard($user->id));
  89. // 第五行:神像buff信息
  90. $row->column(12, $this->buffInfoCard($user->id));
  91. });
  92. }
  93. /**
  94. * 用户基本信息卡片
  95. *
  96. * @param User $user 用户对象
  97. * @return Card
  98. */
  99. protected function userInfoCard(User $user)
  100. {
  101. $userInfo = $user->info;
  102. $avatar = $userInfo ? $userInfo->avatar : '';
  103. $nickname = $userInfo ? $userInfo->nickname : '';
  104. $content = <<<HTML
  105. <div class="row">
  106. <div class="col-md-4">
  107. <img src="{$avatar}" class="img-fluid rounded" style="max-width: 100px;" onerror="this.src='https://via.placeholder.com/100'">
  108. </div>
  109. <div class="col-md-8">
  110. <p><strong>用户ID:</strong>{$user->id}</p>
  111. <p><strong>用户名:</strong>{$user->username}</p>
  112. <p><strong>昵称:</strong>{$nickname}</p>
  113. <p><strong>注册时间:</strong>{$user->created_at}</p>
  114. </div>
  115. </div>
  116. HTML;
  117. return new Card('用户基本信息', $content);
  118. }
  119. /**
  120. * 房屋信息卡片
  121. *
  122. * @param int $userId 用户ID
  123. * @return Card
  124. */
  125. protected function houseInfoCard($userId)
  126. {
  127. // 获取用户的农场信息
  128. $farmUser = FarmUser::where('user_id', $userId)->first();
  129. if (!$farmUser) {
  130. return new Card('房屋信息', new Alert('warning', '该用户没有农场信息'));
  131. }
  132. // 获取房屋配置信息
  133. $houseConfig = FarmHouseConfig::where('level', $farmUser->house_level)->first();
  134. $content = <<<HTML
  135. <div class="row">
  136. <div class="col-md-12">
  137. <p><strong>房屋等级:</strong>{$farmUser->house_level}</p>
  138. <p><strong>最后升级时间:</strong>{$farmUser->last_upgrade_time}</p>
  139. <p><strong>产出加成:</strong>{$houseConfig->output_bonus}</p>
  140. <p><strong>特殊土地上限:</strong>{$houseConfig->special_land_limit}</p>
  141. <p><strong>可用土地数量:</strong>{$houseConfig->available_lands}</p>
  142. HTML;
  143. // 如果有降级天数,显示降级信息
  144. if ($houseConfig->downgrade_days) {
  145. $content .= "<p><strong>降级天数:</strong>{$houseConfig->downgrade_days}</p>";
  146. }
  147. $content .= <<<HTML
  148. </div>
  149. </div>
  150. <div class="row mt-2">
  151. <div class="col-md-12">
  152. <a href="javascript:void(0);" class="btn btn-sm btn-primary" onclick="window.open('farm-house-configs', '_blank')">查看房屋配置</a>
  153. </div>
  154. </div>
  155. HTML;
  156. return new Card('房屋信息', $content);
  157. }
  158. /**
  159. * 土地信息卡片
  160. *
  161. * @param int $userId 用户ID
  162. * @return Card
  163. */
  164. protected function landInfoCard($userId)
  165. {
  166. // 获取用户的土地信息
  167. /**
  168. * @var FarmLand $land
  169. */
  170. $lands = FarmLand::with([ 'landType', 'crop.seed' ])
  171. ->where('user_id', $userId)
  172. ->get();
  173. if ($lands->isEmpty()) {
  174. return new Card('土地信息', new Alert('warning', '该用户没有土地信息'));
  175. }
  176. // 土地类型统计
  177. $landTypeStats = $lands->groupBy('land_type')->map->count();
  178. // 土地状态统计
  179. $landStatusStats = $lands->groupBy('status')->map->count();
  180. // 创建土地类型和状态的统计表格
  181. $statsContent = '<div class="row">';
  182. // 土地类型统计
  183. $statsContent .= '<div class="col-md-6">';
  184. $statsContent .= '<h5>土地类型统计</h5>';
  185. $statsContent .= '<table class="table table-sm table-bordered">';
  186. $statsContent .= '<thead><tr><th>土地类型</th><th>数量</th></tr></thead>';
  187. $statsContent .= '<tbody>';
  188. $landTypeNames = [
  189. 1 => '普通土地',
  190. 2 => '红土地',
  191. 3 => '黑土地',
  192. 4 => '金土地',
  193. 5 => '蓝土地',
  194. 6 => '紫土地',
  195. ];
  196. foreach ($landTypeStats as $typeId => $count) {
  197. $typeName = $landTypeNames[$typeId] ?? "类型{$typeId}";
  198. $statsContent .= "<tr><td>{$typeName}</td><td>{$count}</td></tr>";
  199. }
  200. $statsContent .= '</tbody></table></div>';
  201. // 土地状态统计
  202. $statsContent .= '<div class="col-md-6">';
  203. $statsContent .= '<h5>土地状态统计</h5>';
  204. $statsContent .= '<table class="table table-sm table-bordered">';
  205. $statsContent .= '<thead><tr><th>土地状态</th><th>数量</th></tr></thead>';
  206. $statsContent .= '<tbody>';
  207. $landStatusNames = [
  208. 0 => '空闲',
  209. 1 => '种植中',
  210. 2 => '灾害',
  211. 3 => '可收获',
  212. 4 => '枯萎',
  213. ];
  214. foreach ($landStatusStats as $statusId => $count) {
  215. $statusName = $landStatusNames[$statusId] ?? "状态{$statusId}";
  216. $statsContent .= "<tr><td>{$statusName}</td><td>{$count}</td></tr>";
  217. }
  218. $statsContent .= '</tbody></table></div>';
  219. $statsContent .= '</div>';
  220. // 创建土地详情表格
  221. $headers = [ 'ID','位置', '土地类型', '状态', '种植作物', '种植时间', '生长阶段' ];
  222. $rows = [];
  223. foreach ($lands as $land) {
  224. $landType = $land->landType ? $land->landType->name : "类型{$land->land_type}";
  225. $status = $landStatusNames[$land->status] ?? "状态{$land->status}";
  226. $crop = $land->crop;
  227. $cropInfo = '无';
  228. $plantTime = '';
  229. $growthStage = '';
  230. if ($crop) {
  231. $seedName = $crop->seed ? $crop->seed->name : "种子{$crop->seed_id}";
  232. $cropInfo = $seedName;
  233. $plantTime = $crop->plant_time;
  234. $growthStage = $this->getGrowthStageName($crop->growth_stage);
  235. }
  236. $rows[] = [
  237. $land->id,
  238. $land->position,
  239. $landType,
  240. $status,
  241. $cropInfo,
  242. $plantTime,
  243. $growthStage,
  244. ];
  245. }
  246. $table = new Table($headers, $rows);
  247. $content = $statsContent . '<div class="mt-3">' . $table->render() . '</div>';
  248. $content .= <<<HTML
  249. <div class="row mt-2">
  250. <div class="col-md-12">
  251. <a href="javascript:void(0);" class="btn btn-sm btn-primary" onclick="window.open('farm-lands?user_id={$userId}', '_blank')">查看土地详情</a>
  252. </div>
  253. </div>
  254. HTML;
  255. return new Card('土地信息', $content);
  256. }
  257. /**
  258. * 获取生长阶段名称
  259. *
  260. * @param int $stage 生长阶段值
  261. * @return string 生长阶段名称
  262. */
  263. protected function getGrowthStageName(GROWTH_STAGE $stage)
  264. {
  265. $stageNames = GROWTH_STAGE::getValueDescription();
  266. return $stageNames[$stage->valueInt()] ?? "阶段{$stage}";
  267. }
  268. /**
  269. * 物品信息卡片
  270. *
  271. * @param int $userId 用户ID
  272. * @return Card
  273. */
  274. protected function itemInfoCard($userId)
  275. {
  276. // 获取用户的物品信息
  277. $items = ItemUser::with('item')
  278. ->where('user_id', $userId)
  279. ->orderBy('quantity', 'desc')
  280. ->limit(20)
  281. ->get();
  282. if ($items->isEmpty()) {
  283. return new Card('物品信息', new Alert('warning', '该用户没有物品信息'));
  284. }
  285. // 创建物品表格
  286. $headers = [ '物品ID', '物品名称', '数量', '物品类型', '过期时间' ];
  287. $rows = [];
  288. foreach ($items as $item) {
  289. $itemName = $item->item ? $item->item->name : "物品{$item->item_id}";
  290. $itemType = $item->item ? $this->getItemTypeName($item->item->type) : '';
  291. $rows[] = [
  292. $item->item_id,
  293. $itemName,
  294. $item->quantity,
  295. $itemType,
  296. $item->expire_at ?: '永久',
  297. ];
  298. }
  299. $table = new Table($headers, $rows);
  300. // 获取用户物品总数
  301. $totalCount = ItemUser::where('user_id', $userId)->count();
  302. $content = <<<HTML
  303. <div class="alert alert-info">
  304. 用户共有 {$totalCount} 种物品,下表显示数量最多的前20种物品
  305. </div>
  306. {$table->render()}
  307. <div class="row mt-2">
  308. <div class="col-md-12">
  309. <a href="javascript:void(0);" class="btn btn-sm btn-primary" onclick="window.open('game-items-users?user_id={$userId}', '_blank')">查看物品详情</a>
  310. </div>
  311. </div>
  312. HTML;
  313. return new Card('物品信息', $content);
  314. }
  315. /**
  316. * 获取物品类型名称
  317. *
  318. * @param int $type 物品类型值
  319. * @return string 物品类型名称
  320. */
  321. protected function getItemTypeName(ITEM_TYPE $type)
  322. {
  323. $typeNames = ITEM_TYPE::getValueDescription();
  324. return $typeNames[$type->valueInt()] ?? "类型{$type}";
  325. }
  326. /**
  327. * 代币信息卡片
  328. *
  329. * @param int $userId 用户ID
  330. * @return Card
  331. */
  332. protected function fundInfoCard($userId)
  333. {
  334. // 获取用户的代币信息
  335. $funds = FundModel::where('user_id', $userId)->get();
  336. if ($funds->isEmpty()) {
  337. return new Card('代币信息', new Alert('warning', '该用户没有代币信息'));
  338. }
  339. // 获取资金类型名称映射
  340. $fundNames = AccountService::getFundsDesc();
  341. // 创建代币表格
  342. $headers = [ '账户ID', '账户名称', '余额', '更新时间' ];
  343. $rows = [];
  344. foreach ($funds as $fund) {
  345. $fundName = $fundNames[$fund->fund_id->value()] ?? "账户{$fund->fund_id->value()}";
  346. $balance = $fund->balance;
  347. $rows[] = [
  348. $fund->fund_id->value(),
  349. $fundName,
  350. $balance,
  351. $fund->update_time ? date('Y-m-d H:i:s', $fund->update_time) : '',
  352. ];
  353. }
  354. $table = new Table($headers, $rows);
  355. $content = <<<HTML
  356. {$table->render()}
  357. <div class="row mt-2">
  358. <div class="col-md-12">
  359. <a href="javascript:void(0);" class="btn btn-sm btn-primary" onclick="window.open('fund-accounts?user_id={$userId}', '_blank')">查看账户详情</a>
  360. </div>
  361. </div>
  362. HTML;
  363. return new Card('代币信息', $content);
  364. }
  365. /**
  366. * 神像buff信息卡片
  367. *
  368. * @param int $userId 用户ID
  369. * @return Card
  370. */
  371. protected function buffInfoCard($userId)
  372. {
  373. // 获取用户的神像buff信息
  374. $buffs = FarmGodBuff::where('user_id', $userId)
  375. ->orderBy('expire_time', 'desc')
  376. ->get();
  377. if ($buffs->isEmpty()) {
  378. return new Card('神像加持信息', new Alert('warning', '该用户没有神像加持信息'));
  379. }
  380. // 创建buff表格
  381. $headers = [ '加持类型', '过期时间', '状态' ];
  382. $rows = [];
  383. $buffTypeNames = [
  384. 1 => '丰收之神',
  385. 2 => '雨露之神',
  386. 3 => '屠草之神',
  387. 4 => '拭虫之神',
  388. ];
  389. foreach ($buffs as $buff) {
  390. $buffType = $buffTypeNames[$buff->buff_type] ?? "类型{$buff->buff_type}";
  391. $isActive = now()->lt($buff->expire_time);
  392. $status = $isActive ? '<span class="badge badge-success">生效中</span>' : '<span class="badge badge-secondary">已过期</span>';
  393. $rows[] = [
  394. $buffType,
  395. $buff->expire_time,
  396. $status,
  397. ];
  398. }
  399. $table = new Table($headers, $rows);
  400. $content = $table->render();
  401. return new Card('神像加持信息', $content);
  402. }
  403. /**
  404. * 用户列表网格
  405. *
  406. * @return Grid
  407. */
  408. protected function grid()
  409. {
  410. return Grid::make(User::with([ 'info', 'farmUser' ]), function (Grid $grid) {
  411. $grid->column('id', 'ID')->sortable();
  412. // 用户基本信息
  413. $grid->column('username', '用户名');
  414. $grid->column('info.nickname', '昵称');
  415. // 农场信息
  416. $grid->column('farmUser.house_level', '房屋等级')->sortable();
  417. // 土地统计
  418. $grid->column('id', '土地统计')->display(function ($userId) {
  419. $lands = FarmLand::where('user_id', $userId)->get();
  420. if ($lands->isEmpty()) {
  421. return '<span class="text-muted">无土地</span>';
  422. }
  423. $landTypeStats = $lands->groupBy('land_type')->map->count();
  424. $totalLands = $lands->count();
  425. $html = "<div>总计: {$totalLands}块土地</div>";
  426. $landTypeNames = [
  427. 1 => '普通土地',
  428. 2 => '红土地',
  429. 3 => '黑土地',
  430. 4 => '金土地',
  431. 5 => '蓝土地',
  432. 6 => '紫土地',
  433. ];
  434. foreach ($landTypeStats as $typeId => $count) {
  435. $typeName = $landTypeNames[$typeId] ?? "类型{$typeId}";
  436. $html .= "<div>{$typeName}: {$count}块</div>";
  437. }
  438. return $html;
  439. });
  440. // 作物统计
  441. $grid->column('id', '作物统计')->display(function ($userId) {
  442. $crops = FarmCrop::where('user_id', $userId)->count();
  443. return $crops > 0 ? "{$crops}种作物" : '<span class="text-muted">无作物</span>';
  444. });
  445. // 物品统计
  446. $grid->column('id', '物品统计')->display(function ($userId) {
  447. $itemCount = ItemUser::where('user_id', $userId)->count();
  448. return $itemCount > 0 ? "{$itemCount}种物品" : '<span class="text-muted">无物品</span>';
  449. });
  450. // 代币统计
  451. $grid->column('id', '代币统计')->display(function ($userId) {
  452. $fundCount = FundModel::where('user_id', $userId)->count();
  453. return $fundCount > 0 ? "{$fundCount}个账户" : '<span class="text-muted">无账户</span>';
  454. });
  455. $grid->column('created_at', '创建时间')->sortable();
  456. // 添加查看详情操作
  457. $grid->actions(function (Grid\Displayers\Actions $actions) {
  458. // 禁用默认操作按钮
  459. $actions->disableDelete();
  460. $actions->disableEdit();
  461. $actions->disableQuickEdit();
  462. // 修改查看按钮,使其打开详情页
  463. $actions->append('<a href="' . admin_url('farm-user-summary/' . $actions->getKey()) . '" class="btn btn-sm btn-primary">查看详情</a>');
  464. });
  465. // 禁用创建按钮
  466. $grid->disableCreateButton();
  467. // 禁用批量操作
  468. $grid->disableBatchActions();
  469. // 添加搜索
  470. $grid->filter(function (Grid\Filter $filter) {
  471. $filter->equal('id', '用户ID');
  472. $filter->like('username', '用户名');
  473. $filter->like('info.nickname', '昵称');
  474. $filter->equal('farmUser.house_level', '房屋等级');
  475. });
  476. });
  477. }
  478. }