user_id = $user_id; $this->rule = [2,3,5,7,10,15,'rank']; } /** * Execute the job. * * @return void */ public function handle() { try { $user = User::find($this->user_id); //获取当前已经签到次数 $count = $user->signLogs()->signType(SignLog::NEWYEARTYPE)->count(); if (empty($count)) return true; if ($count < 7 && is_numeric(($this->rule)[$count-1])) { //赠送对应的福币 $coin = ($this->rule)[$count-1]; $this->sendCoin($user, $coin, $count); }elseif ($count == 7 && ($this->rule)[$count-1] == 'rank') { $this->sendSuperRank($user); }else{ $this->sendCoin($user, 2, $count); } return true; } catch (\Exception $e) { $this->getError($e); return false; } } public function sendSuperRank($user) { try { //是否已经有记录 $count = $user->rankHistories()->type(RankHistory::SIGNNEWYEAR)->count(); if ($count) return false; //赠送会员 $user->addSuperRank($day=15, $month=0, $type=RankHistory::SIGNNEWYEAR); } catch (\Exception $e) { $this->getError($e); return false; } } public function sendCoin($user, $coin, $count) { try { $remark='累计'.$count.'天元旦签到'; //判断记录是否存在 $count = $user->coinLogs()->where(['type'=>CoinLog::RECSYSTEM, 'coin'=>$coin, 'remark'=>$remark])->count(); if ($count) return false; \DB::beginTransaction(); //增加福币记录 $user->addCoinLog(CoinLog::RECSYSTEM, 0, $coin, $remark); //修改福币钱包 $user->updateCoinInfo('add', $coin, 'other'); \DB::commit(); return true; } catch (\Exception $e) { \DB::rollback(); $this->getError($e); return false; } } }