GameNumberMappingController.php 7.8 KB

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