ItemUserOutputCounter.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 $dates = [
  41. 'last_reset_time',
  42. 'created_at',
  43. 'updated_at',
  44. ];
  45. /**
  46. * 应该被转换为原生类型的属性
  47. *
  48. * @var array
  49. */
  50. protected $casts = [
  51. 'current_count' => 'integer',
  52. ];
  53. /**
  54. * 获取关联的产出限制
  55. *
  56. * @return BelongsTo
  57. */
  58. public function outputLimit(): BelongsTo
  59. {
  60. return $this->belongsTo(ItemOutputLimit::class, 'limit_id');
  61. }
  62. /**
  63. * 增加计数
  64. *
  65. * @param int $count 增加的数量
  66. * @return bool
  67. */
  68. public function incrementCount(int $count = 1): bool
  69. {
  70. $this->current_count += $count;
  71. return $this->save();
  72. }
  73. /**
  74. * 重置计数
  75. *
  76. * @return bool
  77. */
  78. public function resetCount(): bool
  79. {
  80. $this->current_count = 0;
  81. $this->last_reset_time = now();
  82. return $this->save();
  83. }
  84. /**
  85. * 检查是否达到限制
  86. *
  87. * @return bool
  88. */
  89. public function isLimitReached(): bool
  90. {
  91. $limit = $this->outputLimit;
  92. if (!$limit) {
  93. return false;
  94. }
  95. return $this->current_count >= $limit->max_quantity;
  96. }
  97. }