log_report_points.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package log
  2. import (
  3. "encoding/json"
  4. "go.uber.org/zap"
  5. "log-server/global"
  6. "log-server/model/log"
  7. "log-server/model/log/request"
  8. "log-server/model/log/response"
  9. "time"
  10. )
  11. type ServiceReportPointsLog struct {
  12. }
  13. func (s *ServiceReportPointsLog) CreateReportPointsLog(reportPointsLog *log.ReportPointsLog) (err error) {
  14. return global.GVA_DB.Create(&reportPointsLog).Error
  15. }
  16. func (s *ServiceReportPointsLog) GetReportPointsLogList(api log.ReportPointsLog, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
  17. limit := info.PageSize
  18. offset := info.PageSize * (info.Page - 1)
  19. db := global.GVA_DB.Model(&log.ReportPointsLog{})
  20. var apiList []log.ReportPointsLog
  21. var createDate string
  22. if api.CreateDate == "" {
  23. createDate = time.Now().Format("2006-01-02")
  24. } else {
  25. createDate = api.CreateDate
  26. }
  27. db = db.Where("create_date = ?", createDate)
  28. if api.GameId != 0 {
  29. db = db.Where("game_id = ?", api.GameId)
  30. }
  31. if api.Account != "" {
  32. db = db.Where("account = ?", api.Account)
  33. }
  34. if api.LogUuid != "" {
  35. db = db.Where("log_uuid = ?", api.LogUuid)
  36. }
  37. err = db.Count(&total).Error
  38. if err != nil {
  39. return apiList, total, err
  40. } else {
  41. db = db.Limit(limit).Offset(offset)
  42. if order != "" {
  43. var OrderStr string
  44. // 设置有效排序key 防止sql注入
  45. // 感谢 Tom4t0 提交漏洞信息
  46. orderMap := make(map[string]bool, 4)
  47. orderMap["id"] = true
  48. orderMap["log_uuid"] = true
  49. orderMap["account"] = true
  50. if orderMap[order] {
  51. if desc {
  52. OrderStr = order + " desc"
  53. } else {
  54. OrderStr = order
  55. }
  56. } else { // didn't matched any order key in `orderMap`
  57. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  58. return apiList, total, err
  59. }
  60. err = db.Order(OrderStr).Find(&apiList).Error
  61. } else {
  62. err = db.Order("id").Find(&apiList).Error
  63. }
  64. }
  65. var apisReply []*response.GetReportPointsReply
  66. var reportPointsData []request.ReportPointsData
  67. if total != 0 {
  68. for _, apiInfo := range apiList {
  69. apiReply := new(response.GetReportPointsReply)
  70. apiReply.Id = apiInfo.Id
  71. apiReply.GameId = apiInfo.GameId
  72. apiReply.Status = apiInfo.Status
  73. apiReply.Account = apiInfo.Account
  74. apiReply.ReportPointsNum = apiInfo.ReportPointsNum
  75. _ = json.Unmarshal([]byte(apiInfo.ReportPointsData), &reportPointsData)
  76. apiReply.ReportPointsData = reportPointsData
  77. apiReply.LogUuid = apiInfo.LogUuid
  78. apiReply.CreateDate = apiInfo.CreateDate
  79. apiReply.CreateTime = apiInfo.CreateTime
  80. apisReply = append(apisReply, apiReply)
  81. }
  82. }
  83. return apisReply, total, err
  84. }