ChristmasWheelController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. class ChristmasWheelController
  6. {
  7. /**
  8. * 圣诞大转盘配置页
  9. */
  10. public function index(Request $request)
  11. {
  12. // 活动配置(只取一条最新的)
  13. $activity = DB::connection('write')
  14. ->table('agent.dbo.christmas_wheel_activity')
  15. ->orderBy('id', 'desc')
  16. ->first();
  17. if ($activity) {
  18. $activity = (array)$activity;
  19. $activity['recharge_rules'] = $activity['recharge_rules'] ?? '';
  20. $activity['free_times'] = $activity['free_times'] ?? 0;
  21. } else {
  22. $activity = [
  23. 'id' => 0,
  24. 'start_time' => null,
  25. 'end_time' => null,
  26. 'status' => 0,
  27. 'icon_url' => '',
  28. 'button_url' => '',
  29. 'button_light_url' => '',
  30. 'lunzi_url' => '',
  31. 'dipan_url' => '',
  32. 'free_times' => 0,
  33. 'recharge_rules' => json_encode([
  34. ['amount' => 9.99, 'times' => 1],
  35. ['amount' => 19.99, 'times' => 3],
  36. ['amount' => 49.99, 'times' => 6],
  37. ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
  38. ];
  39. }
  40. // 解析充值规则为数组,供前端更友好编辑
  41. $rules = [];
  42. if (!empty($activity['recharge_rules'])) {
  43. $decodedRules = json_decode($activity['recharge_rules'], true);
  44. if (is_array($decodedRules)) {
  45. $rules = $decodedRules;
  46. }
  47. }
  48. if (empty($rules)) {
  49. $rules = [
  50. ['amount' => 9.99, 'times' => 1],
  51. ['amount' => 19.99, 'times' => 3],
  52. ['amount' => 49.99, 'times' => 6],
  53. ];
  54. }
  55. // 9 档配置
  56. $configs = DB::connection('write')
  57. ->table('agent.dbo.christmas_wheel_config')
  58. ->orderBy('sort_index', 'asc')
  59. ->orderBy('slot_index', 'asc')
  60. ->get();
  61. // 确保有 0-8 共 9 个档位
  62. $byIndex = [];
  63. foreach ($configs as $cfg) {
  64. $byIndex[(int)$cfg->slot_index] = $cfg;
  65. }
  66. $slots = [];
  67. for ($i = 0; $i < 9; $i++) {
  68. if (isset($byIndex[$i])) {
  69. $row = $byIndex[$i];
  70. $slots[] = [
  71. 'id' => $row->id,
  72. 'slot_index' => (int)$row->slot_index,
  73. 'reward' => (float)$row->reward,
  74. 'weight' => (int)$row->weight,
  75. 'sort_index' => (int)$row->sort_index,
  76. ];
  77. } else {
  78. $slots[] = [
  79. 'id' => 0,
  80. 'slot_index' => $i,
  81. 'reward' => 0,
  82. 'weight' => 0,
  83. 'sort_index' => $i,
  84. ];
  85. }
  86. }
  87. return view('admin.christmas_wheel.index', [
  88. 'activity' => $activity,
  89. 'slots' => $slots,
  90. 'rules' => $rules,
  91. ]);
  92. }
  93. /**
  94. * 保存活动配置 + 9档权重
  95. */
  96. public function update(Request $request)
  97. {
  98. // 活动配置
  99. $status = (int)$request->input('status', 0);
  100. $start_time = $request->input('start_time');
  101. $end_time = $request->input('end_time');
  102. $icon_url = $request->input('icon_url', '');
  103. $button_url = $request->input('button_url', '');
  104. $button_light_url = $request->input('button_light_url', '');
  105. $lunzi_url = $request->input('lunzi_url', '');
  106. $dipan_url = $request->input('dipan_url', '');
  107. $free_times = (int)$request->input('free_times', 0);
  108. $recharge_rules = $request->input('recharge_rules', '');
  109. // 支持 datetime-local 格式(YYYY-MM-DDTHH:MM)
  110. if ($start_time && strpos($start_time, 'T') !== false) {
  111. $start_time = str_replace('T', ' ', $start_time) . ':00';
  112. }
  113. if ($end_time && strpos($end_time, 'T') !== false) {
  114. $end_time = str_replace('T', ' ', $end_time) . ':00';
  115. }
  116. // 简单校验 JSON(来自前端构造)
  117. if ($recharge_rules) {
  118. $decoded = json_decode($recharge_rules, true);
  119. if (json_last_error() !== JSON_ERROR_NONE) {
  120. return apiReturnFail('充值规则 JSON 格式错误:' . json_last_error_msg());
  121. }
  122. $recharge_rules = json_encode($decoded, JSON_UNESCAPED_UNICODE);
  123. }
  124. DB::connection('write')->beginTransaction();
  125. try {
  126. // upsert 活动配置(只保留一条)
  127. $activity = DB::connection('write')
  128. ->table('agent.dbo.christmas_wheel_activity')
  129. ->orderBy('id', 'desc')
  130. ->lockForUpdate()
  131. ->first();
  132. $data = [
  133. 'start_time' => $start_time ?: null,
  134. 'end_time' => $end_time ?: null,
  135. 'status' => $status,
  136. 'icon_url' => $icon_url,
  137. 'button_url' => $button_url,
  138. 'button_light_url' => $button_light_url,
  139. 'lunzi_url' => $lunzi_url,
  140. 'dipan_url' => $dipan_url,
  141. 'free_times' => $free_times,
  142. 'recharge_rules' => $recharge_rules,
  143. 'updated_at' => now(),
  144. ];
  145. if ($activity) {
  146. DB::connection('write')
  147. ->table('agent.dbo.christmas_wheel_activity')
  148. ->where('id', $activity->id)
  149. ->update($data);
  150. } else {
  151. $data['created_at'] = now();
  152. DB::connection('write')
  153. ->table('agent.dbo.christmas_wheel_activity')
  154. ->insert($data);
  155. }
  156. // 9 档配置
  157. $slots = $request->input('slots', []);
  158. if (!empty($slots) && is_array($slots)) {
  159. foreach ($slots as $slotIndex => $row) {
  160. $slotIndex = (int)$slotIndex;
  161. if ($slotIndex < 0 || $slotIndex > 8) {
  162. continue;
  163. }
  164. $reward = isset($row['reward']) ? (float)$row['reward'] : 0;
  165. $weight = isset($row['weight']) ? (int)$row['weight'] : 0;
  166. $sortIndex = isset($row['sort_index']) ? (int)$row['sort_index'] : $slotIndex;
  167. $exists = DB::connection('write')
  168. ->table('agent.dbo.christmas_wheel_config')
  169. ->where('slot_index', $slotIndex)
  170. ->first();
  171. $cfgData = [
  172. 'reward' => $reward,
  173. 'weight' => $weight,
  174. 'sort_index' => $sortIndex,
  175. 'updated_at' => now(),
  176. ];
  177. if ($exists) {
  178. DB::connection('write')
  179. ->table('agent.dbo.christmas_wheel_config')
  180. ->where('slot_index', $slotIndex)
  181. ->update($cfgData);
  182. } else {
  183. $cfgData['slot_index'] = $slotIndex;
  184. $cfgData['created_at'] = now();
  185. DB::connection('write')
  186. ->table('agent.dbo.christmas_wheel_config')
  187. ->insert($cfgData);
  188. }
  189. }
  190. }
  191. DB::connection('write')->commit();
  192. } catch (\Throwable $e) {
  193. DB::connection('write')->rollBack();
  194. return apiReturnFail('保存失败:' . $e->getMessage());
  195. }
  196. return apiReturnSuc('保存成功');
  197. }
  198. /**
  199. * 圣诞大转盘 - 抽奖记录列表
  200. */
  201. public function history(Request $request)
  202. {
  203. $userId = (int)$request->input('user_id', 0);
  204. $gameId = (int)$request->input('game_id', 0);
  205. $startDate = $request->input('start_date', '');
  206. $endDate = $request->input('end_date', '');
  207. $query = DB::connection('write')
  208. ->table('agent.dbo.christmas_wheel_history as h')
  209. ->leftJoin('QPAccountsDB.dbo.AccountsInfo as ai', 'h.UserID', '=', 'ai.UserID')
  210. ->select(
  211. 'h.id',
  212. 'h.UserID',
  213. 'ai.GameID',
  214. 'ai.NickName',
  215. 'h.slot_index',
  216. 'h.reward',
  217. 'h.created_at'
  218. )
  219. ->orderBy('h.id', 'desc');
  220. if ($userId > 0) {
  221. $query->where('h.UserID', $userId);
  222. }
  223. if ($gameId > 0) {
  224. $query->where('ai.GameID', $gameId);
  225. }
  226. if ($startDate) {
  227. $query->where('h.created_at', '>=', $startDate . ' 00:00:00');
  228. }
  229. if ($endDate) {
  230. $query->where('h.created_at', '<=', $endDate . ' 23:59:59');
  231. }
  232. $list = $query->paginate(20);
  233. return view('admin.christmas_wheel.history', [
  234. 'list' => $list,
  235. 'user_id' => $userId,
  236. 'game_id' => $gameId,
  237. 'start_date' => $startDate,
  238. 'end_date' => $endDate,
  239. ]);
  240. }
  241. }