quanwei
13 hours ago 408c463c5b66bba2aa1c81d8dca23e04c1608e24
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php
 
namespace app\shop\model\supplier;
 
use app\common\model\supplier\Supplier as SupplierModel;
use app\common\model\supplier\User as SupplierUserModel;
use app\common\model\user\User as UserModel;
use app\shop\model\product\Product as ProductModel;
use Lvht\GeoHash; // by yj
use app\shop\model\store\Store as StoreModel; // by yj
use app\shop\model\order\Order as OrderModel; // by yj
use app\shop\model\store\Coupon as StoreCouponModel; // by yj
use app\shop\service\order\ExportService;
 
/**
 * 后台管理员登录模型
 */
class Supplier extends SupplierModel
{
    /**
     * 获取列表数据
     */
    public function getList($params)
    {
        $model = $this;
        if (isset($params['search']) && $params['search']) {
            $model = $model->where('name', 'like', '%' . $params['search'] . '%');
        }
        if(isset($params['shop_supplier_ids'])&&$params['shop_supplier_ids']){
            $model = $model->where('shop_supplier_id', 'in', $params['shop_supplier_ids']);
        }
        if(isset($params['is_takeout']) && $params['is_takeout'] > -1){
            $model = $model->where('is_takeout', '=', $params['is_takeout']);
        }
        // 查询列表数据
        return $model->with(['logo', 'superUser', 'business', 'qyQrcode'])
            ->where('is_delete', '=', '0')
            ->order(['create_time' => 'desc'])
            ->paginate($params);
    }
 
