feat: stock
This commit is contained in:
@@ -4,23 +4,173 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Amqp\Consumer;
|
||||
|
||||
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\StockTrait;
|
||||
use Exception;
|
||||
use Hyperf\Amqp\Message\Type;
|
||||
use Hyperf\Amqp\Result;
|
||||
use Hyperf\Amqp\Annotation\Consumer;
|
||||
use Hyperf\Amqp\Message\ConsumerMessage;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use PhpAmqpLib\Message\AMQPMessage;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
#[Consumer(exchange: 'OrderGoodStock', routingKey: 'OrderGoodStock', queue: 'OrderGoodStock.change', name: "OrderGoodStockConsumer", nums: 1)]
|
||||
class OrderGoodStockConsumer extends ConsumerMessage
|
||||
{
|
||||
use StockTrait;
|
||||
|
||||
/**
|
||||
* @var Type|string 消息类型
|
||||
*/
|
||||
protected Type|string $type = Type::DIRECT;
|
||||
|
||||
/**
|
||||
* @var Log $log
|
||||
*/
|
||||
#[Inject]
|
||||
protected Log $log;
|
||||
|
||||
/**
|
||||
* @var OrderGood
|
||||
*/
|
||||
#[Inject]
|
||||
protected OrderGood $orderGoodModel;
|
||||
|
||||
/**
|
||||
* @var Sku
|
||||
*/
|
||||
#[Inject]
|
||||
protected Sku $skuModel;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $orderGoodArr;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $skuArr;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $updateArr;
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @param AMQPMessage $message
|
||||
* @return Result
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function consumeMessage($data, AMQPMessage $message): Result
|
||||
{
|
||||
return Result::ACK;
|
||||
if (!$data['order_id'] || !$data['type']) {
|
||||
$this->log->error('OrderGoodStockConsumer:error:NoData:'.json_encode($data));
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
$orderId = (int)$data['order_id'];
|
||||
$this->orderGoodArr = [];
|
||||
$this->skuArr = [];
|
||||
|
||||
$this->orderGoodArr = $this->orderGoodModel->getGoodIdsByOrderId($orderId);
|
||||
if (empty($this->orderGoodArr)) {
|
||||
$this->log->debug('OrderGoodStockConsumer:error:NoOrderGoodData:'.json_encode($orderId));
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
$this->skuArr = $this->skuModel->getDataArrByIds(array_column($this->orderGoodArr, 'sku_id'));
|
||||
if (empty($this->skuArr)) {
|
||||
$this->log->debug('OrderGoodStockConsumer:error:NoSkuData:'.json_encode(array_column($this->orderGoodArr, 'sku_id')));
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
$this->updateArr = [];
|
||||
|
||||
try {
|
||||
match ($data['type']) {
|
||||
OrderCode::WAIT_PAY => $this->waitPaySubStock(),
|
||||
OrderCode::UNCOMPLETED_REFUND => $this->unFinishRefundAddStock(),
|
||||
OrderCode::CANCEL => $this->cancelAddStock(),
|
||||
OrderCode::FINISH_REFUND => $this->finishRefundUpdateData(),
|
||||
default => throw new Exception('OrderGoodStockConsumer:error:无效的订单类型')
|
||||
};
|
||||
|
||||
if (empty($this->updateArr)) {
|
||||
$this->log->debug('OrderGoodStockConsumer:error:NoUpdateData:skuInfo:'.json_encode($this->skuArr).':orderGoodArr:'.json_encode($this->orderGoodArr));
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
if (!(new Sku)->update($this->updateArr)) {
|
||||
$this->log->debug('OrderGoodStockConsumer:error:UpdateSkuDataFail:'.json_encode($this->updateArr));
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
return Result::ACK;
|
||||
} catch (Exception $e) {
|
||||
$this->log->error($e->getMessage());
|
||||
return Result::ACK;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function waitPaySubStock(): void
|
||||
{
|
||||
foreach ($this->orderGoodArr as $orderGood) {
|
||||
$this->updateArr[] = [
|
||||
'id' => $orderGood['sku_id'],
|
||||
''
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function unFinishRefundAddStock(): void
|
||||
{
|
||||
foreach ($this->orderGoodArr as $orderGood) {
|
||||
$this->updateArr[] = [
|
||||
'id' => $orderGood['sku_id'],
|
||||
''
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function cancelAddStock(): void
|
||||
{
|
||||
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'],
|
||||
''
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user