UserLogService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Module\Game\Services;
  3. use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
  4. use App\Module\Game\Logics\UserLogLogic;
  5. use App\Module\Game\Models\UserLog;
  6. use Illuminate\Pagination\LengthAwarePaginator;
  7. /**
  8. * 用户日志服务类
  9. *
  10. * 提供用户日志相关的服务方法,用于外部调用
  11. */
  12. class UserLogService
  13. {
  14. /**
  15. * 记录用户日志
  16. *
  17. * @param int $userId 用户ID
  18. * @param string $message 日志消息
  19. * @param string|null $sourceType 来源类型
  20. * @param int|null $sourceId 来源记录ID
  21. * @param string|null $sourceTable 来源表名
  22. * @return UserLog|null
  23. */
  24. public static function log(
  25. int $userId,
  26. string $message,
  27. ?string $sourceType = null,
  28. ?int $sourceId = null,
  29. ?string $sourceTable = null
  30. ): ?UserLog {
  31. return UserLogLogic::log($userId, $message, $sourceType, $sourceId, $sourceTable);
  32. }
  33. /**
  34. * 使用枚举记录用户日志
  35. *
  36. * @param int $userId 用户ID
  37. * @param string $message 日志消息
  38. * @param REWARD_SOURCE_TYPE|null $sourceType 来源类型枚举
  39. * @param int|null $sourceId 来源记录ID
  40. * @param string|null $sourceTable 来源表名
  41. * @return UserLog|null
  42. */
  43. public static function logWithEnum(
  44. int $userId,
  45. string $message,
  46. ?REWARD_SOURCE_TYPE $sourceType = null,
  47. ?int $sourceId = null,
  48. ?string $sourceTable = null
  49. ): ?UserLog {
  50. $sourceTypeValue = $sourceType ? $sourceType->value : null;
  51. return UserLogLogic::log($userId, $message, $sourceTypeValue, $sourceId, $sourceTable);
  52. }
  53. /**
  54. * 批量记录用户日志
  55. *
  56. * @param array $logs 日志数据数组
  57. * @return bool
  58. */
  59. public static function batchLog(array $logs): bool
  60. {
  61. return UserLogLogic::batchLog($logs);
  62. }
  63. /**
  64. * 获取用户日志列表
  65. *
  66. * @param int $userId 用户ID
  67. * @param int $page 页码
  68. * @param int $pageSize 每页数量
  69. * @param array $filters 过滤条件
  70. * @return LengthAwarePaginator
  71. */
  72. public static function getUserLogs(
  73. int $userId,
  74. int $page = 1,
  75. int $pageSize = 20,
  76. array $filters = []
  77. ): LengthAwarePaginator {
  78. return UserLogLogic::getUserLogs($userId, $page, $pageSize, $filters);
  79. }
  80. /**
  81. * 清空用户日志
  82. *
  83. * @param int $userId 用户ID
  84. * @return bool
  85. */
  86. public static function clearUserLogs(int $userId): bool
  87. {
  88. return UserLogLogic::clearUserLogs($userId);
  89. }
  90. /**
  91. * 清理过期日志
  92. *
  93. * @param int $days 保留天数
  94. * @return int 删除的记录数
  95. */
  96. public static function cleanExpiredLogs(int $days = 30): int
  97. {
  98. return UserLogLogic::cleanExpiredLogs($days);
  99. }
  100. /**
  101. * 获取用户日志统计信息
  102. *
  103. * @param int $userId 用户ID
  104. * @return array
  105. */
  106. public static function getUserLogStats(int $userId): array
  107. {
  108. return UserLogLogic::getUserLogStats($userId);
  109. }
  110. }