116 lines
3.0 KiB
PHP
116 lines
3.0 KiB
PHP
<?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;
|
|
}
|
|
}
|
|
} |