on(OrderRefund::EVENT_REFUND, function ($event) { /** @var OrderRefundEvent $event */ \Yii::$app->setMchId($event->order_refund->mch_id); $orderDetail = $event->order_refund->detail; $orderDetail->refund_status = 2; // 商家同意退款 销毁订单商品赠送的卡券 if (in_array($event->order_refund->type, [1,3]) && $event->order_refund->status == 2) { $orderDetail->is_refund = 1; /* @var UserCard[] $userCards */ $userCards = UserCard::find()->where([ 'order_id' => $event->order_refund->order_id, 'order_detail_id' => $event->order_refund->order_detail_id ])->all(); foreach ($userCards as $userCard) { $userCard->is_delete = 1; $userCard->card->updateCount('add', 1); $res = $userCard->save(); if (!$res) { \Yii::error('卡券销毁事件处理异常'); } } $price = $orderDetail->total_price - min($orderDetail->total_price, $event->order_refund->reality_refund_price); (new AddShareOrder())->refund($orderDetail, $price); } $orderDetail->save(); // 判断queue队列中的售后是否已经触发 $queueId = CoreQueueData::select($event->order_refund->order->token); if ($queueId && !\Yii::$app->queue->isDone($queueId)) { // 若未触发 return; } else { // 若已触发,则重新添加 $id = \Yii::$app->queue->delay(0)->push(new OrderSalesJob([ 'orderId' => $event->order_refund->order_id ])); CoreQueueData::add($id, $event->order_refund->order->token); } }); } }