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
<?php
 
namespace app\common\model\supplier\member;
 
use app\common\model\BaseModel;
 
/**
 * 供应商会员模型
 * 记录供应商拥有的年卡信息
 */
class Member extends BaseModel
{
    // 表名
    protected $name = 'supplier_member';
    protected $pk = 'member_id';
    protected $append=['expire_time_text'];
    /**
     * 过期时间文本
     */
    public function getExpireTimeTextAttr($value, $data)
    {
        return $data['expire_time'] > 0 ? date('Y-m-d H:i:s', $data['expire_time']) : '长期有效';
    }
 
    /**
     * 关联供应商表
     */
    public function supplier()
    {
        return $this->belongsTo('app\common\model\supplier\Supplier', 'shop_supplier_id', 'shop_supplier_id');
    }
 
    /**
     * 关联年卡计划表
     */
    public function plan()
    {
        return $this->belongsTo('app\common\model\supplier\member\Plan', 'plan_id', 'plan_id');
    }
 
    /**
     * 判断是否过期
     */
    public function isExpired()
    {
        return $this->expire_time < time();
    }
 
    /**
     * 获取供应商当前有效的年卡
     */
    public static function getCurrentMember($supplierId, $appId)
    {
        return self::where([
            'shop_supplier_id' => $supplierId,
            'app_id' => $appId,
            'is_expire' => 0
        ])->where('expire_time', '>', time())->find();
    }
    
    /**
     * 获取详情
     */
    public static function detail($memberId)
    {
        return (new static())->where('member_id', '=', $memberId)->where('is_expire', '=', 0)->find();
    }
}