HolidayWheelController.php 8.4 KB

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