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 ); } }