request->input('spu_id'); $spuInfo = $this->spuModel->getInfoById($spuId); if (empty($spuInfo)) throw new ErrException('菜品不存在-01'); $skuList = $this->skuModel->getListBySpuId($spuId); if ($skuList->isEmpty()) throw new ErrException('菜品不存在-02'); $skuList = $skuList->toArray(); $imageIds = array_column($skuList, 'image_id'); $chefIds = array_column($skuList, 'chef_id'); $imageList = $this->getOssObjects($imageIds); $chefList = $this->getChefList($chefIds); foreach ($skuList as $sku) { $sku['image_url'] = $imageList[$sku['image_ids']]['url'] ?? ''; $sku['chef'] = $chefList[$sku['chef_id']] ?? []; } return $this->return->success('success', [ 'spu' => $spuInfo, 'sku' => $skuList ]); } /** * @var Chef */ #[Inject] protected Chef $chefModel; /** * @var EvaluationCache */ #[Inject] protected EvaluationCache $evaluationCache; /** * @param array $chefIds * @return array * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ protected function getChefList(array $chefIds): array { $chef = $this->chefModel ->leftJoin('admin_user', 'admin_user.id', '=', 'chef.user_id') ->whereIn('chef.user_id', $chefIds) ->select([ 'admin_user.id', 'admin_user.username', 'admin_user.avatar', 'admin_user.chinese_name', 'chef.profile', 'chef.specialties', ]) ->get(); if ($chef->isEmpty()) return []; $chefList = $chef->toArray(); $chefList = array_column($chefList, null, 'id'); $avatarList = $this->getOssObjects(array_column($chefList, 'avatar')); foreach ($chefList as &$oneChef) { $oneChef['image_url'] = $avatarList[$oneChef['avatar']]['url'] ?? ''; $oneChef['avg_score'] = $this->evaluationCache->getChefEvaluation($oneChef['id']) ?? 0; } } }