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
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
<?php
 
namespace app\common\model\supplier;
 
use app\common\model\BaseModel;
use app\common\model\shop\Role as RoleModel;
use app\common\model\supplier\member\Member as MemberModel;
use app\common\model\supplier\PointsLog as PointsLogModel;
use app\shop\model\auth\UserRole as UserRoleModel;
 
/**
 * 商家供应商模型
 */
class Supplier extends BaseModel
{
    protected $name = 'supplier';
    protected $pk = 'shop_supplier_id';
 
 
 
    /**
     * 关联应用表
     */
    public function app()
    {
        return $this->belongsTo('app\\common\\model\\app\\App', 'app_id', 'app_id');
    }
 
    /**
     * 关联logo
     */
    public function logo()
    {
        return $this->hasOne('app\\common\\model\\file\\UploadFile', 'file_id', 'logo_id');
    }
    /**
     * 关联企业微信二维码
     */
    public function qyQrcode()
    {
        return $this->hasOne('app\\common\\model\\file\\UploadFile', 'file_id', 'qy_qrcode_id');
    }
    /**
     * 关联品牌类型
     */
    public function category()
    {
        return $this->hasOne('app\\common\\model\\supplier\\Category', 'category_id', 'category_id');
    }
    /**
     * 关联business
     */
    public function business()
    {
        return $this->hasOne('app\\common\\model\\file\\UploadFile', 'file_id', 'business_id');
    }
    /**
     * 关联超管
     */
    public function superUser()
    {
        return $this->hasOne('app\\common\\model\\supplier\\User', 'shop_supplier_id', 'shop_supplier_id')
            ->where('is_super','=', 1);
    }
    /**
     * 详情
     */
    public static function detail($shop_supplier_id, $with = [])
    {
        return (new static())->with($with)->find($shop_supplier_id);
    }
 
    /**
     * 累积供应商结算金额 (批量)
     */
    public function onBatchIncSupplierMoney($data)
    {
        foreach ($data as $supplierId => $supplierMoney) {
            $this->where(['shop_supplier_id' => $supplierId])
                ->inc('total_money', $supplierMoney)
                ->inc('money', $supplierMoney)
                ->update();
        }
        return true;
    }
 
    /**
     * 累积商户的可用积分
     */
    public function setIncPoints($points, $describe, $decPoints = 0)
    {
        // 新增积分变动明细
        PointsLogModel::add([
            'shop_supplier_id' => $this['shop_supplier_id'],
            'value' => $points,
            'describe' => $describe,
            'app_id' => $this['app_id'],
        ]);
 
        // 更新用户可用积分
        $data['points'] = ($this['points'] + $points + $decPoints <= 0) ? 0 : $this['points'] + $points + $decPoints;
        $this->where('branch_id', '=', $this['branch_id'])->update($data);
        
        return true;
    }
 
    //判断营业时间
    public function supplierStatus($shop_supplier_id)
    {
        $supplier = $this->where('shop_supplier_id', '=', $shop_supplier_id)
            ->where('is_delete', '=', 0)
            ->find();
        //是否开启营业
        if (!$supplier || $supplier['status'] != 0) {
            return false;
        }
        //判断是否设置了营业时间
        $business_start_time= $supplier['business_start_time'];
        $business_end_time = $supplier['business_end_time'];
        if (empty($business_start_time) || empty($business_end_time)) {
            return true;
        }
 
        //营业时间
        $start_time = strtotime($business_start_time);
        $end_time = strtotime($business_end_time);
        if($end_time >= $start_time){
            //结束时间大于开始时间
            if (!(time() >= $start_time && time() <= $end_time)) {
                return false;
            }
        }else{
            //开始的时间大于结束时间,即是营业到凌晨
            if (time() >= $end_time && time() <= $start_time) {
                return false;
            }
        }
 
        return true;
    }
 
    /**
     * 通过角色管理的区域获取商户id
     */
    public static function getSupplierIdsByUser($user)
    {
 
        $model = new static();
        //获取用户所有的角色
        $user_role_ids = (new UserRoleModel())::getRoleIds($user['shop_user_id']);
        if(!empty($user_role_ids)){
            //获取所有角色的权限
            $area_arr =RoleModel::getAreaIdsByRoleIds($user_role_ids);
            if(!empty($area_arr)){
                $area_ids = [];
                foreach ($area_arr as $val){
                    if(!is_array($val)){
                        $val = json_decode($val,true);
                    }
                    if(empty($area_ids)){
                        //第一个角色所管理的区域
                        $area_ids = $val;
                    }else{
                        //合并角色管理的区域
                        $area_ids = array_merge($area_ids, $val);
                    }
                }
                if(!empty($area_ids)){
                    //获取加盟商入驻时所选的区域
                    return $model->where('area_id',"in",$area_ids)
                        ->column("shop_supplier_id");
                }
            }
 
        }
        return [];
    }
 
    /**
     * 获取供应商当前有效的年卡
     */
    public function getCurrentMember()
    {
        return MemberModel::getCurrentMember($this['shop_supplier_id'], $this['app_id']);
    }
    
    /**
     * 获取角色管理的区域
     */
    public static function getAreaIdsByUser($user)
    {
        //获取用户所有的角色
        $user_role_ids = (new UserRoleModel())::getRoleIds($user['shop_user_id']);
        if(!empty($user_role_ids)){
            //获取所有角色的权限
            $area_arr =RoleModel::getAreaIdsByRoleIds($user_role_ids);
            if(!empty($area_arr)){
                $area_ids = [];
                foreach ($area_arr as $val){
                    if(!is_array($val)){
                        $val = json_decode($val,true);
                    }
                    if(empty($area_ids)){
                        //第一个角色所管理的区域
                        $area_ids = $val;
                    }else{
                        //合并角色管理的区域
                        $area_ids = array_merge($area_ids, $val);
                    }
                }
                return $area_ids;
            }
 
        }
        return [];
    }
 
    //获取自营的商户 by yj 2024.1.9
    public static function getSupplierSelfSupport()
    {
        $model = new static();
        $supplier = $model->where('store_type', '=', 20)
            ->where('is_delete', '=', 0)
            ->column("shop_supplier_id");
        return $supplier;
    }
 
    //获取名称获取准确的商户 by yj 2024.2.28
    public static function getSupplierIdByName($name)
    {
        $model = new static();
        $supplier = $model->where('name', '=', $name)
            ->where('is_delete', '=', 0)
            ->find();
        return empty($supplier) ? '-1' : $supplier["shop_supplier_id"];
    }
 
    /**
     * 商户独立收款是否开启 by yj 2024.8.6
     */
    public static function getIndependentOpen($shop_supplier_id)
    {
        $model = new static();
        $data = $model->where('shop_supplier_id', '=', $shop_supplier_id)
            ->where('is_delete', '=', 0)
            ->find();
        return empty($data['is_independent']) ? 0 : $data['is_independent'];
    }
    /**
     * 冻结用户资金
     */
    public function freezeMoney($money)
    {
        $this->save(['money' => $this['money'] - $money,'freeze_money'=>$this['freeze_money']+$money]);
    }
    /**
     * 直推供应商多少人
     */
    public function refereeSupplierCount ($referee_id)
    {
        $count = $this->where('referee_id', '=', $referee_id)
            ->where('is_delete', '=', 0)
            ->count();
        return $count;
    }
 
    /**
     * 下级商户数量
     * @param $user_id
     * @return int
     */
    public function getSubordinateNum($user_id)
    {
        return (new static())->where('is_delete', '=', 0)
            ->where('referee_id', 'in', $user_id)
            ->count();
    }
 
}