zm
2021-03-25 d84ff6053b22269a6c59dc005e9efb8de6595988
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
<?php
/**
 * Created by PhpStorm.
 * User: 风哀伤
 * Date: 2019/5/10
 * Time: 15:39
 * @copyright: ©2019 浙江禾匠信息科技
 * @link: http://www.zjhejiang.com
 */
 
namespace app\jobs;
 
 
use app\models\Mall;
use app\models\Share;
use app\models\ShareSetting;
use app\models\User;
use yii\base\BaseObject;
use yii\queue\JobInterface;
 
/**
 * @property User $user
 * @property Mall $mall
 */
class ChangeParentJob extends BaseJob implements JobInterface
{
    public $mall;
    public $user;
    public $user_id;
    public $beforeParentId;// 变更前的上级id
    public $parentId; // 变更后的上级id
    private $level;
 
    public function execute($queue)
    {
        $this->setRequest();
        if ($this->beforeParentId == $this->parentId) {
            return true;
        }
        \Yii::$app->setMall(Mall::findOne($this->mall->id));
        $this->user = User::find()->where(['id' => $this->user_id])->with('share')->one();
        $this->level = ShareSetting::get(\Yii::$app->mall->id, ShareSetting::LEVEL, 0);
        \Yii::error('--上级更改--');
        $t = \Yii::$app->db->beginTransaction();
        try {
            $this->change($this->parentId, 1);
            $this->change($this->beforeParentId, 1);
            $t->commit();
        } catch (\Exception $exception) {
            $t->rollBack();
            \Yii::error('用户变更上级出错');
            \Yii::error($exception);
        }
    }
 
    /**
     * @param $parentId
     * @param int $root
     * @return bool|mixed
     * @throws \Exception
     * 修改分销商的直属下级数量和总下级数量
     */
    private function change($parentId, $root = 3)
    {
        if ($root > $this->level) {
            return true;
        }
 
        if ($parentId == 0) {
            return true;
        }
 
        /* @var Share $parent */
        $parent = Share::find()->with(['userInfo', 'thirdChildren'])
            ->where(['user_id' => $parentId, 'is_delete' => 0, 'mall_id' => \Yii::$app->mall->id])
            ->one();
        if (!$parent) {
            throw new \Exception('错误的上级id');
        }
 
        $parent->first_children = count($parent->firstChildren);
        $parent->all_children = count($parent->firstChildren);
        if ($this->level > 1) {
            $parent->all_children += count($parent->secondChildren);
            if ($this->level > 2) {
                $parent->all_children += count($parent->thirdChildren);
            }
        }
 
        if (!$parent->save()) {
            throw new \Exception('第' . $root . '层出错');
        }
 
        $root++;
        return $this->change($parent->userInfo->parent_id, $root);
    }
}