LotteryService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. namespace app\common\Service;
  3. use app\common\model\lottery\LotteryActivity;
  4. use app\common\model\lottery\LotteryPrize;
  5. use app\common\model\lottery\LotteryCondition;
  6. use app\common\model\lottery\LotteryDrawRecord;
  7. use app\common\model\lottery\LotteryWinRecord;
  8. use app\common\model\lottery\LotteryUserChance;
  9. use think\Db;
  10. use think\Exception;
  11. use think\Cache;
  12. /**
  13. * 抽奖服务类
  14. * 核心抽奖逻辑处理
  15. */
  16. class LotteryService
  17. {
  18. /**
  19. * 执行抽奖
  20. * @param int $activityId 活动ID
  21. * @param int $userId 用户ID
  22. * @param int $triggerType 触发类型
  23. * @param int $triggerOrderId 触发订单ID
  24. * @param float $triggerAmount 触发金额
  25. * @return array 抽奖结果
  26. * @throws Exception
  27. */
  28. public static function drawLottery($activityId, $userId, $triggerType = 1, $triggerOrderId = null, $triggerAmount = null)
  29. {
  30. // 1. 验证活动有效性
  31. $activity = LotteryActivity::find($activityId);
  32. if (!$activity || !$activity->isRunning()) {
  33. throw new Exception('活动不存在或未开始');
  34. }
  35. // 2. 验证抽奖时间
  36. if (!$activity->isInDrawTime()) {
  37. throw new Exception('不在抽奖时间内');
  38. }
  39. // 3. 验证用户资格
  40. if (!static::validateUserQualification($activity, $userId)) {
  41. throw new Exception('用户不符合参与条件');
  42. }
  43. // 4. 检查用户抽奖机会
  44. $userChance = LotteryUserChance::getUserChance($activityId, $userId);
  45. if (!$userChance || !$userChance->hasChance()) {
  46. throw new Exception('没有抽奖机会');
  47. }
  48. // 5. 检查用户参与次数限制
  49. if (!static::checkUserDrawLimit($activity, $userId)) {
  50. throw new Exception('已达到参与次数上限');
  51. }
  52. // 6. 防重复抽奖检查(基于订单)
  53. if ($triggerOrderId && static::hasDrawnForOrder($activityId, $userId, $triggerOrderId)) {
  54. throw new Exception('该订单已参与过抽奖');
  55. }
  56. // 7. 使用Redis锁防止并发
  57. $lockKey = "lottery_lock_{$activityId}_{$userId}";
  58. $lock = Cache::store('redis')->handler()->set($lockKey, 1, 'NX', 'EX', 10);
  59. if (!$lock) {
  60. throw new Exception('操作太频繁,请稍后再试');
  61. }
  62. try {
  63. // 8. 开始抽奖流程
  64. return static::processDrawLottery($activity, $userId, $triggerType, $triggerOrderId, $triggerAmount);
  65. } finally {
  66. // 释放锁
  67. Cache::store('redis')->handler()->del($lockKey);
  68. }
  69. }
  70. /**
  71. * 处理抽奖核心逻辑
  72. */
  73. private static function processDrawLottery($activity, $userId, $triggerType, $triggerOrderId, $triggerAmount)
  74. {
  75. // 开启事务
  76. Db::startTrans();
  77. try {
  78. // 1. 获取可用奖品
  79. $prizes = static::getAvailablePrizes($activity);
  80. if (empty($prizes)) {
  81. throw new Exception('暂无可抽取的奖品');
  82. }
  83. // 2. 执行抽奖算法
  84. $selectedPrize = static::executeLotteryAlgorithm($prizes);
  85. // 3. 减少库存
  86. if (!$selectedPrize->decreaseStock()) {
  87. throw new Exception('奖品库存不足');
  88. }
  89. // 4. 消耗用户抽奖机会
  90. $userChance = LotteryUserChance::getUserChance($activity->id, $userId);
  91. if (!$userChance->useChance()) {
  92. throw new Exception('抽奖机会使用失败');
  93. }
  94. // 5. 创建抽奖记录
  95. $isWin = $selectedPrize->type != LotteryPrize::TYPE_NO_PRIZE;
  96. $winInfo = $isWin ? static::buildWinInfo($selectedPrize) : [];
  97. $drawRecord = LotteryDrawRecord::createRecord(
  98. $activity->id,
  99. $userId,
  100. $selectedPrize->id,
  101. $isWin,
  102. $triggerType,
  103. $triggerOrderId,
  104. $triggerAmount,
  105. $winInfo
  106. );
  107. // 6. 如果中奖,创建中奖记录
  108. $winRecord = null;
  109. if ($isWin) {
  110. $winRecord = LotteryWinRecord::createWinRecord(
  111. $drawRecord->id,
  112. $activity->id,
  113. $userId,
  114. $selectedPrize->id,
  115. $selectedPrize->name,
  116. $selectedPrize->type,
  117. static::buildPrizeValue($selectedPrize)
  118. );
  119. // 自动发放奖品
  120. if ($selectedPrize->deliver_type == LotteryPrize::DELIVER_AUTO) {
  121. static::autoDeliverPrize($winRecord, $selectedPrize);
  122. }
  123. }
  124. // 7. 更新活动统计
  125. static::updateActivityStats($activity, $isWin);
  126. // 提交事务
  127. Db::commit();
  128. // 8. 返回抽奖结果
  129. return static::buildDrawResult($drawRecord, $selectedPrize, $winRecord);
  130. } catch (Exception $e) {
  131. Db::rollback();
  132. throw $e;
  133. }
  134. }
  135. /**
  136. * 获取可用奖品列表
  137. */
  138. private static function getAvailablePrizes($activity)
  139. {
  140. $prizes = LotteryPrize::getValidPrizes($activity->id);
  141. // 如果开启按人数解锁功能
  142. if ($activity->unlock_by_people) {
  143. $currentPeopleCount = LotteryUserChance::getActivityParticipants($activity->id);
  144. $prizes = $prizes->filter(function($prize) use ($currentPeopleCount) {
  145. return $prize->isUnlocked($currentPeopleCount);
  146. });
  147. }
  148. return $prizes;
  149. }
  150. /**
  151. * 执行抽奖算法(基于概率权重)
  152. */
  153. private static function executeLotteryAlgorithm($prizes)
  154. {
  155. // 计算总概率
  156. $totalProbability = $prizes->sum('probability');
  157. // 生成随机数
  158. $randomNumber = mt_rand(1, $totalProbability * 100) / 100;
  159. // 按概率选择奖品
  160. $currentProbability = 0;
  161. foreach ($prizes as $prize) {
  162. $currentProbability += $prize->probability;
  163. if ($randomNumber <= $currentProbability) {
  164. return $prize;
  165. }
  166. }
  167. // 保底返回第一个奖品(通常是未中奖)
  168. return $prizes->first();
  169. }
  170. /**
  171. * 验证用户资格
  172. */
  173. private static function validateUserQualification($activity, $userId)
  174. {
  175. // 检查用户群体限制
  176. switch ($activity->user_limit_type) {
  177. case LotteryActivity::USER_LIMIT_ALL:
  178. return true;
  179. case LotteryActivity::USER_LIMIT_LEVEL:
  180. return static::checkUserLevel($userId, $activity->user_limit_value);
  181. case LotteryActivity::USER_LIMIT_TAG:
  182. return static::checkUserTag($userId, $activity->user_limit_value);
  183. default:
  184. return false;
  185. }
  186. }
  187. /**
  188. * 检查用户等级
  189. */
  190. private static function checkUserLevel($userId, $limitValue)
  191. {
  192. if (empty($limitValue)) {
  193. return true;
  194. }
  195. $user = \app\common\model\User::find($userId);
  196. return $user && in_array($user->level, (array)$limitValue);
  197. }
  198. /**
  199. * 检查用户标签
  200. */
  201. private static function checkUserTag($userId, $limitValue)
  202. {
  203. if (empty($limitValue)) {
  204. return true;
  205. }
  206. // 这里需要根据实际的用户标签系统实现
  207. // 暂时返回true
  208. return true;
  209. }
  210. /**
  211. * 检查用户抽奖次数限制
  212. */
  213. private static function checkUserDrawLimit($activity, $userId)
  214. {
  215. if (!$activity->person_limit_num) {
  216. return true;
  217. }
  218. $drawCount = LotteryDrawRecord::getUserDrawCount($activity->id, $userId);
  219. return $drawCount < $activity->person_limit_num;
  220. }
  221. /**
  222. * 检查订单是否已抽奖
  223. */
  224. private static function hasDrawnForOrder($activityId, $userId, $orderId)
  225. {
  226. return LotteryDrawRecord::where('activity_id', $activityId)
  227. ->where('user_id', $userId)
  228. ->where('trigger_order_id', $orderId)
  229. ->count() > 0;
  230. }
  231. /**
  232. * 构建中奖信息
  233. */
  234. private static function buildWinInfo($prize)
  235. {
  236. return [
  237. 'prize_id' => $prize->id,
  238. 'prize_name' => $prize->name,
  239. 'prize_type' => $prize->type,
  240. 'prize_image' => $prize->image,
  241. 'win_prompt' => $prize->win_prompt
  242. ];
  243. }
  244. /**
  245. * 构建奖品价值信息
  246. */
  247. private static function buildPrizeValue($prize)
  248. {
  249. $prizeValue = [
  250. 'type' => $prize->type,
  251. 'name' => $prize->name,
  252. 'image' => $prize->image
  253. ];
  254. switch ($prize->type) {
  255. case LotteryPrize::TYPE_COUPON:
  256. $prizeValue['coupon_id'] = $prize->coupon_id;
  257. break;
  258. case LotteryPrize::TYPE_RED_PACKET:
  259. $prizeValue['amount'] = $prize->amount;
  260. break;
  261. case LotteryPrize::TYPE_GOODS:
  262. $prizeValue['goods_id'] = $prize->goods_id;
  263. $prizeValue['goods_sku_id'] = $prize->goods_sku_id;
  264. break;
  265. case LotteryPrize::TYPE_EXCHANGE_CODE:
  266. $prizeValue['exchange_code'] = $prize->getAvailableExchangeCode();
  267. break;
  268. }
  269. return $prizeValue;
  270. }
  271. /**
  272. * 自动发放奖品
  273. */
  274. private static function autoDeliverPrize($winRecord, $prize)
  275. {
  276. try {
  277. switch ($prize->type) {
  278. case LotteryPrize::TYPE_COUPON:
  279. static::deliverCoupon($winRecord, $prize);
  280. break;
  281. case LotteryPrize::TYPE_RED_PACKET:
  282. static::deliverRedPacket($winRecord, $prize);
  283. break;
  284. case LotteryPrize::TYPE_EXCHANGE_CODE:
  285. static::deliverExchangeCode($winRecord, $prize);
  286. break;
  287. case LotteryPrize::TYPE_GOODS:
  288. static::deliverGoods($winRecord, $prize);
  289. break;
  290. }
  291. } catch (Exception $e) {
  292. $winRecord->markAsFailed($e->getMessage());
  293. }
  294. }
  295. /**
  296. * 发放优惠券
  297. */
  298. private static function deliverCoupon($winRecord, $prize)
  299. {
  300. // 这里调用优惠券发放接口
  301. // 示例代码,需要根据实际优惠券系统实现
  302. $winRecord->markAsDelivered(['coupon_id' => $prize->coupon_id]);
  303. }
  304. /**
  305. * 发放红包
  306. */
  307. private static function deliverRedPacket($winRecord, $prize)
  308. {
  309. // 这里调用红包发放接口
  310. // 示例代码,需要根据实际红包系统实现
  311. $winRecord->markAsDelivered(['amount' => $prize->amount]);
  312. }
  313. /**
  314. * 发放兑换码
  315. */
  316. private static function deliverExchangeCode($winRecord, $prize)
  317. {
  318. $code = $prize->getAvailableExchangeCode();
  319. if ($code) {
  320. $winRecord->setExchangeCode($code);
  321. $prize->markExchangeCodeUsed($code);
  322. $winRecord->markAsDelivered(['exchange_code' => $code]);
  323. } else {
  324. throw new Exception('兑换码已用完');
  325. }
  326. }
  327. /**
  328. * 发放商城商品
  329. */
  330. private static function deliverGoods($winRecord, $prize)
  331. {
  332. // 这里可以自动加入购物车或创建订单
  333. // 示例代码,需要根据实际商城系统实现
  334. $winRecord->markAsDelivered([
  335. 'goods_id' => $prize->goods_id,
  336. 'goods_sku_id' => $prize->goods_sku_id
  337. ]);
  338. }
  339. /**
  340. * 更新活动统计
  341. */
  342. private static function updateActivityStats($activity, $isWin)
  343. {
  344. $activity->total_draw_count += 1;
  345. if ($isWin) {
  346. $activity->total_win_count += 1;
  347. }
  348. $activity->save();
  349. }
  350. /**
  351. * 构建抽奖结果
  352. */
  353. private static function buildDrawResult($drawRecord, $prize, $winRecord = null)
  354. {
  355. $result = [
  356. 'draw_id' => $drawRecord->id,
  357. 'is_win' => $drawRecord->is_win,
  358. 'prize' => [
  359. 'id' => $prize->id,
  360. 'name' => $prize->name,
  361. 'type' => $prize->type,
  362. 'type_text' => $prize->type_text,
  363. 'image' => $prize->image,
  364. 'win_prompt' => $prize->win_prompt
  365. ],
  366. 'draw_time' => $drawRecord->draw_time
  367. ];
  368. if ($winRecord) {
  369. $result['win_record_id'] = $winRecord->id;
  370. $result['deliver_status'] = $winRecord->deliver_status;
  371. $result['exchange_code'] = $winRecord->exchange_code;
  372. }
  373. return $result;
  374. }
  375. /**
  376. * 获取用户抽奖机会
  377. * @param int $activityId 活动ID
  378. * @param int $userId 用户ID
  379. * @return array
  380. */
  381. public static function getUserChances($activityId, $userId)
  382. {
  383. $userChance = LotteryUserChance::getUserChance($activityId, $userId);
  384. if (!$userChance) {
  385. return [
  386. 'total_chances' => 0,
  387. 'used_chances' => 0,
  388. 'remain_chances' => 0
  389. ];
  390. }
  391. return [
  392. 'total_chances' => $userChance->total_chances,
  393. 'used_chances' => $userChance->used_chances,
  394. 'remain_chances' => $userChance->remain_chances,
  395. 'last_get_time' => $userChance->last_get_time,
  396. 'last_use_time' => $userChance->last_use_time
  397. ];
  398. }
  399. }