HolidayWheelService.php 12 KB

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