UserCoupon.php
2.1 KB
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
<?php
namespace app\common\model;
use think\Model;
class UserCoupon extends Model
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
/**
* 优惠券
*/
public function coupon()
{
return $this->belongsTo('Coupon');
}
/**
* 优惠券
*/
public function getStatusAttr($value,$data)
{
return $data['status'] == '2' ? 'used' : $this['coupon']['status'];
}
/**
* 优惠券列表
*/
public static function couponList($user,$status='all'){
// 筛选条件
$filter = [];
$now_time = time();
$soon_expire_time = $now_time + 86400 * 3;
// 优惠券状态
switch ($status) {
case 'all':
break;
case 'normal':
$filter['uc.status'] = '1';
$filter['c.expiretime'] = ['>',$soon_expire_time];
break;
case 'soon_expire';
$filter = "uc.status = '1' and c.expiretime > {$now_time} and c.expiretime <= {$soon_expire_time}";
break;
case 'expired';
$filter['uc.status'] = '1';
$filter['c.expiretime'] = ['<',$now_time];
break;
case 'used';
$filter['uc.status'] = '2';
break;
}
$list = self::alias('uc')
->join('coupon c','c.id = uc.coupon_id')
->with('coupon')
->where('uc.user_id',$user['id'])
->where($filter)
->field("
uc.id,
uc.coupon_id,
uc.status,
from_unixtime(c.expiretime,'%Y-%m-%d') expiretime
")
->order('c.expiretime asc')
->select();
foreach($list as $v){
$v->visible(['id','status','expiretime','coupon']);
$v->getRelation('coupon')->visible(['id','coupon_name','coupon_price','limit_price','category_ids','rule']);
}
return $list;
}
}