userInfo = $this->getUserInfo($this->adminId); //// $this->cityId = (int)$this->userInfo['city_id']; // } /** * @return array */ public function handle(): array { $limit = (int)$this->request->input('limit', 10); $cycleId = (int)$this->request->input('cycle_id'); $kitchenId = (int)$this->request->input('search_kitchen_id'); $cityId = (int)$this->request->input('search_city_id'); $list = $this->spuModel ->where('cycle_id',$cycleId) ->where('city_id',$cityId) ->where('kitchen_id',$kitchenId) ->where('is_del',GoodCode::SPU_IS_NO_DEL) ->when($this->request->input('type'), function ($query) { $type = (int)$this->request->input('type'); // $query->where('type',$type); if ($type == GoodCode::SPU_TYPE_FAVORABLE) { $query->where('favorable',GoodCode::IS_FAVORABLE)->where('type',GoodCode::SPU_TYPE_OPTIONAL); } else { $query->where('favorable',GoodCode::NOT_FAVORABLE)->where('type',$type); } }) // ->where('type',$this->request->input('type')) ->paginate($limit) ->toArray(); if (empty($list['data'])) return $this->return->success('success', ['list' => $list]); $spuIds = array_column($list['data'], 'id'); $spuImageIds = array_column($list['data'], 'image_id'); $skuList = $this->skuModel ->whereIn('spu_id',$spuIds) ->where('is_del',GoodCode::SKU_IS_NO_DEL) ->get(); if (empty($skuList)) return $this->return->success('success', ['list' => $list]); $skuList = $skuList->toArray(); $imageIdArr = array_column($skuList,'image_ids'); $imageIds = array_unique(explode(',',implode(',',$imageIdArr))); $imageList = $this->getOssObjects(array_merge($imageIds,$spuImageIds)); $skuListArr = []; foreach ($skuList as $sku) { $sku['image_list'] = []; if (!empty($sku['image_ids'])) { $imageOneArr = []; foreach (explode(',',$sku['image_ids']) as $imageId) { $imageOneArr[] = [ 'id' => $imageId, 'url' => $imageList[$imageId]['url'] ]; } $sku['image_list'] = $imageOneArr; } if (empty($skuListArr[$sku['spu_id']])) { $skuListArr[$sku['spu_id']] = []; } $skuListArr[$sku['spu_id']][] = $sku; } foreach ($list['data'] as &$item) { $item['sku_list'] = $skuListArr[$item['id']] ?? []; $item['image_url'] = $imageList[$item['image_id']] ?? ''; } return $this->return->success('success', ['list' => $list]); } /** * @var Cycle */ #[Inject] protected Cycle $cycleModel; /** * 添加 spu * @return array */ public function add(): array { $cycleId = (int)$this->request->input('cycle_id'); $cycleInfo = $this->cycleModel->getInfoById($cycleId); if (empty($cycleInfo)) throw new ErrException('没有该周期,请刷新后重新上传'); $title = $this->request->input('title'); $this->checkInfo(); $info = $this->spuModel->getInfoByCityIdAndCycleId($this->cityId, $cycleInfo->id,$title); if (!empty($info)) throw new ErrException('该菜品在当前城市已存在'); $imageId = $this->request->input('image_id'); $this->updateOssObjects([$imageId]); $insertModel = new Spu(); $insertModel->city_id = $this->cityId; $insertModel->cycle_id = $cycleInfo->id; $insertModel->kitchen_id = $this->request->input('kitchen_id'); // $insertModel->chef_id = $this->request->input('chef_id'); $insertModel->title = $title; $insertModel->sub_title = $this->request->input('sub_title',''); $insertModel->caterer_id = $this->request->input('caterer_id'); $insertModel->category_id = $this->request->input('category_id',0); $insertModel->saleable = $this->request->input('saleable'); $insertModel->type = $this->request->input('type'); $insertModel->sort = $this->request->input('sort'); $insertModel->image_id = $imageId; $insertModel->favorable = GoodCode::NOT_FAVORABLE; if (!$insertModel->save()) throw new ErrException('添加菜品失败'); return $this->return->success(); } /** * @var Kitchen */ #[Inject] protected Kitchen $kitchenModel; /** * @var AdminUser */ #[Inject] protected AdminUser $adminUserModel; /** * @var Chef */ #[Inject] protected Chef $chefModel; /** * @var GoodCache */ #[Inject] protected GoodCache $goodCache; /** * 信息检测 * @return void */ private function checkInfo(): void { $kitchenId = (int)$this->request->input('kitchen_id'); $kitchenInfo = $this->kitchenModel->getInfoById($kitchenId); $this->cityId = (int)$kitchenInfo->city_id; if ($kitchenInfo->status == SiteCode::KITCHEN_DISABLE) throw new ErrException('该厨房已禁用'); // $chefId = (int)$this->request->input('chef_id'); // $chefInfo = $this->chefModel->getInfoById($chefId); // $chefUserInfo = $this->adminUserModel->getAdminInfoById($chefInfo->user_id); // // if ($chefUserInfo->status == UserCode::DISABLE) throw new ErrException('该厨师已禁用'); } /** * 修改 * @return array */ public function edit(): array { $id = (int)$this->request->input('id'); $info = $this->spuModel->getInfoById($id); if (empty($info)) throw new ErrException('数据不存在'); $this->checkInfo(); $kitchenId = (int)$this->request->input('kitchen_id'); $oldKitchenId = (int)$info->kitchen_id; $info->kitchen_id = $kitchenId; // $info->chef_id = $this->request->input('chef_id'); $info->title = $this->request->input('title'); $info->caterer_id = $this->request->input('caterer_id'); $info->sub_title = $this->request->input('sub_title',''); $info->category_id = $this->request->input('category_id',0); $info->saleable = $this->request->input('saleable'); $info->type = $this->request->input('type'); $info->sort = $this->request->input('sort'); $requestOssId = $this->request->input('image_id',0); if ($requestOssId != $info->image_id) { $info->image_id = $requestOssId; $this->updateOssObjects([$requestOssId]); $this->updateOssObjectsDisable([$info->image_ids]); } if (!$info->save()) throw new ErrException('修改菜品失败'); //删除缓存 if ($oldKitchenId != $kitchenId) $this->goodCache->delCache($kitchenId,$info->cycle_id); $this->goodCache->delCache($oldKitchenId,$info->cycle_id); return $this->return->success(); } /** * spu 删除 * @return array * @throws Exception */ public function del(): array { $id = (int)$this->request->input('id'); $info = $this->spuModel->getInfoById($id); if (empty($info)) throw new ErrException('数据已删除'); //todo 需要联动删除sku 已经有用户下单sku该怎么办 //删除所有 sku $this->skuModel->where('spu_id',$info->id)->update(['is_del' => GoodCode::SKU_IS_DELETE]); //没有直接删除菜 $info->is_del = GoodCode::SPU_IS_DELETE; $info->save(); //删除缓存 $this->goodCache->delCache($info->kitchen_id,$info->cycle_id); return $this->return->success(); } /** * @var SystemCity $systemCityModel */ #[Inject] protected SystemCity $systemCityModel; /** * @var Category $categoryModel */ #[Inject] protected Category $categoryModel; /** * spu 详情 * @return array */ public function view(): array { $id = (int)$this->request->input('id'); $info = $this->spuModel->getInfoById($id); if (empty($info)) throw new ErrException('数据不存在'); $info = $info->toArray(); $info['city_name'] = $this->systemCityModel->getCityNameById((int)$info['city_id']); $info['kitchen_name'] = $this->kitchenModel->getNameById((int)$info['kitchen_id']); // $info['chef_name'] = $this->chefModel->getChineseNameById((int)$info['chef_id']); $info['image_url'] = $this->getOssObjectById((int)$info['image_id']) ?? ''; $info['chef_name'] = ''; $info['category_name'] = $this->categoryModel->getNameById((int)$info['category_id']); return $this->return->success('success',$info); } }