feat : rank

This commit is contained in:
2025-04-09 10:13:40 +08:00
parent a4a715a9e3
commit 96cad2a73a
6 changed files with 153 additions and 1 deletions

View File

@@ -10,6 +10,8 @@ use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares; use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene; use Hyperf\Validation\Annotation\Scene;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
#[Controller(prefix: 'api/chef')] #[Controller(prefix: 'api/chef')]
#[Middlewares([ #[Middlewares([
@@ -19,6 +21,8 @@ class ChefController
{ {
/** /**
* @return array * @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
#[RequestMapping(path: "index", methods: "GET")] #[RequestMapping(path: "index", methods: "GET")]
#[Scene(scene: "index")] #[Scene(scene: "index")]

View File

@@ -5,6 +5,7 @@ namespace App\Controller\Api;
use App\Controller\AbstractController; use App\Controller\AbstractController;
use App\Service\Api\Good\AddStapleFoodInfoService; use App\Service\Api\Good\AddStapleFoodInfoService;
use App\Service\Api\Good\MealListService; use App\Service\Api\Good\MealListService;
use App\Service\Api\Good\OptionalInfoService;
use App\Service\Api\Good\OptionalListService; use App\Service\Api\Good\OptionalListService;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
@@ -46,4 +47,15 @@ class GoodController extends AbstractController
{ {
return (new AddStapleFoodInfoService)->handle(); return (new AddStapleFoodInfoService)->handle();
} }
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
#[RequestMapping(path: 'optional_info',methods: 'GET')]
public function optionalInfo()
{
return (new OptionalInfoService)->handle();
}
} }

View File

@@ -134,6 +134,8 @@ class OrderController extends AbstractController
/** /**
* 评论 * 评论
* @return array * @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
#[RequestMapping(path: 'evaluation',methods: 'POST')] #[RequestMapping(path: 'evaluation',methods: 'POST')]
#[Scene(scene: 'evaluation')] #[Scene(scene: 'evaluation')]

View File

@@ -13,6 +13,7 @@ use Hyperf\DbConnection\Model\Model;
* @property int $id * @property int $id
* @property int $spu_id * @property int $spu_id
* @property string $title * @property string $title
* @property string $sub_title
* @property string $image_ids * @property string $image_ids
* @property string $price * @property string $price
* @property string $param * @property string $param
@@ -94,6 +95,21 @@ class Sku extends Model
->get(); ->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 * @param array $ids
* @return array * @return array

View File

@@ -109,6 +109,7 @@ class SkuService extends BaseService
$insertModel->spu_id = $spuId; $insertModel->spu_id = $spuId;
$insertModel->title = $spuInfo->title.$this->request->input('title'); $insertModel->title = $spuInfo->title.$this->request->input('title');
$insertModel->sub_title = $this->request->input('sub_title');
$insertModel->price = $this->request->input('price'); $insertModel->price = $this->request->input('price');
$insertModel->chef_id = $this->request->input('chef_id'); $insertModel->chef_id = $this->request->input('chef_id');
$insertModel->image_ids = $imageIds; $insertModel->image_ids = $imageIds;
@@ -193,6 +194,7 @@ class SkuService extends BaseService
$skuInfo->image_ids = $requestOssIds; $skuInfo->image_ids = $requestOssIds;
} }
$skuInfo->chef_id = $this->request->input('chef_id'); $skuInfo->chef_id = $this->request->input('chef_id');
$skuInfo->sub_title = $this->request->input('sub_title');
$skuInfo->param = $this->request->input('param',''); $skuInfo->param = $this->request->input('param','');
$skuInfo->extra = $this->request->input('extra',''); $skuInfo->extra = $this->request->input('extra','');
$skuInfo->total_stock = $this->request->input('stock'); $skuInfo->total_stock = $this->request->input('stock');

View File

@@ -0,0 +1,116 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\Good;
use App\Cache\Redis\Api\EvaluationCache;
use App\Exception\ErrException;
use App\Model\Chef;
use App\Model\Sku;
use App\Model\Spu;
use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Common\OssTrait;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class OptionalInfoService extends BaseService
{
use OssTrait;
/**
* @var Spu
*/
#[Inject]
protected Spu $spuModel;
/**
* @var Sku
*/
#[Inject]
protected Sku $skuModel;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
$spuId = (int)$this->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;
}
}
}