LotteryRecordService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <?php
  2. namespace app\common\Service\Lottery;
  3. use app\common\model\lottery\LotteryDrawRecord;
  4. use app\common\model\lottery\LotteryWinRecord;
  5. use app\common\model\lottery\LotteryPrize;
  6. use app\common\Enum\LotteryEnum;
  7. use think\Exception;
  8. use app\common\model\lottery\LotteryActivity;
  9. /**
  10. * 抽奖记录服务类
  11. * 专门处理抽奖记录和中奖记录的业务逻辑
  12. */
  13. class LotteryRecordService
  14. {
  15. // ============ 抽奖记录相关方法 ============
  16. /**
  17. * 创建抽奖记录
  18. */
  19. public static function createDrawRecord($activityId, $userId, $prizeId, $isWin, $triggerType, $triggerOrderId = null, $triggerAmount = null, $winInfo = [])
  20. {
  21. $data = [
  22. 'activity_id' => $activityId,
  23. 'user_id' => $userId,
  24. 'prize_id' => $prizeId,
  25. 'is_win' => $isWin ? 1 : 0,
  26. 'trigger_type' => $triggerType,
  27. 'trigger_order_id' => $triggerOrderId,
  28. 'trigger_amount' => $triggerAmount,
  29. 'win_info' => $winInfo ? json_encode($winInfo) : '',
  30. 'draw_ip' => request()->ip(),
  31. 'draw_time' => time(),
  32. 'device_info' => request()->header('user-agent', '')
  33. ];
  34. return LotteryDrawRecord::create($data);
  35. }
  36. /**
  37. * 获取用户抽奖次数
  38. */
  39. public static function getUserDrawCount($activityId, $userId, $timeRange = null)
  40. {
  41. $query = LotteryDrawRecord::where('activity_id', $activityId)
  42. ->where('user_id', $userId);
  43. if ($timeRange) {
  44. if (isset($timeRange['start'])) {
  45. $query->where('draw_time', '>=', $timeRange['start']);
  46. }
  47. if (isset($timeRange['end'])) {
  48. $query->where('draw_time', '<=', $timeRange['end']);
  49. }
  50. }
  51. return $query->count();
  52. }
  53. /**
  54. * 获取用户中奖次数
  55. */
  56. public static function getUserWinCount($activityId, $userId, $timeRange = null)
  57. {
  58. $query = LotteryDrawRecord::where('activity_id', $activityId)
  59. ->where('user_id', $userId)
  60. ->where('is_win', 1);
  61. if ($timeRange) {
  62. if (isset($timeRange['start'])) {
  63. $query->where('draw_time', '>=', $timeRange['start']);
  64. }
  65. if (isset($timeRange['end'])) {
  66. $query->where('draw_time', '<=', $timeRange['end']);
  67. }
  68. }
  69. return $query->count();
  70. }
  71. /**
  72. * 获取活动抽奖统计
  73. */
  74. public static function getActivityDrawStats($activityId, $timeRange = null)
  75. {
  76. $query = LotteryDrawRecord::where('activity_id', $activityId);
  77. if ($timeRange) {
  78. if (isset($timeRange['start'])) {
  79. $query->where('draw_time', '>=', $timeRange['start']);
  80. }
  81. if (isset($timeRange['end'])) {
  82. $query->where('draw_time', '<=', $timeRange['end']);
  83. }
  84. }
  85. return [
  86. 'total_draw' => $query->count(),
  87. 'total_win' => $query->where('is_win', 1)->count(),
  88. 'total_people' => $query->distinct('user_id')->count()
  89. ];
  90. }
  91. /**
  92. * 获取用户抽奖记录列表
  93. */
  94. public static function getUserDrawRecords($userId = 0, $activityId = null,$status = 0, $page = 1, $pageSize = 20)
  95. {
  96. $query = LotteryDrawRecord::where('user_id', $userId);
  97. if ($activityId) {
  98. $query->where('activity_id', $activityId);
  99. }
  100. if ($status) {
  101. $query->where('status', $status);
  102. }
  103. // 1. 先分页查询抽奖记录
  104. $records = $query->field('id,activity_id,user_id,prize_id,is_win,trigger_type,trigger_order_id,trigger_amount,
  105. draw_time,status')
  106. ->order('draw_time', 'desc')
  107. ->paginate($pageSize, false, ['page' => $page]);
  108. if (empty($records) || $records->isEmpty()) {
  109. return $records;
  110. }
  111. // 2. 收集关联ID
  112. $activityIds = [];
  113. $prizeIds = [];
  114. $drawRecordIds = [];
  115. foreach ($records as $record) {
  116. $activityIds[] = $record->activity_id;
  117. $prizeIds[] = $record->prize_id;
  118. if ($record->is_win) {
  119. $drawRecordIds[] = $record->id;
  120. }
  121. }
  122. // 去重
  123. $activityIds = array_unique($activityIds);
  124. $prizeIds = array_unique($prizeIds);
  125. $drawRecordIds = array_unique($drawRecordIds);
  126. // 3. 批量查询关联数据
  127. $activities = [];
  128. if (!empty($activityIds)) {
  129. $activityList = LotteryActivity::whereIn('id', $activityIds)
  130. ->field('id,name,status,lottery_type,lottery_time')
  131. ->select();
  132. foreach ($activityList as $activity) {
  133. $activities[$activity->id] = $activity;
  134. }
  135. }
  136. $prizes = [];
  137. if (!empty($prizeIds)) {
  138. $prizeList = LotteryPrize::whereIn('id', $prizeIds)
  139. ->field('id,name,image,activity_id,type')
  140. ->select();
  141. foreach ($prizeList as $prize) {
  142. $prizes[$prize->id] = $prize;
  143. }
  144. }
  145. $winRecords = [];
  146. if (!empty($drawRecordIds)) {
  147. $winRecordList = LotteryWinRecord::whereIn('draw_record_id', $drawRecordIds)
  148. ->field('id,draw_record_id,deliver_status,deliver_time,exchange_code,receiver_name,receiver_mobile,receiver_address,express_company,express_number,exchange_code')
  149. ->select();
  150. foreach ($winRecordList as $winRecord) {
  151. $winRecords[$winRecord->draw_record_id] = $winRecord;
  152. }
  153. }
  154. // 4. 将关联数据作为字段附加到记录中
  155. foreach ($records as $record) {
  156. // 附加活动信息字段
  157. $activity = $activities[$record->activity_id] ?? null;
  158. $record->activity_name = $activity ? $activity->name : '';
  159. $record->activity_status = $activity ? $activity->status : 0;
  160. $record->activity_lottery_type = $activity ? $activity->lottery_type : 0;
  161. $record->activity_lottery_time = $activity ? $activity->lottery_time : 0;
  162. // 附加奖品信息字段
  163. $prize = $prizes[$record->prize_id] ?? null;
  164. $record->prize_name = $prize ? $prize->name : '';
  165. $record->prize_image = $prize ? cdnurl($prize->image) : '';
  166. $record->prize_type = $prize ? $prize->type : 0;
  167. // 附加中奖记录信息字段(仅限中奖记录)
  168. if ($record->is_win) {
  169. $winRecord = $winRecords[$record->id] ?? null;
  170. $record->win_record_id = $winRecord ? $winRecord->id : 0;
  171. $record->deliver_status = $winRecord ? $winRecord->deliver_status : 0;
  172. $record->deliver_time = $winRecord ? $winRecord->deliver_time : 0;
  173. $record->exchange_code = $winRecord ? $winRecord->exchange_code : '';
  174. $record->receiver_name = $winRecord ? $winRecord->receiver_name : '';
  175. $record->receiver_mobile = $winRecord ? $winRecord->receiver_mobile : '';
  176. $record->receiver_address = $winRecord ? $winRecord->receiver_address : '';
  177. $record->express_company = $winRecord ? $winRecord->express_company : '';
  178. $record->express_number = $winRecord ? $winRecord->express_number : '';
  179. } else {
  180. $record->win_record_id = 0;
  181. $record->deliver_status = 0;
  182. $record->deliver_time = 0;
  183. $record->exchange_code = '';
  184. $record->receiver_name = '';
  185. $record->receiver_mobile = '';
  186. $record->receiver_address = '';
  187. $record->express_company = '';
  188. $record->express_number = '';
  189. }
  190. // 添加触发类型文本
  191. $record->trigger_type_text = LotteryEnum::getTriggerTypeMap($record->trigger_type);
  192. }
  193. return $records;
  194. }
  195. /**
  196. * 获取抽奖记录详情
  197. *
  198. * @param int $drawRecordId 抽奖记录ID
  199. * @param int $userId 用户ID(可选,用于验证权限)
  200. * @return LotteryDrawRecord|null 抽奖记录详情,包含关联信息
  201. */
  202. public static function getDrawRecordDetail($drawRecordId, $userId = 0)
  203. {
  204. // 1. 查询抽奖记录基础信息
  205. $query = LotteryDrawRecord::where('id', $drawRecordId);
  206. // 如果指定了用户ID,则验证权限
  207. if ($userId) {
  208. $query->where('user_id', $userId);
  209. }
  210. $record = $query->field('id,activity_id,user_id,prize_id,is_win,trigger_type,trigger_order_id,
  211. trigger_amount,draw_time,remark,status,createtime')
  212. ->find();
  213. if (!$record) {
  214. return null;
  215. }
  216. // 2. 查询关联的活动信息
  217. $activity = LotteryActivity::where('id', $record->activity_id)
  218. ->field('id,name,type,status,lottery_type,lottery_time')
  219. ->find();
  220. // 3. 查询关联的奖品信息
  221. $prize = null;
  222. if ($record->prize_id > 0) {
  223. $prize = LotteryPrize::where('id', $record->prize_id)
  224. ->field('id,name,image,type')
  225. ->find();
  226. }
  227. // 4. 查询关联的中奖记录信息(仅限中奖记录)
  228. $winRecord = null;
  229. if ($record->is_win) {
  230. $winRecord = LotteryWinRecord::where('draw_record_id', $record->id)
  231. ->field('id,draw_record_id,prize_name,prize_type,deliver_status,
  232. deliver_time,receiver_name,receiver_mobile,receiver_address,
  233. express_company,express_number,exchange_code,code_used_time')
  234. ->find();
  235. }
  236. // 5. 将关联数据作为字段附加到记录中
  237. // 附加活动信息字段
  238. $record->activity_name = $activity ? $activity->name : '';
  239. $record->activity_type = $activity ? $activity->type : 0;
  240. $record->activity_status = $activity ? $activity->status : 0;
  241. $record->activity_lottery_type = $activity ? $activity->lottery_type : 0;
  242. $record->activity_lottery_time = $activity ? $activity->lottery_time : 0;
  243. // 附加奖品信息字段
  244. $record->prize_name = $prize ? $prize->name : '';
  245. $record->prize_image = $prize ? cdnurl($prize->image) : '';
  246. $record->prize_type = $prize ? $prize->type : 0;
  247. // 附加中奖记录信息字段
  248. if ($winRecord) {
  249. $record->win_record_id = $winRecord->id;
  250. $record->win_prize_name = $winRecord->prize_name;
  251. $record->win_prize_type = $winRecord->prize_type;
  252. $record->deliver_status = $winRecord->deliver_status;
  253. $record->deliver_time = $winRecord->deliver_time;
  254. $record->receiver_name = $winRecord->receiver_name;
  255. $record->receiver_mobile = $winRecord->receiver_mobile;
  256. $record->receiver_address = $winRecord->receiver_address;
  257. $record->express_company = $winRecord->express_company;
  258. $record->express_number = $winRecord->express_number;
  259. $record->exchange_code = $winRecord->exchange_code;
  260. $record->code_used_time = $winRecord->code_used_time;
  261. } else {
  262. $record->win_record_id = 0;
  263. $record->win_prize_name = '';
  264. $record->win_prize_type = 0;
  265. $record->deliver_status = 0;
  266. $record->deliver_time = 0;
  267. $record->receiver_name = '';
  268. $record->receiver_mobile = '';
  269. $record->receiver_address = '';
  270. $record->express_company = '';
  271. $record->express_number = '';
  272. $record->exchange_code = '';
  273. $record->code_used_time = 0;
  274. }
  275. return $record;
  276. }
  277. /**
  278. * 检查用户是否已为指定订单抽奖
  279. */
  280. public static function hasUserDrawnForOrder($activityId, $userId, $orderId)
  281. {
  282. return LotteryDrawRecord::where('activity_id', $activityId)
  283. ->where('user_id', $userId)
  284. ->where('trigger_order_id', $orderId)
  285. ->count() > 0;
  286. }
  287. /**
  288. * 获取用户抽奖历史统计
  289. */
  290. public static function getUserDrawHistoryStats($userId)
  291. {
  292. $stats = LotteryDrawRecord::where('user_id', $userId)
  293. ->field([
  294. 'COUNT(*) as total_draws',
  295. 'SUM(is_win) as total_wins',
  296. 'COUNT(DISTINCT activity_id) as total_activities'
  297. ])
  298. ->find();
  299. return [
  300. 'total_draws' => $stats['total_draws'] ?? 0,
  301. 'total_wins' => $stats['total_wins'] ?? 0,
  302. 'total_activities' => $stats['total_activities'] ?? 0,
  303. 'win_rate' => $stats['total_draws'] > 0 ?
  304. round(($stats['total_wins'] / $stats['total_draws']) * 100, 2) : 0
  305. ];
  306. }
  307. /**
  308. * 获取活动抽奖排行榜
  309. */
  310. public static function getActivityDrawRanking($activityId, $limit = 50)
  311. {
  312. return LotteryDrawRecord::where('activity_id', $activityId)
  313. ->field('user_id, COUNT(*) as draw_count, SUM(is_win) as win_count')
  314. ->with('user')
  315. ->group('user_id')
  316. ->order('draw_count desc, win_count desc')
  317. ->limit($limit)
  318. ->select();
  319. }
  320. /**
  321. * 获取活动抽奖时间分布统计
  322. */
  323. public static function getActivityDrawTimeDistribution($activityId, $dateRange = null)
  324. {
  325. $query = LotteryDrawRecord::where('activity_id', $activityId);
  326. if ($dateRange) {
  327. if (isset($dateRange['start'])) {
  328. $query->where('draw_time', '>=', $dateRange['start']);
  329. }
  330. if (isset($dateRange['end'])) {
  331. $query->where('draw_time', '<=', $dateRange['end']);
  332. }
  333. }
  334. return $query->field('DATE(FROM_UNIXTIME(draw_time)) as date, COUNT(*) as draw_count, SUM(is_win) as win_count')
  335. ->group('date')
  336. ->order('date asc')
  337. ->select();
  338. }
  339. // ============ 中奖记录相关方法 ============
  340. /**
  341. * 获取中奖记录详情
  342. */
  343. public static function getWinRecordDetail($winRecordId, $userId = 0)
  344. {
  345. $query = LotteryWinRecord::where('id', $winRecordId);
  346. if ($userId) {
  347. $query->where('user_id', $userId);
  348. }
  349. return $query->find();
  350. }
  351. /**
  352. * 创建中奖记录
  353. */
  354. public static function createWinRecord($drawRecordId, $activityId, $userId, $prizeId, $prizeName, $prizeType, $prizeValue = [])
  355. {
  356. $data = [
  357. 'draw_record_id' => $drawRecordId,
  358. 'activity_id' => $activityId,
  359. 'user_id' => $userId,
  360. 'prize_id' => $prizeId,
  361. 'prize_name' => $prizeName,
  362. 'prize_type' => $prizeType,
  363. 'prize_value' => is_array($prizeValue) ? json_encode($prizeValue) : $prizeValue,
  364. 'deliver_status' => LotteryEnum::DELIVER_STATUS_PENDING
  365. ];
  366. return LotteryWinRecord::create($data);
  367. }
  368. /**
  369. * 标记中奖记录为发放成功
  370. */
  371. public static function markWinRecordAsDelivered(LotteryWinRecord $winRecord, $deliverInfo = [])
  372. {
  373. $winRecord->deliver_status = LotteryEnum::DELIVER_STATUS_SUCCESS;
  374. $winRecord->deliver_time = time();
  375. if ($deliverInfo) {
  376. $winRecord->deliver_info = json_encode($deliverInfo);
  377. }
  378. return $winRecord->save();
  379. }
  380. /**
  381. * 标记中奖记录为发放失败
  382. */
  383. public static function markWinRecordAsFailed(LotteryWinRecord $winRecord, $reason = '')
  384. {
  385. $winRecord->deliver_status = LotteryEnum::DELIVER_STATUS_FAILED;
  386. $winRecord->fail_reason = $reason;
  387. return $winRecord->save();
  388. }
  389. /**
  390. * 设置中奖记录收货地址
  391. */
  392. public static function setWinRecordDeliveryAddress(LotteryWinRecord $winRecord, $name, $mobile, $address)
  393. {
  394. $winRecord->receiver_name = $name;
  395. $winRecord->receiver_mobile = $mobile;
  396. $winRecord->receiver_address = $address;
  397. return $winRecord->save();
  398. }
  399. /**
  400. * 设置中奖记录快递信息
  401. */
  402. public static function setWinRecordExpressInfo(LotteryWinRecord $winRecord, $company, $number)
  403. {
  404. $winRecord->express_company = $company;
  405. $winRecord->express_number = $number;
  406. return $winRecord->save();
  407. }
  408. /**
  409. * 设置中奖记录兑换码
  410. */
  411. public static function setWinRecordExchangeCode(LotteryWinRecord $winRecord, $code)
  412. {
  413. $winRecord->exchange_code = $code;
  414. return $winRecord->save();
  415. }
  416. /**
  417. * 标记中奖记录兑换码已使用
  418. */
  419. public static function markWinRecordCodeUsed(LotteryWinRecord $winRecord)
  420. {
  421. $winRecord->code_used_time = time();
  422. return $winRecord->save();
  423. }
  424. /**
  425. * 获取待发放的中奖记录
  426. */
  427. public static function getPendingWinRecords($limit = 100)
  428. {
  429. return LotteryWinRecord::where('deliver_status', LotteryEnum::DELIVER_STATUS_PENDING)
  430. ->order('createtime', 'asc')
  431. ->limit($limit)
  432. ->select();
  433. }
  434. /**
  435. * 获取用户中奖记录
  436. */
  437. public static function getUserWinRecords($userId, $page = 1, $limit = 20)
  438. {
  439. return LotteryWinRecord::where('user_id', $userId)
  440. ->with(['activity', 'prize'])
  441. ->order('createtime', 'desc')
  442. ->page($page, $limit)
  443. ->select();
  444. }
  445. /**
  446. * 获取活动中奖统计
  447. */
  448. public static function getActivityWinStats($activityId)
  449. {
  450. return [
  451. 'total_win' => LotteryWinRecord::where('activity_id', $activityId)->count(),
  452. 'delivered' => LotteryWinRecord::where('activity_id', $activityId)
  453. ->where('deliver_status', LotteryEnum::DELIVER_STATUS_SUCCESS)
  454. ->count(),
  455. 'pending' => LotteryWinRecord::where('activity_id', $activityId)
  456. ->where('deliver_status', LotteryEnum::DELIVER_STATUS_PENDING)
  457. ->count(),
  458. 'failed' => LotteryWinRecord::where('activity_id', $activityId)
  459. ->where('deliver_status', LotteryEnum::DELIVER_STATUS_FAILED)
  460. ->count()
  461. ];
  462. }
  463. /**
  464. * 批量处理待发放的中奖记录
  465. */
  466. public static function batchProcessPendingWinRecords($limit = 50)
  467. {
  468. $pendingRecords = static::getPendingWinRecords($limit);
  469. $processedCount = 0;
  470. foreach ($pendingRecords as $winRecord) {
  471. try {
  472. $prize = LotteryPrize::find($winRecord->prize_id);
  473. if ($prize && $prize->deliver_type == LotteryEnum::DELIVER_TYPE_AUTO) {
  474. static::autoDeliverPrize($winRecord, $prize);
  475. $processedCount++;
  476. }
  477. } catch (Exception $e) {
  478. static::markWinRecordAsFailed($winRecord, $e->getMessage());
  479. trace('自动发放奖品失败: ' . $e->getMessage(), 'error');
  480. }
  481. }
  482. return $processedCount;
  483. }
  484. /**
  485. * 手动发放奖品(管理员操作)
  486. */
  487. public static function manualDeliverPrize($winRecordId, $deliverInfo = [], $adminId = 0)
  488. {
  489. $winRecord = LotteryWinRecord::find($winRecordId);
  490. if (!$winRecord) {
  491. throw new Exception('中奖记录不存在');
  492. }
  493. if ($winRecord->deliver_status != LotteryEnum::DELIVER_STATUS_PENDING) {
  494. throw new Exception('该记录已处理,无法重复发放');
  495. }
  496. $deliverInfo['admin_id'] = $adminId;
  497. $deliverInfo['manual_deliver_time'] = time();
  498. return static::markWinRecordAsDelivered($winRecord, $deliverInfo);
  499. }
  500. /**
  501. * 取消中奖记录
  502. */
  503. public static function cancelWinRecord($winRecordId, $reason = '', $adminId = 0)
  504. {
  505. $winRecord = LotteryWinRecord::find($winRecordId);
  506. if (!$winRecord) {
  507. throw new Exception('中奖记录不存在');
  508. }
  509. $winRecord->deliver_status = LotteryEnum::DELIVER_STATUS_CANCELLED;
  510. $winRecord->fail_reason = $reason;
  511. $winRecord->cancel_time = time();
  512. $winRecord->cancel_admin_id = $adminId;
  513. return $winRecord->save();
  514. }
  515. /**
  516. * 自动发放奖品
  517. */
  518. public static function autoDeliverPrize($winRecord, $prize)
  519. {
  520. try {
  521. switch ($prize->type) {
  522. case LotteryEnum::PRIZE_TYPE_COUPON:
  523. static::deliverCoupon($winRecord, $prize);
  524. break;
  525. case LotteryEnum::PRIZE_TYPE_REDPACK:
  526. static::deliverRedPacket($winRecord, $prize);
  527. break;
  528. case LotteryEnum::PRIZE_TYPE_CODE:
  529. static::deliverExchangeCode($winRecord, $prize);
  530. break;
  531. case LotteryEnum::PRIZE_TYPE_SHOP_GOODS:
  532. static::deliverGoods($winRecord, $prize);
  533. break;
  534. }
  535. } catch (Exception $e) {
  536. static::markWinRecordAsFailed($winRecord, $e->getMessage());
  537. }
  538. }
  539. /**
  540. * 发放优惠券
  541. */
  542. private static function deliverCoupon($winRecord, $prize)
  543. {
  544. // 这里调用优惠券发放接口
  545. // 示例代码,需要根据实际优惠券系统实现
  546. static::markWinRecordAsDelivered($winRecord, ['coupon_id' => $prize->coupon_id]);
  547. }
  548. /**
  549. * 发放红包
  550. */
  551. private static function deliverRedPacket($winRecord, $prize)
  552. {
  553. // 这里调用红包发放接口
  554. // 示例代码,需要根据实际红包系统实现
  555. static::markWinRecordAsDelivered($winRecord, ['amount' => $prize->amount]);
  556. }
  557. /**
  558. * 发放兑换码
  559. */
  560. private static function deliverExchangeCode($winRecord, $prize)
  561. {
  562. $code = LotteryService::getAvailableExchangeCode($prize);
  563. if ($code) {
  564. static::setWinRecordExchangeCode($winRecord, $code);
  565. LotteryService::markExchangeCodeUsed($prize, $code);
  566. static::markWinRecordAsDelivered($winRecord, ['exchange_code' => $code]);
  567. } else {
  568. throw new Exception('兑换码已用完');
  569. }
  570. }
  571. /**
  572. * 发放商城商品
  573. */
  574. private static function deliverGoods($winRecord, $prize)
  575. {
  576. // 这里可以自动加入购物车或创建订单
  577. // 示例代码,需要根据实际商城系统实现
  578. static::markWinRecordAsDelivered($winRecord, [
  579. 'goods_id' => $prize->goods_id,
  580. 'goods_sku_id' => $prize->goods_sku_id
  581. ]);
  582. }
  583. }