187 lines
4.6 KiB
PHP
187 lines
4.6 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\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 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'];
|
|
}
|
|
|
|
|
|
public function handle()
|
|
{
|
|
|
|
}
|
|
|
|
/**
|
|
* @var Cycle
|
|
*/
|
|
#[Inject]
|
|
protected Cycle $cycleModel;
|
|
|
|
/**
|
|
* 添加 spu
|
|
* @return array
|
|
*/
|
|
public function add(): array
|
|
{
|
|
$date = $this->request->input('date',date('Y-m-d'));
|
|
|
|
$cycleInfo = $this->cycleModel->getInfoByDate($date);
|
|
|
|
if (empty($cycleInfo)) throw new ErrException('没有该周期,请刷新后重新上传');
|
|
|
|
$info = $this->spuModel->getInfoByCityIdAndCycleId($this->cityId, $cycleInfo->id);
|
|
|
|
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 = $this->request->input('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
|
|
{
|
|
$editId = $this->request->input('edit_id');
|
|
|
|
$info = $this->spuModel->getInfoById($editId);
|
|
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 = $this->request->input('id');
|
|
|
|
$info = $this->spuModel->getInfoById($id);
|
|
if (empty($info)) throw new ErrException('数据已删除');
|
|
|
|
//todo 需要联动删除sku 已经有用户下单sku该怎么办
|
|
|
|
//没有直接删除菜
|
|
$info->delete();
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
/**
|
|
* spu 详情
|
|
* @return array
|
|
*/
|
|
public function view(): array
|
|
{
|
|
$id = $this->request->input('id');
|
|
|
|
$info = $this->spuModel->getInfoById($id);
|
|
if (empty($info)) throw new ErrException('数据不存在');
|
|
|
|
return $this->return->success('success',$info->toArray());
|
|
}
|
|
} |