| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace App\Module\Shop\Models;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Support\Facades\DB;
- use UCore\ModelCore;
- use App\Module\GameItems\Models\Item;
- use App\Module\Shop\Models\ShopCategory;
- use App\Module\Shop\Models\ShopPromotion;
- use App\Module\Shop\Models\ShopPromotionItem;
- /**
- * 商店物品模型
- *
- * field start
- * field end
- */
- class ShopItem extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'shop_items';
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- protected $fillable = [
- 'name',
- 'description',
- 'category_id',
- 'item_id',
- 'item_quantity',
- 'price',
- 'currency_id',
- 'max_buy',
- 'is_active',
- 'sort_order',
- 'image',
- 'start_time',
- 'end_time',
- ];
- /**
- * 应该被转换为日期的属性
- *
- * @var array
- */
- protected $dates = [
- 'start_time',
- 'end_time',
- 'created_at',
- 'updated_at',
- ];
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'is_active' => 'boolean',
- ];
- /**
- * 获取关联的物品
- *
- * @return BelongsTo
- */
- public function item(): BelongsTo
- {
- return $this->belongsTo(Item::class, 'item_id');
- }
- /**
- * 获取关联的分类
- *
- * @return BelongsTo
- */
- public function category(): BelongsTo
- {
- return $this->belongsTo(ShopCategory::class, 'category_id');
- }
- /**
- * 获取用户已购买数量
- *
- * @param int $userId 用户ID
- * @return int 已购买数量
- */
- public function getUserBoughtCount(int $userId): int
- {
- return ShopPurchaseLog::where('user_id', $userId)
- ->where('shop_item_id', $this->id)
- ->sum('quantity');
- }
- /**
- * 获取该商品关联的促销活动
- *
- * @return BelongsToMany
- */
- public function promotions(): BelongsToMany
- {
- return $this->belongsToMany(
- ShopPromotion::class,
- 'shop_promotion_items',
- 'shop_item_id',
- 'promotion_id'
- )->withPivot('custom_discount_value')
- ->withTimestamps();
- }
- /**
- * 获取商品的促销关联记录
- *
- * @return HasMany
- */
- public function promotionItems(): HasMany
- {
- return $this->hasMany(ShopPromotionItem::class, 'shop_item_id');
- }
- /**
- * 获取当前有效的促销活动
- *
- * @return ShopPromotion|null
- */
- public function getActivePromotion(): ?ShopPromotion
- {
- $now = now();
- return $this->promotions()
- ->where('is_active', true)
- ->where(function ($query) use ($now) {
- $query->whereNull('start_time')
- ->orWhere('start_time', '<=', $now);
- })
- ->where(function ($query) use ($now) {
- $query->whereNull('end_time')
- ->orWhere('end_time', '>=', $now);
- })
- ->orderBy('sort_order')
- ->first();
- }
- /**
- * 获取折扣后的价格
- *
- * @return int
- */
- public function getDiscountedPrice(): int
- {
- $promotion = $this->getActivePromotion();
- if (!$promotion) {
- return $this->price;
- }
- $promotionItem = $this->promotionItems()
- ->where('promotion_id', $promotion->id)
- ->first();
- $customDiscountValue = $promotionItem ? $promotionItem->custom_discount_value : null;
- return $promotion->calculateDiscountedPrice($this->price, $customDiscountValue);
- }
- /**
- * 记录购买记录
- *
- * @param int $userId 用户ID
- * @param int $quantity 购买数量
- * @param int $totalPrice 总价
- * @return ShopPurchaseLog 购买记录
- */
- public function recordPurchase(int $userId, int $quantity, int $totalPrice): ShopPurchaseLog
- {
- $log = new ShopPurchaseLog([
- 'user_id' => $userId,
- 'shop_item_id' => $this->id,
- 'item_id' => $this->item_id,
- 'quantity' => $quantity,
- 'price' => $this->price,
- 'total_price' => $totalPrice,
- 'currency_id' => $this->currency_id,
- 'purchase_time' => now(),
- 'ip_address' => request()->ip(),
- 'device_info' => request()->userAgent(),
- ]);
- $log->save();
- return $log;
- }
- }
|