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
<?php
 
 
namespace app\operations\model\store;
 
use app\common\model\store\Clerk as ClerkModel;
 
/**
 * 店员模型
 */
class Clerk extends ClerkModel
{
    // 定义全局的查询范围
    protected $globalScope = ['status'];
    public function scopeStatus($query)
    {
        $shop_supplier_ids = \app\operations\model\supplier\Supplier::getSupplierIdsByUser(session('jjjshop_operations')['user']);
        if (empty($shop_supplier_ids)){
            $query->where($query->getTable() . '.shop_supplier_id', -1);
        }else{
            $query->where($query->getTable() . '.shop_supplier_id', 'in', $shop_supplier_ids);
        }
    }
    const FORM_SCENE_ADD = 'add';
    const FORM_SCENE_EDIT = 'edit';
 
    /**
     * 获取列表数据
     */
    public function getList($status = -1, $store_id = 0, $search = '', $params)
    {
        $model = $this;
        if ($status > -1) {
            $model = $model->where('status', '=', (int)$status);
        }
        if ($store_id > 0) {
            $model = $model->where('store_id', '=', (int)$store_id);
        }
        if (!empty($search)) {
            $model = $model->where('real_name|mobile', 'like', '%' . $search . '%');
        }
        if(isset($params['shop_supplier_ids'])&&$params['shop_supplier_ids']){
            $model = $model->where('shop_supplier_id', 'in', $params['shop_supplier_ids']);
        }
        // 查询列表数据
        return $model->with(['store.supplier', 'user'])
            ->where('is_delete', '=', '0')
            ->order(['create_time' => 'desc'])
            ->paginate($params);
    }
 
    /**
     * 查询所有列表数据
     */
    public function getAll($shop_supplier_id = 0)
    {
        $model = $this;
        if($shop_supplier_id > 0){
            $model = $model->where('shop_supplier_id', '=', $shop_supplier_id);
        }
        // 查询列表数据
        return $model->where('is_delete', '=', '0')->with(['store'])->select();
 
    }
 
    /**
     * 新增记录
     */
    public function add($data)
    {
        $data['app_id'] = self::$app_id;
        return self::create($data);
    }
 
    /**
     * 编辑记录
     */
    public function edit($data)
    {
        // 表单验证
        if (!$this->validateForm($data, self::FORM_SCENE_EDIT)) {
            return false;
        }
 
        return $this->save($data);
    }
 
    /**
     * 软删除
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }
 
    /**
     * 表单验证
     */
    private function validateForm($data, $scene = self::FORM_SCENE_ADD)
    {
        if ($scene === self::FORM_SCENE_ADD) {
            if (!isset($data['user_id']) || empty($data['user_id'])) {
                $this->error = '请选择用户';
                return false;
            }
            if (self::detail(['user_id' => $data['user_id'], 'is_delete' => 0])) {
                $this->error = '该用户已经是店员,无需重复添加';
                return false;
            }
        }
        return true;
    }
 
}