Coupon.php
1.8 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
<?php
namespace app\common\model;
use think\Model;
class Coupon extends Model
{
/**
* 隐藏字段
* @var array
*/
protected $append = [
'rule',
];
/**
* 优惠券状态
*/
public function getStatusAttr($value,$data)
{
$now_time = time();
$soon_expire_time = $now_time + 86400 * 3;
if($data['expiretime'] > $soon_expire_time){
return 'normal';
}
if($data['expiretime'] > $now_time && $data['expiretime'] <= $soon_expire_time){
return 'soon_expire';
}
if($data['expiretime'] < $now_time){
return 'expired';
}
}
/**
* 优惠券使用规则
*/
public function getRuleAttr($value,$data)
{
if(empty($data['category_ids'])){
return '全场通用';
}
$category_name_arr = Category::where('id','in',$data['category_ids'])->column('name');
if(!empty($category_name_arr)){
return implode('、', $category_name_arr).'品类';
}
return '优惠券不可用';
}
/**
* 优惠券列表
*/
public static function couponList($user){
$user_id = !$user ? 0 : $user['id'];
return self::alias('c')
->join('user_coupon uc','uc.coupon_id = c.id and uc.user_id = '.$user_id,'left')
->where('c.expiretime','>',time())
->field("
c.id,
c.coupon_name,
c.coupon_price,
c.limit_price,
from_unixtime(c.expiretime,'%Y-%m-%d') expiretime,
if(uc.coupon_id > 0,'1','0') isreceive,
category_ids
")
->group('c.id')
->order('c.expiretime asc')
->select();
}
}