feat: sku
This commit is contained in:
@@ -10,12 +10,151 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\Good;
|
||||
|
||||
use App\Constants\Common\GoodCode;
|
||||
use App\Exception\ErrException;
|
||||
use App\Model\Sku;
|
||||
use App\Service\Admin\BaseService;
|
||||
use App\Service\ServiceTrait\Common\OssTrait;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class SkuService extends BaseService
|
||||
{
|
||||
use OssTrait;
|
||||
|
||||
/**
|
||||
* @var Sku $skuModel
|
||||
*/
|
||||
#[Inject]
|
||||
protected Sku $skuModel;
|
||||
|
||||
public function handle()
|
||||
{
|
||||
//todo Write logic
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function add(): array
|
||||
{
|
||||
$spuId = $this->request->input('spu_id');
|
||||
|
||||
$insertModel = new Sku();
|
||||
|
||||
$imageIds = $this->request->input('image_ids');
|
||||
|
||||
$this->updateOssObjects(explode(',',$imageIds));
|
||||
|
||||
$insertModel->spu_id = $spuId;
|
||||
$insertModel->title = $this->request->input('title');
|
||||
$insertModel->price = $this->request->input('price');
|
||||
$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->ahead_refund_num = 0;
|
||||
$insertModel->behind_refund_num = 0;
|
||||
$insertModel->saleable = $this->request->input('saleable');
|
||||
|
||||
if (!$insertModel->save()) throw new ErrException('添加失败');
|
||||
|
||||
//todo 生成缓存
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function edit(): array
|
||||
{
|
||||
$id = (int)$this->request->input('id');
|
||||
|
||||
$skuInfo = $this->skuModel->getInfoById($id);
|
||||
|
||||
if (empty($skuInfo)) 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('库存不能小于已下单量');
|
||||
}
|
||||
}
|
||||
|
||||
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('oss_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->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');
|
||||
|
||||
if (!$skuInfo->save()) throw new ErrException('修改失败');
|
||||
|
||||
if ($forceCleanStockCache == 1) return $this->return->success();//todo 修改缓存
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function del(): array
|
||||
{
|
||||
$id = (int)$this->request->input('id');
|
||||
$skuInfo = $this->skuModel->getInfoById($id);
|
||||
|
||||
if (empty($skuInfo)) throw new ErrException('数据已删除,无法修改');
|
||||
|
||||
$skuInfo->is_del = GoodCode::SKU_IS_DELETE;
|
||||
|
||||
if (!$skuInfo->save()) throw new ErrException('删除失败');
|
||||
|
||||
//todo 删除缓存
|
||||
|
||||
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'] = $imageList;
|
||||
|
||||
return $this->return->success('success',$res);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user