huangsijun
2025-09-22 a78c011de350b188afb03beb2f26a73f35f71986
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
<?php
 
namespace app\api\controller\plus\region;
 
use app\api\controller\Controller;
use app\api\model\plus\region\Apply as regionApplyModel;
use app\common\model\plus\region\Setting;
use app\common\exception\BaseException;
use app\api\model\plus\agent\User as AgentUserModel;
use app\api\model\plus\team\Order as TeamOrder;
use app\api\model\order\Order as OrderModel;
 
/**
 * 队长申请
 */
class Apply extends Controller
{
    // 当前用户
    private $user;
    // 代理设置
    private $setting;
 
    /**
     * 构造方法
     */
    public function initialize()
    {
        $this->user = $this->getUser();   // 用户信息
        // 代理设置
        $this->setting = Setting::getAll();
        // 分销商用户信息
        $this->agent = AgentUserModel::detail($this->user['user_id']);
    }
 
    /**
     * 验证是否可以申请
     */
    public function check()
    {
        $data = $this->postData();
        if (empty($data['region_level']) || empty($data['province_id'])) {
            throw new BaseException(['msg' => '代理数据不完整']);
        }
        if ($data['region_level'] == 2 && empty($data['city_id'])) {
            throw new BaseException(['msg' => '代理数据不完整']);
        }
        if ($data['region_level'] == 3 && empty($data['area_id'])) {
            throw new BaseException(['msg' => '代理数据不完整']);
        }
        if (empty($data['name']) || empty($data['mobile'])) {
            throw new BaseException(['msg' => '用户名或者手机号为空']);
        }
        $setting = $this->setting['basic']['values'];
        $levels = [
            '1' => $setting['province_condition'],
            '2' => $setting['city_condition'],
            '3' => $setting['area_condition']
        ];
        $level_condition = $levels[$data['region_level']];
        $agent_total = $agent_money = $team_money = 0;
        $is_pass = false;
        $checkData = [];
        $checkData = [
            'region_level' => intval($data['region_level']),
            'become' => $setting['become'],
            'target' => $level_condition
        ];
        if ($setting['become'] == 10) {
            $is_pass = true;
        }
        //统计下级分销商总数
        if ($setting['become'] == '40' && $this->agent) {
            $agent_total = $this->agent['first_num'] + $this->agent['second_num']+ $this->agent['third_num'];
            $is_pass = $agent_total >= $level_condition;
            $checkData['now_target'] = $agent_total;
            $checkData['text'] = '下级分销商人数';
            $checkData['unit'] = '人';
        }
        // 累计佣金
        if ($setting['become'] == '50' && $this->agent) {
            $agent_money = $this->agent['total_money'];
            $is_pass = $agent_money >= $level_condition;
            $checkData['now_target'] = $agent_money;
            $checkData['text'] = '累计分销佣金';
            $checkData['unit'] = '元';
        }
        // 累计团队业绩
        if ($setting['become'] == '70') {
            $team_money = TeamOrder::getOrderAllPrice($this->user['user_id']);
            $is_pass = $team_money >= $level_condition;
            $checkData['now_target'] = $team_money;
            $checkData['text'] = '累计团队业绩';
            $checkData['unit'] = '元';
        }
        // 单次消费
        if ($setting['become'] == '90') {
            $maxMoney = (new OrderModel)->where('user_id', '=', $this->user['user_id'])
                ->where('order_status', '=', 30)
                ->max('total_price');
            $is_pass = $maxMoney >= $level_condition;
            $checkData['now_target'] = $maxMoney;
            $checkData['text'] = '单次最大消费';
            $checkData['unit'] = '元';
        }
        // 购买商品
        // $productList = [];
        // if ($setting['become'] == '100') {
        //     if ($setting['become__buy_product_ids']) {
        //         $productList = (new ProductModel)->getListByIds($setting['become__buy_product_ids']);
        //         $userBuyCount = (new OrderProductModel)->where('product_id', 'IN', $setting['become__buy_product_ids'])
        //             ->where('user_id', '=', $this->user['user_id'])
        //             ->count();
        //         $is_pass = $userBuyCount > 0;
        //     }
        // }
        return $this->renderSuccess('', [
            'is_pass' => $is_pass,
            'checkData' => $checkData
        ]);
    }
 
    /**
     * 提交队长申请
     */
    public function submit()
    {
        $data = $this->postData();
        $model = new regionApplyModel;
        if ($model->submit($this->user, $data)) {
            return $this->renderSuccess('成功');
        }
        return $this->renderError($model->getError() ?: '提交失败');
    }
 
    /*
     *获取队长协议
     */
    public function getAgreement()
    {
        $model = new Setting();
        $data = $model->getItem('license');
        return $this->renderSuccess('', compact('data'));
    }
 
}