UserLogService.php 2.3 KB

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