From 96cad2a73ab58e760c44f778272403c22069f933 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Wed, 9 Apr 2025 10:13:40 +0800 Subject: [PATCH] feat : rank --- app/Controller/Api/ChefController.php | 4 + app/Controller/Api/GoodController.php | 12 ++ app/Controller/Api/OrderController.php | 2 + app/Model/Sku.php | 18 ++- app/Service/Admin/Good/SkuService.php | 2 + app/Service/Api/Good/OptionalInfoService.php | 116 +++++++++++++++++++ 6 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 app/Service/Api/Good/OptionalInfoService.php diff --git a/app/Controller/Api/ChefController.php b/app/Controller/Api/ChefController.php index 792cfff..0229a2e 100644 --- a/app/Controller/Api/ChefController.php +++ b/app/Controller/Api/ChefController.php @@ -10,6 +10,8 @@ use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Middlewares; use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\Validation\Annotation\Scene; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; #[Controller(prefix: 'api/chef')] #[Middlewares([ @@ -19,6 +21,8 @@ class ChefController { /** * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ #[RequestMapping(path: "index", methods: "GET")] #[Scene(scene: "index")] diff --git a/app/Controller/Api/GoodController.php b/app/Controller/Api/GoodController.php index 13f0af7..c7b31d5 100644 --- a/app/Controller/Api/GoodController.php +++ b/app/Controller/Api/GoodController.php @@ -5,6 +5,7 @@ namespace App\Controller\Api; use App\Controller\AbstractController; use App\Service\Api\Good\AddStapleFoodInfoService; use App\Service\Api\Good\MealListService; +use App\Service\Api\Good\OptionalInfoService; use App\Service\Api\Good\OptionalListService; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; @@ -46,4 +47,15 @@ class GoodController extends AbstractController { return (new AddStapleFoodInfoService)->handle(); } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + #[RequestMapping(path: 'optional_info',methods: 'GET')] + public function optionalInfo() + { + return (new OptionalInfoService)->handle(); + } } \ No newline at end of file diff --git a/app/Controller/Api/OrderController.php b/app/Controller/Api/OrderController.php index caea376..81324fe 100644 --- a/app/Controller/Api/OrderController.php +++ b/app/Controller/Api/OrderController.php @@ -134,6 +134,8 @@ class OrderController extends AbstractController /** * 评论 * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ #[RequestMapping(path: 'evaluation',methods: 'POST')] #[Scene(scene: 'evaluation')] diff --git a/app/Model/Sku.php b/app/Model/Sku.php index 7652ced..0842c11 100644 --- a/app/Model/Sku.php +++ b/app/Model/Sku.php @@ -12,7 +12,8 @@ use Hyperf\DbConnection\Model\Model; /** * @property int $id * @property int $spu_id - * @property string $title + * @property string $title + * @property string $sub_title * @property string $image_ids * @property string $price * @property string $param @@ -94,6 +95,21 @@ class Sku extends Model ->get(); } + /** + * @param int $spuId + * @return Collection + */ + public function getListBySpuId(int $spuId): Collection + { + return $this + ->where('spu_id',$spuId) + ->where('is_del',GoodCode::SKU_IS_NO_DEL) + ->where('saleable',GoodCode::LISTING) + ->orderBy('sort') + ->select(['id','spu_id','title','image_ids','price','param','extra','total_stock','surplus_stock','order_num','is_add_staple_food','occupied','chef_id','sub_title']) + ->get(); + } + /** * @param array $ids * @return array diff --git a/app/Service/Admin/Good/SkuService.php b/app/Service/Admin/Good/SkuService.php index e3caba6..ada9825 100644 --- a/app/Service/Admin/Good/SkuService.php +++ b/app/Service/Admin/Good/SkuService.php @@ -109,6 +109,7 @@ class SkuService extends BaseService $insertModel->spu_id = $spuId; $insertModel->title = $spuInfo->title.$this->request->input('title'); + $insertModel->sub_title = $this->request->input('sub_title'); $insertModel->price = $this->request->input('price'); $insertModel->chef_id = $this->request->input('chef_id'); $insertModel->image_ids = $imageIds; @@ -193,6 +194,7 @@ class SkuService extends BaseService $skuInfo->image_ids = $requestOssIds; } $skuInfo->chef_id = $this->request->input('chef_id'); + $skuInfo->sub_title = $this->request->input('sub_title'); $skuInfo->param = $this->request->input('param',''); $skuInfo->extra = $this->request->input('extra',''); $skuInfo->total_stock = $this->request->input('stock'); diff --git a/app/Service/Api/Good/OptionalInfoService.php b/app/Service/Api/Good/OptionalInfoService.php new file mode 100644 index 0000000..6593399 --- /dev/null +++ b/app/Service/Api/Good/OptionalInfoService.php @@ -0,0 +1,116 @@ +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; + } + } +} \ No newline at end of file