TempDataController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. namespace App\Module\Game\AdminControllers;
  3. use App\Module\Game\AdminControllers\Actions\ViewTempDataAction;
  4. use App\Module\Game\Services\FundTempService;
  5. use App\Module\Game\Services\HouseTempService;
  6. use App\Module\Game\Services\ItemTempService;
  7. use App\Module\Game\Services\LandTempService;
  8. use App\Module\Game\Services\PetTempService;
  9. use App\Module\User\Models\User;
  10. use Dcat\Admin\Grid;
  11. use Dcat\Admin\Layout\Content;
  12. use Dcat\Admin\Widgets\Card;
  13. use Illuminate\Support\Facades\Log;
  14. use Spatie\RouteAttributes\Attributes\Get;
  15. use Spatie\RouteAttributes\Attributes\Prefix;
  16. use UCore\DcatAdmin\AdminController;
  17. /**
  18. * 暂存数据管理控制器
  19. *
  20. * 用于查看用户的各种暂存数据,包括物品、土地、房屋、宠物和资金等
  21. */
  22. class TempDataController extends AdminController
  23. {
  24. /**
  25. * 页面标题
  26. *
  27. * @var string
  28. */
  29. protected $title = '暂存数据管理';
  30. /**
  31. * 各种暂存服务
  32. */
  33. protected $itemTempService;
  34. protected $landTempService;
  35. protected $houseTempService;
  36. protected $petTempService;
  37. protected $fundTempService;
  38. /**
  39. * 构造函数
  40. */
  41. public function __construct()
  42. {
  43. $this->itemTempService = new ItemTempService();
  44. $this->landTempService = new LandTempService();
  45. $this->houseTempService = new HouseTempService();
  46. $this->petTempService = new PetTempService();
  47. $this->fundTempService = new FundTempService();
  48. }
  49. /**
  50. * 用户列表页面
  51. *
  52. * @param Content $content
  53. * @return Content
  54. */
  55. #[Get('game-temp-data', name: 'dcat.admin.game-temp-data')]
  56. public function index(Content $content)
  57. {
  58. return $content
  59. ->title($this->title)
  60. ->description('查看用户的暂存数据')
  61. ->body($this->grid());
  62. }
  63. /**
  64. * 查看指定用户的暂存数据
  65. *
  66. * @param int $userId 用户ID
  67. * @param Content $content
  68. * @return Content
  69. */
  70. #[Get('game-temp-data/{userId}', name: 'dcat.admin.game-temp-data.show')]
  71. public function show($userId, Content $content)
  72. {
  73. // 查找用户
  74. $user = User::find($userId);
  75. if (!$user) {
  76. admin_error('错误', '用户不存在');
  77. return redirect()->route('dcat.admin.game-temp-data');
  78. }
  79. // 用户信息卡片
  80. $userCard = new Card('用户信息', "ID: {$user->id} | 用户名: {$user->username}");
  81. // 获取所有暂存数据
  82. $itemChanges = $this->itemTempService->getUserItemChanges($userId);
  83. $landChanges = $this->landTempService->getUserLandChanges($userId);
  84. $landStatusChanges = $this->landTempService->getUserLandStatusChanges($userId);
  85. $houseChange = $this->houseTempService->getUserHouseChange($userId);
  86. $petUpdates = $this->petTempService->getUserPetUpdates($userId);
  87. $petStatus = $this->petTempService->getUserPetStatus($userId);
  88. $fundChanges = $this->fundTempService->getUserFundChanges($userId);
  89. // 检查是否有任何暂存数据
  90. $hasAnyData = !empty($itemChanges) || !empty($landChanges) || !empty($landStatusChanges)
  91. || $houseChange !== null || !empty($petUpdates) || !empty($petStatus)
  92. || !empty($fundChanges);
  93. // 构建统一的暂存数据视图
  94. $unifiedView = view('admin.temp-data.unified-view', [
  95. 'userId' => $userId,
  96. 'username' => $user->username,
  97. 'itemChanges' => $itemChanges,
  98. 'landChanges' => $landChanges,
  99. 'landStatusChanges' => $landStatusChanges,
  100. 'houseChange' => $houseChange,
  101. 'petUpdates' => $petUpdates,
  102. 'petStatus' => $petStatus,
  103. 'fundChanges' => $fundChanges,
  104. 'hasAnyData' => $hasAnyData
  105. ]);
  106. return $content
  107. ->title("用户 {$user->username} 的暂存数据")
  108. ->description('查看用户的所有暂存数据')
  109. ->body($userCard)
  110. ->body($unifiedView)
  111. ->body(view('admin.temp-data.clear-buttons', ['userId' => $userId]));
  112. }
  113. /**
  114. * 清除用户的物品暂存数据
  115. *
  116. * @param int $userId 用户ID
  117. * @return \Illuminate\Http\RedirectResponse
  118. */
  119. #[Get('game-temp-data/{userId}/clear-item', name: 'dcat.admin.game-temp-data.clear-item')]
  120. public function clearItem($userId)
  121. {
  122. try {
  123. // 查找用户
  124. $user = User::find($userId);
  125. if (!$user) {
  126. admin_error('错误', '用户不存在');
  127. return redirect()->route('dcat.admin.game-temp-data');
  128. }
  129. // 清除用户的物品暂存数据
  130. $result = $this->itemTempService->clearUserItemChanges($userId);
  131. if ($result) {
  132. admin_success('成功', "已清除用户 {$user->username} 的物品暂存数据");
  133. } else {
  134. admin_error('错误', "清除用户 {$user->username} 的物品暂存数据失败");
  135. }
  136. } catch (\Exception $e) {
  137. Log::error('清除用户物品暂存数据失败', [
  138. 'user_id' => $userId,
  139. 'error' => $e->getMessage(),
  140. ]);
  141. admin_error('错误', '清除用户物品暂存数据失败: ' . $e->getMessage());
  142. }
  143. return redirect()->route('dcat.admin.game-temp-data.show', ['userId' => $userId]);
  144. }
  145. /**
  146. * 清除用户的土地暂存数据
  147. *
  148. * @param int $userId 用户ID
  149. * @return \Illuminate\Http\RedirectResponse
  150. */
  151. #[Get('game-temp-data/{userId}/clear-land', name: 'dcat.admin.game-temp-data.clear-land')]
  152. public function clearLand($userId)
  153. {
  154. try {
  155. // 查找用户
  156. $user = User::find($userId);
  157. if (!$user) {
  158. admin_error('错误', '用户不存在');
  159. return redirect()->route('dcat.admin.game-temp-data');
  160. }
  161. // 清除用户的土地暂存数据
  162. $this->landTempService->clearUserAllLandTemp($userId);
  163. admin_success('成功', "已清除用户 {$user->username} 的土地暂存数据");
  164. } catch (\Exception $e) {
  165. Log::error('清除用户土地暂存数据失败', [
  166. 'user_id' => $userId,
  167. 'error' => $e->getMessage(),
  168. ]);
  169. admin_error('错误', '清除用户土地暂存数据失败: ' . $e->getMessage());
  170. }
  171. return redirect()->route('dcat.admin.game-temp-data.show', ['userId' => $userId]);
  172. }
  173. /**
  174. * 清除用户的房屋暂存数据
  175. *
  176. * @param int $userId 用户ID
  177. * @return \Illuminate\Http\RedirectResponse
  178. */
  179. #[Get('game-temp-data/{userId}/clear-house', name: 'dcat.admin.game-temp-data.clear-house')]
  180. public function clearHouse($userId)
  181. {
  182. try {
  183. // 查找用户
  184. $user = User::find($userId);
  185. if (!$user) {
  186. admin_error('错误', '用户不存在');
  187. return redirect()->route('dcat.admin.game-temp-data');
  188. }
  189. // 清除用户的房屋暂存数据
  190. $this->houseTempService->clearUserHouseChange($userId);
  191. admin_success('成功', "已清除用户 {$user->username} 的房屋暂存数据");
  192. } catch (\Exception $e) {
  193. Log::error('清除用户房屋暂存数据失败', [
  194. 'user_id' => $userId,
  195. 'error' => $e->getMessage(),
  196. ]);
  197. admin_error('错误', '清除用户房屋暂存数据失败: ' . $e->getMessage());
  198. }
  199. return redirect()->route('dcat.admin.game-temp-data.show', ['userId' => $userId]);
  200. }
  201. /**
  202. * 清除用户的宠物暂存数据
  203. *
  204. * @param int $userId 用户ID
  205. * @return \Illuminate\Http\RedirectResponse
  206. */
  207. #[Get('game-temp-data/{userId}/clear-pet', name: 'dcat.admin.game-temp-data.clear-pet')]
  208. public function clearPet($userId)
  209. {
  210. try {
  211. // 查找用户
  212. $user = User::find($userId);
  213. if (!$user) {
  214. admin_error('错误', '用户不存在');
  215. return redirect()->route('dcat.admin.game-temp-data');
  216. }
  217. // 清除用户的宠物暂存数据
  218. $this->petTempService->clearUserAllPetTemp($userId);
  219. admin_success('成功', "已清除用户 {$user->username} 的宠物暂存数据");
  220. } catch (\Exception $e) {
  221. Log::error('清除用户宠物暂存数据失败', [
  222. 'user_id' => $userId,
  223. 'error' => $e->getMessage(),
  224. ]);
  225. admin_error('错误', '清除用户宠物暂存数据失败: ' . $e->getMessage());
  226. }
  227. return redirect()->route('dcat.admin.game-temp-data.show', ['userId' => $userId]);
  228. }
  229. /**
  230. * 清除用户的资金暂存数据
  231. *
  232. * @param int $userId 用户ID
  233. * @return \Illuminate\Http\RedirectResponse
  234. */
  235. #[Get('game-temp-data/{userId}/clear-fund', name: 'dcat.admin.game-temp-data.clear-fund')]
  236. public function clearFund($userId)
  237. {
  238. try {
  239. // 查找用户
  240. $user = User::find($userId);
  241. if (!$user) {
  242. admin_error('错误', '用户不存在');
  243. return redirect()->route('dcat.admin.game-temp-data');
  244. }
  245. // 清除用户的资金暂存数据
  246. $this->fundTempService->clearUserFundChanges($userId);
  247. admin_success('成功', "已清除用户 {$user->username} 的资金暂存数据");
  248. } catch (\Exception $e) {
  249. Log::error('清除用户资金暂存数据失败', [
  250. 'user_id' => $userId,
  251. 'error' => $e->getMessage(),
  252. ]);
  253. admin_error('错误', '清除用户资金暂存数据失败: ' . $e->getMessage());
  254. }
  255. return redirect()->route('dcat.admin.game-temp-data.show', ['userId' => $userId]);
  256. }
  257. /**
  258. * 清除用户的所有暂存数据
  259. *
  260. * @param int $userId 用户ID
  261. * @return \Illuminate\Http\RedirectResponse
  262. */
  263. #[Get('game-temp-data/{userId}/clear-all', name: 'dcat.admin.game-temp-data.clear-all')]
  264. public function clearAll($userId)
  265. {
  266. try {
  267. // 查找用户
  268. $user = User::find($userId);
  269. if (!$user) {
  270. admin_error('错误', '用户不存在');
  271. return redirect()->route('dcat.admin.game-temp-data');
  272. }
  273. // 清除用户的所有暂存数据
  274. $this->itemTempService->clearUserItemChanges($userId);
  275. $this->landTempService->clearUserAllLandTemp($userId);
  276. $this->houseTempService->clearUserHouseChange($userId);
  277. $this->petTempService->clearUserAllPetTemp($userId);
  278. $this->fundTempService->clearUserFundChanges($userId);
  279. admin_success('成功', "已清除用户 {$user->username} 的所有暂存数据");
  280. } catch (\Exception $e) {
  281. Log::error('清除用户所有暂存数据失败', [
  282. 'user_id' => $userId,
  283. 'error' => $e->getMessage(),
  284. ]);
  285. admin_error('错误', '清除用户所有暂存数据失败: ' . $e->getMessage());
  286. }
  287. return redirect()->route('dcat.admin.game-temp-data.show', ['userId' => $userId]);
  288. }
  289. /**
  290. * 用户列表网格
  291. *
  292. * @return Grid
  293. */
  294. protected function grid()
  295. {
  296. return Grid::make(User::class, function (Grid $grid) {
  297. $grid->column('id', 'ID')->sortable();
  298. $grid->column('username', '用户名');
  299. $grid->column('created_at', '创建时间');
  300. $grid->column('updated_at', '更新时间');
  301. // 添加查看暂存数据操作
  302. $grid->actions(function (Grid\Displayers\Actions $actions) {
  303. // 禁用默认操作按钮
  304. $actions->disableDelete();
  305. $actions->disableEdit();
  306. $actions->disableQuickEdit();
  307. $actions->disableView();
  308. // 添加查看暂存数据操作
  309. $actions->append(new ViewTempDataAction());
  310. });
  311. // 禁用创建按钮
  312. $grid->disableCreateButton();
  313. // 禁用批量操作
  314. $grid->disableBatchActions();
  315. // 添加搜索
  316. $grid->filter(function (Grid\Filter $filter) {
  317. $filter->equal('id', '用户ID');
  318. $filter->like('username', '用户名');
  319. });
  320. });
  321. }
  322. }