ItemUserOutputCounter.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Module\GameItems\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. /**
  6. * App\Module\GameItems\Models\ItemUserOutputCounter
  7. *
  8. * field start
  9. * @property int $id 记录ID,主键
  10. * @property int $user_id 用户ID
  11. * @property int $limit_id 产出限制ID,外键关联kku_item_output_limits表
  12. * @property int $current_count 当前产出计数
  13. * @property string $record_date 记录日期(用于日期统计和重置)
  14. * @property \Carbon\Carbon $created_at 创建时间
  15. * @property \Carbon\Carbon $updated_at 更新时间
  16. * field end
  17. */
  18. class ItemUserOutputCounter extends Model
  19. {
  20. /**
  21. * 与模型关联的表名
  22. *
  23. * @var string
  24. */
  25. protected $table = 'item_user_output_counters';
  26. // attrlist start
  27. protected $fillable = [
  28. 'id',
  29. 'user_id',
  30. 'limit_id',
  31. 'current_count',
  32. 'record_date',
  33. ];
  34. // attrlist end
  35. /**
  36. * 可批量赋值的属性
  37. *
  38. * @var array
  39. */
  40. protected $fillable = [
  41. 'user_id',
  42. 'limit_id',
  43. 'current_count',
  44. 'last_reset_time',
  45. ];
  46. /**
  47. * 应该被转换为日期的属性
  48. *
  49. * @var array
  50. */
  51. protected $dates = [
  52. 'last_reset_time',
  53. 'created_at',
  54. 'updated_at',
  55. ];
  56. /**
  57. * 应该被转换为原生类型的属性
  58. *
  59. * @var array
  60. */
  61. protected $casts = [
  62. 'current_count' => 'integer',
  63. ];
  64. /**
  65. * 获取关联的产出限制
  66. *
  67. * @return BelongsTo
  68. */
  69. public function outputLimit(): BelongsTo
  70. {
  71. return $this->belongsTo(ItemOutputLimit::class, 'limit_id');
  72. }
  73. /**
  74. * 增加计数
  75. *
  76. * @param int $count 增加的数量
  77. * @return bool
  78. */
  79. public function incrementCount(int $count = 1): bool
  80. {
  81. $this->current_count += $count;
  82. return $this->save();
  83. }
  84. /**
  85. * 重置计数
  86. *
  87. * @return bool
  88. */
  89. public function resetCount(): bool
  90. {
  91. $this->current_count = 0;
  92. $this->last_reset_time = now();
  93. return $this->save();
  94. }
  95. /**
  96. * 检查是否达到限制
  97. *
  98. * @return bool
  99. */
  100. public function isLimitReached(): bool
  101. {
  102. $limit = $this->outputLimit;
  103. if (!$limit) {
  104. return false;
  105. }
  106. return $this->current_count >= $limit->max_quantity;
  107. }
  108. }