thisWeekInfo = DateUtil::getThisWeekInfo(); if ( empty($this->thisWeekInfo) || empty($this->thisWeekInfo['start_date']) || empty($this->thisWeekInfo['end_date']) || empty($this->thisWeekInfo['week_number']) || empty($this->thisWeekInfo['year']) ) throw new Exception(__CLASS__.'数据生成失败,获取上周信息失败'); $this->nowDate = date("Y-m-d H:i:s"); $flag = $this->leaderboardHistoryModel->where('time_key',$this->thisWeekInfo['year'].$this->thisWeekInfo['week_number'])->first(); if (!empty($flag)) throw new Exception(__CLASS__.'数据生成失败,上周数据已生成'); $chefInfo = $this->chefModel ->join('admin_user', function ($join) { $join->on('admin_user.id', '=', 'chef.user_id') ->where('admin_user.is_del', '=', UserCode::IS_NO_DEL) ->select([ 'admin_user.id', 'chef.kitchen_id', 'admin_user.city_id' ]); }) ->get(); if ($chefInfo->isEmpty()) throw new Exception(__CLASS__.'数据生成失败,获取厨师信息失败'); $this->chefInfo = $chefInfo->toArray(); // $kitchenIds = array_column($this->chefInfo, 'kitchen_id'); // $this->kitchenSiteList = $this->kitchenModel->whereIn('id',$kitchenIds)->pluck('city_id','id')->toArray(); // if (empty($this->kitchenSiteList)) throw new Exception(__CLASS__.'数据生成失败,获取厨房城市信息失败'); $this->buildChefEvaluationRankData(); $this->buildChefStatementRankData(); if (empty($this->insertData)) throw new Exception(__CLASS__.'数据生成失败,获取厨师排行榜数据失败'); Db::transaction(function () { $res = (new LeaderboardHistory)->insert($this->insertData); if (!$res) throw new Exception(__CLASS__.'数据生成失败,插入数据失败'); }); }catch (Exception $e){ $this->log->error($e->getMessage()); } } /** * @return void */ private function buildChefEvaluationRankData(): void { $data = $this->chefStatementModel ->whereBetween('date',[$this->thisWeekInfo['start_date'],$this->thisWeekInfo['end_date']]) ->select('chef_id', Db::raw('SUM(`sale`) as total_sale')) ->groupBy('chef_id') ->get(); if ($data->isEmpty()) $this->buildChefStatementNullData(LeaderboardHistoryCode::TYPE_CHEF_SALE); $data = array_column($data->toArray(),'total_sale','chef_id'); foreach ($this->chefInfo as $v){ $this->insertData[] = [ 'chef_id' => $v->id, 'kitchen_id' => $v->kitchen_id, 'city_id' => $v->city_id, 'time_key' => $this->thisWeekInfo['year'].$this->thisWeekInfo['week_number'], 'board_type' => LeaderboardHistoryCode::TYPE_CHEF_SALE, 'score' => $data[$v->id] ?? 0, 'create_time' => $this->nowDate, ]; } } private function buildChefStatementNullData(int $type): void { foreach ($this->chefInfo as $v) { $this->insertData[] = [ 'chef_id' => $v->id, 'kitchen_id' => $v->kitchen_id, 'city_id' => $v->city_id, 'time_key' => $this->thisWeekInfo['year'].$this->thisWeekInfo['week_number'], 'board_type' => $type, 'score' => 0, 'create_time' => $this->nowDate, ]; } } /** * @return void */ private function buildChefStatementRankData(): void { $data = $this->evaluationModel ->whereBetween('date',[$this->thisWeekInfo['start_date'],$this->thisWeekInfo['end_date']]) ->select('chef_id',Db::raw('AVG(`score`) as total_score')) ->groupBy('chef_id') ->get(); if ($data->isEmpty()) $this->buildChefStatementNullData(LeaderboardHistoryCode::TYPE_CHEF_SCORE); $data = array_column($data->toArray(),'total_score','chef_id'); foreach ($this->chefInfo as $v){ $this->insertData[] = [ 'chef_id' => $v->id, 'kitchen_id' => $v->kitchen_id, 'city_id' => $v->city_id, 'time_key' => $this->thisWeekInfo['year'].$this->thisWeekInfo['week_number'], 'board_type' => LeaderboardHistoryCode::TYPE_CHEF_SCORE, 'score' => $data[$v->id] ?? 0, 'create_time' => $this->nowDate, ]; } } }