| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace App\Module\Shop\Models;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use UCore\ModelCore;
- /**
- * 商店促销活动模型
- *
- * field start
- * @property int $id 促销ID,主键
- * @property string $name 促销名称
- * @property string $description 促销描述
- * @property string $banner 促销横幅图片
- * @property int $discount_type 折扣类型(1:固定折扣, 2:百分比折扣)
- * @property int $discount_value 折扣值(固定折扣为具体金额,百分比折扣为1-100的整数)
- * @property bool $is_active 是否激活(0:否, 1:是)
- * @property int $sort_order 排序权重
- * @property string $start_time 开始时间
- * @property string $end_time 结束时间
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class ShopPromotion extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'shop_promotions';
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- protected $fillable = [
- 'name',
- 'description',
- 'banner',
- 'discount_type',
- 'discount_value',
- 'is_active',
- 'sort_order',
- 'start_time',
- 'end_time',
- ];
- /**
- * 应该被转换为日期的属性
- *
- * @var array
- */
- protected $dates = [
- 'start_time',
- 'end_time',
- 'created_at',
- 'updated_at',
- ];
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'is_active' => 'boolean',
- ];
- /**
- * 折扣类型:固定折扣
- */
- const DISCOUNT_TYPE_FIXED = 1;
- /**
- * 折扣类型:百分比折扣
- */
- const DISCOUNT_TYPE_PERCENTAGE = 2;
- /**
- * 获取折扣类型文本
- *
- * @return string
- */
- public function getDiscountTypeTextAttribute(): string
- {
- return match ($this->discount_type) {
- self::DISCOUNT_TYPE_FIXED => '固定折扣',
- self::DISCOUNT_TYPE_PERCENTAGE => '百分比折扣',
- default => '未知',
- };
- }
- /**
- * 获取该促销活动关联的商品
- *
- * @return BelongsToMany
- */
- public function items(): BelongsToMany
- {
- return $this->belongsToMany(
- ShopItem::class,
- 'shop_promotion_items',
- 'promotion_id',
- 'shop_item_id'
- )->withPivot('custom_discount_value')
- ->withTimestamps();
- }
- /**
- * 获取促销活动的商品关联记录
- *
- * @return HasMany
- */
- public function promotionItems(): HasMany
- {
- return $this->hasMany(ShopPromotionItem::class, 'promotion_id');
- }
- /**
- * 检查促销活动是否有效
- *
- * @return bool
- */
- public function isValid(): bool
- {
- if (!$this->is_active) {
- return false;
- }
- $now = now();
- if ($this->start_time && $now < $this->start_time) {
- return false;
- }
- if ($this->end_time && $now > $this->end_time) {
- return false;
- }
- return true;
- }
- /**
- * 计算折扣后的价格
- *
- * @param int $originalPrice 原价
- * @param int|null $customDiscountValue 自定义折扣值
- * @return int 折扣后的价格
- */
- public function calculateDiscountedPrice(int $originalPrice, ?int $customDiscountValue = null): int
- {
- $discountValue = $customDiscountValue ?? $this->discount_value;
- if ($this->discount_type === self::DISCOUNT_TYPE_FIXED) {
- return max(0, $originalPrice - $discountValue);
- } elseif ($this->discount_type === self::DISCOUNT_TYPE_PERCENTAGE) {
- return (int)($originalPrice * (100 - $discountValue) / 100);
- }
- return $originalPrice;
- }
- }
|