| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- namespace Longyi\FreeGift\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Support\Facades\Storage;
- use Longyi\FreeGift\Config\Gift;
- use Longyi\FreeGift\Contracts\FreeGiftRule as FreeGiftRuleContract;
- use Webkul\CartRule\Models\CartRule;
- /**
- * 赠品规则配置,1:1 挂在 `cart_rules` 上。
- */
- class FreeGiftRule extends Model implements FreeGiftRuleContract
- {
- /**
- * The table associated with the model.
- *
- * @var string
- */
- protected $table = 'free_gift_rules';
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $fillable = [
- 'cart_rule_id',
- 'sku',
- 'type',
- 'items_discount',
- 'minimal_items_price',
- 'apply_tax',
- 'apply_shipping',
- 'auto_add',
- 'show_banner',
- 'banner_image',
- 'banner_alt',
- ];
- /**
- * The attributes that should be cast.
- *
- * @var array
- */
- protected $casts = [
- 'cart_rule_id' => 'integer',
- 'type' => 'integer',
- 'minimal_items_price' => 'float',
- 'apply_tax' => 'boolean',
- 'apply_shipping' => 'boolean',
- 'auto_add' => 'boolean',
- 'show_banner' => 'boolean',
- ];
- /**
- * 关联的营销规则。
- */
- public function cart_rule(): BelongsTo
- {
- return $this->belongsTo(CartRule::class, 'cart_rule_id');
- }
- /**
- * Banner 图片的公开访问地址。
- */
- public function bannerImageUrl(): ?string
- {
- if (empty($this->banner_image)) {
- return null;
- }
- return Storage::url($this->banner_image);
- }
- /**
- * 赠品 SKU 列表。
- *
- * @return string[]
- */
- public function skuList(): array
- {
- if (empty($this->sku)) {
- return [];
- }
- return array_values(array_filter(array_map('trim', explode(',', $this->sku))));
- }
- /**
- * 是否是「一组合计送 N 个」。
- */
- public function isOneOf(): bool
- {
- return (int) $this->type === Gift::TYPE_ONE;
- }
- /**
- * 是否是「每个 SKU 各送 N 个」。
- */
- public function isAllOf(): bool
- {
- return (int) $this->type === Gift::TYPE_ALL;
- }
- }
|