FreeGiftRule.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Longyi\FreeGift\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use Illuminate\Support\Facades\Storage;
  6. use Longyi\FreeGift\Config\Gift;
  7. use Longyi\FreeGift\Contracts\FreeGiftRule as FreeGiftRuleContract;
  8. use Webkul\CartRule\Models\CartRule;
  9. /**
  10. * 赠品规则配置,1:1 挂在 `cart_rules` 上。
  11. */
  12. class FreeGiftRule extends Model implements FreeGiftRuleContract
  13. {
  14. /**
  15. * The table associated with the model.
  16. *
  17. * @var string
  18. */
  19. protected $table = 'free_gift_rules';
  20. /**
  21. * The attributes that are mass assignable.
  22. *
  23. * @var array
  24. */
  25. protected $fillable = [
  26. 'cart_rule_id',
  27. 'sku',
  28. 'type',
  29. 'items_discount',
  30. 'minimal_items_price',
  31. 'apply_tax',
  32. 'apply_shipping',
  33. 'auto_add',
  34. 'show_banner',
  35. 'banner_image',
  36. 'banner_alt',
  37. ];
  38. /**
  39. * The attributes that should be cast.
  40. *
  41. * @var array
  42. */
  43. protected $casts = [
  44. 'cart_rule_id' => 'integer',
  45. 'type' => 'integer',
  46. 'minimal_items_price' => 'float',
  47. 'apply_tax' => 'boolean',
  48. 'apply_shipping' => 'boolean',
  49. 'auto_add' => 'boolean',
  50. 'show_banner' => 'boolean',
  51. ];
  52. /**
  53. * 关联的营销规则。
  54. */
  55. public function cart_rule(): BelongsTo
  56. {
  57. return $this->belongsTo(CartRule::class, 'cart_rule_id');
  58. }
  59. /**
  60. * Banner 图片的公开访问地址。
  61. */
  62. public function bannerImageUrl(): ?string
  63. {
  64. if (empty($this->banner_image)) {
  65. return null;
  66. }
  67. return Storage::url($this->banner_image);
  68. }
  69. /**
  70. * 赠品 SKU 列表。
  71. *
  72. * @return string[]
  73. */
  74. public function skuList(): array
  75. {
  76. if (empty($this->sku)) {
  77. return [];
  78. }
  79. return array_values(array_filter(array_map('trim', explode(',', $this->sku))));
  80. }
  81. /**
  82. * 是否是「一组合计送 N 个」。
  83. */
  84. public function isOneOf(): bool
  85. {
  86. return (int) $this->type === Gift::TYPE_ONE;
  87. }
  88. /**
  89. * 是否是「每个 SKU 各送 N 个」。
  90. */
  91. public function isAllOf(): bool
  92. {
  93. return (int) $this->type === Gift::TYPE_ALL;
  94. }
  95. }