| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package log
- import (
- "go.uber.org/zap"
- "log-server/global"
- "log-server/model/log"
- "log-server/model/log/request"
- "time"
- )
- type ServiceIpLog struct {
- }
- func (s *ServiceIpLog) GetIpLogList(api log.IpLogRequest, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
- limit := info.PageSize
- offset := info.PageSize * (info.Page - 1)
- db := global.GVA_DB.Model(&log.IpLog{})
- var apiList []log.IpLogResponse
- startDate := time.Now().Format("2006-01-02")
- endDate := time.Now().Format("2006-01-02")
- if len(api.Date) == 2 {
- startDate = api.Date[0]
- endDate = api.Date[1]
- }
- db = db.Where("create_date >= ? and create_date <= ?", startDate, endDate)
- if api.GameId != 0 {
- db = db.Where("game_id = ?", api.GameId)
- }
- if api.PcCode != "" {
- db = db.Where("pc_code = ?", api.PcCode)
- }
- if api.Ip != "" {
- db = db.Where("ip = ?", api.Ip)
- }
- db = db.Select("create_date,ip,pc_code,game_id,count(*) as count_total,count(distinct(ip)) as count_distinct_ip")
- db = db.Group("pc_code, game_id, create_date")
- //db.Select("count(*)").Count(&countTotal)
- //db.Select("count(distinct(ip))").Count(&countDistinctIp)
- err = db.Count(&total).Error
- if err != nil {
- return apiList, total, err
- } else {
- db = db.Limit(limit).Offset(offset)
- if order != "" {
- var OrderStr string
- // 设置有效排序key 防止sql注入
- // 感谢 Tom4t0 提交漏洞信息
- orderMap := make(map[string]bool, 3)
- orderMap["game_id"] = true
- orderMap["count_distinct_ip"] = true
- orderMap["count_total"] = true
- if orderMap[order] {
- if desc {
- OrderStr = order + " desc"
- } else {
- OrderStr = order
- }
- } else { // didn't matched any order key in `orderMap`
- global.GVA_LOG.Error("获取失败!", zap.Error(err))
- return apiList, total, err
- }
- err = db.Order(OrderStr).Find(&apiList).Error
- } else {
- err = db.Order("id").Find(&apiList).Error
- }
- }
- //遍历更改日期格式
- for i, _ := range apiList {
- apiList[i].CreateDate = apiList[i].CreateDate[:10]
- }
- return apiList, total, err
- }
- func (s *ServiceIpLog) GetIp(iplog log.IpLogResponse) (list interface{}, total int64, err error) {
- var ipList []log.QueryIpList
- 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")
- //db.Group("ip").Count(&total)
- err = db.Find(&ipList).Count(&total).Error
- return ipList, total, err
- }
|