diff --git a/app/Console/Commands/AdminSendCoupon.php b/app/Console/Commands/AdminSendCoupon.php index e82e5907..4e48eedd 100644 --- a/app/Console/Commands/AdminSendCoupon.php +++ b/app/Console/Commands/AdminSendCoupon.php @@ -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, ]; diff --git a/app/Endpoint/Api/Filters/UserCouponFilter.php b/app/Endpoint/Api/Filters/UserCouponFilter.php index 6fc71d59..e3039165 100644 --- a/app/Endpoint/Api/Filters/UserCouponFilter.php +++ b/app/Endpoint/Api/Filters/UserCouponFilter.php @@ -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; } } diff --git a/app/Helpers/Numeric.php b/app/Helpers/Numeric.php index e006d74f..d7a69146 100644 --- a/app/Helpers/Numeric.php +++ b/app/Helpers/Numeric.php @@ -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'; diff --git a/app/Models/UserCoupon.php b/app/Models/UserCoupon.php index 5e61dea9..1e948a2a 100644 --- a/app/Models/UserCoupon.php +++ b/app/Models/UserCoupon.php @@ -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); } /** diff --git a/database/migrations/2021_12_07_115801_create_user_coupons_table.php b/database/migrations/2021_12_07_115801_create_user_coupons_table.php index ed97c8ae..2ae10107 100644 --- a/database/migrations/2021_12_07_115801_create_user_coupons_table.php +++ b/database/migrations/2021_12_07_115801_create_user_coupons_table.php @@ -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');