Files
hyperf_service/app/Service/Admin/Good/SkuService.php
2025-04-10 15:51:22 +08:00

264 lines
8.4 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\Cache\Redis\Api\ApiRedisKey;
use App\Cache\Redis\Api\GoodCache;
use App\Constants\Admin\UserCode;
use App\Constants\Common\GoodCode;
use App\Exception\ErrException;
use App\Model\AdminUser;
use App\Model\Chef;
use App\Model\Sku;
use App\Model\Spu;
use App\Service\Admin\BaseService;
use App\Service\ServiceTrait\Common\OssTrait;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class SkuService extends BaseService
{
use OssTrait;
/**
* @var Sku $skuModel
*/
#[Inject]
protected Sku $skuModel;
/**
* @var Spu
*/
#[Inject]
protected Spu $spuModel;
/**
* @var Chef
*/
#[Inject]
protected Chef $chefModel;
/**
* @var AdminUser
*/
#[Inject]
protected AdminUser $adminUserModel;
/**
* @return array
*/
public function handle(): array
{
$limit = (int)$this->request->input('limit', 10);
$spuId = (int)$this->request->input('spu_id');
$list = $this->skuModel->where('spu_id',$spuId)->paginate($limit)->toArray();
$imageIdArr = array_column($list['data'],'image_ids');
$imageIds = array_unique(explode(',',implode(',',$imageIdArr)));
$imageList = $this->getOssObjects($imageIds);
foreach ($list['data'] as &$item) {
$imageOneArr = [];
foreach (explode(',',$item['image_ids']) as $imageId) {
$imageOneArr[] = [
'id' => $imageId,
'url' => $imageList[$imageId]['url']
];
}
$item['image_list'] = $imageOneArr;
}
return $this->return->success('success', ['list' => $list]);
}
/**
* @var GoodCache
*/
#[Inject]
protected GoodCache $goodCache;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function add(): array
{
$spuId = $this->request->input('spu_id');
$spuInfo = $this->spuModel->getInfoById($spuId);
if (empty($spuInfo)) throw new ErrException('请选择spu');
$this->checkChefInfo();
$insertModel = new Sku();
$imageIds = $this->request->input('image_ids');
$this->updateOssObjects(explode(',',$imageIds));
$insertModel->spu_id = $spuId;
$insertModel->title = $this->request->input('title');
$insertModel->sub_title = $this->request->input('sub_title');
$insertModel->price = $this->request->input('price');
$insertModel->chef_id = $this->request->input('chef_id');
$insertModel->image_ids = $imageIds;
$insertModel->param = $this->request->input('param','');
$insertModel->extra = $this->request->input('extra','');
$insertModel->total_stock = $this->request->input('stock');
$insertModel->surplus_stock = $this->request->input('stock');
$insertModel->sales_num = 0;
$insertModel->order_num = 0;
$insertModel->cancel_num = 0;
$insertModel->refund_num = 0;
$insertModel->occupied = $this->request->input('occupied',1);
// $insertModel->behind_refund_num = 0;
$insertModel->saleable = $this->request->input('saleable');
$insertModel->is_add_staple_food = $this->request->input('is_add_staple_food');
$insertModel->code_number = $this->request->input('code_number');
$insertModel->sort = $this->request->input('sort');
if (!$insertModel->save()) throw new ErrException('添加失败');
//删除缓存
$this->goodCache->delCache($spuInfo->kitchen_id,$spuInfo->cycle_id);
return $this->return->success();
}
/**
* @return void
*/
private function checkChefInfo()
{
$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
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function edit(): array
{
$id = (int)$this->request->input('id');
$skuInfo = $this->skuModel->getInfoById($id);
$this->checkChefInfo();
if (empty($skuInfo)) throw new ErrException('原数据不存在,无法修改');
$spuInfo = $this->spuModel->getInfoById($skuInfo->spu_id);
if (empty($spuInfo)) throw new ErrException('数据出错');
$stock = $this->request->input('stock');
$forceCleanStockCache = 0;
if ($skuInfo->order_num > 0) { //已产生订单 修改库存需要强制删除库存缓存
$forceCleanStockCache = 1;
if ($skuInfo->total_stock < $stock) $skuInfo->total_stock = $stock;
if ($skuInfo->total_stock > $stock) {
$skuInfo->total_stock = $stock;
if ($skuInfo->order_num >= $stock) throw new ErrException('库存不能小于已下单量');
}
}
$skuInfo->surplus_stock = $skuInfo->total_stock - $skuInfo->order_num;
if ($skuInfo->order_num > 0 && $skuInfo->price != $this->request->input('price')) throw new ErrException('已有订单不可改价,退单后即可操作');
else $skuInfo->price = $this->request->input('price');
$requestOssIds = $this->request->input('image_ids');
$updateOssIds = array_diff(explode(',',$requestOssIds), explode(',',$skuInfo->image_ids));
$delOssIds = array_diff(explode(',',$skuInfo->image_ids), explode(',',$requestOssIds));
if (!empty($updateOssIds)) $this->updateOssObjects($updateOssIds);
if (!empty($delOssIds)) $this->updateOssObjectsDisable($delOssIds);
if (!empty($updateOssIds) || !empty($delOssIds)) {
$skuInfo->image_ids = $requestOssIds;
}
$skuInfo->chef_id = $this->request->input('chef_id');
$skuInfo->sub_title = $this->request->input('sub_title');
$skuInfo->param = $this->request->input('param','');
$skuInfo->extra = $this->request->input('extra','');
$skuInfo->total_stock = $this->request->input('stock');
$skuInfo->saleable = $this->request->input('saleable');
$skuInfo->title = $this->request->input('title');
$skuInfo->sort = $this->request->input('sort');
$skuInfo->is_add_staple_food = $this->request->input('is_add_staple_food');
$skuInfo->code_number = $this->request->input('code_number');
$skuInfo->occupied = $this->request->input('occupied',1);
if (!$skuInfo->save()) throw new ErrException('修改失败');
if ($forceCleanStockCache == 1) return $this->return->success();
//删除缓存
$this->goodCache->delCache($spuInfo->kitchen_id,$spuInfo->cycle_id);
return $this->return->success();
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function del(): array
{
$id = (int)$this->request->input('id');
$skuInfo = $this->skuModel->getInfoById($id);
if (empty($skuInfo)) throw new ErrException('数据已删除,无法修改');
$spuInfo = $this->spuModel->getInfoById($skuInfo->spu_id);
if (empty($spuInfo)) throw new ErrException('数据出错');
$skuInfo->is_del = GoodCode::SKU_IS_DELETE;
if (!$skuInfo->save()) throw new ErrException('删除失败');
//删除缓存
$this->goodCache->delCache($spuInfo->kitchen_id,$spuInfo->cycle_id);
return $this->return->success();
}
/**
* @return array
*/
public function view(): array
{
$id = (int)$this->request->input('id');
$skuInfo = $this->skuModel->getInfoById($id);
if (empty($skuInfo)) throw new ErrException('数据不存在');
$imageIds = explode(',',$skuInfo->image_ids);
$imageList = $this->getOssObjects($imageIds);
$res = $skuInfo->toArray();
$res['image_list'] = array_values($imageList);
$res['chef_name'] = $this->chefModel->getChineseNameById((int)$res['chef_id']);
return $this->return->success('success',$res);
}
}