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 } }