quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
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
<?php
 
namespace app\operations\model\plus\team;
 
use app\operations\model\plus\team\Referee as RefereeModel;
use app\common\model\plus\team\User as UserModel;
 
/**
 * 队长用户模型
 * Class User
 * @package app\operations\model\plus\team
 */
class User extends UserModel
{
    /**
     * 获取队长用户列表
     */
    public function getList($search, $limit = 15)
    {
        // 构建查询规则
        $model = $this->alias('team')
            ->field('team.*, user.nickName, user.avatarUrl')
            ->with(['referee','grade'])
            ->join('user', 'user.user_id = team.user_id')
            ->where('team.is_delete', '=', 0)
            ->order(['team.create_time' => 'desc']);
        // 查询条件
        if (!empty($search)) {
            $model = $model->where('user.nickName|team.real_name|team.mobile', 'like', '%' . $search . '%');
        }
        // 获取列表数据
        $list = $model->paginate($limit);
        foreach ($list as $user){
            $user['total_money'] = sprintf('%.2f', $user['money'] + $user['freeze_money'] + $user['total_money']);
        }
        return $list;
    }
    /**
     * 编辑队长用户
     * @param $data
     * @return bool
     */
    public function edit($data)
    {
        return $this->save($data) !== false;
    }
 
    /**
     * 删除队长用户
     * @return mixed
     */
    public function setDelete()
    {
        return $this->transaction(function () {
            // 获取一级团队成员ID集
            $RefereeModel = new RefereeModel;
            // 清空下级推荐记录
            $RefereeModel->onClearTeam($this['user_id']);
            // 标记当前队长记录为已删除
            return $this->save([
                'is_delete' => 1
            ]);
        });
    }
 
    /**
     * 一级团队成员归属到平台
     * @param $userIds
     * @return false|int
     */
    private function setFromplatform($userIds)
    {
        return $this->where('user_id', 'in', $userIds)
            ->where('is_delete', '=', 0)
            ->save(['referee_id' => 0]);
    }
 
    /**
     * 递减队长成员数量
     */
    private function setDecTeamNum($team_id, $level, $number)
    {
        $field = [1 => 'first_num', 2 => 'second_num', 3 => 'third_num'];
        return $this->where('user_id', '=', $team_id)
            ->where('is_delete', '=', 0)
            ->dec($field[$level], $number);
    }
    /**
     * 提现打款成功:累积提现佣金
     */
    public static function totalMoney($user_id, $money)
    {
        $model = self::detail($user_id);
        return $model->save([
            'freeze_money' => $model['freeze_money'] - $money,
            'total_money' => $model['total_money'] + $money,
        ]);
    }
 
    /**
     * 提现驳回:解冻队长资金
     */
    public static function backFreezeMoney($user_id, $money)
    {
        $model = self::detail($user_id);
        return $model->save([
            'money' => $model['money'] + $money,
            'freeze_money' => $model['freeze_money'] - $money,
        ]);
    }
 
    /**
     * 删除用户的上级推荐关系
     * @param $userId
     * @return bool
     * @throws \think\Exception
     */
    public function onDeleteReferee($userId)
    {
        // 获取推荐人列表
        $list = RefereeModel::getRefereeList($userId);
        if (!$list->isEmpty()) {
            // 递减推荐人的下级成员数量
            foreach ($list as $item) {
                $item['team1'] && $this->setDecTeamNum($item['team_id'], $item['level'], 1);
            }
            // 清空上级推荐关系
            (new RefereeModel)->onClearReferee($userId);
        }
        return true;
    }
 
    /**
     * 获取平台的总销售额
     */
    public function getTotalMoney($type = 'all_money')
    {
        $model = $this;
        if($type == 'money'){
            return $model->sum('money');
        } else if($type == 'freeze_money'){
            return $model->sum('freeze_money');
        } else if($type == 'total_money'){
            return $model->sum('total_money');
        } else if($type == 'all_money'){
            return $model->sum('total_money') + $model->sum('freeze_money') + $model->sum('money');
        }
        return 0;
    }
 
    /**
     * 指定会员等级下是否存在用户
     */
    public static function checkExistByGradeId($gradeId)
    {
        $model = new static;
        return !!$model->where('grade_id', '=', (int)$gradeId)
            ->where('is_delete', '=', 0)
            ->value('user_id');
    }
}