HolidayWheelService.php 11 KB

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