LotteryRecordService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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')
  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. // 附加奖品信息字段
  162. $prize = $prizes[$record->prize_id] ?? null;
  163. $record->prize_name = $prize ? $prize->name : '';
  164. $record->prize_image = $prize ? cdnurl($prize->image) : '';
  165. $record->prize_type = $prize ? $prize->type : 0;
  166. // 附加中奖记录信息字段(仅限中奖记录)
  167. if ($record->is_win) {
  168. $winRecord = $winRecords[$record->id] ?? null;
  169. $record->win_record_id = $winRecord ? $winRecord->id : 0;
  170. $record->deliver_status = $winRecord ? $winRecord->deliver_status : 0;
  171. $record->deliver_time = $winRecord ? $winRecord->deliver_time : 0;
  172. $record->exchange_code = $winRecord ? $winRecord->exchange_code : '';
  173. $record->receiver_name = $winRecord ? $winRecord->receiver_name : '';
  174. $record->receiver_mobile = $winRecord ? $winRecord->receiver_mobile : '';
  175. $record->receiver_address = $winRecord ? $winRecord->receiver_address : '';
  176. $record->express_company = $winRecord ? $winRecord->express_company : '';
  177. $record->express_number = $winRecord ? $winRecord->express_number : '';
  178. } else {
  179. $record->win_record_id = 0;
  180. $record->deliver_status = 0;
  181. $record->deliver_time = 0;
  182. $record->exchange_code = '';
  183. $record->receiver_name = '';
  184. $record->receiver_mobile = '';
  185. $record->receiver_address = '';
  186. $record->express_company = '';
  187. $record->express_number = '';
  188. }
  189. // 添加触发类型文本
  190. $record->trigger_type_text = LotteryEnum::getTriggerTypeMap($record->trigger_type);
  191. }
  192. return $records;
  193. }
  194. /**
  195. * 获取活动抽奖记录列表
  196. */
  197. public static function getActivityDrawRecords($activityId, $page = 1, $limit = 20, $filters = [])
  198. {
  199. $query = LotteryDrawRecord::where('activity_id', $activityId);
  200. // 应用过滤器
  201. if (isset($filters['is_win'])) {
  202. $query->where('is_win', $filters['is_win']);
  203. }
  204. if (isset($filters['trigger_type'])) {
  205. $query->where('trigger_type', $filters['trigger_type']);
  206. }
  207. if (isset($filters['start_time'])) {
  208. $query->where('draw_time', '>=', $filters['start_time']);
  209. }
  210. if (isset($filters['end_time'])) {
  211. $query->where('draw_time', '<=', $filters['end_time']);
  212. }
  213. return $query->with(['user', 'prize', 'winRecord'])
  214. ->order('draw_time', 'desc')
  215. ->page($page, $limit)
  216. ->select();
  217. }
  218. /**
  219. * 获取抽奖记录详情
  220. *
  221. * @param int $drawRecordId 抽奖记录ID
  222. * @param int $userId 用户ID(可选,用于验证权限)
  223. * @return LotteryDrawRecord|null 抽奖记录详情,包含关联信息
  224. */
  225. public static function getDrawRecordDetail($drawRecordId, $userId = 0)
  226. {
  227. // 1. 查询抽奖记录基础信息
  228. $query = LotteryDrawRecord::where('id', $drawRecordId);
  229. // 如果指定了用户ID,则验证权限
  230. if ($userId) {
  231. $query->where('user_id', $userId);
  232. }
  233. $record = $query->field('id,activity_id,user_id,prize_id,is_win,trigger_type,trigger_order_id,
  234. trigger_amount,draw_time,remark,status,createtime')
  235. ->find();
  236. if (!$record) {
  237. return null;
  238. }
  239. // 2. 查询关联的活动信息
  240. $activity = LotteryActivity::where('id', $record->activity_id)
  241. ->field('id,name,type,status,lottery_type,lottery_time')
  242. ->find();
  243. // 3. 查询关联的奖品信息
  244. $prize = null;
  245. if ($record->prize_id > 0) {
  246. $prize = LotteryPrize::where('id', $record->prize_id)
  247. ->field('id,name,image,type')
  248. ->find();
  249. }
  250. // 4. 查询关联的中奖记录信息(仅限中奖记录)
  251. $winRecord = null;
  252. if ($record->is_win) {
  253. $winRecord = LotteryWinRecord::where('draw_record_id', $record->id)
  254. ->field('id,draw_record_id,prize_name,prize_type,deliver_status,
  255. deliver_time,receiver_name,receiver_mobile,receiver_address,
  256. express_company,express_number,exchange_code,code_used_time')
  257. ->find();
  258. }
  259. // 5. 将关联数据作为字段附加到记录中
  260. // 附加活动信息字段
  261. $record->activity_name = $activity ? $activity->name : '';
  262. $record->activity_type = $activity ? $activity->type : 0;
  263. $record->activity_status = $activity ? $activity->status : 0;
  264. $record->activity_lottery_type = $activity ? $activity->lottery_type : 0;
  265. $record->activity_lottery_time = $activity ? $activity->lottery_time : 0;
  266. // 附加奖品信息字段
  267. $record->prize_name = $prize ? $prize->name : '';
  268. $record->prize_image = $prize ? cdnurl($prize->image) : '';
  269. $record->prize_type = $prize ? $prize->type : 0;
  270. // 附加中奖记录信息字段
  271. if ($winRecord) {
  272. $record->win_record_id = $winRecord->id;
  273. $record->win_prize_name = $winRecord->prize_name;
  274. $record->win_prize_type = $winRecord->prize_type;
  275. $record->deliver_status = $winRecord->deliver_status;
  276. $record->deliver_time = $winRecord->deliver_time;
  277. $record->receiver_name = $winRecord->receiver_name;
  278. $record->receiver_mobile = $winRecord->receiver_mobile;
  279. $record->receiver_address = $winRecord->receiver_address;
  280. $record->express_company = $winRecord->express_company;
  281. $record->express_number = $winRecord->express_number;
  282. $record->exchange_code = $winRecord->exchange_code;
  283. $record->code_used_time = $winRecord->code_used_time;
  284. } else {
  285. $record->win_record_id = 0;
  286. $record->win_prize_name = '';
  287. $record->win_prize_type = 0;
  288. $record->deliver_status = 0;
  289. $record->deliver_time = 0;
  290. $record->receiver_name = '';
  291. $record->receiver_mobile = '';
  292. $record->receiver_address = '';
  293. $record->express_company = '';
  294. $record->express_number = '';
  295. $record->exchange_code = '';
  296. $record->code_used_time = 0;
  297. }
  298. return $record;
  299. }
  300. /**
  301. * 检查用户是否已为指定订单抽奖
  302. */
  303. public static function hasUserDrawnForOrder($activityId, $userId, $orderId)
  304. {
  305. return LotteryDrawRecord::where('activity_id', $activityId)
  306. ->where('user_id', $userId)
  307. ->where('trigger_order_id', $orderId)
  308. ->count() > 0;
  309. }
  310. /**
  311. * 获取用户抽奖历史统计
  312. */
  313. public static function getUserDrawHistoryStats($userId)
  314. {
  315. $stats = LotteryDrawRecord::where('user_id', $userId)
  316. ->field([
  317. 'COUNT(*) as total_draws',
  318. 'SUM(is_win) as total_wins',
  319. 'COUNT(DISTINCT activity_id) as total_activities'
  320. ])
  321. ->find();
  322. return [
  323. 'total_draws' => $stats['total_draws'] ?? 0,
  324. 'total_wins' => $stats['total_wins'] ?? 0,
  325. 'total_activities' => $stats['total_activities'] ?? 0,
  326. 'win_rate' => $stats['total_draws'] > 0 ?
  327. round(($stats['total_wins'] / $stats['total_draws']) * 100, 2) : 0
  328. ];
  329. }
  330. /**
  331. * 获取活动抽奖排行榜
  332. */
  333. public static function getActivityDrawRanking($activityId, $limit = 50)
  334. {
  335. return LotteryDrawRecord::where('activity_id', $activityId)
  336. ->field('user_id, COUNT(*) as draw_count, SUM(is_win) as win_count')
  337. ->with('user')
  338. ->group('user_id')
  339. ->order('draw_count desc, win_count desc')
  340. ->limit($limit)
  341. ->select();
  342. }
  343. /**
  344. * 获取活动抽奖时间分布统计
  345. */
  346. public static function getActivityDrawTimeDistribution($activityId, $dateRange = null)
  347. {
  348. $query = LotteryDrawRecord::where('activity_id', $activityId);
  349. if ($dateRange) {
  350. if (isset($dateRange['start'])) {
  351. $query->where('draw_time', '>=', $dateRange['start']);
  352. }
  353. if (isset($dateRange['end'])) {
  354. $query->where('draw_time', '<=', $dateRange['end']);
  355. }
  356. }
  357. return $query->field('DATE(FROM_UNIXTIME(draw_time)) as date, COUNT(*) as draw_count, SUM(is_win) as win_count')
  358. ->group('date')
  359. ->order('date asc')
  360. ->select();
  361. }
  362. // ============ 中奖记录相关方法 ============
  363. /**
  364. * 创建中奖记录
  365. */
  366. public static function createWinRecord($drawRecordId, $activityId, $userId, $prizeId, $prizeName, $prizeType, $prizeValue = [])
  367. {
  368. $data = [
  369. 'draw_record_id' => $drawRecordId,
  370. 'activity_id' => $activityId,
  371. 'user_id' => $userId,
  372. 'prize_id' => $prizeId,
  373. 'prize_name' => $prizeName,
  374. 'prize_type' => $prizeType,
  375. 'prize_value' => is_array($prizeValue) ? json_encode($prizeValue) : $prizeValue,
  376. 'deliver_status' => LotteryEnum::DELIVER_STATUS_PENDING
  377. ];
  378. return LotteryWinRecord::create($data);
  379. }
  380. /**
  381. * 标记中奖记录为发放成功
  382. */
  383. public static function markWinRecordAsDelivered(LotteryWinRecord $winRecord, $deliverInfo = [])
  384. {
  385. $winRecord->deliver_status = LotteryEnum::DELIVER_STATUS_SUCCESS;
  386. $winRecord->deliver_time = time();
  387. if ($deliverInfo) {
  388. $winRecord->deliver_info = json_encode($deliverInfo);
  389. }
  390. return $winRecord->save();
  391. }
  392. /**
  393. * 标记中奖记录为发放失败
  394. */
  395. public static function markWinRecordAsFailed(LotteryWinRecord $winRecord, $reason = '')
  396. {
  397. $winRecord->deliver_status = LotteryEnum::DELIVER_STATUS_FAILED;
  398. $winRecord->fail_reason = $reason;
  399. return $winRecord->save();
  400. }
  401. /**
  402. * 设置中奖记录收货地址
  403. */
  404. public static function setWinRecordDeliveryAddress(LotteryWinRecord $winRecord, $name, $mobile, $address)
  405. {
  406. $winRecord->receiver_name = $name;
  407. $winRecord->receiver_mobile = $mobile;
  408. $winRecord->receiver_address = $address;
  409. return $winRecord->save();
  410. }
  411. /**
  412. * 设置中奖记录快递信息
  413. */
  414. public static function setWinRecordExpressInfo(LotteryWinRecord $winRecord, $company, $number)
  415. {
  416. $winRecord->express_company = $company;
  417. $winRecord->express_number = $number;
  418. return $winRecord->save();
  419. }
  420. /**
  421. * 设置中奖记录兑换码
  422. */
  423. public static function setWinRecordExchangeCode(LotteryWinRecord $winRecord, $code)
  424. {
  425. $winRecord->exchange_code = $code;
  426. return $winRecord->save();
  427. }
  428. /**
  429. * 标记中奖记录兑换码已使用
  430. */
  431. public static function markWinRecordCodeUsed(LotteryWinRecord $winRecord)
  432. {
  433. $winRecord->code_used_time = time();
  434. return $winRecord->save();
  435. }
  436. /**
  437. * 获取待发放的中奖记录
  438. */
  439. public static function getPendingWinRecords($limit = 100)
  440. {
  441. return LotteryWinRecord::where('deliver_status', LotteryEnum::DELIVER_STATUS_PENDING)
  442. ->order('createtime', 'asc')
  443. ->limit($limit)
  444. ->select();
  445. }
  446. /**
  447. * 获取用户中奖记录
  448. */
  449. public static function getUserWinRecords($userId, $page = 1, $limit = 20)
  450. {
  451. return LotteryWinRecord::where('user_id', $userId)
  452. ->with(['activity', 'prize'])
  453. ->order('createtime', 'desc')
  454. ->page($page, $limit)
  455. ->select();
  456. }
  457. /**
  458. * 获取活动中奖统计
  459. */
  460. public static function getActivityWinStats($activityId)
  461. {
  462. return [
  463. 'total_win' => LotteryWinRecord::where('activity_id', $activityId)->count(),
  464. 'delivered' => LotteryWinRecord::where('activity_id', $activityId)
  465. ->where('deliver_status', LotteryEnum::DELIVER_STATUS_SUCCESS)
  466. ->count(),
  467. 'pending' => LotteryWinRecord::where('activity_id', $activityId)
  468. ->where('deliver_status', LotteryEnum::DELIVER_STATUS_PENDING)
  469. ->count(),
  470. 'failed' => LotteryWinRecord::where('activity_id', $activityId)
  471. ->where('deliver_status', LotteryEnum::DELIVER_STATUS_FAILED)
  472. ->count()
  473. ];
  474. }
  475. /**
  476. * 批量处理待发放的中奖记录
  477. */
  478. public static function batchProcessPendingWinRecords($limit = 50)
  479. {
  480. $pendingRecords = static::getPendingWinRecords($limit);
  481. $processedCount = 0;
  482. foreach ($pendingRecords as $winRecord) {
  483. try {
  484. $prize = LotteryPrize::find($winRecord->prize_id);
  485. if ($prize && $prize->deliver_type == LotteryEnum::DELIVER_TYPE_AUTO) {
  486. static::autoDeliverPrize($winRecord, $prize);
  487. $processedCount++;
  488. }
  489. } catch (Exception $e) {
  490. static::markWinRecordAsFailed($winRecord, $e->getMessage());
  491. trace('自动发放奖品失败: ' . $e->getMessage(), 'error');
  492. }
  493. }
  494. return $processedCount;
  495. }
  496. /**
  497. * 手动发放奖品(管理员操作)
  498. */
  499. public static function manualDeliverPrize($winRecordId, $deliverInfo = [], $adminId = 0)
  500. {
  501. $winRecord = LotteryWinRecord::find($winRecordId);
  502. if (!$winRecord) {
  503. throw new Exception('中奖记录不存在');
  504. }
  505. if ($winRecord->deliver_status != LotteryEnum::DELIVER_STATUS_PENDING) {
  506. throw new Exception('该记录已处理,无法重复发放');
  507. }
  508. $deliverInfo['admin_id'] = $adminId;
  509. $deliverInfo['manual_deliver_time'] = time();
  510. return static::markWinRecordAsDelivered($winRecord, $deliverInfo);
  511. }
  512. /**
  513. * 取消中奖记录
  514. */
  515. public static function cancelWinRecord($winRecordId, $reason = '', $adminId = 0)
  516. {
  517. $winRecord = LotteryWinRecord::find($winRecordId);
  518. if (!$winRecord) {
  519. throw new Exception('中奖记录不存在');
  520. }
  521. $winRecord->deliver_status = LotteryEnum::DELIVER_STATUS_CANCELLED;
  522. $winRecord->fail_reason = $reason;
  523. $winRecord->cancel_time = time();
  524. $winRecord->cancel_admin_id = $adminId;
  525. return $winRecord->save();
  526. }
  527. /**
  528. * 自动发放奖品
  529. */
  530. public static function autoDeliverPrize($winRecord, $prize)
  531. {
  532. try {
  533. switch ($prize->type) {
  534. case LotteryEnum::PRIZE_TYPE_COUPON:
  535. static::deliverCoupon($winRecord, $prize);
  536. break;
  537. case LotteryEnum::PRIZE_TYPE_REDPACK:
  538. static::deliverRedPacket($winRecord, $prize);
  539. break;
  540. case LotteryEnum::PRIZE_TYPE_CODE:
  541. static::deliverExchangeCode($winRecord, $prize);
  542. break;
  543. case LotteryEnum::PRIZE_TYPE_SHOP_GOODS:
  544. static::deliverGoods($winRecord, $prize);
  545. break;
  546. }
  547. } catch (Exception $e) {
  548. static::markWinRecordAsFailed($winRecord, $e->getMessage());
  549. }
  550. }
  551. /**
  552. * 发放优惠券
  553. */
  554. private static function deliverCoupon($winRecord, $prize)
  555. {
  556. // 这里调用优惠券发放接口
  557. // 示例代码,需要根据实际优惠券系统实现
  558. static::markWinRecordAsDelivered($winRecord, ['coupon_id' => $prize->coupon_id]);
  559. }
  560. /**
  561. * 发放红包
  562. */
  563. private static function deliverRedPacket($winRecord, $prize)
  564. {
  565. // 这里调用红包发放接口
  566. // 示例代码,需要根据实际红包系统实现
  567. static::markWinRecordAsDelivered($winRecord, ['amount' => $prize->amount]);
  568. }
  569. /**
  570. * 发放兑换码
  571. */
  572. private static function deliverExchangeCode($winRecord, $prize)
  573. {
  574. $code = LotteryService::getAvailableExchangeCode($prize);
  575. if ($code) {
  576. static::setWinRecordExchangeCode($winRecord, $code);
  577. LotteryService::markExchangeCodeUsed($prize, $code);
  578. static::markWinRecordAsDelivered($winRecord, ['exchange_code' => $code]);
  579. } else {
  580. throw new Exception('兑换码已用完');
  581. }
  582. }
  583. /**
  584. * 发放商城商品
  585. */
  586. private static function deliverGoods($winRecord, $prize)
  587. {
  588. // 这里可以自动加入购物车或创建订单
  589. // 示例代码,需要根据实际商城系统实现
  590. static::markWinRecordAsDelivered($winRecord, [
  591. 'goods_id' => $prize->goods_id,
  592. 'goods_sku_id' => $prize->goods_sku_id
  593. ]);
  594. }
  595. }