feat : spu
This commit is contained in:
159
app/Service/Amqp/Statement/RefundStatementService.php
Normal file
159
app/Service/Amqp/Statement/RefundStatementService.php
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user