log->error('CancelOrderConsumer:error:NoData:'.json_encode($data)); return Result::ACK; } $orderId = (int)$data['order_id']; $orderType = (int)$data['type']; $orderInfo = match ($orderType) { OrderCode::ORDER_TYPE_GOOD => $this->orderModel->getInfoById($orderId), default => null, }; if (empty($orderInfo)) { $this->log->debug('CancelOrderConsumer:error:NoOrderData:'.json_encode($data)); return Result::ACK; } if ($orderInfo->status != OrderCode::WAIT_PAY) { $this->log->debug('CancelOrderConsumer:error:orderStatusError:'.json_encode($orderInfo->toArray())); return Result::ACK; } try { Db::beginTransaction(); $this->rollbackCoupon($orderType, $orderInfo); $orderInfo->status = OrderCode::CANCEL; if (!$orderInfo->save()) throw new Exception('CancelOrderConsumer:error:orderStatusSaveError:'.json_encode($orderInfo->toArray())); Db::commit(); }catch (Exception $e){ Db::rollback(); $this->log->debug($e->getMessage()); return Result::ACK; } $this->sendStockMq($orderInfo->id,OrderCode::CANCEL); return Result::ACK; } /** * @param $orderType * @param $orderInfo * @return void * @throws Exception */ private function rollbackCoupon($orderType,$orderInfo): void { if ($orderType != OrderCode::ORDER_TYPE_GOOD) return; if ($orderInfo->coupon_id <= 0) return; $couponInfo = $this->userCouponModel->where('coupon_id', $orderInfo->coupon_id)->where('user_id',$orderInfo->user_id)->first(); if (empty($couponInfo)) return; $couponInfo->status = CouponCode::COUPON_STATUS_UNUSED; if (date('Y-m-d H:i:s') > $couponInfo->validity_end_time) $couponInfo->status = CouponCode::COUPON_STATUS_EXPIRE; if (!$couponInfo->save()) throw new Exception('CancelOrderConsumer:error:couponStatusUpdateError:'.json_encode($orderInfo->toArray())); } }