quanwei
2025-11-28 3ea53e61cc23fdb3ddf8b38a199ca60a6da8c407
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
<?php
 
namespace app\common\model\plus\vip;
 
use app\common\model\BaseModel;
use app\common\model\plus\vip\GradeLog as GradeLogModel;
use app\common\enum\user\grade\ChangeTypeEnum;
 
/**
 * VIP专区用户模型
 */
class User extends BaseModel
{
    protected $name = 'vip_area_user';
    protected $pk = 'user_id';
 
    /**
     * 关联会员记录表
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('app\\common\\model\\user\\User');
    }
 
    /**
     * 关联推荐人表
     * @return \think\model\relation\BelongsTo
     */
    public function referee()
    {
        return $this->belongsTo('app\\common\\model\\user\\User', 'referee_id', 'user_id');
    }
 
    /**
     * 关联VIP等级记录表
     * @return \think\model\relation\BelongsTo
     */
    public function grade()
    {
        return $this->belongsTo('app\\common\\model\\plus\\vip\\Grade','grade_id', 'grade_id');
    }
 
    /**
     * 详情
     */
    public static function detail($user_id, $with = ['user', 'referee', 'grade'])
    {
        return (new static())->with($with)->find($user_id);
    }
 
    /**
     * 是否为VIP专区用户
     */
    public static function isVipUser($user_id)
    {
        $vip = self::detail($user_id);
        return !!$vip && !$vip['is_delete'];
    }
 
    /**
     * 新增VIP专区用户记录
     * @param $user_id
     * @param $data
     * @return bool
     */
    public static function add($user_id, $data)
    {
        $model = static::detail($user_id) ?: new static;
        $user=(new \app\common\model\user\User())->detail($user_id);
        $model->save(array_merge([
            'user_id' => $user_id,
            'real_name' => $user['real_name']?: $user['nickName'],
            'mobile' => $user['mobile'],
            'is_delete' => 0,
            'app_id' => $model::$app_id,
            'grade_id' => Grade::getDefaultGradeId()
        ], $data));
        return true;
    }
 
    /**
     * 发放VIP专区佣金
     * @param $user_id
     * @param $money
     * @param string $description 佣金描述
     * @return bool
     */
    public static function grantMoney($user_id, $money, $description = '订单佣金结算')
    {
        // VIP专区用户详情
        $model = static::detail($user_id);
        if (!$model || $model['is_delete']) {
            return false;
        }
        // 累加VIP专区用户可提现佣金
        $model->where('user_id', '=', $user_id)->inc('money', $money)->update();
        // 记录VIP专区资金明细
        Capital::add([
            'user_id' => $user_id,
            'flow_type' => 10,
            'money' => $money,
            'describe' => $description,
            'app_id' => $model['app_id'],
        ]);
        return true;
    }
 
    /**
     * 批量设置会员等级
     */
    public function upgradeGrade($user, $upgradeGrade)
    {
        // 更新会员等级的数据
        $this->where('user_id', '=', $user['user_id'])
            ->update([
                'grade_id' => $upgradeGrade['grade_id']
            ]);
        (new GradeLogModel)->save([
            'old_grade_id' => $user['grade_id'],
            'new_grade_id' => $upgradeGrade['grade_id'],
            'change_type' => ChangeTypeEnum::AUTO_UPGRADE,
            'user_id' => $user['user_id'],
            'app_id' => $user['app_id']
        ]);
        return true;
    }
 
    /**
     * 提现打款成功:累积提现佣金
     */
    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,
        ]);
    }
 
    /**
     * 提现驳回:解冻VIP专区资金
     */
    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,
        ]);
    }
 
    /**
     * 冻结VIP专区资金
     */
    public static function freezeMoney($user_id, $money)
    {
        $model = self::detail($user_id);
        if ($model['money'] < $money) {
            return false;
        }
        return $model->save([
            'money' => $model['money'] - $money,
            'freeze_money' => $model['freeze_money'] + $money,
        ]);
    }
}