TempDataController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. $houseChange = $this->houseTempService->getUserHouseChange($userId);
  85. $petUpdates = $this->petTempService->getUserPetUpdates($userId);
  86. $petStatus = $this->petTempService->getUserPetStatus($userId);
  87. $fundChanges = $this->fundTempService->getUserFundChanges($userId);
  88. // 检查是否有任何暂存数据
  89. $hasAnyData = !empty($itemChanges) || !empty($landChanges)
  90. || $houseChange !== null || !empty($petUpdates) || !empty($petStatus)
  91. || !empty($fundChanges);
  92. // 构建统一的暂存数据视图
  93. $unifiedView = view('admin.temp-data.unified-view', [
  94. 'userId' => $userId,
  95. 'username' => $user->username,
  96. 'itemChanges' => $itemChanges,
  97. 'landChanges' => $landChanges,
  98. 'houseChange' => $houseChange,
  99. 'petUpdates' => $petUpdates,
  100. 'petStatus' => $petStatus,
  101. 'fundChanges' => $fundChanges,
  102. 'hasAnyData' => $hasAnyData
  103. ]);
  104. return $content
  105. ->title("用户 {$user->username} 的暂存数据")
  106. ->description('查看用户的所有暂存数据')
  107. ->body($userCard)
  108. ->body($unifiedView)
  109. ->body(view('admin.temp-data.clear-buttons', ['userId' => $userId]));
  110. }
  111. /**
  112. * 清除用户的物品暂存数据
  113. *
  114. * @param int $userId 用户ID
  115. * @return \Illuminate\Http\RedirectResponse
  116. */
  117. #[Get('game-temp-data/{userId}/clear-item', name: 'dcat.admin.game-temp-data.clear-item')]
  118. public function clearItem($userId)
  119. {
  120. try {
  121. // 查找用户
  122. $user = User::find($userId);
  123. if (!$user) {
  124. admin_error('错误', '用户不存在');
  125. return redirect()->route('dcat.admin.game-temp-data');
  126. }
  127. // 清除用户的物品暂存数据
  128. $result = $this->itemTempService->clearUserItemChanges($userId);
  129. if ($result) {
  130. admin_success('成功', "已清除用户 {$user->username} 的物品暂存数据");
  131. } else {
  132. admin_error('错误', "清除用户 {$user->username} 的物品暂存数据失败");
  133. }
  134. } catch (\Exception $e) {
  135. Log::error('清除用户物品暂存数据失败', [
  136. 'user_id' => $userId,
  137. 'error' => $e->getMessage(),
  138. ]);
  139. admin_error('错误', '清除用户物品暂存数据失败: ' . $e->getMessage());
  140. }
  141. return redirect()->route('dcat.admin.game-temp-data.show', ['userId' => $userId]);
  142. }
  143. /**
  144. * 清除用户的土地暂存数据
  145. *
  146. * @param int $userId 用户ID
  147. * @return \Illuminate\Http\RedirectResponse
  148. */
  149. #[Get('game-temp-data/{userId}/clear-land', name: 'dcat.admin.game-temp-data.clear-land')]
  150. public function clearLand($userId)
  151. {
  152. try {
  153. // 查找用户
  154. $user = User::find($userId);
  155. if (!$user) {
  156. admin_error('错误', '用户不存在');
  157. return redirect()->route('dcat.admin.game-temp-data');
  158. }
  159. // 清除用户的土地暂存数据
  160. $this->landTempService->clearUserAllLandTemp($userId);
  161. admin_success('成功', "已清除用户 {$user->username} 的土地暂存数据");
  162. } catch (\Exception $e) {
  163. Log::error('清除用户土地暂存数据失败', [
  164. 'user_id' => $userId,
  165. 'error' => $e->getMessage(),
  166. ]);
  167. admin_error('错误', '清除用户土地暂存数据失败: ' . $e->getMessage());
  168. }
  169. return redirect()->route('dcat.admin.game-temp-data.show', ['userId' => $userId]);
  170. }
  171. /**
  172. * 清除用户的房屋暂存数据
  173. *
  174. * @param int $userId 用户ID
  175. * @return \Illuminate\Http\RedirectResponse
  176. */
  177. #[Get('game-temp-data/{userId}/clear-house', name: 'dcat.admin.game-temp-data.clear-house')]
  178. public function clearHouse($userId)
  179. {
  180. try {
  181. // 查找用户
  182. $user = User::find($userId);
  183. if (!$user) {
  184. admin_error('错误', '用户不存在');
  185. return redirect()->route('dcat.admin.game-temp-data');
  186. }
  187. // 清除用户的房屋暂存数据
  188. $this->houseTempService->clearUserHouseChange($userId);
  189. admin_success('成功', "已清除用户 {$user->username} 的房屋暂存数据");
  190. } catch (\Exception $e) {
  191. Log::error('清除用户房屋暂存数据失败', [
  192. 'user_id' => $userId,
  193. 'error' => $e->getMessage(),
  194. ]);
  195. admin_error('错误', '清除用户房屋暂存数据失败: ' . $e->getMessage());
  196. }
  197. return redirect()->route('dcat.admin.game-temp-data.show', ['userId' => $userId]);
  198. }
  199. /**
  200. * 清除用户的宠物暂存数据
  201. *
  202. * @param int $userId 用户ID
  203. * @return \Illuminate\Http\RedirectResponse
  204. */
  205. #[Get('game-temp-data/{userId}/clear-pet', name: 'dcat.admin.game-temp-data.clear-pet')]
  206. public function clearPet($userId)
  207. {
  208. try {
  209. // 查找用户
  210. $user = User::find($userId);
  211. if (!$user) {
  212. admin_error('错误', '用户不存在');
  213. return redirect()->route('dcat.admin.game-temp-data');
  214. }
  215. // 清除用户的宠物暂存数据
  216. $this->petTempService->clearUserAllPetTemp($userId);
  217. admin_success('成功', "已清除用户 {$user->username} 的宠物暂存数据");
  218. } catch (\Exception $e) {
  219. Log::error('清除用户宠物暂存数据失败', [
  220. 'user_id' => $userId,
  221. 'error' => $e->getMessage(),
  222. ]);
  223. admin_error('错误', '清除用户宠物暂存数据失败: ' . $e->getMessage());
  224. }
  225. return redirect()->route('dcat.admin.game-temp-data.show', ['userId' => $userId]);
  226. }
  227. /**
  228. * 清除用户的资金暂存数据
  229. *
  230. * @param int $userId 用户ID
  231. * @return \Illuminate\Http\RedirectResponse
  232. */
  233. #[Get('game-temp-data/{userId}/clear-fund', name: 'dcat.admin.game-temp-data.clear-fund')]
  234. public function clearFund($userId)
  235. {
  236. try {
  237. // 查找用户
  238. $user = User::find($userId);
  239. if (!$user) {
  240. admin_error('错误', '用户不存在');
  241. return redirect()->route('dcat.admin.game-temp-data');
  242. }
  243. // 清除用户的资金暂存数据
  244. $this->fundTempService->clearUserFundChanges($userId);
  245. admin_success('成功', "已清除用户 {$user->username} 的资金暂存数据");
  246. } catch (\Exception $e) {
  247. Log::error('清除用户资金暂存数据失败', [
  248. 'user_id' => $userId,
  249. 'error' => $e->getMessage(),
  250. ]);
  251. admin_error('错误', '清除用户资金暂存数据失败: ' . $e->getMessage());
  252. }
  253. return redirect()->route('dcat.admin.game-temp-data.show', ['userId' => $userId]);
  254. }
  255. /**
  256. * 清除用户的所有暂存数据
  257. *
  258. * @param int $userId 用户ID
  259. * @return \Illuminate\Http\RedirectResponse
  260. */
  261. #[Get('game-temp-data/{userId}/clear-all', name: 'dcat.admin.game-temp-data.clear-all')]
  262. public function clearAll($userId)
  263. {
  264. try {
  265. // 查找用户
  266. $user = User::find($userId);
  267. if (!$user) {
  268. admin_error('错误', '用户不存在');
  269. return redirect()->route('dcat.admin.game-temp-data');
  270. }
  271. // 清除用户的所有暂存数据
  272. $this->itemTempService->clearUserItemChanges($userId);
  273. $this->landTempService->clearUserAllLandTemp($userId);
  274. $this->houseTempService->clearUserHouseChange($userId);
  275. $this->petTempService->clearUserAllPetTemp($userId);
  276. $this->fundTempService->clearUserFundChanges($userId);
  277. admin_success('成功', "已清除用户 {$user->username} 的所有暂存数据");
  278. } catch (\Exception $e) {
  279. Log::error('清除用户所有暂存数据失败', [
  280. 'user_id' => $userId,
  281. 'error' => $e->getMessage(),
  282. ]);
  283. admin_error('错误', '清除用户所有暂存数据失败: ' . $e->getMessage());
  284. }
  285. return redirect()->route('dcat.admin.game-temp-data.show', ['userId' => $userId]);
  286. }
  287. /**
  288. * 用户列表网格
  289. *
  290. * @return Grid
  291. */
  292. protected function grid()
  293. {
  294. return Grid::make(User::class, function (Grid $grid) {
  295. $grid->column('id', 'ID')->sortable();
  296. $grid->column('username', '用户名');
  297. $grid->column('created_at', '创建时间');
  298. $grid->column('updated_at', '更新时间');
  299. // 添加查看暂存数据操作
  300. $grid->actions(function (Grid\Displayers\Actions $actions) {
  301. // 禁用默认操作按钮
  302. $actions->disableDelete();
  303. $actions->disableEdit();
  304. $actions->disableQuickEdit();
  305. $actions->disableView();
  306. // 添加查看暂存数据操作
  307. $actions->append(new ViewTempDataAction());
  308. });
  309. // 禁用创建按钮
  310. $grid->disableCreateButton();
  311. // 禁用批量操作
  312. $grid->disableBatchActions();
  313. // 添加搜索
  314. $grid->filter(function (Grid\Filter $filter) {
  315. $filter->equal('id', '用户ID');
  316. $filter->like('username', '用户名');
  317. });
  318. });
  319. }
  320. }