feat: sku
This commit is contained in:
@@ -93,15 +93,16 @@ class OrderGoodStockConsumer extends ConsumerMessage
|
||||
$this->log->debug('OrderGoodStockConsumer:error:NoSkuData:'.json_encode(array_column($this->orderGoodArr, 'sku_id')));
|
||||
return Result::ACK;
|
||||
}
|
||||
$this->skuArr = array_column($this->skuArr, null,'id');
|
||||
|
||||
$this->updateArr = [];
|
||||
|
||||
try {
|
||||
//todo 是否做个优化 截单后 不再增加库存
|
||||
match ($data['type']) {
|
||||
OrderCode::WAIT_PAY => $this->waitPaySubStock(),
|
||||
OrderCode::UNCOMPLETED_REFUND => $this->unFinishRefundAddStock(),
|
||||
OrderCode::CANCEL => $this->cancelAddStock(),
|
||||
OrderCode::FINISH_REFUND => $this->finishRefundUpdateData(),
|
||||
OrderCode::FINISH_REFUND,OrderCode::UNCOMPLETED_REFUND => $this->RefundUpdateData($data),
|
||||
default => throw new Exception('OrderGoodStockConsumer:error:无效的订单类型')
|
||||
};
|
||||
|
||||
@@ -130,20 +131,28 @@ class OrderGoodStockConsumer extends ConsumerMessage
|
||||
foreach ($this->orderGoodArr as $orderGood) {
|
||||
$this->updateArr[] = [
|
||||
'id' => $orderGood['sku_id'],
|
||||
''
|
||||
'sales_num' => ($this->skuArr[$orderGood['sku_id']]['sales_num'] ?? 0) + $orderGood['quantity'],
|
||||
'order_num' => ($this->skuArr[$orderGood['sku_id']]['order_num'] ?? 0) + 1,
|
||||
'surplus_stock' => ($this->skuArr[$orderGood['sku_id']]['surplus_stock'] ?? 0) - $orderGood['quantity'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
private function unFinishRefundAddStock(): void
|
||||
private function RefundUpdateData($data): void
|
||||
{
|
||||
foreach ($this->orderGoodArr as $orderGood) {
|
||||
if (empty($data['refund_goods'])) throw new Exception('OrderGoodStockConsumer:error:UpdateSkuDataFail:'.json_encode($data));
|
||||
|
||||
foreach ($data['refund_goods'] as $orderGood) {
|
||||
$this->updateArr[] = [
|
||||
'id' => $orderGood['sku_id'],
|
||||
''
|
||||
'refund_num' => ($this->skuArr[$orderGood['sku_id']]['refund_num'] ?? 0) + $orderGood['quantity'],
|
||||
'order_num' => ($this->skuArr[$orderGood['sku_id']]['order_num'] ?? 0) - 1,
|
||||
'surplus_stock' => ($this->skuArr[$orderGood['sku_id']]['surplus_stock'] ?? 0) + $orderGood['quantity'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -156,20 +165,9 @@ class OrderGoodStockConsumer extends ConsumerMessage
|
||||
foreach ($this->orderGoodArr as $orderGood) {
|
||||
$this->updateArr[] = [
|
||||
'id' => $orderGood['sku_id'],
|
||||
''
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function finishRefundUpdateData(): void
|
||||
{
|
||||
foreach ($this->orderGoodArr as $orderGood) {
|
||||
$this->updateArr[] = [
|
||||
'id' => $orderGood['sku_id'],
|
||||
''
|
||||
'cancel_num' => ($this->skuArr[$orderGood['sku_id']]['cancel_num'] ?? 0) + $orderGood['quantity'],
|
||||
'order_num' => ($this->skuArr[$orderGood['sku_id']]['order_num'] ?? 0) - 1,
|
||||
'surplus_stock' => ($this->skuArr[$orderGood['sku_id']]['surplus_stock'] ?? 0) + $orderGood['quantity'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,4 +286,30 @@ class GoodCache
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $kitchenId
|
||||
* @param int $cycleId
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function delCache(int $kitchenId,int $cycleId)
|
||||
{
|
||||
$keyArr = [
|
||||
ApiRedisKey::mealGoodListKey($cycleId,$kitchenId),
|
||||
ApiRedisKey::optionalGoodListKey($cycleId,$kitchenId),
|
||||
ApiRedisKey::goodStockKey($cycleId,$kitchenId)
|
||||
];
|
||||
|
||||
$script = <<<LUA
|
||||
local corpseCount = 0
|
||||
for _, key in ipairs(KEYS) do
|
||||
corpseCount = corpseCount + redis.call('DEL', key)
|
||||
end
|
||||
return corpseCount
|
||||
LUA;
|
||||
|
||||
$this->redis->eval($script, $keyArr,count($keyArr));
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ class OrderCode
|
||||
CONST DEPART = 31; //已出发
|
||||
CONST ARRIVE = 41; // 已送达(待取餐)
|
||||
CONST FINISH = 51; //已完成
|
||||
const UNCOMPLETED_REFUND = 97; //未完成退款
|
||||
const UNCOMPLETED_REFUND = 97; //未完成退款 部分退款
|
||||
CONST FINISH_REFUND = 98; //完成后退款
|
||||
CONST CANCEL = 99; //取消
|
||||
|
||||
|
||||
@@ -10,12 +10,17 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\Good;
|
||||
|
||||
use App\Cache\Redis\Api\ApiRedisKey;
|
||||
use App\Cache\Redis\Api\GoodCache;
|
||||
use App\Constants\Common\GoodCode;
|
||||
use App\Exception\ErrException;
|
||||
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
|
||||
{
|
||||
@@ -27,6 +32,12 @@ class SkuService extends BaseService
|
||||
#[Inject]
|
||||
protected Sku $skuModel;
|
||||
|
||||
/**
|
||||
* @var Spu
|
||||
*/
|
||||
#[Inject]
|
||||
protected Spu $spuModel;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
@@ -56,12 +67,22 @@ class SkuService extends BaseService
|
||||
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');
|
||||
|
||||
$insertModel = new Sku();
|
||||
|
||||
@@ -87,13 +108,16 @@ class SkuService extends BaseService
|
||||
|
||||
if (!$insertModel->save()) throw new ErrException('添加失败');
|
||||
|
||||
//todo 生成缓存
|
||||
//删除缓存
|
||||
$this->goodCache->delCache($spuInfo->kitchen_id,$spuInfo->cycle_id);
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function edit(): array
|
||||
{
|
||||
@@ -103,6 +127,9 @@ class SkuService extends BaseService
|
||||
|
||||
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) { //已产生订单 修改库存需要强制删除库存缓存
|
||||
@@ -142,26 +169,33 @@ class SkuService extends BaseService
|
||||
if (!$skuInfo->save()) throw new ErrException('修改失败');
|
||||
|
||||
if ($forceCleanStockCache == 1) return $this->return->success();
|
||||
//todo 修改缓存
|
||||
|
||||
//删除缓存
|
||||
$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('删除失败');
|
||||
|
||||
//todo 删除缓存
|
||||
//删除缓存
|
||||
$this->goodCache->delCache($spuInfo->kitchen_id,$spuInfo->cycle_id);
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
@@ -186,4 +220,5 @@ class SkuService extends BaseService
|
||||
|
||||
return $this->return->success('success',$res);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user