commit | author | age
|
2207d6
|
1 |
<?php |
W |
2 |
/** |
|
3 |
* Created by PhpStorm. |
|
4 |
* User: 风哀伤 |
|
5 |
* Date: 2019/5/10 |
|
6 |
* Time: 15:39 |
|
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\ShareSetting; |
|
17 |
use app\models\User; |
|
18 |
use yii\base\BaseObject; |
|
19 |
use yii\queue\JobInterface; |
|
20 |
|
|
21 |
/** |
|
22 |
* @property User $user |
|
23 |
* @property Mall $mall |
|
24 |
*/ |
|
25 |
class ChangeParentJob extends BaseJob implements JobInterface |
|
26 |
{ |
|
27 |
public $mall; |
|
28 |
public $user; |
|
29 |
public $user_id; |
|
30 |
public $beforeParentId;// 变更前的上级id |
|
31 |
public $parentId; // 变更后的上级id |
|
32 |
private $level; |
|
33 |
|
|
34 |
public function execute($queue) |
|
35 |
{ |
|
36 |
$this->setRequest(); |
|
37 |
if ($this->beforeParentId == $this->parentId) { |
|
38 |
return true; |
|
39 |
} |
|
40 |
\Yii::$app->setMall(Mall::findOne($this->mall->id)); |
|
41 |
$this->user = User::find()->where(['id' => $this->user_id])->with('share')->one(); |
|
42 |
$this->level = ShareSetting::get(\Yii::$app->mall->id, ShareSetting::LEVEL, 0); |
|
43 |
\Yii::error('--上级更改--'); |
|
44 |
$t = \Yii::$app->db->beginTransaction(); |
|
45 |
try { |
|
46 |
$this->change($this->parentId, 1); |
|
47 |
$this->change($this->beforeParentId, 1); |
|
48 |
$t->commit(); |
|
49 |
} catch (\Exception $exception) { |
|
50 |
$t->rollBack(); |
|
51 |
\Yii::error('用户变更上级出错'); |
|
52 |
\Yii::error($exception); |
|
53 |
} |
|
54 |
} |
|
55 |
|
|
56 |
/** |
|
57 |
* @param $parentId |
|
58 |
* @param int $root |
|
59 |
* @return bool|mixed |
|
60 |
* @throws \Exception |
|
61 |
* 修改分销商的直属下级数量和总下级数量 |
|
62 |
*/ |
|
63 |
private function change($parentId, $root = 3) |
|
64 |
{ |
|
65 |
if ($root > $this->level) { |
|
66 |
return true; |
|
67 |
} |
|
68 |
|
|
69 |
if ($parentId == 0) { |
|
70 |
return true; |
|
71 |
} |
|
72 |
|
|
73 |
/* @var Share $parent */ |
|
74 |
$parent = Share::find()->with(['userInfo', 'thirdChildren']) |
|
75 |
->where(['user_id' => $parentId, 'is_delete' => 0, 'mall_id' => \Yii::$app->mall->id]) |
|
76 |
->one(); |
|
77 |
if (!$parent) { |
|
78 |
throw new \Exception('错误的上级id'); |
|
79 |
} |
|
80 |
|
|
81 |
$parent->first_children = count($parent->firstChildren); |
|
82 |
$parent->all_children = count($parent->firstChildren); |
|
83 |
if ($this->level > 1) { |
|
84 |
$parent->all_children += count($parent->secondChildren); |
|
85 |
if ($this->level > 2) { |
|
86 |
$parent->all_children += count($parent->thirdChildren); |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
if (!$parent->save()) { |
|
91 |
throw new \Exception('第' . $root . '层出错'); |
|
92 |
} |
|
93 |
|
|
94 |
$root++; |
|
95 |
return $this->change($parent->userInfo->parent_id, $root); |
|
96 |
} |
|
97 |
} |