log.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package admin
  2. import (
  3. "context"
  4. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  5. admin "github.com/go-nunu/nunu-layout-advanced/api/v1/admin"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  8. "math"
  9. "strings"
  10. )
  11. type LogRepository interface {
  12. GetLog(ctx context.Context, id int64) (*model.Log, error)
  13. GetLogList(ctx context.Context, req admin.SearchLogParams) (*v1.PaginatedResponse[model.Log], error)
  14. }
  15. func NewLogRepository(
  16. repository *repository.Repository,
  17. ) LogRepository {
  18. return &logRepository{
  19. Repository: repository,
  20. }
  21. }
  22. type logRepository struct {
  23. *repository.Repository
  24. }
  25. func (r *logRepository) GetLog(ctx context.Context, id int64) (*model.Log, error) {
  26. var res model.Log
  27. return &res, r.DBWithName(ctx,"admin").Where("id = ?", id).First(&res).Error
  28. }
  29. func (r *logRepository) GetLogList(ctx context.Context, req admin.SearchLogParams) (*v1.PaginatedResponse[model.Log], error) {
  30. var res []model.Log
  31. var total int64
  32. query := r.DBWithName(ctx,"admin").Model(&model.Log{})
  33. if req.RequestIp != "" {
  34. trimmedName := strings.TrimSpace(req.RequestIp)
  35. // 使用 LIKE 进行模糊匹配
  36. query = query.Where("request_ip LIKE CONCAT('%', ?, '%')", trimmedName)
  37. }
  38. if req.Uid != 0 {
  39. query = query.Where("uid = ?", req.Uid)
  40. }
  41. if req.Api != "" {
  42. trimmedName := strings.TrimSpace(req.Api)
  43. // 使用 LIKE 进行模糊匹配
  44. query = query.Where("api LIKE CONCAT('%', ?, '%')", trimmedName)
  45. }
  46. if req.Message != "" {
  47. trimmedName := strings.TrimSpace(req.Message)
  48. // 使用 LIKE 进行模糊匹配
  49. query = query.Where("message LIKE CONCAT('%', ?, '%')", trimmedName)
  50. }
  51. if req.ExtraData != "" {
  52. trimmedName := strings.TrimSpace(req.ExtraData)
  53. // 使用 LIKE 进行模糊匹配
  54. query = query.Where("extra_data LIKE CONCAT('%', ?, '%')", trimmedName)
  55. }
  56. if req.Column != "" {
  57. query = query.Order(req.Column + " " + req.Order)
  58. }
  59. if err := query.Count(&total).Error; err != nil {
  60. // 如果连计数都失败了,直接返回错误
  61. return nil, err
  62. }
  63. page := req.Current
  64. pageSize := req.PageSize
  65. if page <= 0 {
  66. page = 1
  67. }
  68. if pageSize <= 0 {
  69. pageSize = 10 // 默认每页 10 条
  70. } else if pageSize > 100 {
  71. pageSize = 100 // 每页最多 100 条
  72. }
  73. // 计算 offset (偏移量)
  74. // 例如,第 1 页,offset = (1-1)*10 = 0 (从第0条开始)
  75. // 第 2 页,offset = (2-1)*10 = 10 (从第10条开始)
  76. offset := (page - 1) * pageSize
  77. // 3. 执行最终的查询
  78. // 在所有条件都添加完毕后,再执行 .Find()
  79. result := query.Offset(offset).Limit(pageSize).Find(&res)
  80. if result.Error != nil {
  81. // 这里的错误可能是数据库连接问题等,而不是“未找到记录”
  82. return nil, result.Error
  83. }
  84. return &v1.PaginatedResponse[model.Log]{
  85. Records: res,
  86. Page: page,
  87. PageSize: pageSize,
  88. Total: total,
  89. TotalPages: int(math.Ceil(float64(total) / float64(pageSize))),
  90. }, nil
  91. }