    /**
     * 添加
     */
    public function add($data)
    {
        // 开启事务
        $this->startTrans();
        try {
            $supplier = $data['supplier'];
            if (SupplierUserModel::checkExist($supplier['user_name'])) {
                $this->error = '用户名已存在';
                return false;
            }
            // 用户是否已绑定
            $user = null;
            if($supplier['user_id'] > 0){
                $user = UserModel::detail($supplier['user_id']);
                if ($user['user_type'] != 1) {
                    $this->error = '该用户已绑定';
                    return false;
                }
                $isApply = $this->isApply($supplier['user_id']);
                if ($isApply) {
                    $this->error = '已经申请开店';
                    return false;
                }
            }
            $supplier = $this->createData($supplier);
            // 添加供应商
            $supplier['app_id'] = self::$app_id;
            $this->save($supplier);
            // 添加登录用户
            $user_model = new SupplierUserModel();
            $user_model->save([
                'user_id' => $supplier['user_id'],
                'user_name' => $supplier['user_name'],
                'password' => salt_hash($supplier['password']),
                'real_name' => $supplier['user_name'],
                'shop_supplier_id' => $this['shop_supplier_id'],
                'is_super' => 1,
                'app_id' => self::$app_id,
            ]);
            // 后台添加的直接算审核通过
            if($user){
                $user->save([
                    'user_type' => 2
                ]);
            }
            // 同时添加门店 by lyzflash
            $this->addStore($supplier);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 创建数据
     */
    private function createData($data)
    {
        $data['app_id'] = self::$app_id;
        // 格式化坐标信息
        $coordinate = explode(',', $data['coordinate']);
        $data['latitude'] = $coordinate[0];
        $data['longitude'] = $coordinate[1];
 
        // 生成geohash
        $Geohash = new Geohash;
        $data['geohash'] = $Geohash->encode($data['longitude'], $data['latitude']);
        return $data;
    }
 
    /**
     * 同时创建门店
     */
    private function addStore($data)
    {
        $data['store_name'] = $data['name'];
        $data['linkman'] = $data['link_name'];
        $data['phone'] = $data['link_phone'];
        $data['logo_image_id'] = $data['logo_id'];
        $data['summary'] = $data['description'];
        $data['shop_supplier_id'] = $this['shop_supplier_id'];
        $data['status'] = 1;
        return (new StoreModel)->save($data);
    }
 
    /**
     * 修改
     */
    public function edit($data)
    {
        // 开启事务
        $this->startTrans();
        try {
            $supplier = $data['supplier'];
            $old_user_id = 0;
            if($this['superUser']){
                $old_user_id = $this['superUser']['user_id'];
            }
            if ($this['superUser'] && $supplier['user_name'] != $this['superUser']['user_name'] && SupplierUserModel::checkExist($supplier['user_name'])) {
                $this->error = '用户名已存在';
                return false;
            }
 
            // 用户是否已绑定
            $user = null;
            $userChange = false;
            if (!empty($supplier['user_id'])){
                if($this['superUser'] && $supplier['user_id'] > 0 && $supplier['user_id'] != $this['superUser']['user_id']){
                    $user = UserModel::detail($supplier['user_id']);
                    if ($user['user_type'] != 1) {
                        $this->error = '该用户已绑定';
                        return false;
                    }
                    $isApply = $this->isApply($supplier['user_id']);
                    if ($isApply) {
                        $this->error = '已经申请开店';
                        return false;
                    }
                    $userChange = true;
                }
                $user=UserModel::detail($supplier['user_id']);
                $supplier['referee_id'] = $user['referee_id'];
            }
 
            // 修改供应商
            $this->save($supplier);
            // 修改登录用户
            $user_model = $this['superUser'];
            $user_data = [
                'user_id' => $supplier['user_id'],
                'user_name' => $supplier['user_name']
            ];
            if (isset($supplier['password']) && !empty($supplier['password'])) {
                $user_data['password'] = salt_hash($supplier['password']);
            }
            $user_model->save($user_data);
            // 后台添加的直接算审核通过
            if($userChange){
                $user->save([
                    'user_type' => 2
                ]);
                //取消原来的
                if ($old_user_id > 0){
                    (new UserModel())->where('user_id', '=', $old_user_id)->update([
                        'user_type' => 1
                    ]);
                }
            }
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 软删除
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }
    /**
     * 开启禁止
     */
    public function setRecycle($is_recycle)
    {   
        // 开启事务
        $this->startTrans();
        try {
            if($is_recycle==1){
                //产品下架
                (new ProductModel())->where('shop_supplier_id','=',$this['shop_supplier_id'])->update(['product_status' => 20]);
            }
            //更改店铺状态
            $this->save(['is_recycle' => $is_recycle]); 
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
        
         
    }
    /**
     * 获取列表数据
     */
    public static function getAll()
    {
        $model = new static();
        // 查询列表数据
        return $model->field(['shop_supplier_id,name'])->where('is_delete', '=', '0')
            ->order(['create_time' => 'desc'])
            ->select();
    }
 
 
    /**
     * 提现驳回:解冻资金
     */
    public static function backFreezeMoney($shop_supplier_id, $money)
    {
        $model = self::detail($shop_supplier_id);
        return $model->save([
            'money' => $model['money'] + $money,
            'freeze_money' => $model['freeze_money'] - $money,
        ]);
    }
 
    /**
     * 提现打款成功:累积提现金额
     */
    public static function totalMoney($shop_supplier_id, $money)
    {
        $model = self::detail($shop_supplier_id);
        return $model->save([
            'freeze_money' => $model['freeze_money'] - $money,
            'cash_money' => $model['cash_money'] + $money,
        ]);
    }
    /**
     * 获取供应商数量
     */
    public static function getTotal($where)
    {
        $model = new static;
        return $model->where($where)->count();
    }
 
    /**
     * 获取供应商总数量
     */
    public function getSupplierTotal()
    {
        return $this->where(['is_delete' => 0])->count();
    }
 
    /**
     * 获取供应商总数量
     */
    public static function getSupplierTotalByDay($day)
    {
        $startTime = strtotime($day);
        return (new static())->where('create_time', '>=', $startTime)
            ->where('create_time', '<', $startTime + 86400)
            ->count();
    }
 
    /**
     * 获取供应商统计数量
     */
    public function getSupplierData($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);
        }
        if($type == 'supplier_total' || $type == 'supplier_add'){
            return $model->count();
        }
        return 0;
    }
 
    /**
     * 获取平台的总销售额
     */
    public function getTotalMoney($type = 'total_money',$postData=[])
    {
        $model = $this;
        if(!empty($postData["supplier_name"])){
            //获取商户id
            $shop_supplier_id = SupplierModel::getSupplierIdByName($postData['supplier_name']);
            $model = $model->where('shop_supplier_id', '=', $shop_supplier_id);
        }
        if($type == 'total'){
            return $model->sum('total_money');
        } else if($type == 'money'){
            return $model->sum('money');
        } else if($type == 'freeze_money'){
            return $model->sum('freeze_money');
        } else if($type == 'cash_money'){
            return $model->sum('cash_money');
        } else if($type == 'deposit_money'){
            return $model->sum('deposit_money');
        }
        return 0;
    }
 
    //判断用户是否申请
    public function isApply($user_id)
    {
        return $this->where('user_id', '=', $user_id)->where('status', '=', 0)->count();
    }
 
    /**
     * 获取列表数据
     */
    public function getStatisticsList($params,$is_page = true)
    {
        $model = $this;
        if (isset($params['search']) && $params['search']) {
            $model = $model->where('name', 'like', '%' . $params['search'] . '%');
        }
 
        //搜索时间段
        $create_time = empty($params['create_time']) ? 0 : $params['create_time'];
        $model = $model->field("shop_supplier_id,name")
            ->where('is_delete', '=', '0')
            ->order(['create_time' => 'desc']);
 
        // 查询列表数据
        if($is_page){
            $list = $model->paginate($params);
            foreach ($list->toArray()["data"] as $key => $value) {
                $list[$key]["coupon_money"] = orderModel::getCouponMoneyBySupplier($value["shop_supplier_id"],$create_time);
                $list[$key]["coupon_num"] = orderModel::getCouponNumBySupplier($value["shop_supplier_id"],$create_time);
                $list[$key]["store_coupon_money"] = StoreCouponModel::getCouponMoneyBySupplier($value["shop_supplier_id"],$create_time);
                $list[$key]["store_coupon_num"] = StoreCouponModel::getCouponNumBySupplier($value["shop_supplier_id"],$create_time);
                $list[$key]["total_coupon_money"] =  $list[$key]["coupon_money"] + $list[$key]["store_coupon_money"];
            }
        }else{
            $list = $model->select();
            foreach ($list as $key => $value) {
                $list[$key]["coupon_money"] = orderModel::getCouponMoneyBySupplier($value["shop_supplier_id"],$create_time);
                $list[$key]["coupon_num"] = orderModel::getCouponNumBySupplier($value["shop_supplier_id"],$create_time);
                $list[$key]["store_coupon_money"] = StoreCouponModel::getCouponMoneyBySupplier($value["shop_supplier_id"],$create_time);
                $list[$key]["store_coupon_num"] = StoreCouponModel::getCouponNumBySupplier($value["shop_supplier_id"],$create_time);
                $list[$key]["total_coupon_money"] =  $list[$key]["coupon_money"] + $list[$key]["store_coupon_money"];
            }
        }
 
        return $list;
    }
 
    /**
     * 订单导出
     */
    public function exportList($params)
    {
        // 获取订单列表
        $list = $this->getStatisticsList($params,false);
        // 导出excel文件
        return (new Exportservice)->supplierStatisticsList($list);
    }
}