GameNumberMappingController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Redis;
  6. use Illuminate\Support\Facades\Validator;
  7. class GameNumberMappingController
  8. {
  9. /**
  10. * 数字游戏映射列表
  11. */
  12. public function index(Request $request)
  13. {
  14. // 从 SQL Server 查询所有映射关系
  15. $mappings = DB::connection('write')
  16. ->table('agent.dbo.game_number_mapping')
  17. ->select('id', 'number', 'game_id')
  18. ->orderBy('number', 'asc')
  19. ->get();
  20. // 获取所有游戏ID
  21. $gameIds = $mappings->pluck('game_id')->unique()->toArray();
  22. // 从 MySQL 查询游戏信息
  23. $games = [];
  24. if (!empty($gameIds)) {
  25. $gamesData = DB::connection('mysql')
  26. ->table('webgame.games')
  27. ->whereIn('id', $gameIds)
  28. ->select('id', 'brand', 'title')
  29. ->get();
  30. // 转换为以 game_id 为键的数组
  31. foreach ($gamesData as $game) {
  32. $games[$game->id] = $game;
  33. }
  34. }
  35. // 合并数据
  36. foreach ($mappings as $mapping) {
  37. if (isset($games[$mapping->game_id])) {
  38. $mapping->brand = $games[$mapping->game_id]->brand;
  39. $mapping->title = $games[$mapping->game_id]->title;
  40. } else {
  41. $mapping->brand = null;
  42. $mapping->title = null;
  43. }
  44. }
  45. return view('admin.game_number_mapping.index', compact('mappings'));
  46. }
  47. /**
  48. * 添加映射页面
  49. */
  50. public function add(Request $request)
  51. {
  52. if ($request->isMethod('post')) {
  53. // 手动检查数字是否已存在
  54. $exists = DB::connection('write')
  55. ->table('agent.dbo.game_number_mapping')
  56. ->where('number', $request->number)
  57. ->exists();
  58. if ($exists) {
  59. return apiReturnFail('该数字已被使用');
  60. }
  61. // 从 MySQL 检查游戏是否存在
  62. $gameExists = DB::connection('mysql')
  63. ->table('webgame.games')
  64. ->where('id', $request->game_id)
  65. ->exists();
  66. if (!$gameExists) {
  67. return apiReturnFail('选择的游戏不存在');
  68. }
  69. $validator = Validator::make($request->all(), [
  70. 'number' => 'required|integer|min:0|max:9',
  71. 'game_id' => 'required|integer',
  72. ], [
  73. 'number.required' => '数字不能为空',
  74. 'number.integer' => '数字必须是0-9之间的整数',
  75. 'number.min' => '数字必须在0-9之间',
  76. 'number.max' => '数字必须在0-9之间',
  77. 'game_id.required' => '请选择游戏',
  78. ]);
  79. if ($validator->fails()) {
  80. return apiReturnFail($validator->errors()->first());
  81. }
  82. try {
  83. DB::connection('write')
  84. ->table('agent.dbo.game_number_mapping')
  85. ->insert([
  86. 'number' => $request->number,
  87. 'game_id' => $request->game_id,
  88. 'created_at' => now(),
  89. 'updated_at' => now(),
  90. ]);
  91. // 清除缓存
  92. $cacheKey = 'game_number_mapping:' . $request->number;
  93. Redis::del($cacheKey);
  94. return apiReturnSuc('添加成功');
  95. } catch (\Exception $e) {
  96. return apiReturnFail('添加失败:' . $e->getMessage());
  97. }
  98. }
  99. // 从 MySQL 获取所有游戏列表
  100. $games = DB::connection('mysql')
  101. ->table('webgame.games')
  102. ->select('id', 'brand', 'title')
  103. ->where('state', '>', 0)
  104. ->orderBy('brand')
  105. ->orderBy('title')
  106. ->get();
  107. // 获取已使用的数字
  108. $usedNumbers = DB::connection('write')
  109. ->table('agent.dbo.game_number_mapping')
  110. ->pluck('number')
  111. ->toArray();
  112. return view('admin.game_number_mapping.add', compact('games', 'usedNumbers'));
  113. }
  114. /**
  115. * 修改映射页面
  116. */
  117. public function update(Request $request, $id)
  118. {
  119. $mapping = DB::connection('write')
  120. ->table('agent.dbo.game_number_mapping')
  121. ->where('id', $id)
  122. ->first();
  123. if (!$mapping) {
  124. return apiReturnFail('映射不存在');
  125. }
  126. if ($request->isMethod('post')) {
  127. // 手动检查数字是否已被其他记录使用
  128. $exists = DB::connection('write')
  129. ->table('agent.dbo.game_number_mapping')
  130. ->where('number', $request->number)
  131. ->where('id', '!=', $id)
  132. ->exists();
  133. if ($exists) {
  134. return apiReturnFail('该数字已被使用');
  135. }
  136. // 从 MySQL 检查游戏是否存在
  137. $gameExists = DB::connection('mysql')
  138. ->table('webgame.games')
  139. ->where('id', $request->game_id)
  140. ->exists();
  141. if (!$gameExists) {
  142. return apiReturnFail('选择的游戏不存在');
  143. }
  144. $validator = Validator::make($request->all(), [
  145. 'number' => 'required|integer|min:0|max:9',
  146. 'game_id' => 'required|integer',
  147. ], [
  148. 'number.required' => '数字不能为空',
  149. 'number.integer' => '数字必须是0-9之间的整数',
  150. 'number.min' => '数字必须在0-9之间',
  151. 'number.max' => '数字必须在0-9之间',
  152. 'game_id.required' => '请选择游戏',
  153. ]);
  154. if ($validator->fails()) {
  155. return apiReturnFail($validator->errors()->first());
  156. }
  157. try {
  158. // 获取旧的 number,用于清除旧缓存
  159. $oldNumber = $mapping->number;
  160. DB::connection('write')
  161. ->table('agent.dbo.game_number_mapping')
  162. ->where('id', $id)
  163. ->update([
  164. 'number' => $request->number,
  165. 'game_id' => $request->game_id,
  166. 'updated_at' => now(),
  167. ]);
  168. // 清除旧的和新的缓存
  169. $oldCacheKey = 'game_number_mapping:' . $oldNumber;
  170. $newCacheKey = 'game_number_mapping:' . $request->number;
  171. Redis::del([$oldCacheKey, $newCacheKey]);
  172. return apiReturnSuc('修改成功');
  173. } catch (\Exception $e) {
  174. return apiReturnFail('修改失败:' . $e->getMessage());
  175. }
  176. }
  177. // 从 MySQL 获取游戏信息
  178. $game = DB::connection('mysql')
  179. ->table('webgame.games')
  180. ->where('id', $mapping->game_id)
  181. ->first();
  182. // 从 MySQL 获取所有游戏列表
  183. $games = DB::connection('mysql')
  184. ->table('webgame.games')
  185. ->select('id', 'brand', 'title')
  186. ->where('state', '>', 0)
  187. ->orderBy('brand')
  188. ->orderBy('title')
  189. ->get();
  190. // 获取已使用的数字(排除当前记录)
  191. $usedNumbers = DB::connection('write')
  192. ->table('agent.dbo.game_number_mapping')
  193. ->where('id', '!=', $id)
  194. ->pluck('number')
  195. ->toArray();
  196. return view('admin.game_number_mapping.update', compact('mapping', 'game', 'games', 'usedNumbers'));
  197. }
  198. /**
  199. * 删除映射
  200. */
  201. public function delete(Request $request, $id)
  202. {
  203. try {
  204. // 先获取要删除的记录,用于清除缓存
  205. $mapping = DB::connection('write')
  206. ->table('agent.dbo.game_number_mapping')
  207. ->where('id', $id)
  208. ->first();
  209. $deleted = DB::connection('write')
  210. ->table('agent.dbo.game_number_mapping')
  211. ->where('id', $id)
  212. ->delete();
  213. if ($deleted) {
  214. // 清除缓存
  215. if ($mapping) {
  216. $cacheKey = 'game_number_mapping:' . $mapping->number;
  217. Redis::del($cacheKey);
  218. }
  219. return apiReturnSuc('删除成功');
  220. } else {
  221. return apiReturnFail('删除失败,记录不存在');
  222. }
  223. } catch (\Exception $e) {
  224. return apiReturnFail('删除失败:' . $e->getMessage());
  225. }
  226. }
  227. }