Coupons.php
8.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
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
<?php
namespace addons\shopro\model;
use think\Model;
use addons\shopro\exception\Exception;
use traits\model\SoftDelete;
/**
* 优惠券模型
*/
class Coupons extends Model
{
use SoftDelete;
// 表名,不含前缀
protected $name = 'shopro_coupons';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
protected $hidden = ['createtime', 'updatetime', 'deletetime'];
// 追加属性
protected $append = [
];
const COUPONS_CENTER = 0; // 领券中心
const COUPONS_CAN_USE = 1; // 可使用
const COUPONS_USED = 2; // 已使用
const COUPONS_EXPIRED = 3; // 已失效
public static function getCoupon($id)
{
$coupon = self::get($id);
$user = User::info();
if (!$coupon) {
throw new Exception('未找到优惠券');
}
if ($coupon['gettimestart'] > time() || $coupon['gettimeend'] < time()) {
throw new Exception('优惠券领取已结束');
}
$getList = UserCoupons::all([
'user_id' => $user->id,
'coupons_id' => $id
]);
if (count($getList) >= $coupon->limit) {
throw new Exception('您已经领取过');
}
if ($coupon->stock <= 0) {
throw new Exception('优惠券已经被领完了');
}
$coupon->stock -= 1;
$coupon->save();
$result = UserCoupons::create([
'user_id' => $user->id,
'coupons_id' => $id,
]);
return $result;
}
public static function getCouponsListByIds($ids)
{
$couponsIdsArray = explode(',', $ids);
$where = [
'id' => ['in', $couponsIdsArray]
];
$coupons = self::all($where);
return $coupons;
}
public static function getCouponsDetail($id, $user_coupons_id = 0)
{
$user = User::info();
$coupon = self::get($id);
// 查询并返回用户的优惠券状态
if ($coupon && $user_coupons_id && $user) {
$userCoupons = UserCoupons::where('id', $user_coupons_id)
->where('coupons_id', $coupon->id)
->where('user_id', $user->id)->find();
if ($userCoupons) {
$coupon->user_coupons_id = $userCoupons->id;
if ($userCoupons->usetime) {
$coupon->status_code = 'used';
$coupon->status_name = '已使用';
} else {
//开通会员后赠送的优惠券
if($userCoupons->package_coupons_time > 0){
if ($userCoupons->package_coupons_time >= time()) {
$coupon->status_code = 'no_use';
$coupon->status_name = '未使用';
} else {
$coupon->status_code = 'expired';
$coupon->status_name = '已过期';
}
}else{
//正常领取的优惠券
if ($coupon->usetimestart <= time() && $coupon->usetimeend >= time()) {
$coupon->status_code = 'no_use';
$coupon->status_name = '未使用';
} else if ($coupon->usetimeend <= time()) {
$coupon->status_code = 'expired';
$coupon->status_name = '已过期';
} else {
// 未到使用日期
$coupon->status_code = 'no_can_use';
$coupon->status_name = '不可使用';
}
}
}
}
}
return $coupon;
}
public static function getGoodsByCoupons($id)
{
$goodsIds = self::where('id', $id)->value('goods_ids');
return Goods::getGoodsListByIds($goodsIds);
}
public static function getCouponsList($type)
{
$user = User::info();
$couponsList = [];
switch ($type) {
case self::COUPONS_CENTER:
$couponsList = self::all([
'gettimestart' => ['elt', time()],
'gettimeend' => ['egt', time()]
]);
break;
case self::COUPONS_CAN_USE:
$userCoupons = UserCoupons::where(['user_id' => $user->id,'usetime' => null])->select();
foreach ($userCoupons as $u) {
$coupon = self::get($u->coupons_id);
if ($coupon) {
if($u->package_coupons_time > 0){
// 开通会员后赠送的优惠券
if($u->package_coupons_time >= time()){
$coupon->user_coupons_id = $u->id;
$coupon->status_code = 'no_use';
$coupon->status_name = '未使用';
$couponsList[] = $coupon;
}
}else if($coupon->usetimestart <= time() && $coupon->usetimeend >= time()){
// 正常领取的优惠券
$coupon->user_coupons_id = $u->id;
$coupon->status_code = 'no_use';
$coupon->status_name = '未使用';
$couponsList[] = $coupon;
}
}
}
break;
case self::COUPONS_USED:
$userCoupons = UserCoupons::where('user_id', $user->id)->where('usetime', 'not null')->select();
foreach ($userCoupons as $u) {
$coupon = self::get($u->coupons_id);
// if ($coupon && $coupon->usetimestart <= time() && $coupon->usetimeend >= time()) {
$coupon->user_coupons_id = $u->id;
$coupon->status_code = 'used';
$coupon->status_name = '已使用';
$couponsList[] = $coupon;
// }
}
break;
case self::COUPONS_EXPIRED:
$userCoupons = UserCoupons::where(['user_id' => $user->id,'usetime' => null])->select();
foreach ($userCoupons as $u) {
$coupon = self::get($u->coupons_id);
if($coupon){
if($u->package_coupons_time > 0){
// 开通会员后赠送的优惠券
if($u->package_coupons_time <= time()){
$coupon->user_coupons_id = $u->id;
$coupon->status_code = 'expired';
$coupon->status_name = '已过期';
$couponsList[] = $coupon;
}
}else if($coupon->usetimeend <= time()){
// 正常领取的优惠券
$coupon->user_coupons_id = $u->id;
$coupon->status_code = 'expired';
$coupon->status_name = '已过期';
$couponsList[] = $coupon;
}
}
}
break;
}
foreach ($couponsList as $v){
if(!empty($v->user_coupons_id)){
$user_coupons = UserCoupons::get($v->user_coupons_id);
if($user_coupons && $user_coupons['package_coupons_time'] > 0){
$v->usetime = [
'start' => date('Y.m.d',$user_coupons['createtime']),
'end' => date('Y.m.d',$user_coupons['package_coupons_time']),
];
}
}
}
return $couponsList;
}
public function getUsetimeAttr($value, $data)
{
if(is_array($value)){
return $value;
}
$usetimeArray = explode(' - ', $value);
$usetime['start'] = date('Y.m.d',strtotime($usetimeArray[0]));
$usetime['end'] = date('Y.m.d',strtotime($usetimeArray[1]));
return $usetime;
}
public function getGettimeAttr($value, $data)
{
$gettimeArray = explode(' - ', $value);
$gettime['start'] = strtotime($gettimeArray[0]);
$gettime['end'] = strtotime($gettimeArray[1]);
return $gettime;
}
//定义关联方法
public function userCoupons(){
//hasMany('租客表名','租客表外键','宿舍主键',['模型别名定义']);
return $this->hasMany('userCoupons','coupons_id','id');
}
}