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
162
163
164
165
166
167
168
<?php
 
namespace app\shop\model\supplier;
 
use app\common\library\sms\Driver as SmsDriver;
use app\common\model\supplier\Apply as ApplyModel;
use app\common\model\user\User as UserModel;
use app\api\model\supplier\Supplier as SupplierModel;
use app\api\model\settings\Setting as SettingModel;
use app\common\model\supplier\Category as SupplierCategoryModel;
/**
 * 供应商模型
 */
class Apply extends ApplyModel
{
 
    /**
     * 获取列表数据
     */
    public function getList($params)
    {
        $model = $this;
        if ($params['status'] != '') {
            $model = $model->where('status', '=', $params['status']);
        }
        if ($params['store_name']) {
            $model = $model->where('store_name', 'like', '%' . trim($params['store_name']) . '%');
        }
        if(!empty($params['supplier_type'])){
            $model = $model->where('supplier_type', '=', $params['supplier_type']);
        }
        if(isset($params['area_ids'])&&$params['area_ids']){
            $model = $model->where('area_id', 'in', $params['area_ids']);
        }
        // 查询列表数据
        $list = $model->with(['user', 'category','area',  'businessImage'])
            ->order(['create_time' => 'desc'])
            ->paginate($params);
        // 整理列表数据并返回
        return $this->setListData($list, true);
    }
 
    /**
     * 设置商品展示的数据
     */
    protected function setListData($data, $isMultiple = true, callable $callback = null)
    {
        if (!$isMultiple) $dataSource = [&$data]; else $dataSource = &$data;
 
        return $data;
    }
 
    /**
     * 详情
     */
    public static function detail($supplier_apply_id, $with = [])
    {
        return (new static())->with($with)->find($supplier_apply_id);
    }
 
    /**
     * 审核
     */
    public function audit($data)
    {
        // 开启事务
        $this->startTrans();
        try {
            if ($data['status'] == 2) {
                if (empty($data['content'])) {
                    $this->error = "备注不能为空";
                    return false;
                }
                $template_code = "supplier_reject_code";
                UserModel::updateType($this['user_id'], 1);
                // 申请状态
                $this->save(['status' => 2]);
            } else if ($data['status'] == 1) {
                $template_code = "supplier_pass_code";
                //用户为供应商
                UserModel::updateType($this['user_id'], 2);
                // 申请状态
                $this->save(['status' => 1]);
                //添加供应商账号更新
                $supplier_data = [
                    'user_name' => $this['mobile'],
                    'password' => $this['password'],
                    'name' => $this['store_name'],
                    'user_id' => $this['user_id'],
                    'real_name' => $this['user_name'],
                    'link_phone' => $this['mobile'],
                    'link_name' => $this['user_name'],
                    'business_id' => $this['business_id'],
                    'category_id' => $this['category_id'],
                    'referee_id' => $this['referee_id'],
                    'supplier_type' => $this['supplier_type'],
                    'area_id' => $this['area_id'],
                    'app_id' => self::$app_id,
                    // 添加缺失的字段
                    'address' => $this['address'] ?? '',
                    'province_id' => $this['province_id'] ?? 0,
                    'city_id' => $this['city_id'] ?? 0,
                    'region_id' => $this['region_id'] ?? 0,
                    'longitude' => $this['longitude'] ?? '',
                    'latitude' => $this['latitude'] ?? ''
                ];
                // 供应商状态,如果类目保证金为0,则直接通过,否则需要交纳保证金
                $category = SupplierCategoryModel::detail($this['category_id']);
                if($category['deposit_money'] > 0){
                    $supplier_data['status'] = 20;
                } else{
                    $supplier_data['status'] = 0;
                }
                $SupplierModel = new SupplierModel;
                if (!$SupplierModel->addData($supplier_data)) {
                    $this->error = $SupplierModel->getError();
                    return false;
                }
            }
 
            $smsConfig = SettingModel::getItem('sms', self::$app_id);
            $send_template_code = $smsConfig['engine']['aliyun'][$template_code];
            $SmsDriver = new SmsDriver($smsConfig);
            $SmsDriver->sendSms($this['mobile'], $send_template_code, '');
 
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 获取供应商申请数
     */
    public static function getApplyCount(){
        return (new static())->where('status', '=', 0)->count();
    }
 
    /**
     * 获取供应商申请数
     */
    public static function getApplyCountByDay($day){
        $startTime = strtotime($day);
        return (new static())->where('create_time', '>=', $startTime)
            ->where('create_time', '<', $startTime + 86400)
            ->count();
    }
 
    /**
     * 获取供应商申请统计数量
     */
    public function getApplyData($startDate = null, $endDate = null, $type)
    {
        $model = $this;
        if(!is_null($startDate)){
            $model = $model->where('create_time', '>=', strtotime($startDate));
        }
        if(is_null($endDate)){
            $model = $model->where('create_time', '<', strtotime($startDate) + 86400);
        }else{
            $model = $model->where('create_time', '<', strtotime($endDate) + 86400);
        }
        return $model->count();
    }
}