log_ip.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package log
  2. import (
  3. "go.uber.org/zap"
  4. "log-server/global"
  5. "log-server/model/log"
  6. "log-server/model/log/request"
  7. "time"
  8. )
  9. type ServiceIpLog struct {
  10. }
  11. func (s *ServiceIpLog) GetIpLogList(api log.IpLogRequest, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
  12. limit := info.PageSize
  13. offset := info.PageSize * (info.Page - 1)
  14. db := global.GVA_DB.Model(&log.IpLog{})
  15. var apiList []log.IpLogResponse
  16. startDate := time.Now().Format("2006-01-02")
  17. endDate := time.Now().Format("2006-01-02")
  18. if len(api.Date) == 2 {
  19. startDate = api.Date[0]
  20. endDate = api.Date[1]
  21. }
  22. db = db.Where("create_date >= ? and create_date <= ?", startDate, endDate)
  23. if api.GameId != 0 {
  24. db = db.Where("game_id = ?", api.GameId)
  25. }
  26. if api.PcCode != "" {
  27. db = db.Where("pc_code = ?", api.PcCode)
  28. }
  29. if api.Ip != "" {
  30. db = db.Where("ip = ?", api.Ip)
  31. }
  32. db = db.Select("create_date,ip,pc_code,game_id,count(*) as count_total,count(distinct(ip)) as count_distinct_ip")
  33. db = db.Group("pc_code, game_id, create_date")
  34. //db.Select("count(*)").Count(&countTotal)
  35. //db.Select("count(distinct(ip))").Count(&countDistinctIp)
  36. err = db.Count(&total).Error
  37. if err != nil {
  38. return apiList, total, err
  39. } else {
  40. db = db.Limit(limit).Offset(offset)
  41. if order != "" {
  42. var OrderStr string
  43. // 设置有效排序key 防止sql注入
  44. // 感谢 Tom4t0 提交漏洞信息
  45. orderMap := make(map[string]bool, 3)
  46. orderMap["game_id"] = true
  47. orderMap["count_distinct_ip"] = true
  48. orderMap["count_total"] = true
  49. if orderMap[order] {
  50. if desc {
  51. OrderStr = order + " desc"
  52. } else {
  53. OrderStr = order
  54. }
  55. } else { // didn't matched any order key in `orderMap`
  56. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  57. return apiList, total, err
  58. }
  59. err = db.Order(OrderStr).Find(&apiList).Error
  60. } else {
  61. err = db.Order("id").Find(&apiList).Error
  62. }
  63. }
  64. //遍历更改日期格式
  65. for i, _ := range apiList {
  66. apiList[i].CreateDate = apiList[i].CreateDate[:10]
  67. }
  68. return apiList, total, err
  69. }
  70. func (s *ServiceIpLog) GetIp(iplog log.IpLogResponse) (list interface{}, total int64, err error) {
  71. var ipList []log.QueryIpList
  72. db := global.GVA_DB.Model(&log.IpLog{}).Select("ip, COUNT(1) as count").Where("game_id = ? and pc_code = ? and create_date = ?", iplog.GameId, iplog.PcCode, iplog.CreateDate).Group("ip")
  73. //db.Group("ip").Count(&total)
  74. err = db.Find(&ipList).Count(&total).Error
  75. return ipList, total, err
  76. }