| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace App\Module\Shop\Models;
- use App\Module\Shop\Enums\PURCHASE_LIMIT_PERIOD;
- use App\Module\Shop\Enums\PURCHASE_LIMIT_TYPE;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use UCore\ModelCore;
- /**
- * 商店限购配置模型
- *
- * field start
- * @property int $id 限购配置ID,主键
- * @property int $shop_item_id 商品ID,外键关联kku_shop_items表
- * @property \App\Module\Shop\Enums\PURCHASE_LIMIT_TYPE $limit_type 限购类型(1:单次购买限制, 2:周期性购买限制)
- * @property \App\Module\Shop\Enums\PURCHASE_LIMIT_PERIOD $limit_period 限购周期(0:永久, 1:每日, 2:每周, 3:每月, 4:每年)
- * @property int $max_quantity 最大购买数量
- * @property string $name 限购规则名称
- * @property string $description 限购规则描述
- * @property bool $is_active 是否激活(0:否, 1:是)
- * @property int $sort_order 排序权重
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class ShopPurchaseLimit extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'shop_purchase_limits';
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- protected $fillable = [
- 'shop_item_id',
- 'limit_type',
- 'limit_period',
- 'max_quantity',
- 'name',
- 'description',
- 'is_active',
- 'sort_order',
- ];
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'is_active' => 'boolean',
- 'limit_type' => PURCHASE_LIMIT_TYPE::class,
- 'limit_period' => PURCHASE_LIMIT_PERIOD::class,
- ];
- /**
- * 获取关联的商品
- *
- * @return BelongsTo
- */
- public function shopItem(): BelongsTo
- {
- return $this->belongsTo(ShopItem::class, 'shop_item_id');
- }
- /**
- * 获取用户购买计数记录
- *
- * @return HasMany
- */
- public function userCounters(): HasMany
- {
- return $this->hasMany(ShopUserPurchaseCounter::class, 'limit_id');
- }
- /**
- * 获取用户在此限购规则下的购买计数
- *
- * @param int $userId 用户ID
- * @return int 购买计数
- */
- public function getUserPurchaseCount(int $userId): int
- {
- $counter = $this->userCounters()
- ->where('user_id', $userId)
- ->first();
- if (!$counter) {
- return 0;
- }
- // 检查是否需要重置计数
- if ($this->limit_period->shouldReset($counter->last_reset_time)) {
- $counter->resetCount();
- return 0;
- }
- return $counter->current_count;
- }
- /**
- * 增加用户购买计数
- *
- * @param int $userId 用户ID
- * @param int $quantity 购买数量
- * @return bool
- */
- public function incrementUserPurchaseCount(int $userId, int $quantity = 1): bool
- {
- $counter = ShopUserPurchaseCounter::firstOrCreate(
- [
- 'limit_id' => $this->id,
- 'user_id' => $userId,
- ],
- [
- 'current_count' => 0,
- 'last_reset_time' => now(),
- ]
- );
- // 检查是否需要重置计数
- if ($this->limit_period->shouldReset($counter->last_reset_time)) {
- $counter->resetCount();
- }
- $counter->current_count += $quantity;
- return $counter->save();
- }
- /**
- * 检查用户是否可以购买指定数量
- *
- * @param int $userId 用户ID
- * @param int $quantity 购买数量
- * @return array [是否可购买, 错误消息, 剩余可购买数量]
- */
- public function canUserPurchase(int $userId, int $quantity): array
- {
- if (!$this->is_active) {
- return [true, '', $this->max_quantity]; // 限购规则未激活,不限制
- }
- $currentCount = $this->getUserPurchaseCount($userId);
- $remainingQuantity = $this->max_quantity - $currentCount;
- if ($quantity > $remainingQuantity) {
- $periodName = $this->limit_period->getName($this->limit_period->value);
- $typeName = $this->limit_type->getName($this->limit_type->value);
-
- $message = match ($this->limit_type) {
- PURCHASE_LIMIT_TYPE::SINGLE_PURCHASE => "单次购买数量不能超过{$this->max_quantity}个",
- PURCHASE_LIMIT_TYPE::PERIODIC_PURCHASE => "{$periodName}内最多购买{$this->max_quantity}个,剩余可购买{$remainingQuantity}个",
- };
- return [false, $message, $remainingQuantity];
- }
- return [true, '', $remainingQuantity];
- }
- /**
- * 获取限购类型文本
- *
- * @return string
- */
- public function getLimitTypeTextAttribute(): string
- {
- return $this->limit_type->getName($this->limit_type->value);
- }
- /**
- * 获取限购周期文本
- *
- * @return string
- */
- public function getLimitPeriodTextAttribute(): string
- {
- return $this->limit_period->getName($this->limit_period->value);
- }
- }
|