Files
hyperf_service/app/Service/Admin/Good/SkuService.php
2025-05-20 15:59:37 +08:00

312 lines
11 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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\Purchase;
use App\Model\Sku;
use App\Model\Spu;
use App\Service\Admin\BaseService;
use App\Service\ServiceTrait\Common\OssTrait;
use Hyperf\DbConnection\Db;
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 = [];
$item['image_list'] = $imageOneArr;
if (empty($item['image_ids'])) continue;
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');
$insertModel->origin_sku_id = 0;
$insertModel->favorable = GoodCode::NOT_FAVORABLE;
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('数据出错');
$originSkuInfo = null;
if ($skuInfo->favorable == GoodCode::IS_FAVORABLE) {
$originSkuInfo = $this->skuModel->find($skuInfo->origin_sku_id);
if (empty($originSkuInfo)) throw new ErrException('原sku数据不存在无法修改');
}
$stock = $this->request->input('stock');
$forceCleanStockCache = 0;
if ($skuInfo->order_num > 0) { //已产生订单 修改库存需要强制删除库存缓存
$forceCleanStockCache = 1;
$originTotalStock = $skuInfo->total_stock;
if ($skuInfo->total_stock < $stock) $skuInfo->total_stock = $stock;
else {
$skuInfo->total_stock = $stock;
if ($skuInfo->order_num >= $stock) throw new ErrException('库存不能小于已下单量');
}
if ($originSkuInfo && $skuInfo->total_stock > $originSkuInfo->total_stock) throw new ErrException('修改后的库存不能大于原商品库存');
$skuInfo->surplus_stock = $skuInfo->total_stock - $skuInfo->order_num;
if ($originSkuInfo) {
$originSkuInfo->total_stock = $originSkuInfo->total_stock - $originTotalStock + ($originTotalStock - $skuInfo->order_num);
$originSkuInfo->surplus_stock = $originSkuInfo->surplus_stock - $originTotalStock + ($originTotalStock - $skuInfo->order_num);
if ($originSkuInfo->total_stock < 0 || $originSkuInfo->surplus_stock < 0) throw new ErrException('秒杀库存数据过多,原商品库存不足');
}
} else {
$skuInfo->total_stock = $stock;
$skuInfo->surplus_stock = $stock;
}
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',[]);
$originSkuImageIds = !empty($skuInfo->image_ids) ? explode(',',$skuInfo->image_ids) : [];
$updateOssIds = array_diff(explode(',',$requestOssIds),$originSkuImageIds);
$delOssIds = array_diff($originSkuImageIds, 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();
}
/**
* @var Purchase
*/
#[Inject]
protected Purchase $purchaseModel;
/**
* @return array
*/
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('数据出错');
$purchaseInfo = $this->purchaseModel->getListByCycleIdAndKitchenId($skuInfo->cycle_id,$spuInfo->kitchen_id);
$purchaseIds = [];
if (!empty($purchaseInfo)) {
foreach ($purchaseInfo->toArray() as $one) {
$oneSkuArr = explode(',',$one['sku_ids']);
if (!in_array($skuInfo->id,$oneSkuArr)) continue;
$purchaseIds = $one['id'];
}
}
Db::transaction(function () use ($id,$purchaseIds,$spuInfo,$skuInfo) {
$skuInfo->is_del = GoodCode::SKU_IS_DELETE;
if (!$skuInfo->save()) throw new ErrException('删除失败');
if (!empty($purchaseIds) && !$this->purchaseModel->whereIn('id',$purchaseIds)->delete()) throw new ErrException('删除联动数据失败');
//删除缓存
$this->goodCache->delCache($spuInfo->kitchen_id,$spuInfo->cycle_id);
$this->goodCache->delPurchaseCache($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);
}
}