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