8 //九宫格 ]; /** * 抽奖类型 * @var string[] */ protected $lottery_factor = [ '1' => '积分', '2' => '余额', '3' => '下单支付成功', '4' => '订单评价', '5' => '关注公众号' ]; public function getLottery($id, $field = '*',$with = ['prize']) { $time = time(); return Info::where('id',$id) ->with($with) ->where('status', 1) ->where('start_time', '<=', $time) ->where('end_time', '>=', $time) ->find(); } public function getLotteryInfo($id, $field = '*',$with = ['prize']) { return Info::where('id',$id) ->where('status', 1) ->find(); } /** * 抽奖 * @param int $uid * @param int $lottery_id * @return mixed * @throws \Psr\SimpleCache\InvalidArgumentException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function luckLottery( $userInfo, $lottery_id) { $lottery = $this->getLotteryInfo($lottery_id, '*', [], true); if (!$lottery) { return [1,'抽奖活动不存在']; } $uid = $userInfo['id']; $lottery = $lottery->toArray(); if ( $lottery['lottery_num_term'] > 0 && $this->getPaperDateGradeCount($lottery_id,$uid) >= $lottery['lottery_num_term']) { return [1,'当前抽奖次数已达今日上限,明天再来吧~']; } $lotteryPrizeServices = new LuckPrizeServices(); $lotteryPrize = $lotteryPrizeServices->getLotteryPrizeList($lottery_id); if (!$lotteryPrize) { return [1,'抽奖奖品不存在']; } $lotteryPrize = collection($lotteryPrize)->toArray(); return Db::transaction(function () use ($uid, $lotteryPrize, $userInfo, $lottery) { $luckPrizeServices =new LuckPrizeServices(); //随机抽奖 $prize = $luckPrizeServices->getLuckPrize($lotteryPrize); if (!$prize) { return [1,'抽奖异常']; } //中奖扣除积分、余额 list($nError,$msg) = $this->lotteryFactor($uid, $userInfo, $lottery); if ($nError == 1) { return [1,$msg]; } //中奖减少奖品数量 list($nError,$msg) = $luckPrizeServices->decPrizeNum($prize['id'], $prize); if ($nError == 1) { return [1,$msg]; } $lotteryRecordServices = new LuckLotteryRecordServices(); //中奖写入记录 list($nError,$record) = $lotteryRecordServices->insertPrizeRecord($uid, $prize, $userInfo); if ($nError == 1) { return [1,$record]; } //不是站内商品直接领奖 /*if ($prize['type'] != 6) { $lotteryRecordServices->receivePrize($uid, (int)$record->id); }*/ $prize['lottery_record_id'] = $record->id; return [0,$prize]; }); } /** * 次数 * @param int $paper_id 试卷ID * @param string $date 日期 * @return int|string */ public static function getPaperDateGradeCount($lottery_id,$uid) { if (!$lottery_id) { return 0; } return Record::where('lottery_id', $lottery_id)->where('user_id',$uid)->whereTime('createtime', 'today')->count(); } /** * 抽奖消耗扣除用户积分、余额等 * @param int $uid * @param array $userInfo * @param array $lottery * @return bool * @throws \Psr\SimpleCache\InvalidArgumentException */ public function lotteryFactor(int $uid, array $userInfo, array $lottery) { if (!$userInfo || !$lottery) { return true; } if (!User::where('id',$uid)->setDec('lottery_num')) { return [1,'抽奖次数修改失败']; } return [0,'抽奖次数修改成功']; } /** * 增加抽奖次数 * @param int $uid * @param array $userInfo * @param array $lottery * @return bool * @throws \Psr\SimpleCache\InvalidArgumentException */ public function lotteryNum( $uid) { if (!User::where('id',$uid)->setInc('lottery_num')) { return [1,'抽奖次数增加失败']; } return [0,'抽奖次数成功增加']; } }