Files
hyperf_service/app/Service/Api/Evaluation/EvaluationService.php
2025-04-09 09:44:05 +08:00

107 lines
3.5 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\Evaluation;
use App\Cache\Redis\Api\EvaluationCache;
use App\Constants\Common\OrderCode;
use App\Exception\ErrException;
use App\Model\Evaluation;
use App\Model\Order;
use App\Model\OrderGood;
use App\Model\Sku;
use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Common\OssTrait;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use function Hyperf\Coroutine\co;
class EvaluationService extends BaseService
{
use OssTrait;
/**
* @var Order
*/
#[Inject]
protected Order $orderModel;
/**
* @var OrderGood
*/
#[Inject]
protected OrderGood $orderGoodModel;
/**
* @var Sku
*/
#[Inject]
protected Sku $skuModel;
/**
* @var EvaluationCache
*/
#[Inject]
protected EvaluationCache $evaluationCache;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
$orderGoodInfo = $this->orderGoodModel->find($this->request->input('order_good_id'));
if (empty($orderGoodInfo)) throw new ErrException('订单信息异常,请稍后再试或者联系客服');
if ($orderGoodInfo->is_comment == OrderCode::GOOD_COMMENT_FINISH) throw new ErrException('该商品您已完成评价,感谢您的参与');
$orderInfo = $this->orderModel->find($orderGoodInfo->order_id);
if (empty($orderInfo)) throw new ErrException('订单不存在');
if ($orderInfo->status != OrderCode::FINISH) throw new ErrException('订单尚未完成,请稍候');
$skuInfo = $this->skuModel->find($orderGoodInfo->sku_id);
if (empty($skuInfo)) throw new ErrException('该菜品信息不存在,请稍后再试');
$insertModel = Db::transaction(function () use ($orderInfo, $skuInfo, $orderGoodInfo) {
$insertModel = new Evaluation();
$insertModel->user_id = $orderInfo->user_id;
$insertModel->order_id = $orderGoodInfo->order_id;
$insertModel->order_good_id = $orderGoodInfo->id;
$insertModel->sku_id = $orderGoodInfo->sku_id;
$insertModel->chef_id = $skuInfo->chef_id;
$insertModel->score = $this->request->input('score');
if (!$this->request->input('image_ids')) {
$imageIds = explode(',',$this->request->input('image_ids'));
if (count($imageIds) > 9) throw new ErrException('超出可以传图片最大值');
$this->updateOssObjects($imageIds);
$insertModel->image_ids = $this->request->input('image_ids');
}
// $insertModel->image_ids = $imageIds ?? '';
$insertModel->content = $this->request->input('content');
if (!$insertModel->save()) throw new ErrException('评论失败-001');
$orderGoodInfo->is_comment = OrderCode::GOOD_COMMENT_FINISH;
if (!$orderGoodInfo->save()) throw new ErrException('评论失败-002');
//todo 厨师商品评分]
return $insertModel;
});
$this->evaluationCache->addChefEvaluation($skuInfo->chef_id, (int)$this->request->input('score'));
return $this->return->success('success', ['id' => $insertModel->id]);
}
}