HolidayWheelService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. return [
  156. 'error' => ['web.activity.wheel_too_frequent', __('messages.web.activity.wheel_too_frequent')],
  157. ];
  158. }
  159. try {
  160. $activity = DB::connection('write')
  161. ->table('agent.dbo.holiday_wheel_activity')
  162. ->orderBy('id', 'desc')
  163. ->first();
  164. if (!$activity || (int)$activity->status !== 1) {
  165. return [
  166. 'error' => ['web.activity.wheel_not_open', __('messages.web.activity.wheel_not_open')],
  167. ];
  168. }
  169. $now = now();
  170. if (!empty($activity->start_time) && $now->lt($activity->start_time)) {
  171. return [
  172. 'error' => ['web.activity.wheel_not_started', __('messages.web.activity.wheel_not_started')],
  173. ];
  174. }
  175. if (!empty($activity->end_time) && $now->gt($activity->end_time)) {
  176. return [
  177. 'error' => ['web.activity.wheel_ended', __('messages.web.activity.wheel_ended')],
  178. ];
  179. }
  180. $slots = DB::connection('write')
  181. ->table('agent.dbo.holiday_wheel_config')
  182. ->orderBy('sort_index', 'asc')
  183. ->orderBy('slot_index', 'asc')
  184. ->get();
  185. if ($slots->isEmpty()) {
  186. return [
  187. 'error' => ['web.activity.wheel_config_missing', __('messages.web.activity.wheel_config_missing')],
  188. ];
  189. }
  190. DB::connection('write')->beginTransaction();
  191. $userRow = DB::connection('write')
  192. ->table('agent.dbo.holiday_wheel_user')
  193. ->where('UserID', $userId)
  194. ->lockForUpdate()
  195. ->first();
  196. if (!$userRow || (int)$userRow->left_times <= 0) {
  197. DB::connection('write')->rollBack();
  198. return [
  199. 'error' => ['web.activity.wheel_no_times', __('messages.web.activity.wheel_no_times')],
  200. ];
  201. }
  202. $totalWeight = 0;
  203. $weights = [];
  204. foreach ($slots as $slot) {
  205. $w = (int)$slot->weight;
  206. if ($w < 0) {
  207. $w = 0;
  208. }
  209. $weights[] = $w;
  210. $totalWeight += $w;
  211. }
  212. if ($totalWeight <= 0) {
  213. DB::connection('write')->rollBack();
  214. return [
  215. 'error' => ['web.activity.wheel_weight_error', __('messages.web.activity.wheel_weight_error')],
  216. ];
  217. }
  218. $rand = mt_rand(1, $totalWeight);
  219. $cumulative = 0;
  220. $selectedIndex = 0;
  221. foreach ($slots as $idx => $slot) {
  222. $cumulative += $weights[$idx];
  223. if ($rand <= $cumulative) {
  224. $selectedIndex = $idx;
  225. break;
  226. }
  227. }
  228. $selectedSlot = $slots[$selectedIndex];
  229. $reward = (float)$selectedSlot->reward;
  230. $slotIndex = (int)$selectedSlot->slot_index;
  231. DB::connection('write')
  232. ->table('agent.dbo.holiday_wheel_user')
  233. ->where('UserID', $userId)
  234. ->update([
  235. 'left_times' => (int)$userRow->left_times - 1,
  236. 'updated_at' => now(),
  237. ]);
  238. DB::connection('write')
  239. ->table('agent.dbo.holiday_wheel_history')
  240. ->insert([
  241. 'UserID' => $userId,
  242. 'slot_index' => $slotIndex,
  243. 'reward' => $reward,
  244. 'created_at' => now(),
  245. ]);
  246. DB::connection('write')->commit();
  247. OuroGameService::AddScore($userId,$reward*NumConfig::NUM_VALUE,110,false);
  248. return [
  249. 'slot_index' => $slotIndex,
  250. 'reward' => $reward,
  251. 'left_times' => (int)$userRow->left_times - 1,
  252. ];
  253. } catch (\Throwable $e) {
  254. DB::connection('write')->rollBack();
  255. throw $e;
  256. } finally {
  257. SetNXLock::release($redisKey);
  258. }
  259. }
  260. /**
  261. * 默认充值规则:
  262. * - 基于固定金额和次数:[9.99=>1次, 19.99=>3次, 49.99=>6次]
  263. * - 额外从 recharge_gear 中取出相同金额的档位详情(类似商城列表的数据),
  264. * times 字段保持不变。
  265. */
  266. public function getDefaultRechargeRules(): array
  267. {
  268. // 固定的基础规则(金额+次数)
  269. // 1) 先从活动配置中读取 recharge_rules
  270. $rules = [];
  271. $activity = DB::connection('write')
  272. ->table('agent.dbo.holiday_wheel_activity')
  273. ->orderBy('id', 'desc')
  274. ->first();
  275. if ($activity && !empty($activity->recharge_rules)) {
  276. $decoded = json_decode($activity->recharge_rules, true);
  277. if (is_array($decoded)) {
  278. $rules = $decoded;
  279. }
  280. }
  281. // 2) 如果没有配置,则使用默认规则
  282. if (empty($rules)) {
  283. $rules = [
  284. ['amount' => 9.99, 'times' => 1],
  285. ['amount' => 19.99, 'times' => 3],
  286. ['amount' => 19.99, 'times' => 3],
  287. ['amount' => 49.99, 'times' => 6],
  288. ];
  289. }
  290. // 为每条规则附加与金额相同的 recharge_gear 配置(类似商城 gear 列表)
  291. foreach ($rules as &$rule) {
  292. $amount = $rule['amount'];
  293. $item = DB::connection('write')
  294. ->table('agent.dbo.recharge_gear')
  295. ->where('status', 1)
  296. // ->where('in_shop', 1)
  297. ->where('money', $amount)
  298. ->select('id', 'money', 'favorable_price', 'give', 'recommend', 'gear')
  299. ->orderBy('id', 'asc')
  300. ->first();
  301. if ($item) {
  302. // 过滤 gear 支付方式,跟商城接口一致
  303. if (!empty($item->gear)) {
  304. $item->gear = Util::filterGearByDevice($item->gear);
  305. }
  306. $rule['gear'] = [
  307. 'id' => $item->id,
  308. 'money' => (float)$item->money,
  309. 'favorable_price' => (float)$item->favorable_price,
  310. 'give' => (float)$item->give,
  311. 'recommend' => (int)($item->recommend ?? 0),
  312. 'gear' => $item->gear,
  313. 'gift_id' => 401,
  314. ];
  315. } else {
  316. $rule['gear'] = null;
  317. }
  318. }
  319. unset($rule);
  320. return $rules;
  321. }
  322. }