最新服务器上的版本,以后用这个
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
 * Created by PhpStorm.
 * User: 风哀伤
 * Date: 2019/5/14
 * Time: 10:01
 * @copyright: ©2019 浙江禾匠信息科技
 * @link: http://www.zjhejiang.com
 */
 
namespace app\jobs;
 
 
use app\models\Mall;
use app\models\Share;
use app\models\ShareOrder;
use yii\base\BaseObject;
use yii\queue\JobInterface;
 
/**
 * @property Mall $mall
 * @property ShareOrder $shareOrder
 * 分销订单总数:
 * 按订单来算即一个订单最多算一个分销订单;
 * 取消的订单不算分销订单;
 * 当订单中有商品发生退款时,
 * 若订单中其他商品还有分销商品则仍算一个分销订单,
 * 若没有则不算分销订单;
 * ---------------------
 * 分销总佣金:
 * 订单取消或退款时,需要将相应商品的佣金减去;
 */
class ChangeShareOrderJob extends BaseJob implements JobInterface
{
    public $mall;
    public $shareOrder;
    public $before;
    public $order;
    public $beforeList;
    public $type = 'add';
    private $allOrder; // 是否计算总订单
 
    public function execute($queue)
    {
        $this->setRequest();
        \Yii::$app->setMall(Mall::findOne($this->mall->id));
        $t = \Yii::$app->db->beginTransaction();
        try {
            $this->search('first_parent_id', 'first_price');
            $this->search('second_parent_id', 'second_price');
            $this->search('third_parent_id', 'third_price');
            $t->commit();
        } catch (\Exception $exception) {
            $t->rollBack();
            \Yii::error('分销记录:');
            \Yii::error($exception);
        }
    }
 
    /**
     * @param $idKey
     * @param $priceKey
     * @throws \Exception
     */
    public function search($idKey, $priceKey)
    {
        if ($this->order) {
            $shareOrderList = ShareOrder::find()->where([
                'mall_id' => $this->order->mall_id,
                'order_id' => $this->order->id,
                'user_id' => $this->order->user_id,
                'is_delete' => 0,
            ])->all();
            $beforeList = $this->beforeList;
            $this->allOrder = true;
        } else {
            $shareOrderList = [$this->shareOrder];
            $beforeList = [$this->before];
            $other = ShareOrder::find()->where([
                'mall_id' => $this->order->mall_id,
                'order_id' => $this->order->id,
                'user_id' => $this->order->user_id,
                'is_delete' => 0,
            ])->andWhere(['!=', 'id', $this->shareOrder->id])->all();
            if (empty($other)) {
                // 没有其他的分销订单,需要计算总订单
                $this->allOrder = true;
            } else {
                $this->allOrder = false;
            }
        }
        foreach ($shareOrderList as $shareOrder) {
            if ($shareOrder[$idKey]) {
                if ($this->type === 'add') {
                    $beforeUserId = 0;
                    $beforeMoney = 0;
                    if (!empty($beforeList)) {
                        $before = null;
                        foreach ($beforeList as $before) {
                            if ($before['id'] == $shareOrder['id']) {
                                $beforeUserId = $before[$idKey];
                                $beforeMoney = $before[$priceKey];
                                break;
                            }
                        }
                    }
                    $this->add($shareOrder[$idKey], $shareOrder[$priceKey], $beforeUserId, $beforeMoney);
                } elseif ($this->type === 'sub') {
                    $this->sub($shareOrder[$idKey], $shareOrder[$priceKey]);
                } else {
                    throw new \Exception('错误的状态');
                }
            }
        }
    }
 
    /**
     * @param $afterUserId
     * @param $afterMoney
     * @param int $beforeUserId
     * @param int $beforeMoney
     * @throws \Exception
     * 增加分销商分销订单总数及总佣金
     */
    public function add($afterUserId, $afterMoney, $beforeUserId = 0, $beforeMoney = 0)
    {
        $after = Share::findOne(['user_id' => $afterUserId]);
        if ($beforeUserId) {
            if ($afterUserId == $beforeUserId) {
                $afterMoney = floatval($after->all_money) + $afterMoney - $beforeMoney;
            } else {
                $before = Share::findOne(['user_id' => $beforeUserId]);
                $beforeMoney = floatval($before->all_money) - min(floatval($before->all_money), $beforeMoney);
                $before->all_money = price_format($beforeMoney);
                if ($this->allOrder) {
                    $before->all_order = intval($before->all_order - min($before->all_order, 1));
                    $after->all_order += 1;
                }
                if (!$before->save()) {
                    throw new \Exception('分销保存出错');
                }
                $afterMoney = floatval($after->all_money) + $afterMoney;
            }
        } else {
            if ($this->allOrder) {
                $after->all_order += 1;
            }
            $afterMoney = floatval($after->all_money) + $afterMoney;
        }
        $after->all_money = price_format($afterMoney);
        if (!$after->save()) {
            throw new \Exception('分销保存出错');
        }
        $this->allOrder = false;
    }
 
    /**
     * @param $userId
     * @param $money
     * @throws \Exception
     * 减少分销商分销订单总数及总佣金
     */
    public function sub($userId, $money)
    {
        $before = Share::findOne(['user_id' => $userId]);
        $beforeMoney = floatval($before->all_money) - min(floatval($before->all_money), $money);
        $before->all_money = price_format($beforeMoney);
        if ($this->allOrder) {
            $before->all_order = intval($before->all_order - min($before->all_order, 1));
        }
        if (!$before->save()) {
            throw new \Exception('分销保存出错');
        }
        $this->allOrder = false;
    }
}