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
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
<?php
 
namespace app\api\model\supplier;
 
use app\common\model\plus\shareholder\Apply as ShareholderApplyModel;
use app\common\model\supplier\Supplier as SupplierModel;
use app\common\model\supplier\User as SupplierUserModel;
use app\api\model\user\Favorite as FavoriteModel;
use app\api\model\product\Product as ProductModel;
use app\api\model\plus\team\Apply as teamApplyModel;
/**
 * 供应商模型类
 */
class Supplier extends SupplierModel
{
    /**
     * 添加
     */
    public function addData($data)
    {
        // 开启事务
        $this->startTrans();
        try {
            if (SupplierUserModel::checkExist($data['user_name'])) {
                $this->error = '用户名已存在';
                return false;
            }
            // 添加供应商
            $this->save($data);
            //添加供应商账号
            $SupplierUserModel = new SupplierUserModel;
            $data['shop_supplier_id'] = $this['shop_supplier_id'];
            $data['is_super'] = 1;
            $SupplierUserModel->save($data);
            (new teamApplyModel())->becomeTeamByAgent($data['referee_id'],70,$data['app_id']);
            //根据团队人数判断股东 by yj
            $shareholderModel = new ShareholderApplyModel;
            $shareholderModel->becomeShareholderByTeam($data['referee_id'], 80, $data['app_id']);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    public function getUserName($user_name)
    {
        return $this->where('user_name', '=', $user_name)->count();
    }
 
    //获取店铺信息
    public function getDetail($data, $user)
    {
        $detail = $this->alias('s')->where(['shop_supplier_id' => $data['shop_supplier_id']])
            ->field("name as store_name,shop_supplier_id,logo_id,category_id,server_score,fav_count,user_id,product_sales,address,notice,description")
            ->with(['logo', 'category'])
            ->find();
        if ($detail) {
            $detail['logos'] = $detail['logo']?$detail['logo']['file_path']:'';
            $detail['category_name'] = $detail['category']['name'];
            unset($detail['logo']);
            unset($detail['category']);
            $detail['isfollow'] = 0;
            $detail['notice'] = empty($detail['notice']) ? '欢迎光临' . $detail['store_name'] . '~' : $detail['notice'];
            $detail['supplier_user_id'] = (new SupplierUserModel())->where('shop_supplier_id', '=', $detail['shop_supplier_id'])->value('supplier_user_id');
            if ($user) {
                $detail['isfollow'] = (new FavoriteModel)
                    ->where('pid', '=', $data['shop_supplier_id'])
                    ->where('user_id', '=', $user['user_id'])
                    ->where('type', '=', 10)
                    ->count();
            }
        }
        return $detail;
    }
 
    //获取微店账号信息
    public function getAccount($shop_supplier_id, $field = "*")
    {
        $detail = $this->where(['shop_supplier_id' => $shop_supplier_id])->field("$field")->find();
        return $detail;
    }
 
    //店铺列表
    public function supplierList($param)
    {
        // 排序规则
        $sort = [];
        if ($param['sortType'] === 'all') {
            $sort = ['s.create_time' => 'desc'];
        } else if ($param['sortType'] === 'sales') {
            $sort = ['product_sales' => 'desc'];
        } else if ($param['sortType'] === 'score') {
            $sort = ['server_score' => 'desc'];
        }
 
        $model = $this;
        if (isset($param['name']) && $param['name']) {
            $model = $model->where('name', 'like', '%' . $param['name'] . '%');
        }
        if (isset($param['category_id']) && $param['category_id']) {
            $model = $model->where('category_id', '=', $param['category_id']);
        }
        // 查询列表数据
        $list = $model->alias('s')->with(['logo', 'category'])
            ->where('s.is_delete', '=', '0')
            ->where('s.is_recycle', '=', 0)
            //->where('s.is_full', '=', 1)
            ->field("s.shop_supplier_id,s.name,s.fav_count,logo_id,category_id,server_score,product_sales,address,link_phone,longitude,latitude")
            ->order($sort)
            ->paginate($param);
        $product_model = new ProductModel();
        foreach ($list as $key => &$v) {
            $productList = $product_model->with(['image.file'])
                ->where([
                    'shop_supplier_id' => $v['shop_supplier_id'],
                    'product_status' => 10,
                    'audit_status' => 10,
                    'is_delete' => 0
                ])
                ->order('product_sort asc,product_id desc')
                ->limit(3)
                ->field('product_id,product_price,product_name,sales_initial,sales_actual,line_price')
                ->select();
            $v['productList'] = $productList;
            $v['logos'] = isset($v['logo'])?$v['logo']['file_path']:'';
            $v['category_name'] = $v['category']['name'];
            $v['latitude'] = (float)$v['latitude'];
            $v['longitude'] = (float)$v['longitude'];
            unset($v['logo']);
            unset($v['category']);
        }
        return $list;
    }
    
    //查询用户申请供应商状态
    public static function getStatus($user)
    {
        $apply = (new Apply())->where('user_id', '=', $user['user_id'])
            ->find();
        $supplier = (new static())->where('user_id', '=', $user['user_id'])
            ->where('is_delete', '=', 0)
            ->count();
        $status = 0;
        if ($user['user_type'] == 2) {
            if ($supplier) {
                $status = 2;
            } else {
                $status = 3;
            }
        } else {
            if ($apply) {
                if ($apply['status'] == 0) {
                    $status = 1;
                }
            }
        }
        return $status;
    }
    //店铺列表
    public function getLists($shop_supplier_id='')
    {
        $model = $this;
        // 查询列表数据
        $list = $model->with(['qyQrcode'])->where('is_delete', '=', '0')
            ->where('is_recycle', '=', 0)
            ->select();
        if(!empty($list)){
            foreach ($list as $key => &$val){
                if($val["shop_supplier_id"] == $shop_supplier_id){
                    $index=$key;
                }
                $val["add_group"]="加群";
            }
        }
        $data["list"]=$list;
        $data["index"]=empty($index) ? '-1' : $index;
        return $data;
    }
 
    //店铺列表(简约)
    public function getListSimple($params = [])
    {
        $model = $this;
        if (!empty($params['keyword'])) {
            $model = $model->where('name', 'like', '%'. $params['keyword'] .'%');
        }
        return $model->where('is_delete', '=', '0')
            ->where('is_recycle', '=', 0)
            ->order(['create_time' => 'desc'])
            ->paginate($params);
    }
    public static function getUserStore($user_id)
    {
        $model = new self();
        return $model->where('user_id', '=', $user_id)
            ->where('is_delete', '=', 0)
            ->where('is_recycle', '=', 0)
            ->find();
    }
}