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
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
<?php
 
 
namespace app\common\model\user;
 
use app\common\library\helper;
use app\common\model\BaseModel;
use app\common\model\plus\bonus\Setting as bonusSettingModel;
use app\common\model\user\Grade as GradeModel;
use app\common\model\user\PointsLog as PointsLogModel;
use app\common\model\supplier\User as SupplierUserModel;
use app\common\model\store\Clerk as StoreClerkModel;
use app\common\model\user\User as UserModel;
use app\common\model\settings\Setting as SettingModel;
use think\facade\Db; // by lyzflash
use app\common\model\plus\bonus\User as BonusUserModel;
use app\common\model\user\ConsumptionLog as ConsumptionLogModel;
/**
 * 用户模型
 */
class User extends BaseModel
{
    protected $pk = 'user_id';
    protected $name = 'user';
 
    /**
     * 修改器
     */
    public function getBirthdayAttr($value)
    {
        return $value ? date("Y-m-d",$value) : '';
    }
 
    /**
     * 修改器
     */
    public function setBirthdayAttr($value)
    {
        return $value ? strtotime($value) : 0;
    }
 
    /**
     * 关联会员等级表
     */
    public function grade()
    {
        return $this->belongsTo('app\\common\\model\\user\\Grade', 'grade_id', 'grade_id');
    }
 
    /**
     * 关联收货地址表
     */
    public function address()
    {
        return $this->hasMany('app\\common\\model\\user\\UserAddress', 'address_id', 'address_id');
    }
 
    /**
     * 关联供应商表
     */
    public function supplierUser()
    {
        return $this->hasOne('app\\common\\model\\supplier\\User', 'user_id', 'user_id');
    }
    /**
     * 关联店员表
     */
    public function clerkUser()
    {
        return $this->hasOne('app\\common\\model\\store\\Clerk', 'user_id', 'user_id');
    }
    /**
     * 关联收货地址表 (默认地址)
     */
    public function addressDefault()
    {
        return $this->belongsTo('app\\common\\model\\user\\UserAddress', 'address_id', 'address_id');
    }
 
    /**
    * 关联用户详细信息表
    */
//    public function userInfo()
//    {
//        return $this->hasOne('app\\common\\model\\user\\UserInfo', 'user_id', 'user_id');
//    }
    /**
 
    /**
     * 获取用户信息
     */
    public static function detail($where)
    {
        $model = new static;
        $filter = ['is_delete' => 0];
        if (is_array($where)) {
            $filter = array_merge($filter, $where);
        } else {
            $filter['user_id'] = (int)$where;
        }
        return $model->where($filter)->with(['address', 'addressDefault', 'grade'])->find();
    }
 
    /**
     * 获取用户信息
     */
    public static function detailByUnionid($unionid)
    {
        $model = new static;
        $filter = ['is_delete' => 0];
        $filter = array_merge($filter, ['union_id' => $unionid]);
        return $model->where($filter)->with(['address', 'addressDefault', 'grade'])->find();
    }
 
    /**
     * 指定会员等级下是否存在用户
     */
    public static function checkExistByGradeId($gradeId)
    {
        $model = new static;
        return !!$model->where('grade_id', '=', (int)$gradeId)
            ->where('is_delete', '=', 0)
            ->value('user_id');
    }
 
    /**
     * 累积用户总消费金额
     */
    public function setIncPayMoney($money)
    {
        return $this->where('user_id', '=', $this['user_id'])->inc('pay_money', $money)->update();
    }
 
    /**
     * 累积用户实际消费的金额 (批量)
     */
    public function onBatchIncExpendMoney($data)
    {
        foreach ($data as $userId => $expendMoney) {
            $this->where(['user_id' => $userId])->inc('expend_money', $expendMoney)->update();
            event('UserGrade', $userId);
        }
        return true;
    }
 
