UserOutputCounter.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Module\GameItems\Logics;
  3. use App\Module\GameItems\Models\ItemUserOutputCounter;
  4. /**
  5. * 用户产出计数逻辑类
  6. */
  7. class UserOutputCounter
  8. {
  9. /**
  10. * 增加计数
  11. *
  12. * @param ItemUserOutputCounter $counter 用户产出计数模型
  13. * @param int $count 增加的数量
  14. * @return bool
  15. */
  16. public function incrementCount(ItemUserOutputCounter $counter, int $count = 1): bool
  17. {
  18. $counter->current_count += $count;
  19. return $counter->save();
  20. }
  21. /**
  22. * 重置计数
  23. *
  24. * @param ItemUserOutputCounter $counter 用户产出计数模型
  25. * @return bool
  26. */
  27. public function resetCount(ItemUserOutputCounter $counter): bool
  28. {
  29. $counter->current_count = 0;
  30. $counter->last_reset_time = now();
  31. return $counter->save();
  32. }
  33. /**
  34. * 检查是否达到限制
  35. *
  36. * @param ItemUserOutputCounter $counter 用户产出计数模型
  37. * @return bool
  38. */
  39. public function isLimitReached(ItemUserOutputCounter $counter): bool
  40. {
  41. $limit = $counter->outputLimit;
  42. if (!$limit) {
  43. return false;
  44. }
  45. return $counter->current_count >= $limit->max_quantity;
  46. }
  47. }