feat : finish order
This commit is contained in:
@@ -9,11 +9,15 @@ use App\Constants\Common\RefundCode;
|
||||
use App\Event\RefundGoodOrderFinishEvent;
|
||||
use App\Lib\Log;
|
||||
use App\Model\ChefStatement;
|
||||
use App\Model\Cycle;
|
||||
use App\Model\FinancesStatement;
|
||||
use App\Model\Order;
|
||||
use App\Model\OrderGood;
|
||||
use App\Model\RefundOrder;
|
||||
use App\Model\RefundStatement;
|
||||
use Exception;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Event\Annotation\Listener;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
@@ -29,7 +33,9 @@ class RefundGoodOrderFinishStatementListener implements ListenerInterface
|
||||
protected OrderGood $orderGoodModel,
|
||||
protected ChefStatement $chefStatementModel,
|
||||
protected FinancesStatement $financesStatementModel,
|
||||
protected Log $log
|
||||
protected Log $log,
|
||||
protected RefundStatement $refundStatementModel,
|
||||
protected Cycle $cycleModel,
|
||||
) {}
|
||||
|
||||
public function listen(): array
|
||||
@@ -54,6 +60,11 @@ class RefundGoodOrderFinishStatementListener implements ListenerInterface
|
||||
*/
|
||||
protected array $orderGoodIds;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected bool $isRollBackFlag = false;
|
||||
|
||||
public function process(object $event): void
|
||||
{
|
||||
try {
|
||||
@@ -66,10 +77,17 @@ class RefundGoodOrderFinishStatementListener implements ListenerInterface
|
||||
$this->orderGoodIds = $this->orderGoodModel->where('order_id',$event->orderId)->pluck('id')->toArray();
|
||||
if (empty($this->orderGoodIds)) throw new Exception('订单商品信息不存在');
|
||||
|
||||
Db::beginTransaction();
|
||||
|
||||
$this->isRollBackFlag = true;
|
||||
|
||||
$this->rollBackChefData();
|
||||
|
||||
$this->rollBackFinancesData();
|
||||
|
||||
Db::commit();
|
||||
} catch (Exception $e) {
|
||||
if ($this->isRollBackFlag) Db::rollBack();
|
||||
$this->log->error(__CLASS__.':退款完成后生成订单结算数据失败:'.$e->getMessage());
|
||||
}
|
||||
}
|
||||
@@ -113,12 +131,44 @@ class RefundGoodOrderFinishStatementListener implements ListenerInterface
|
||||
if (empty($refundOrderGoodIds)) throw new Exception('订单商品信息获取失败');
|
||||
|
||||
$skuIds = $this->orderGoodModel->whereIn('id',$refundOrderGoodIds)->pluck('quantity','sku_id')->toArray();
|
||||
if (empty($skuIds)) throw new Exception('订单商品信息02获取失败');
|
||||
|
||||
//todo first
|
||||
$updateData = $this->chefStatementModel->whereIn('sku_id',array_keys($skuIds))->get();
|
||||
if ($updateData->isEmpty()) return;
|
||||
$updateData = array_column($updateData->toArray(),null,'sku_id');
|
||||
|
||||
foreach ($skuIds as $skuId => $quantity) {
|
||||
if (empty($updateData[$skuId])) throw new Exception('个别数据还未生成,但是有数据生成了,数据异常:'.json_encode($this->refundOrderInfo));
|
||||
|
||||
$updateInfo = [
|
||||
'sale' => $updateData[$skuId]['sale'] - $quantity,
|
||||
'refund' => $updateData[$skuId]['refund'] + $quantity,
|
||||
];
|
||||
|
||||
if (!(new ChefStatement)->where('id',$updateData['sku_id']['id'])->update($updateInfo)) throw new Exception('更新订单数据失败'.json_encode($this->refundOrderInfo));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
private function rollBackFinancesData(): void
|
||||
{
|
||||
$refundStatementInfo = $this->refundStatementModel->where('cycle_id',$this->orderInfo->cycle_id)->where('kitchen_id',$this->orderInfo->kitchen_id)->first();
|
||||
$cycleInfo = $this->cycleModel->find($this->orderInfo->cycle_id);
|
||||
if (empty($cycleInfo)) throw new Exception('周期信息不存在,数据错误');
|
||||
if (empty($refundStatementInfo)) {
|
||||
$refundStatementInfo = new RefundStatement();
|
||||
|
||||
$refundStatementInfo->date = $cycleInfo->dates;
|
||||
$refundStatementInfo->cycle_id = $this->orderInfo->cycle_id;
|
||||
$refundStatementInfo->kitchen_id = $this->orderInfo->kitchen_id;
|
||||
$refundStatementInfo->refund_price = $this->refundOrderInfo->refund_money;
|
||||
} else {
|
||||
$refundStatementInfo->refund_price = bcadd($refundStatementInfo->refund_price,$this->refundOrderInfo->refund_money,2);
|
||||
}
|
||||
|
||||
if (!$refundStatementInfo->save()) throw new Exception('更新退款数据失败'.json_encode($this->refundOrderInfo));
|
||||
}
|
||||
}
|
||||
|
||||
34
app/Model/RefundStatement.php
Normal file
34
app/Model/RefundStatement.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use Hyperf\DbConnection\Model\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $date
|
||||
* @property int $cycle_id
|
||||
* @property int $kitchen_id
|
||||
* @property string $refund_price
|
||||
* @property string $create_time
|
||||
* @property string $update_time
|
||||
*/
|
||||
class RefundStatement extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'refund_statement';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = ['id' => 'integer', 'cycle_id' => 'integer', 'kitchen_id' => 'integer'];
|
||||
}
|
||||
Reference in New Issue
Block a user