quanwei
7 days ago 30563323a53b0d0260c97d08a9e8bd4cc8227a95
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
168
169
170
171
172
173
174
175
176
<?php
 
namespace app\shop\model\plus\shareholder;
 
use app\shop\model\plus\shareholder\Referee as RefereeModel;
use app\common\model\plus\shareholder\User as UserModel;
use app\shop\model\order\Order as OrderModel;
use app\shop\model\order\OrderProduct as OrderProductModel;
/**
 * 股东用户模型
 * Class User
 * @package app\shop\model\plus\shareholder
 */
class User extends UserModel
{
    public static function checkExistByGradeId($grade_id)
    {
        self::where('grade_id',$grade_id)->find();
    }
 
    /**
     * 获取股东用户列表
     */
    public function getList($search, $limit = 15)
    {
        // 构建查询规则
        $model = $this->alias('shareholder')
            ->field('shareholder.*, user.nickName, user.avatarUrl')
            ->with(['referee','grade'])
            ->join('user', 'user.user_id = shareholder.user_id')
            ->where('shareholder.is_delete', '=', 0)
            ->order(['shareholder.create_time' => 'desc']);
        // 查询条件
        if (!empty($search)) {
            $model = $model->where('user.nickName|shareholder.real_name|shareholder.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;
    }
 
    public static function getListAll($grade_id = 0)
    {
        $model = new static;
        if($grade_id > 0) {
            $model->where('grade_id', '=', $grade_id);
        }
        return $model->where('is_delete', '=', 0)->with(['grade'])->select();
    }
    
    /**
     * 获取满足分红条件的股东列表
     */
    public static function getEligibleListAll($bonusData)
    {
        // 获取基础设置
        $setting = Setting::getItem('basic');
        
        // 先获取所有股东
        $model = new static;
        $shareholders = $model->where('is_delete', '=', 0)->with(['grade'])->select();
        // 如果没有设置分红条件,返回所有股东
        if (empty($setting['consumption_amount']) && empty($setting['condition_purchase_count'])) {
            return $shareholders;
        }
 
        // 过滤满足条件的股东
        $eligibleShareholders = [];
        foreach ($shareholders as $shareholder) {
            $userId = $shareholder['user_id'];
            $isEligible = true;
 
            // 检查消费金额条件
            if (!empty($setting['consumption_amount'])) {
                $totalConsumption = (new OrderModel)->getUserTotalConsumption($userId,$bonusData);
                if ($totalConsumption < $setting['consumption_amount']) {
                    $isEligible = false;
                }
            }
            
            // 检查购买次数条件
            if (!empty($setting['condition_purchase_count']) && $isEligible) {
                $purchaseCount = (new OrderProductModel)::getPurchaseCount($userId,$bonusData);
                if ($purchaseCount < $setting['condition_purchase_count']) {
                    $isEligible = false;
                }
            }
            
            // 如果满足所有条件,添加到结果集中
            if ($isEligible) {
                $eligibleShareholders[] = $shareholder;
            }
        }
        
        // 转换为 Collection 对象以保持接口一致性
        return collect($eligibleShareholders);
    }
 
    /**
     * 新增记录
     */
    public function addUser($data)
    {
        $data['app_id'] = self::$app_id;
        return $this->save($data);
    }
 
    /**
     * 编辑股东用户
     * @param $data
     * @return bool
     */
    public function edit($data)
    {
        return $this->save($data) !== false;
    }
 
    /**
     * 删除股东用户
     * @return mixed
     */
    public function setDelete()
    {
        return $this->transaction(function () {
            // 标记当前股东记录为已删除
            return $this->save([
                'is_delete' => 1
            ]);
        });
    }
    
    /**
     * 提现打款成功:累积提现佣金
     */
    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,
        ]);
    }
 
    /**
     * 获取平台的总销售额
     */
    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;
    }
}