LuckLotteryServices.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\services\lottery;
  3. use app\admin\model\lottery\Info;
  4. use app\admin\model\lottery\Record;
  5. use app\admin\model\User;
  6. use think\Db;
  7. /**
  8. *
  9. * Class LuckLotteryServices
  10. * @package app\services\activity\lottery
  11. */
  12. class LuckLotteryServices
  13. {
  14. /**
  15. * 抽奖形式,奖品数量
  16. * @var int[]
  17. */
  18. protected $lottery_type = [
  19. '1' => 8 //九宫格
  20. ];
  21. /**
  22. * 抽奖类型
  23. * @var string[]
  24. */
  25. protected $lottery_factor = [
  26. '1' => '积分',
  27. '2' => '余额',
  28. '3' => '下单支付成功',
  29. '4' => '订单评价',
  30. '5' => '关注公众号'
  31. ];
  32. public function getLottery($id, $field = '*',$with = ['prize'])
  33. {
  34. $time = time();
  35. return Info::where('id',$id)
  36. ->with($with)
  37. ->where('status', 1)
  38. ->where('start_time', '<=', $time)
  39. ->where('end_time', '>=', $time)
  40. ->find();
  41. }
  42. public function getLotteryInfo($id, $field = '*',$with = ['prize'])
  43. {
  44. return Info::where('id',$id)
  45. ->where('status', 1)
  46. ->find();
  47. }
  48. /**
  49. * 抽奖
  50. * @param int $uid
  51. * @param int $lottery_id
  52. * @return mixed
  53. * @throws \Psr\SimpleCache\InvalidArgumentException
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function luckLottery( $userInfo, $lottery_id)
  59. {
  60. $lottery = $this->getLotteryInfo($lottery_id, '*', [], true);
  61. if (!$lottery) {
  62. return [1,'抽奖活动不存在'];
  63. }
  64. $uid = $userInfo['id'];
  65. $lottery = $lottery->toArray();
  66. if ( $lottery['lottery_num_term'] > 0 && $this->getPaperDateGradeCount($lottery_id,$uid) >= $lottery['lottery_num_term']) {
  67. return [1,'当前抽奖次数已达今日上限,明天再来吧~'];
  68. }
  69. $lotteryPrizeServices = new LuckPrizeServices();
  70. $lotteryPrize = $lotteryPrizeServices->getLotteryPrizeList($lottery_id);
  71. if (!$lotteryPrize) {
  72. return [1,'抽奖奖品不存在'];
  73. }
  74. $lotteryPrize = collection($lotteryPrize)->toArray();
  75. return Db::transaction(function () use ($uid, $lotteryPrize, $userInfo, $lottery) {
  76. $luckPrizeServices =new LuckPrizeServices();
  77. //随机抽奖
  78. $prize = $luckPrizeServices->getLuckPrize($lotteryPrize);
  79. if (!$prize) {
  80. return [1,'抽奖异常'];
  81. }
  82. //中奖扣除积分、余额
  83. list($nError,$msg) = $this->lotteryFactor($uid, $userInfo, $lottery);
  84. if ($nError == 1) {
  85. return [1,$msg];
  86. }
  87. //中奖减少奖品数量
  88. list($nError,$msg) = $luckPrizeServices->decPrizeNum($prize['id'], $prize);
  89. if ($nError == 1) {
  90. return [1,$msg];
  91. }
  92. $lotteryRecordServices = new LuckLotteryRecordServices();
  93. //中奖写入记录
  94. list($nError,$record) = $lotteryRecordServices->insertPrizeRecord($uid, $prize, $userInfo);
  95. if ($nError == 1) {
  96. return [1,$record];
  97. }
  98. //不是站内商品直接领奖
  99. /*if ($prize['type'] != 6) {
  100. $lotteryRecordServices->receivePrize($uid, (int)$record->id);
  101. }*/
  102. $prize['lottery_record_id'] = $record->id;
  103. return [0,$prize];
  104. });
  105. }
  106. /**
  107. * 次数
  108. * @param int $paper_id 试卷ID
  109. * @param string $date 日期
  110. * @return int|string
  111. */
  112. public static function getPaperDateGradeCount($lottery_id,$uid)
  113. {
  114. if (!$lottery_id) {
  115. return 0;
  116. }
  117. return Record::where('lottery_id', $lottery_id)->where('user_id',$uid)->whereTime('createtime', 'today')->count();
  118. }
  119. /**
  120. * 抽奖消耗扣除用户积分、余额等
  121. * @param int $uid
  122. * @param array $userInfo
  123. * @param array $lottery
  124. * @return bool
  125. * @throws \Psr\SimpleCache\InvalidArgumentException
  126. */
  127. public function lotteryFactor(int $uid, array $userInfo, array $lottery)
  128. {
  129. if (!$userInfo || !$lottery) {
  130. return true;
  131. }
  132. if (!User::where('id',$uid)->setDec('lottery_num')) {
  133. return [1,'抽奖次数修改失败'];
  134. }
  135. return [0,'抽奖次数修改成功'];
  136. }
  137. /**
  138. * 增加抽奖次数
  139. * @param int $uid
  140. * @param array $userInfo
  141. * @param array $lottery
  142. * @return bool
  143. * @throws \Psr\SimpleCache\InvalidArgumentException
  144. */
  145. public function lotteryNum( $uid)
  146. {
  147. if (!User::where('id',$uid)->setInc('lottery_num')) {
  148. return [1,'抽奖次数增加失败'];
  149. }
  150. return [0,'抽奖次数成功增加'];
  151. }
  152. }