| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace App\Module\Shop\Models;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use UCore\ModelCore;
- /**
- * 商店促销商品关联模型
- *
- * field start
- * @property int $id 关联ID,主键
- * @property int $promotion_id 促销ID,外键关联kku_shop_promotions表
- * @property int $shop_item_id 商品ID,外键关联kku_shop_items表
- * @property int $custom_discount_value 自定义折扣值(可为空,优先于促销活动的折扣值)
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class ShopPromotionItem extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'shop_promotion_items';
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- protected $fillable = [
- 'promotion_id',
- 'shop_item_id',
- 'custom_discount_value',
- ];
- /**
- * 获取关联的促销活动
- *
- * @return BelongsTo
- */
- public function promotion(): BelongsTo
- {
- return $this->belongsTo(ShopPromotion::class, 'promotion_id');
- }
- /**
- * 获取关联的商品
- *
- * @return BelongsTo
- */
- public function shopItem(): BelongsTo
- {
- return $this->belongsTo(ShopItem::class, 'shop_item_id');
- }
- /**
- * 计算折扣后的价格
- *
- * @return int|null 折扣后的价格,如果商品或促销活动不存在则返回null
- */
- public function getDiscountedPrice(): ?int
- {
- $shopItem = $this->shopItem;
- $promotion = $this->promotion;
- if (!$shopItem || !$promotion || !$promotion->isValid()) {
- return null;
- }
- return $promotion->calculateDiscountedPrice(
- $shopItem->price,
- $this->custom_discount_value
- );
- }
- }
|