6
0
Fork 0

优惠券

release
李静 2021-12-14 10:42:49 +08:00
parent 1f66b5064f
commit 9f7867c6b8
5 changed files with 29 additions and 13 deletions

View File

@ -83,7 +83,6 @@ class AdminSendCoupon extends Command
'coupon_threshold'=> (int) bcmul($coupon->threshold, 100),
'use_start_at' => $useStartAt,
'use_end_at' => $useEndAt,
'status' => 0,
'created_at' => $nowTime,
'updated_at' => $nowTime,
];

View File

@ -10,15 +10,15 @@ class UserCouponFilter extends ModelFilter
{
switch ($status) {
case 'unuse':
$this->isUnuse();
$this->onlyUnuse();
break;
case 'used':
$this->isUsed();
$this->onlyUsed();
break;
case 'expired':
$this->isExpired();
$this->onlyExpired();
break;
}
}

View File

@ -13,7 +13,7 @@ class Numeric
public static function trimZero($value)
{
if (is_numeric($value) && strpos($value, '.') !== false) {
$value = rtrim(trim($value, '0'), '.');
$value = rtrim(rtrim($value, '0'), '.');
}
return $value ?: '0';

View File

@ -12,6 +12,23 @@ class UserCoupon extends Model
use HasFactory;
use Filterable;
/**
* @var array
*/
protected $attributes = [
'is_use' => false,
];
/**
* @var array
*/
protected $casts = [
'is_use' => 'bool',
];
/**
* @var array
*/
protected $fillable = [
'user_id',
'coupon_id',
@ -21,7 +38,7 @@ class UserCoupon extends Model
'coupon_threshold',
'use_start_at',
'use_end_at',
'status',
'is_use',
'created_at',
'updated_at',
];
@ -29,27 +46,27 @@ class UserCoupon extends Model
/**
* 仅查询已过期的优惠券
*/
public function scopeIsExpired($query)
public function scopeOnlyExpired($query)
{
return $query->where('status', 0)->where('use_end_at', '<=', now());
return $query->where('is_use', false)->where('use_end_at', '<=', now());
}
/**
* 仅查询已使用的优惠券
*/
public function scopeIsUsed($query)
public function scopeOnlyUsed($query)
{
return $query->where('status', 1);
return $query->where('is_use', true);
}
/**
* 仅查询未使用的优惠券
*/
public function scopeIsUnuse($query)
public function scopeOnlyUnuse($query)
{
$time = now();
return $query->where('status', 0)->where('use_start_at', '<', $time)->where('use_end_at', '>', $time);
return $query->where('is_use', false)->where('use_start_at', '<', $time)->where('use_end_at', '>', $time);
}
/**

View File

@ -23,7 +23,7 @@ class CreateUserCouponsTable extends Migration
$table->unsignedBigInteger('coupon_threshold')->default(0)->comment('使用门槛金额:分');
$table->timestamp('use_start_at')->comment('使用开始时间');
$table->timestamp('use_end_at')->comment('使用结束时间');
$table->unsignedTinyInteger('status')->default(0)->comment('状态0未使用1已使用');
$table->boolean('is_use')->default(false)->comment('是否使用');
$table->timestamps();
$table->index('user_id');