    /**
     * 累积用户的可用积分数量 (批量)
     */
    public function onBatchIncPoints($data)
    {
        foreach ($data as $userId => $expendPoints) {
            $this->where(['user_id' => $userId])
                ->inc('points', $expendPoints)
                ->inc('total_points', $expendPoints)
                ->update();
            event('UserGrade', $this['user_id']);
        }
        return true;
    }
    /**
     * 累积用户的可用消费券数量 (批量)
     */
    public function onBatchIncConsumption($data)
    {
        foreach ($data as $userId => $expendPoints) {
            $this->where(['user_id' => $userId])
                ->inc('consumer_coupon', $expendPoints)
                ->inc('total_expend_money', $expendPoints)
                ->update();
            event('UserGrade', $this['user_id']);
        }
        return true;
    }
 
    /**
     * 累积用户的可用积分
     */
    public function setIncPoints($points, $describe, $decPoints = 0, $upgrade = true)
    {
        // 新增积分变动明细
        PointsLogModel::add([
            'user_id' => $this['user_id'],
            'value' => $points,
            'describe' => $describe,
            'app_id' => $this['app_id'],
        ]);
 
        // 更新用户可用积分
        $data['points'] = ($this['points'] + $points + $decPoints <= 0) ? 0 : $this['points'] + $points + $decPoints;
        // 用户总积分
        if ($points > 0) {
            $data['total_points'] = $this['total_points'] + $points;
        }
        $this->where('user_id', '=', $this['user_id'])->update($data);
        if($upgrade) {
            event('UserGrade', $this['user_id']);
        }
        return true;
    }
    /**
     * 累积用户的可用消费券
     */
    public function setIncConsumption($consumption, $describe, $decConsumption = 0, $upgrade = true)
    {
        // 新增消费券变动明细
        ConsumptionLogModel::add([
            'user_id' => $this['user_id'],
            'value' => $consumption,
            'describe' => $describe,
            'app_id' => $this['app_id'],
        ]);
 
        // 更新用户可用消费券
        $data['consumer_coupon'] = ($this['consumer_coupon'] + $consumption + $decConsumption <= 0) ? 0 : $this['consumer_coupon'] + $consumption + $decConsumption;
        // 用户总消费券
        if ($consumption > 0) {
            $data['total_expend_money'] = $this['total_expend_money'] + $consumption;
        }
        $this->where('user_id', '=', $this['user_id'])->update($data);
        if($upgrade) {
            event('UserGrade', $this['user_id']);
        }
        return true;
    }
 
    //更新用户类型
    public static function updateType($user_id, $user_type)
    {
        $model = new static;
        return $model->where('user_id', '=', $user_id)->update([
            'user_type' => $user_type
        ]);
    }
 
    /**
     * 用户是否成功成为供应商,如果不是则为审核中
     * 申请中的不算
     */
    public static function isSupplier($user_id)
    {
        return SupplierUserModel::detail([
                'user_id' => $user_id
            ]) != null;
    }
    /**
     * 用户是否成功成为供应商,如果不是则为审核中
     * 申请中的不算
     */
    public static function isStoreClerk($user_id)
    {
        return StoreClerkModel::detail([
                'user_id' => $user_id
            ]) != null;
    }
    public static function isStoreClerkGly($user_id)
    {
        return StoreClerkModel::detail([
                'user_id' => $user_id,
                'type'=>20
            ]) != null;
    }
    /**
     * 累计邀请数
     */
    public function setIncInvite($user_id)
    {
        $this->where('user_id', '=', $user_id)->inc('total_invite')->update();
        event('UserGrade', $user_id);
    }
    /**
     * 修改推荐人,减去旧推荐人邀请人数,加上新推荐人邀请数 by yj
     */
    public function setOnInvite($user_id,$referee_id)
    {
        $user = $this->where('user_id', '=', $user_id)->find();
        //判断有没有推荐人
        if($user["referee_id"]){
            $this->where('user_id', '=', $user["referee_id"])->dec('total_invite')->update();
        }
        //更新新推荐人的邀请数
        $this->where('user_id', '=', $referee_id)->inc('total_invite')->update();
        event('UserGrade', $referee_id);
    }
 
