287 lines
8.0 KiB
PHP
287 lines
8.0 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\Category;
|
|
use App\Model\Chef;
|
|
use App\Model\Cycle;
|
|
use App\Model\Kitchen;
|
|
use App\Model\Sku;
|
|
use App\Model\Spu;
|
|
use App\Model\SystemCity;
|
|
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');
|
|
$kitchenId = (int)$this->request->input('search_kitchen_id');
|
|
$cityId = (int)$this->request->input('search_city_id');
|
|
|
|
$list = $this->spuModel
|
|
->where('cycle_id',$cycleId)
|
|
->where('city_id',$cityId)
|
|
->where('kitchen_id',$kitchenId)
|
|
->where('is_del',GoodCode::SPU_IS_NO_DEL)
|
|
->where('type',$this->request->input('type'))
|
|
->paginate($limit)
|
|
->toArray();
|
|
|
|
if (empty($list['data'])) return $this->return->success('success', ['list' => $list]);
|
|
|
|
$spuIds = array_column($list['data'], 'id');
|
|
|
|
$skuList = $this->skuModel
|
|
->whereIn('spu_id',$spuIds)
|
|
->where('is_del',GoodCode::SKU_IS_NO_DEL)
|
|
->get();
|
|
|
|
if (empty($skuList)) return $this->return->success('success', ['list' => $list]);
|
|
$skuList = $skuList->toArray();
|
|
|
|
$imageIdArr = array_column($skuList,'image_ids');
|
|
$imageIds = array_unique(explode(',',implode(',',$imageIdArr)));
|
|
$imageList = $this->getOssObjects($imageIds);
|
|
|
|
$skuListArr = [];
|
|
foreach ($skuList as $sku) {
|
|
$imageOneArr = [];
|
|
foreach (explode(',',$sku['image_ids']) as $imageId) {
|
|
$imageOneArr[] = [
|
|
'id' => $imageId,
|
|
'url' => $imageList[$imageId]['url']
|
|
];
|
|
}
|
|
|
|
$sku['image_list'] = $imageOneArr;
|
|
|
|
if (empty($skuListArr[$sku['spu_id']])) {
|
|
$skuListArr[$sku['spu_id']] = [];
|
|
}
|
|
|
|
$skuListArr[$sku['spu_id']][] = $sku;
|
|
}
|
|
|
|
foreach ($list['data'] as &$item) {
|
|
$item['sku_list'] = $skuListArr[$item['id']] ?? [];
|
|
}
|
|
|
|
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');
|
|
|
|
$this->checkInfo();
|
|
|
|
$info = $this->spuModel->getInfoByCityIdAndCycleId($this->cityId, $cycleInfo->id,$title);
|
|
|
|
if (!empty($info)) throw new ErrException('该菜品在当前城市已存在');
|
|
|
|
|
|
$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->caterer_id = $this->request->input('caterer_id');
|
|
$insertModel->category_id = $this->request->input('category_id',0);
|
|
$insertModel->saleable = $this->request->input('saleable');
|
|
$insertModel->type = $this->request->input('type');
|
|
$insertModel->sort = $this->request->input('sort');
|
|
|
|
if (!$insertModel->save()) throw new ErrException('添加菜品失败');
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
/**
|
|
* @var Kitchen
|
|
*/
|
|
#[Inject]
|
|
protected Kitchen $kitchenModel;
|
|
|
|
/**
|
|
* @var AdminUser
|
|
*/
|
|
#[Inject]
|
|
protected AdminUser $adminUserModel;
|
|
|
|
/**
|
|
* @var Chef
|
|
*/
|
|
#[Inject]
|
|
protected Chef $chefModel;
|
|
|
|
/**
|
|
* 信息检测
|
|
* @return void
|
|
*/
|
|
private function checkInfo(): void
|
|
{
|
|
$kitchenId = (int)$this->request->input('kitchen_id');
|
|
$kitchenInfo = $this->kitchenModel->getInfoById($kitchenId);
|
|
$this->cityId = (int)$kitchenInfo->city_id;
|
|
|
|
if ($kitchenInfo->status == SiteCode::KITCHEN_DISABLE) throw new ErrException('该厨房已禁用');
|
|
|
|
$chefId = (int)$this->request->input('chef_id');
|
|
$chefInfo = $this->chefModel->getInfoById($chefId);
|
|
$chefUserInfo = $this->adminUserModel->getAdminInfoById($chefInfo->user_id);
|
|
|
|
if ($chefUserInfo->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->caterer_id = $this->request->input('caterer_id');
|
|
$info->sub_title = $this->request->input('sub_title','');
|
|
$info->category_id = $this->request->input('category_id',0);
|
|
$info->saleable = $this->request->input('saleable');
|
|
$info->type = $this->request->input('type');
|
|
$info->sort = $this->request->input('sort');
|
|
|
|
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();
|
|
}
|
|
|
|
/**
|
|
* @var SystemCity $systemCityModel
|
|
*/
|
|
#[Inject]
|
|
protected SystemCity $systemCityModel;
|
|
|
|
/**
|
|
* @var Category $categoryModel
|
|
*/
|
|
#[Inject]
|
|
protected Category $categoryModel;
|
|
|
|
|
|
/**
|
|
* spu 详情
|
|
* @return array
|
|
*/
|
|
public function view(): array
|
|
{
|
|
$id = (int)$this->request->input('id');
|
|
|
|
$info = $this->spuModel->getInfoById($id);
|
|
if (empty($info)) throw new ErrException('数据不存在');
|
|
|
|
$info = $info->toArray();
|
|
|
|
$info['city_name'] = $this->systemCityModel->getCityNameById((int)$info['city_id']);
|
|
$info['kitchen_name'] = $this->kitchenModel->getNameById((int)$info['kitchen_id']);
|
|
$info['chef_name'] = $this->chefModel->getChineseNameById((int)$info['chef_id']);
|
|
$info['category_name'] = $this->categoryModel->getNameById((int)$info['category_id']);
|
|
|
|
return $this->return->success('success',$info);
|
|
}
|
|
} |