最新服务器上的版本,以后用这个
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
 * link: http://www.zjhejiang.com/
 * copyright: Copyright (c) 2018 浙江禾匠信息科技有限公司
 * author: wxf
 */
 
 
namespace app\handlers;
 
use app\events\OrderEvent;
use app\events\OrderRefundEvent;
use app\forms\common\goods\CommonGoods;
use app\forms\common\share\AddShareOrder;
use app\jobs\ChangeShareOrderJob;
use app\jobs\OrderSalesJob;
use app\models\CoreQueueData;
use app\models\GoodsAttr;
use app\models\Order;
use app\models\OrderDetail;
use app\models\OrderRefund;
use app\models\ShareOrder;
use app\models\UserCard;
use yii\db\Exception;
 
class OrderRefundConfirmedHandler extends HandlerBase
{
 
    /**
     * 事件处理注册
     */
    public function register()
    {
        \Yii::$app->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);
            }
        });
    }
}