    /* 处理瑞和旧系统的积分 */
    public function syncPoints($open_id)
    {
        $user = static::detail(['open_id' => $open_id]);
        $old_user = Db::connect('ruihe')->table('ims_ewei_shop_member')->where('openid_wa', '=', $open_id)->find();
        if ($old_user['credit1'] > 0 && $old_user['is_sync'] == 0) {
            $user->setIncPoints($old_user['credit1'], '旧系统剩余积分');
            // 标识旧系统积分同步状态
            Db::connect('ruihe')->table('ims_ewei_shop_member')->where('openid_wa', '=', $open_id)->update(['is_sync' => 1]);
        }
        return true;
    }
    
    public static function getUserByIds($ids, $field = '')
    {  
        $ids = is_array($ids) ?: explode(',', $ids);
        $model = new static;
        if ($field) {
            $model = $model->field($field);
        }
        return $model->where('user_id', 'in', $ids)->select();
    }
 
    /**
     * 购买商品成功:累积购买商品次数
     */
    public static function setRepurchaseFrequency($user_id)
    {
        $user= self::detail($user_id);
        $purchase_count=$user['purchase_count']+1;
        if ($purchase_count==1&&$user['referee_id']){
            $referee= self::detail($user['referee_id']);
            if ($referee){
                $settingConsumption=SettingModel::getItem('consumption');
                if ($settingConsumption['give_away']){
                    $referee->setIncConsumption($settingConsumption['give_away'], '直推首单礼包');
                }
            }
        }
        /*if ($purchase_count>11){
            return false;
        }*/
        self::where('user_id',$user_id)->update([
            'purchase_count'=>$purchase_count
        ]);
        // 用户等级
        event('UserGrade', $user_id);
        $bonusUser=BonusUserModel::detail($user_id);
        if ($bonusUser){
            return BonusUserModel::detail($user_id)->save([
                'purchase_count'=>$purchase_count
            ]);
        }
        return true;
    }
    /**
     * 取消订单:减少累积购买商品次数
     */
    public static function decreasePurchaseCount($user_id,$product,$app_id)
    {
        // 验证是否设置
        $config = bonusSettingModel::getItem('basic', $app_id);
        if (empty($config['become__buy_product_ids'])) {
            return false;
        }
        // 整理商品id集
        $productIds = helper::getArrayColumn($product, 'product_id');
        // 判断商品是否在设置范围内
        $intersect = array_intersect($productIds, $config['become__buy_product_ids']);
        if (empty($intersect)) {
            return false;
        }
        $quantity=0;
        foreach ($product as $item){
            // 检查商品是否是指定的分红商品
            if (in_array($item['product_id'], $config['become__buy_product_ids'])||$item['is_vip']==1) {
                // 获取商品数量
                $quantity+= $item['total_num'];
            }
        }
        $user= self::detail($user_id);
        $purchase_count=$user['purchase_count']-$quantity;
        if ($purchase_count<0){
            $purchase_count=0;
        }
        $grade=(new GradeModel())->where('purchase_count','<=',$purchase_count)->order('purchase_count','desc')->find();
        self::where('user_id',$user_id)->update([
            'purchase_count'=>$purchase_count,
            'grade_id'=>$grade['grade_id']
        ]);
        // 用户等级
        event('UserGrade', $user_id);
        $bonusUser=BonusUserModel::detail($user_id);
        if ($bonusUser){
            return BonusUserModel::detail($user_id)->save([
                'purchase_count'=>$purchase_count
            ]);
        }
        return true;
    }
    /**
     * 获取指定等级推荐人数量
     */
    public function getRefereeGradeCount($user_id,$referee_grade_ids)
    {
        return $this->where('referee_id', 'in', $user_id)->where('grade_id', 'in', $referee_grade_ids)->count();
    }
 
}