| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace App\Module\GameItems\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- /**
- * App\Module\GameItems\Models\ItemUserOutputCounter
- *
- * field start
- * @property int $id 记录ID,主键
- * @property int $user_id 用户ID
- * @property int $limit_id 产出限制ID,外键关联kku_item_output_limits表
- * @property int $current_count 当前产出计数
- * @property string $record_date 记录日期(用于日期统计和重置)
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class ItemUserOutputCounter extends Model
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'item_user_output_counters';
- // attrlist start
- protected $fillable = [
- 'id',
- 'user_id',
- 'limit_id',
- 'current_count',
- 'record_date',
- ];
- // attrlist end
-
- /**
- * 应该被转换为日期的属性
- *
- * @var array
- */
- protected $dates = [
- 'last_reset_time',
- 'created_at',
- 'updated_at',
- ];
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'current_count' => 'integer',
- ];
- /**
- * 获取关联的产出限制
- *
- * @return BelongsTo
- */
- public function outputLimit(): BelongsTo
- {
- return $this->belongsTo(ItemOutputLimit::class, 'limit_id');
- }
- /**
- * 增加计数
- *
- * @param int $count 增加的数量
- * @return bool
- */
- public function incrementCount(int $count = 1): bool
- {
- $this->current_count += $count;
- return $this->save();
- }
- /**
- * 重置计数
- *
- * @return bool
- */
- public function resetCount(): bool
- {
- $this->current_count = 0;
- $this->last_reset_time = now();
- return $this->save();
- }
- /**
- * 检查是否达到限制
- *
- * @return bool
- */
- public function isLimitReached(): bool
- {
- $limit = $this->outputLimit;
- if (!$limit) {
- return false;
- }
- return $this->current_count >= $limit->max_quantity;
- }
- }
|