FarmGodBuffRepository.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Module\Farm\Repositories;
  3. use App\Module\Farm\Models\FarmGodBuff;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. use Illuminate\Database\Eloquent\Collection;
  6. /**
  7. * 神灵加持仓库
  8. *
  9. * 提供神灵加持数据的访问和操作功能。
  10. * 该类是神灵加持模块与后台管理系统的桥梁,用于处理神灵加持数据的CRUD操作。
  11. */
  12. class FarmGodBuffRepository extends EloquentRepository
  13. {
  14. /**
  15. * 模型类名
  16. *
  17. * @var string
  18. */
  19. protected $eloquentClass = FarmGodBuff::class;
  20. /**
  21. * 获取用户的所有神灵加持
  22. *
  23. * @param int $userId
  24. * @return Collection
  25. */
  26. public function findByUserId(int $userId): Collection
  27. {
  28. return FarmGodBuff::where('user_id', $userId)->get();
  29. }
  30. /**
  31. * 获取用户的有效神灵加持
  32. *
  33. * @param int $userId
  34. * @return Collection
  35. */
  36. public function findActiveByUserId(int $userId): Collection
  37. {
  38. return FarmGodBuff::where('user_id', $userId)
  39. ->where('expire_time', '>', now())
  40. ->get();
  41. }
  42. /**
  43. * 获取用户指定类型的神灵加持
  44. *
  45. * @param int $userId
  46. * @param int $buffType
  47. * @return FarmGodBuff|null
  48. */
  49. public function findByUserIdAndType(int $userId, int $buffType): ?FarmGodBuff
  50. {
  51. return FarmGodBuff::where('user_id', $userId)
  52. ->where('buff_type', $buffType)
  53. ->first();
  54. }
  55. /**
  56. * 获取用户指定类型的有效神灵加持
  57. *
  58. * @param int $userId
  59. * @param int $buffType
  60. * @return FarmGodBuff|null
  61. */
  62. public function findActiveByUserIdAndType(int $userId, int $buffType): ?FarmGodBuff
  63. {
  64. return FarmGodBuff::where('user_id', $userId)
  65. ->where('buff_type', $buffType)
  66. ->where('expire_time', '>', now())
  67. ->first();
  68. }
  69. /**
  70. * 清理过期的神灵加持
  71. *
  72. * @return int
  73. */
  74. public function deleteExpired(): int
  75. {
  76. return FarmGodBuff::where('expire_time', '<', now())->delete();
  77. }
  78. }