feat : spu

This commit is contained in:
2025-03-26 17:48:48 +08:00
parent b1f79620fb
commit c7d0a222ab
3 changed files with 226 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Amqp\Consumer\Statement;
use App\Lib\Log;
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;
#[Consumer(exchange: 'RefundStatement', routingKey: 'RefundStatement', queue: 'RefundStatement.count', name: "RefundFinishConsumer", nums: 1)]
class RefundFinishConsumer extends ConsumerMessage
{
/**
* @var Type|string 消息类型
*/
protected Type|string $type = Type::DIRECT;
/**
* @var Log
*/
#[Inject]
protected Log $log;
public function consumeMessage($data, AMQPMessage $message): Result
{
if (!$data['order_id'] || !$data['refund_order_id']) {
$this->log->error('RefundFinishConsumer:error:NoData:'.json_encode($data));
return Result::ACK;
}
return Result::ACK;
}
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace App\Amqp\Producer\Statement;
use Hyperf\Amqp\Annotation\Producer;
use Hyperf\Amqp\Message\ProducerMessage;
use Hyperf\Amqp\Message\Type;
#[Producer(exchange: 'RefundStatement', routingKey: 'RefundStatement')]
class RefundFinishProducer extends ProducerMessage
{
/**
* @var Type|string 消息类型
*/
protected Type|string $type = Type::DIRECT;
public function __construct($data)
{
/**
* $data string array => {"order_id":"order_id","refund_order_id":"refund_order_id"}
*/
$this->payload = $data;
}
}

View File

@@ -0,0 +1,159 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Amqp\Statement;
use App\Constants\Common\OrderCode;
use App\Constants\Common\RefundCode;
use App\Model\ChefStatement;
use App\Model\FinancesStatement;
use App\Model\Order;
use App\Model\OrderGood;
use App\Model\RefundOrder;
use Exception;
use Hyperf\Di\Annotation\Inject;
class RefundStatementService
{
/**
* @var int
*/
public int $orderId;
/**
* @var int
*/
public int $refundOrderId;
/**
* @var RefundOrder
*/
#[Inject]
protected RefundOrder $refundOrderModel;
/**
* @var Order
*/
#[Inject]
protected Order $orderModel;
/**
* @var OrderGood
*/
#[Inject]
protected OrderGood $orderGoodModel;
/**
* @var ChefStatement
*/
#[Inject]
protected ChefStatement $chefStatementModel;
/**
* @var FinancesStatement
*/
#[Inject]
protected FinancesStatement $financesStatementModel;
/**
* @var Order
*/
protected Order $orderInfo;
/**
* @var RefundOrder
*/
protected RefundOrder $refundOrderInfo;
/**
* @var array
*/
protected array $orderGoodIds;
public function handle()
{
$this->refundOrderInfo = $this->refundOrderModel->find($this->refundOrderId);
if (empty($this->refundOrderInfo) || $this->refundOrderInfo->status != RefundCode::REFUND_SUCCESS) throw new Exception('退款信息不存在');
$this->orderInfo = $this->orderModel->find($this->orderId);
if (empty($this->orderInfo) || !in_array($this->orderInfo->status,[OrderCode::FINISH_REFUND,OrderCode::UNCOMPLETED_REFUND])) throw new Exception('订单信息不存在');
$this->orderGoodIds = $this->orderGoodModel->where('order_id',$this->orderId)->pluck('id')->toArray();
if (empty($this->orderGoodIds)) throw new Exception('订单商品信息不存在');
$this->rollBackChefData();
$this->createRefundData();
}
/**
* @return void
* @throws Exception
*/
private function rollBackChefData(): void
{
$refundOrderGoodIds = [];
switch ($this->refundOrderInfo->type) {
case RefundCode::PARTIAL_GOOD_REFUND:
$refundOrderGoodIds = json_decode($this->refundOrderInfo->good_ids,true);
break;
case RefundCode::FULL_GOOD_REFUND:
$isRefundIds = $this->refundOrderModel
->where('order_id',$this->orderInfo->id)
->where('order_type',OrderCode::ORDER_TYPE_GOOD)
->where('type',RefundCode::PARTIAL_GOOD_REFUND)
->where('status',RefundCode::REFUND_SUCCESS)
->pluck('good_ids')
->toArray();
if (empty($isRefundIds)) {
$refundOrderGoodIds = $this->orderGoodIds;
break;
}
$isRefundArr = [];
foreach ($isRefundIds as $oneDay) {
array_merge($isRefundArr,json_decode($oneDay,true));
}
if (empty($isRefundArr)) {
$refundOrderGoodIds = $this->orderGoodIds;
break;
}
$refundOrderGoodIds = array_diff($this->orderGoodIds,$isRefundArr);
break;
}
if (empty($refundOrderGoodIds)) throw new Exception('订单商品信息获取失败-01');
$skuList = $this->orderGoodModel->whereIn('id',$refundOrderGoodIds)->selectRaw('SUM(`quantity`) as quantity')->select('sku_id')->groupBy('sku_id')->get();
if ($skuList->isEmpty()) throw new Exception('订单商品信息获取失败-02');
$skuList = $skuList->toArray();
foreach ($skuList as $skuInfo) {
$info = $this->chefStatementModel->where('sku_id',$skuInfo['sku_id'])->first();
if (empty($info)) continue;
$info->sale -= $skuInfo['quantity'];
$info->refund += $skuInfo['quantity'];
if (!$info->save()) throw new Exception('更新报表信息失败-03');
}
}
private function createRefundData()
{
//todo
}
}