HolidayWheelService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. namespace App\Services;
  3. use App\Game\Services\OuroGameService;
  4. use App\Http\helper\NumConfig;
  5. use App\Utility\SetNXLock;
  6. use App\Util;
  7. use App\Facade\TableName;
  8. use Illuminate\Support\Facades\DB;
  9. class HolidayWheelService
  10. {
  11. /**
  12. * 获取节假日大转盘配置 + 用户状态
  13. */
  14. public function getWheelInfo(int $userId): array
  15. {
  16. $activity = DB::connection('write')
  17. ->table('agent.dbo.holiday_wheel_activity')
  18. ->orderBy('id', 'desc')
  19. ->first();
  20. $now = now();
  21. $status = 0;
  22. $startTime = null;
  23. $endTime = null;
  24. $logo = '';
  25. $button_url = '';
  26. $button_light_url = '';
  27. $lunzi_url = '';
  28. $dipan_url = '';
  29. // 默认规则从活动配置 + 商城档位中计算(包含金额、次数和档位信息)
  30. $rechargeRules = $this->getDefaultRechargeRules($userId);
  31. if ($activity) {
  32. $status = (int)($activity->status ?? 0);
  33. $startTime = $activity->start_time;
  34. $endTime = $activity->end_time;
  35. $logo = $activity->icon_url ?? '';
  36. $button_url = $activity->button_url ?? '';
  37. $button_light_url = $activity->button_light_url ?? '';
  38. $lunzi_url = $activity->lunzi_url ?? '';
  39. $dipan_url = $activity->dipan_url ?? '';
  40. // 如果当前时间不在活动时间范围内,则前端状态强制视为关闭(0)
  41. if (!empty($startTime) && $now->lt($startTime)) {
  42. $status = 0;
  43. }
  44. if (!empty($endTime) && $now->gt($endTime)) {
  45. $status = 0;
  46. }
  47. }
  48. // 前端展示时只需要简单的奖励值数组,例如 [177, 1, 0.2, ...],隐藏 slot_index 和 weight
  49. $slots = DB::connection('write')
  50. ->table('agent.dbo.holiday_wheel_config')
  51. ->orderBy('sort_index', 'asc')
  52. ->orderBy('slot_index', 'asc')
  53. ->pluck('reward')
  54. ->map(function ($reward) {
  55. return (float)$reward;
  56. })
  57. ->values()
  58. ->toArray();
  59. $userTimes = DB::connection('write')
  60. ->table('agent.dbo.holiday_wheel_user')
  61. ->where('UserID', $userId)
  62. ->first();
  63. $leftTimes = $userTimes ? (int)$userTimes->left_times : 0;
  64. $totalTimes = $userTimes ? (int)$userTimes->total_times : 0;
  65. return [
  66. 'status' => $status,
  67. 'start_time' => $startTime,
  68. 'end_time' => $endTime,
  69. 'logo_url' => $logo,
  70. 'button_url' => $button_url,
  71. 'button_light_url' => $button_light_url,
  72. 'lunzi_url' => $lunzi_url,
  73. 'dipan_url' => $dipan_url,
  74. 'slots' => $slots,
  75. 'user' => [
  76. 'left_times' => $leftTimes,
  77. 'total_times' => $totalTimes,
  78. ],
  79. 'recharge_rules' => $rechargeRules,
  80. 'now' => $now->toDateTimeString(),
  81. ];
  82. }
  83. /**
  84. * 充值赠送转盘次数(在充值成功后调用)
  85. * 有 gift_id=401 的充值添加次数
  86. */
  87. public function grantTimesOnRecharge(int $userId, float $payAmt, int $giftId = 0): void
  88. {
  89. if ($giftId != 401) {
  90. return;
  91. }
  92. $activity = DB::connection('write')
  93. ->table('agent.dbo.holiday_wheel_activity')
  94. ->orderBy('id', 'desc')
  95. ->first();
  96. if (!$activity || (int)$activity->status !== 1) {
  97. return;
  98. }
  99. $now = now();
  100. if (!empty($activity->start_time) && $now->lt($activity->start_time)) {
  101. return;
  102. }
  103. if (!empty($activity->end_time) && $now->gt($activity->end_time)) {
  104. return;
  105. }
  106. // 统一使用默认规则(已基于活动配置 + 商城档位计算),保持金额与 times 一致
  107. $rules = $this->getDefaultRechargeRules($userId);
  108. $payStr = number_format($payAmt, 2, '.', '');
  109. $addTimes = 0;
  110. foreach ($rules as $rule) {
  111. $amount = isset($rule['amount']) ? number_format($rule['amount'], 2, '.', '') : null;
  112. $times = (int)($rule['times'] ?? 0);
  113. if ($amount !== null && $amount === $payStr && $times > 0) {
  114. $addTimes += $times;
  115. }
  116. }
  117. if ($addTimes <= 0) {
  118. return;
  119. }
  120. DB::connection('write')->transaction(function () use ($userId, $addTimes) {
  121. $row = DB::connection('write')
  122. ->table('agent.dbo.holiday_wheel_user')
  123. ->where('UserID', $userId)
  124. ->lockForUpdate()
  125. ->first();
  126. if ($row) {
  127. DB::connection('write')
  128. ->table('agent.dbo.holiday_wheel_user')
  129. ->where('UserID', $userId)
  130. ->update([
  131. 'left_times' => (int)$row->left_times + $addTimes,
  132. 'total_times' => (int)$row->total_times + $addTimes,
  133. 'updated_at' => now(),
  134. ]);
  135. } else {
  136. DB::connection('write')
  137. ->table('agent.dbo.holiday_wheel_user')
  138. ->insert([
  139. 'UserID' => $userId,
  140. 'left_times' => $addTimes,
  141. 'total_times' => $addTimes,
  142. 'created_at' => now(),
  143. 'updated_at' => now(),
  144. ]);
  145. }
  146. });
  147. }
  148. /**
  149. * 执行一次转盘抽奖
  150. */
  151. public function spin(int $userId): array
  152. {
  153. $redisKey = 'holiday_wheel_spin_' . $userId;
  154. $lock = SetNXLock::getExclusiveLock($redisKey, 5);
  155. if (!$lock) {
  156. throw new \RuntimeException('操作太频繁,请稍后再试');
  157. }
  158. try {
  159. $activity = DB::connection('write')
  160. ->table('agent.dbo.holiday_wheel_activity')
  161. ->orderBy('id', 'desc')
  162. ->first();
  163. if (!$activity || (int)$activity->status !== 1) {
  164. throw new \RuntimeException('活动未开启');
  165. }
  166. $now = now();
  167. if (!empty($activity->start_time) && $now->lt($activity->start_time)) {
  168. throw new \RuntimeException('活动未开始');
  169. }
  170. if (!empty($activity->end_time) && $now->gt($activity->end_time)) {
  171. throw new \RuntimeException('活动已结束');
  172. }
  173. $slots = DB::connection('write')
  174. ->table('agent.dbo.holiday_wheel_config')
  175. ->orderBy('sort_index', 'asc')
  176. ->orderBy('slot_index', 'asc')
  177. ->get();
  178. if ($slots->isEmpty()) {
  179. throw new \RuntimeException('转盘配置不存在');
  180. }
  181. DB::connection('write')->beginTransaction();
  182. $userRow = DB::connection('write')
  183. ->table('agent.dbo.holiday_wheel_user')
  184. ->where('UserID', $userId)
  185. ->lockForUpdate()
  186. ->first();
  187. if (!$userRow || (int)$userRow->left_times <= 0) {
  188. DB::connection('write')->rollBack();
  189. throw new \RuntimeException($userId.'没有可用的转盘次数');
  190. }
  191. $totalWeight = 0;
  192. $weights = [];
  193. foreach ($slots as $slot) {
  194. $w = (int)$slot->weight;
  195. if ($w < 0) {
  196. $w = 0;
  197. }
  198. $weights[] = $w;
  199. $totalWeight += $w;
  200. }
  201. if ($totalWeight <= 0) {
  202. DB::connection('write')->rollBack();
  203. throw new \RuntimeException('转盘权重配置错误');
  204. }
  205. $rand = mt_rand(1, $totalWeight);
  206. $cumulative = 0;
  207. $selectedIndex = 0;
  208. foreach ($slots as $idx => $slot) {
  209. $cumulative += $weights[$idx];
  210. if ($rand <= $cumulative) {
  211. $selectedIndex = $idx;
  212. break;
  213. }
  214. }
  215. $selectedSlot = $slots[$selectedIndex];
  216. $reward = (float)$selectedSlot->reward;
  217. $slotIndex = (int)$selectedSlot->slot_index;
  218. DB::connection('write')
  219. ->table('agent.dbo.holiday_wheel_user')
  220. ->where('UserID', $userId)
  221. ->update([
  222. 'left_times' => (int)$userRow->left_times - 1,
  223. 'updated_at' => now(),
  224. ]);
  225. DB::connection('write')
  226. ->table('agent.dbo.holiday_wheel_history')
  227. ->insert([
  228. 'UserID' => $userId,
  229. 'slot_index' => $slotIndex,
  230. 'reward' => $reward,
  231. 'created_at' => now(),
  232. ]);
  233. DB::connection('write')->commit();
  234. OuroGameService::AddScore($userId,$reward*NumConfig::NUM_VALUE,110,false);
  235. return [
  236. 'slot_index' => $slotIndex,
  237. 'reward' => $reward,
  238. 'left_times' => (int)$userRow->left_times - 1,
  239. ];
  240. } catch (\Throwable $e) {
  241. DB::connection('write')->rollBack();
  242. throw $e;
  243. } finally {
  244. SetNXLock::release($redisKey);
  245. }
  246. }
  247. /**
  248. * 默认充值规则:
  249. * - 基于固定金额和次数:[9.99=>1次, 19.99=>3次, 49.99=>6次]
  250. * - 额外从 recharge_gear 中取出相同金额的档位详情(类似商城列表的数据),
  251. * times 字段保持不变。
  252. */
  253. public function getDefaultRechargeRules(int $userId = 0): array
  254. {
  255. // 固定的基础规则(金额+次数)
  256. // 1) 先从活动配置中读取 recharge_rules
  257. $rules = [];
  258. $activity = DB::connection('write')
  259. ->table('agent.dbo.holiday_wheel_activity')
  260. ->orderBy('id', 'desc')
  261. ->first();
  262. if ($activity && !empty($activity->recharge_rules)) {
  263. $decoded = json_decode($activity->recharge_rules, true);
  264. if (is_array($decoded)) {
  265. $rules = $decoded;
  266. }
  267. }
  268. // 2) 如果没有配置,则使用默认规则
  269. if (empty($rules)) {
  270. $rules = [
  271. ['amount' => 9.99, 'times' => 1],
  272. ['amount' => 19.99, 'times' => 3],
  273. ['amount' => 19.99, 'times' => 3],
  274. ['amount' => 49.99, 'times' => 6],
  275. ];
  276. }
  277. $vip = 0;
  278. if ($userId > 0) {
  279. $vip = DB::table(TableName::QPAccountsDB() . 'YN_VIPAccount')
  280. ->where('UserID', $userId)
  281. ->value('Recharge') ?: 0;
  282. }
  283. // 为每条规则附加与金额相同的 recharge_gear 配置(类似商城 gear 列表)
  284. foreach ($rules as &$rule) {
  285. $amount = $rule['amount'];
  286. $item = DB::connection('write')
  287. ->table('agent.dbo.recharge_gear')
  288. ->where('status', 1)
  289. // ->where('in_shop', 1)
  290. ->where('money', $amount)
  291. ->select('id', 'money', 'favorable_price', 'give', 'recommend', 'gear')
  292. ->orderBy('id', 'asc')
  293. ->first();
  294. if ($item) {
  295. // 过滤 gear 支付方式,跟商城接口一致
  296. if (!empty($item->gear)) {
  297. $item->gear = Util::filterGearByDevice($item->gear, $vip);
  298. }
  299. $rule['gear'] = [
  300. 'id' => $item->id,
  301. 'money' => (float)$item->money,
  302. 'favorable_price' => (float)$item->favorable_price,
  303. 'give' => (float)$item->give,
  304. 'recommend' => (int)($item->recommend ?? 0),
  305. 'gear' => $item->gear,
  306. 'gift_id' => 401,
  307. ];
  308. } else {
  309. $rule['gear'] = null;
  310. }
  311. }
  312. unset($rule);
  313. return $rules;
  314. }
  315. }