Files
hyperf_service/app/Service/Api/Chef/IndexService.php
2025-04-09 09:40:05 +08:00

148 lines
4.2 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\Chef;
use App\Cache\Redis\Api\EvaluationCache;
use App\Exception\ErrException;
use App\Model\AdminUser;
use App\Model\Chef;
use App\Model\Evaluation;
use App\Model\Order;
use App\Model\Sku;
use App\Model\User;
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 IndexService extends BaseService
{
use OssTrait;
/**
* @var AdminUser
*/
#[Inject]
protected AdminUser $adminUserModel;
/**
* @var Chef
*/
#[Inject]
protected Chef $chefModel;
/**
* @var EvaluationCache
*/
protected EvaluationCache $evaluationCache;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
$chefId = $this->request->input('chef_id');
$chef = $this->chefModel
->leftJoin('admin_user', 'admin_user.id', '=', 'chef.user_id')
->where('chef.user_id', $chefId)
->select([
'admin_user.id',
'admin_user.username',
'admin_user.avatar',
'admin_user.chinese_name',
'chef.profile',
'chef.specialties',
])
->first();
if (empty($chef)) throw new ErrException('厨师不存在');
$avatarUrl = $this->getOssObjectById($chef->avatar);
$res = $chef->toArray();
$res['avatar_url'] = $avatarUrl;
$res['avg_score'] = $this->evaluationCache->getChefEvaluation($chefId) ?? 0;
// 评价补全
$res['evaluation_list'] = $this->getEvaluationList($chefId);
return $this->return->success('success', $res);
}
/**
* @var Evaluation
*/
#[Inject]
protected Evaluation $evaluationModel;
/**
* @var Sku
*/
#[Inject]
protected Sku $skuModel;
/**
* @var Order
*/
#[Inject]
protected Order $orderModel;
/**
* @var User
*/
#[Inject]
protected User $userModel;
private function getEvaluationList(int $chefId): array
{
$limit = $this->request->input('limit') ?? 10;
$data = $this->evaluationModel
->where('chef_id', $chefId)
->orderByDesc('id')
->paginate($limit,['content','image_ids','id','order_id','user_id','sku_id','score','content'])
->toArray();
if (empty($data['data'])) return [];
$skuIds = array_column($data['data'], 'sku_id');
$orderIds = array_column($data['data'], 'order_id');
$userIds = array_column($data['data'], 'user_id');
$skuList = $this->skuModel->whereIn('id', $skuIds)->pluck('title','id')->toArray();
$orderTimes = $this->orderModel->whereIn('id', $orderIds)->pluck('create_time','id')->toArray();
// $skuList = array_column($skuList, null,'id');
$userList = $this->userModel->whereIn('id', $userIds)->select(['nickname','id','avatar_id'])->get()->toArray();
$userList = array_column($userList, null,'id');
$imageIds = array_column($userList, 'avatar_id');
$imageIdArr = array_column($data['data'],'image_ids');
$listImageIds = array_unique(explode(',',implode(',',$imageIdArr)));
$totalImages = array_merge($imageIds,$listImageIds);
unset($listImageIds,$imageIds);
$imageList = $this->getOssObjects($totalImages);
foreach ($data['data'] as &$item) {
$item['sku_title'] = $skuList[$item['sku_id']]['title'] ?? '';
$item['order_time'] = $orderTimes[$item['order_id']] ?? '';
$oneImage = [];
foreach (explode(',',$item['image_ids']) as $imageId) {
$oneImage[] = $imageList[$imageId]['url'] ?? '';
}
$item['content_image_list'] = $oneImage;
$item['nickname'] = $userList[$item['user_id']]['nickname'] ?? '';
$item['avatar'] = $imageList[$userList[$item['user_id']]['avatar_id']]['url'] ?? '';
}
return $data;
}
}