feat : refund
This commit is contained in:
@@ -4,12 +4,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Amqp\Consumer;
|
||||
|
||||
use App\Cache\Redis\Api\ApiRedisKey;
|
||||
use App\Constants\Common\OrderCode;
|
||||
use App\Lib\Log;
|
||||
use App\Model\Order;
|
||||
use App\Model\OrderGood;
|
||||
use App\Model\Sku;
|
||||
use App\Service\ServiceTrait\Api\OrderTrait;
|
||||
use App\Service\ServiceTrait\Common\CycleTrait;
|
||||
use App\Service\ServiceTrait\Common\StockTrait;
|
||||
use Exception;
|
||||
use Hyperf\Amqp\Message\Type;
|
||||
@@ -18,6 +20,7 @@ use Hyperf\Amqp\Annotation\Consumer;
|
||||
use Hyperf\Amqp\Message\ConsumerMessage;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Redis\Redis;
|
||||
use PhpAmqpLib\Message\AMQPMessage;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
@@ -27,6 +30,7 @@ use Throwable;
|
||||
class OrderGoodStockConsumer extends ConsumerMessage
|
||||
{
|
||||
use StockTrait;
|
||||
use CycleTrait;
|
||||
|
||||
/**
|
||||
* @var Type|string 消息类型
|
||||
@@ -51,6 +55,18 @@ class OrderGoodStockConsumer extends ConsumerMessage
|
||||
#[Inject]
|
||||
protected Sku $skuModel;
|
||||
|
||||
/**
|
||||
* @var Order
|
||||
*/
|
||||
#[Inject]
|
||||
protected Order $orderModel;
|
||||
|
||||
/**
|
||||
* @var Order
|
||||
*/
|
||||
protected Order $orderInfo;
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
@@ -66,6 +82,11 @@ class OrderGoodStockConsumer extends ConsumerMessage
|
||||
*/
|
||||
private array $updateArr;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $reduceStockCache = [];
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @param AMQPMessage $message
|
||||
@@ -81,9 +102,22 @@ class OrderGoodStockConsumer extends ConsumerMessage
|
||||
}
|
||||
|
||||
$orderId = (int)$data['order_id'];
|
||||
$this->orderInfo = $this->orderModel->getInfoById($orderId);
|
||||
|
||||
if ($this->orderInfo->cycle_id != $this->initTodayCycleId()) {
|
||||
$this->log->error('OrderGoodStockConsumer:error:cycleIdError:'.json_encode([
|
||||
'order_id' => $orderId,
|
||||
'cycle_id' => $this->initTodayCycleId(),
|
||||
'order' => $this->orderInfo->toArray(),
|
||||
]));
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
$this->orderGoodArr = [];
|
||||
$this->skuArr = [];
|
||||
|
||||
|
||||
|
||||
$this->orderGoodArr = $this->orderGoodModel->getGoodIdsByOrderId($orderId);
|
||||
// $this->log->debug('OrderGoodStockConsumer:'.json_encode($this->orderGoodArr));
|
||||
if (empty($this->orderGoodArr)) {
|
||||
@@ -101,11 +135,11 @@ class OrderGoodStockConsumer extends ConsumerMessage
|
||||
$this->updateArr = [];
|
||||
|
||||
try {
|
||||
//todo 是否做个优化 截单后 不再增加库存
|
||||
// 是否做个优化 截单后 不再增加库存
|
||||
match ($data['type']) {
|
||||
OrderCode::WAIT_PAY => $this->waitPaySubStock(),
|
||||
OrderCode::CANCEL => $this->cancelAddStock(),
|
||||
// OrderCode::FINISH_REFUND,OrderCode::UNCOMPLETED_REFUND => $this->RefundUpdateData($data),//todo 退款后 库存不回收 因为存在退款一部分 无法核查
|
||||
OrderCode::FINISH_REFUND => $this->RefundUpdateData(),// 用户退款后 库存回收 后台操作不回收 因为存在退款一部分 无法核查
|
||||
default => throw new Exception('OrderGoodStockConsumer:error:无效的订单类型')
|
||||
};
|
||||
|
||||
@@ -131,13 +165,53 @@ class OrderGoodStockConsumer extends ConsumerMessage
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return Result::ACK;
|
||||
} catch (Exception $e) {
|
||||
if (!empty($this->rollbackStockCache)) {
|
||||
foreach ($this->rollbackStockCache as $goodId => $stock) {
|
||||
$this->redis->zIncrBy($this->stockKey,-$stock,$goodId);
|
||||
}
|
||||
}
|
||||
|
||||
$this->log->error($e->getMessage());
|
||||
return Result::ACK;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @var Redis
|
||||
*/
|
||||
#[Inject]
|
||||
protected Redis $redis;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $rollbackStockCache = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $stockKey;
|
||||
|
||||
private function stockCacheAction()
|
||||
{
|
||||
$this->stockKey = ApiRedisKey::goodStockKey($this->orderInfo->cycle_id,$this->orderInfo->kitchen_id);
|
||||
if (empty($this->reduceStockCache)) return;
|
||||
|
||||
$this->rollbackStockCache = [];
|
||||
|
||||
foreach ($this->reduceStockCache as $goodId => $stock) {
|
||||
$this->rollbackStockCache[$goodId] = $stock;
|
||||
if (!($this->redis->zIncrBy($this->stockKey,$stock,$goodId))) {
|
||||
throw new Exception('cache error');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
@@ -154,21 +228,19 @@ class OrderGoodStockConsumer extends ConsumerMessage
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
private function RefundUpdateData($data): void
|
||||
private function RefundUpdateData(): void
|
||||
{
|
||||
if (empty($data['refund_goods'])) throw new Exception('OrderGoodStockConsumer:error:UpdateSkuDataFail:'.json_encode($data));
|
||||
|
||||
foreach ($data['refund_goods'] as $orderGood) {
|
||||
foreach ($this->orderGoodArr as $orderGood) {
|
||||
$this->updateArr[] = [
|
||||
'id' => $orderGood['sku_id'],
|
||||
'refund_num' => ($this->skuArr[$orderGood['sku_id']]['refund_num'] ?? 0) + $orderGood['quantity'],
|
||||
'refund_num' => ($this->skuArr[$orderGood['sku_id']]['cancel_num'] ?? 0) + $orderGood['quantity'],
|
||||
'order_num' => ($this->skuArr[$orderGood['sku_id']]['order_num'] ?? 0) - $orderGood['quantity'],
|
||||
'surplus_stock' => ($this->skuArr[$orderGood['sku_id']]['surplus_stock'] ?? 0) + $orderGood['quantity'],
|
||||
];
|
||||
|
||||
$this->reduceStockCache[$orderGood['sku_id']] = $orderGood['quantity'] + ($this->reduceStockCache[$orderGood['sku_id']] ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,6 +256,8 @@ class OrderGoodStockConsumer extends ConsumerMessage
|
||||
'order_num' => ($this->skuArr[$orderGood['sku_id']]['order_num'] ?? 0) - $orderGood['quantity'],
|
||||
'surplus_stock' => ($this->skuArr[$orderGood['sku_id']]['surplus_stock'] ?? 0) + $orderGood['quantity'],
|
||||
];
|
||||
|
||||
$this->reduceStockCache[$orderGood['sku_id']] = $orderGood['quantity'] + ($this->reduceStockCache[$orderGood['sku_id']] ?? 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user