diff --git a/app/Models/CouponRange.php b/app/Models/CouponRange.php index 642ba26b..c70fa767 100644 --- a/app/Models/CouponRange.php +++ b/app/Models/CouponRange.php @@ -11,8 +11,55 @@ class CouponRange extends Model use HasFactory; use HasDateTimeFormatter; + public const TYPE_CATEGORY = 1; + public const TYPE_PRODUCT = 2; + + /** + * 仅查询可用的券规则 + */ + public function scopeIsEnable() + { + return $this->where('is_enable', true); + } + public function coupon() { return $this->belongsTo(Coupon::class, 'coupon_id'); } + + /** + * 确认券规则是否仅分类可用 + * + * @return bool + */ + public function isTypeCategory() + { + return $this->type === static::TYPE_CATEGORY; + } + + /** + * 确认券规则是否仅商品可用 + * + * @return bool + */ + public function isTypeProduct() + { + return $this->type === static::TYPE_PRODUCT; + } + + /** + * 获取此规则的范围IDs + * + * @return array + */ + public function getRangeIdsAttribute(): array + { + $value = $this->attributes['ranges']; + + if ((string) $value === '') { + return []; + } + + return explode(',', $value); + } } diff --git a/database/migrations/2021_12_07_111745_create_coupon_ranges_table.php b/database/migrations/2021_12_07_111745_create_coupon_ranges_table.php index ab0a3486..d2e56063 100644 --- a/database/migrations/2021_12_07_111745_create_coupon_ranges_table.php +++ b/database/migrations/2021_12_07_111745_create_coupon_ranges_table.php @@ -18,7 +18,7 @@ class CreateCouponRangesTable extends Migration $table->unsignedBigInteger('coupon_id')->comment('优惠券ID'); $table->unsignedTinyInteger('type')->default(0)->comment('类型:1商品分类,2sku商品'); $table->text('ranges')->comment('范围IDS'); - $table->unsignedTinyInteger('status')->default(0)->comment('0未启用,1启用'); + $table->boolean('is_enable')->default(false)->comment('是否启用'); $table->timestamps(); }); }