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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
 
namespace app\common\model\plus\shareholder;
 
use app\common\model\BaseModel;
use app\common\model\plus\agent\User as AgentUserModel;
use app\common\model\plus\team\User as TeamUserModel;
use app\common\model\plus\team\Order as TeamOrderModel;
use app\common\model\plus\vip\User as VipUserModel;
use app\common\model\plus\vip\Order as VipOrderModel;
 
/**
 * 股东申请模型
 */
class Apply extends BaseModel
{
    protected $name = 'shareholder_apply';
    protected $pk = 'apply_id';
 
    /**
     * 申请状态
     * @var array
     */
    public $applyStatus = [
        10 => '待审核',
        20 => '审核通过',
        30 => '驳回',
    ];
 
    /**
     * 申请时间
     * @param $value
     * @return false|string
     */
    public function getApplyTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }
 
    /**
     * 审核时间
     * @param $value
     * @return false|int|string
     */
    public function getAuditTimeAttr($value)
    {
        return $value > 0 ? date('Y-m-d H:i:s', $value) : 0;
    }
 
    /**
     * 关联推荐人表
     * @return \think\model\relation\BelongsTo
     */
    public function referee()
    {
        return $this->belongsTo('app\common\model\user\User', 'referee_id')
            ->field(['user_id', 'nickName']);
    }
 
    /**
     * 申请记录详情
     * @param $where
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function detail($where)
    {
        $filter = is_array($where) ? $where : ['apply_id' => $where];
        return (new static())->where($filter)->find();
    }
 
    /**
     * 根据分销判断成为股东
     * @param $userId
     * @param $appId
     * @return bool
     */
    public function becomeShareholderByAgent($userId, $become_type, $appId)
    {
        // 验证是否设置
        $config = Setting::getItem('basic', $appId);
        if (empty($config['is_open'])) {
            return false;
        }
        $agent = AgentUserModel::detail($userId);
        if (!$agent) {
            return false;
        }
        $becomeShareholder = false;
        //分销商总数
        if ($config['become'] == '40') {
            $agent_total = $agent['first_num'] + $agent['second_num'] + $agent['third_num'];
            if ($agent_total >= $config['totalfxs_down']) {
                $becomeShareholder = true;
            }
        }
        //分销佣金总数
        if ($config['become'] == '50') {
            $agent_money = $agent['total_money'] + $agent['money'] + $agent['freeze_money'];
            if ($agent_money >= $config['total_money']) {
                $becomeShareholder = true;
            }
        }
        // 新增股东用户
        if ($becomeShareholder) {
            User::add($userId, [
                'referee_id' => $agent['referee_id'], //推荐人id
                'app_id' => $appId,
            ]);
        }
        return true;
    }
 
    /**
     * 根据团队分红判断成为股东
     * @param $userId
     * @param $appId
     * @return bool
     */
    public function becomeShareholderByTeam($userId, $become_type, $appId)
    {
        // 验证是否设置
        $config = Setting::getItem('basic', $appId);
        if (empty($config['is_open'])) {
            return false;
        }
        $teamUser = TeamUserModel::detail($userId);
        if (!$teamUser) {
            return false;
        }
        if (User::isshareholderUser($userId)) {
            return false;
        }
        $becomeShareholder = false;
        //团队总业绩
        if ($config['become'] == '70') {
            $total_money = TeamOrderModel::getOrderAllPrice($userId);
            if ($config['self_buy_money']) {
                $total_money += $teamUser['user']['expend_money'];
            }
            if ($total_money >= $config['total_team_money']) {
                $becomeShareholder = true;
            }
        }
        //团队总人数
        if ($config['become'] == '80') {
            if ($teamUser['total_num'] >= $config['total_team_user']) {
                $becomeShareholder = true;
            }
        }
        // 新增股东用户
        if ($becomeShareholder) {
            User::add($userId, [
                'referee_id' => $teamUser['referee_id'], //推荐人id
                'app_id' => $appId,
            ]);
        }
        return true;
    }
 
    /**
     * 审核状态
     * @param $value
     * @return array
     */
    public function getApplyStatusAttr($value)
    {
        $method = [10 => '待审核', 20 => '审核通过', '30' => '驳回'];
        return ['text' => $method[$value], 'value' => $value];
    }
 
    /**
     * 审核方式
     * @param $value
     * @return array
     */
    public function getApplyTypeAttr($value)
    {
        $method = [10 => '后台审核', 20 => '无需审核'];
        return ['text' => $method[$value], 'value' => $value];
    }
 
    /**
     * 统计VIP会员直接推荐数量
     * @param $userId
     * @return int
     */
    public function countVipRecommendations($userId)
    {
        return VipUserModel::where('referee_id', '=', $userId)->count();
    }
 
    /**
     * 统计企业商户入驻数量
     * @param $userId
     * @return int
     */
    public function countMerchantSettlements($userId)
    {
        // 假设商家入驻与推广团队的关联字段是referee_id
        // 需要根据实际数据库结构调整
        return \app\common\model\Shop::where('referee_id', '=', $userId)->count();
    }
 
    /**
     * 统计VIP专区商品购买次数
     * @param $userId
     * @return int
     */
    public function countSelfVipPurchases($userId)
    {
        return VipOrderModel::where('user_id', '=', $userId)
            ->where('is_vip', '=', 1)
            ->count();
    }
 
    /**
     * 检查股东候选人是否满足新的三个条件
     * @param $userId
     * @param $appId
     * @return bool
     */
    public function checkNewShareholderConditions($userId, $appId)
    {
        $config = Setting::getItem('basic', $appId);
        
        // 1. 检查VIP会员直接推荐数量
        $vipRecommendCount = $this->countVipRecommendations($userId);
        if ($vipRecommendCount < $config['vip_recommend_count']) {
            return false;
        }
        
        // 2. 检查企业商户入驻数量
        $merchantCount = $this->countMerchantSettlements($userId);
        if ($merchantCount < $config['merchant_settle_count']) {
            return false;
        }
        
        // 3. 检查VIP专区商品购买次数
        $selfVipPurchaseCount = $this->countSelfVipPurchases($userId);
        if ($selfVipPurchaseCount < $config['self_vip_purchase_count']) {
            return false;
        }
        
        // 所有条件都满足
        return true;
    }
 
},