Files
hyperf_service/app/Service/Admin/Good/SpuService.php
2025-01-08 15:45:28 +08:00

201 lines
5.2 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\Good;
use App\Constants\Admin\UserCode;
use App\Constants\Common\GoodCode;
use App\Constants\Common\SiteCode;
use App\Exception\ErrException;
use App\Model\AdminUser;
use App\Model\Cycle;
use App\Model\Kitchen;
use App\Model\Sku;
use App\Model\Spu;
use App\Service\Admin\BaseService;
use App\Service\ServiceTrait\Admin\GetUserInfoTrait;
use App\Service\ServiceTrait\Common\OssTrait;
use Exception;
use Hyperf\Di\Annotation\Inject;
class SpuService extends BaseService
{
use GetUserInfoTrait,OssTrait;
private $userInfo;
private int $cityId;
/**
* @var Spu
*/
#[Inject]
protected Spu $spuModel;
/**
* @var Sku
*/
#[Inject]
protected Sku $skuModel;
public function __construct()
{
parent::__construct();
$this->userInfo = $this->getUserInfo($this->adminId);
$this->cityId = (int)$this->userInfo['city_id'];
}
/**
* @return array
*/
public function handle(): array
{
$limit = (int)$this->request->input('limit', 10);
$cycleId = (int)$this->request->input('cycle_id');
$list = $this->spuModel->where('cycle_id',$cycleId)->where('city_id',$this->cityId)->paginate($limit)->toArray();
return $this->return->success('success', ['list' => $list]);
}
/**
* @var Cycle
*/
#[Inject]
protected Cycle $cycleModel;
/**
* 添加 spu
* @return array
*/
public function add(): array
{
$cycleId = (int)$this->request->input('cycle_id');
$cycleInfo = $this->cycleModel->getInfoById($cycleId);
if (empty($cycleInfo)) throw new ErrException('没有该周期,请刷新后重新上传');
$title = $this->request->input('title');
$info = $this->spuModel->getInfoByCityIdAndCycleId($this->cityId, $cycleInfo->id,$title);
if (!empty($info)) throw new ErrException('该菜品在当前城市已存在');
$this->checkInfo();
$insertModel = new Spu();
$insertModel->city_id = $this->cityId;
$insertModel->cycle_id = $cycleInfo->id;
$insertModel->kitchen_id = $this->request->input('kitchen_id');
$insertModel->chef_id = $this->request->input('chef_id');
$insertModel->title = $title;
$insertModel->sub_title = $this->request->input('sub_title','');
$insertModel->category_id = $this->request->input('category_id');
$insertModel->saleable = $this->request->input('saleable');
if (!$insertModel->save()) throw new ErrException('添加菜品失败');
return $this->return->success();
}
/**
* @var Kitchen
*/
#[Inject]
protected Kitchen $kitchenModel;
/**
* @var AdminUser
*/
#[Inject]
protected AdminUser $adminUserModel;
/**
* 信息检测
* @return void
*/
private function checkInfo(): void
{
$kitchenId = $this->request->input('kitchen_id');
$kitchenInfo = $this->kitchenModel->getInfoById($kitchenId);
if ($kitchenInfo->status == SiteCode::KITCHEN_DISABLE) throw new ErrException('该厨房已禁用');
$chefId = $this->request->input('chef_id');
$chefInfo = $this->adminUserModel->getAdminInfoById($chefId);
if ($chefInfo->status == UserCode::DISABLE) throw new ErrException('该厨师已禁用');
}
/**
* 修改
* @return array
*/
public function edit(): array
{
$id = (int)$this->request->input('id');
$info = $this->spuModel->getInfoById($id);
if (empty($info)) throw new ErrException('数据不存在');
$this->checkInfo();
$info->kitchen_id = $this->request->input('kitchen_id');
$info->chef_id = $this->request->input('chef_id');
$info->title = $this->request->input('title');
$info->sub_title = $this->request->input('sub_title','');
$info->category_id = $this->request->input('category_id');
$info->saleable = $this->request->input('saleable');
if (!$info->save()) throw new ErrException('修改菜品失败');
return $this->return->success();
}
/**
* spu 删除
* @return array
* @throws Exception
*/
public function del(): array
{
$id = (int)$this->request->input('id');
$info = $this->spuModel->getInfoById($id);
if (empty($info)) throw new ErrException('数据已删除');
//todo 需要联动删除sku 已经有用户下单sku该怎么办
//删除所有 sku
$this->skuModel->where('spu_id',$info->id)->update(['is_del' => GoodCode::SKU_IS_DELETE]);
//没有直接删除菜
$info->is_del = GoodCode::SPU_IS_DELETE;
$info->save();
return $this->return->success();
}
/**
* spu 详情
* @return array
*/
public function view(): array
{
$id = (int)$this->request->input('id');
$info = $this->spuModel->getInfoById($id);
if (empty($info)) throw new ErrException('数据不存在');
return $this->return->success('success',$info->toArray());
}
}