package loging import ( "context" "encoding/json" "errors" "fmt" "github.com/go-redis/redis/v8" "go.uber.org/zap" "gorm.io/gorm" "log-server/global" "log-server/model/log" "log-server/model/log/request" "log-server/service/cache" "log-server/utils" "strconv" "strings" "time" ) var ( FailStatus = "fail" OkStatus = "ok" NoLogStatus = "notStatus" GameCacheKey = "%s:game" NodeGameCacheKey = "%s:log" reportPointsKey = "%s:logUuid:" logUuidAccountGameId = "%s:logUuid:account:gameId" ComputerCacheKey = "%s:computer:list" ComputerPullAccountCacheKey = "%s:computer:%s:pullAccount:" ComputerEnterMainCacheKey = "%s:computer:%s:enterMain:" ComputerTaskSuccessCacheKey = "%s:computer:%s:taskSuccess:" GameFeeRateCacheKey = "%s:game:gameFeeRate:%d" GameComputerRateCacheKey = "%s:Computer:Rate:%s:" GamePcFeeRateCacheKey = "%s:gamePc:gameFeeRate:%d:%s" OnLineComputerNum = "%s:OnLineComputerNum:" PcReportingLog = "%s:pc:Reporting:%s" GameDeviceErrKey = "%s:device:%d" GameDeviceAccountErrKey = "%s:deviceAccount:%d" ) type LogicalLog struct { Status int // 状态 Request request.AddLogRequest cache cache.Cache ScriptType int } func (s *LogicalLog) CurrentDate() (current string) { current = time.Now().Format("2006-01-02") return } func (s *LogicalLog) YesterdayDate() (yesterday string) { yesterday = time.Now().Add(-time.Hour * 24).Format("2006-01-02") return } func (s *LogicalLog) DataAdd() (err error) { logInfo, err := s.RepositoryData() if err != nil { return err } err = s.LogAdd(logInfo) if err != nil { return } /*if s.Status == 0 { _ = s.errLogAdd(logInfo) }*/ ctx := context.Background() err = s.SetGameCache(ctx, s.CurrentDate(), logInfo.GameId) if err != nil { return } err = s.SetAccountGameIdCache(ctx, s.CurrentDate(), logInfo) if err != nil { return } if logInfo.PcCode != "" { err = s.SetComputerCache(ctx, s.CurrentDate(), logInfo.PcCode, logInfo.Operator) _ = s.StatisticsComputerRateData(ctx, logInfo.PcCode, logInfo.GameId) _ = s.SetOnlineComputerNumCache(ctx, s.CurrentDate(), logInfo.PcCode, logInfo.Operator) s.SetPcReportingLog(ctx, logInfo.PcCode, logInfo.Operator) } return } func (s *LogicalLog) NoLogStatusDataAdd(ctx context.Context) (err error) { logInfo, err := s.RepositoryData() if err != nil { return err } err = s.LogAdd(logInfo) return } func (s *LogicalLog) getLogByUuid(uuid string) (log log.Loging, err error) { createDate := time.Now().Format("2006-01") db := global.GVA_DB.Table("loging_" + createDate) db = db.Where("log_uuid = ?", uuid) db = db.Where("coding = 4101099 or coding = 4103099") db.First(&log) return } func (s *LogicalLog) GetLogByUuidCoding(uuid string, coding int, scriptType int) (log log.Loging, err error) { createDate := time.Now().Format("2006-01") db := global.GVA_DB.Table("loging_" + createDate) db = db.Where("log_uuid = ?", uuid) db = db.Where("coding = ?", coding) db = db.Where("script_type = ?", scriptType) db.First(&log) return } func (s *LogicalLog) RepositoryData() (*log.Loging, error) { nodeCode, typeCode, err := s.codeData() if err != nil { return nil, err } if s.Request.GameId == 0 { loging, err := s.GetAccountGameIdCache(context.Background(), s.CurrentDate(), s.Request.LogUuid) if err != nil { return nil, err } if loging.GameId == 0 { return nil, errors.New("没有找到数据" + s.Request.LogUuid) } s.Request.GameId = loging.GameId s.Request.Account = loging.Account s.Request.DeviceId = loging.DeviceId s.Request.ComputerType = loging.ComputerType s.Request.EnvCode = loging.EnvCode s.Request.Operator = loging.Operator s.Request.PcCode = loging.PcCode s.Request.PcIp = loging.PcIp s.Request.PcMac = loging.PcMac s.Request.SimulatorIp = loging.SimulatorIp s.Request.SimulatorMac = loging.SimulatorMac s.Request.AccountType = loging.AccountType s.Request.TaskType = loging.TaskType } logInfo := new(log.Loging) logInfo.Coding = s.Request.Coding logInfo.GameId = s.Request.GameId logInfo.Status = s.Status logInfo.Account = s.Request.Account logInfo.NodeCoding = nodeCode logInfo.TypeCoding = typeCode logInfo.DeviceId = s.Request.DeviceId logInfo.ComputerType = s.Request.ComputerType logInfo.EnvCode = s.Request.EnvCode logInfo.LogUuid = s.Request.LogUuid logInfo.Operator = s.Request.Operator logInfo.PcCode = s.Request.PcCode logInfo.PcIp = s.Request.PcIp logInfo.SimulatorIp = s.Request.SimulatorIp logInfo.SimulatorMac = s.Request.SimulatorMac logInfo.PcMac = s.Request.PcMac logInfo.AccountType = s.Request.AccountType logInfo.TaskType = s.Request.TaskType logInfo.ScriptType = s.ScriptType logInfo.Remarks = s.Request.Remarks logInfo.CreateDate = time.Now().Format("2006-01-02") logInfo.CreateTime = time.Now().Format("2006-01-02 15:04:05") return logInfo, err } func (s *LogicalLog) codeData() (nodeCode, typeCode int, err error) { code := strconv.Itoa(s.Request.Coding) nodeCodeStr := code[:3] typeCodeStr := code[:5] nodeCode, err = strconv.Atoi(nodeCodeStr) if err != nil { return } typeCode, err = strconv.Atoi(typeCodeStr) return } func (s *LogicalLog) LogAdd(l *log.Loging) error { createDate := l.CreateDate splitTime := strings.Split(createDate, "-") date := splitTime[0] + "-" + splitTime[1] if date != "2022-11" { table := "loging_" + splitTime[0] + "-" + splitTime[1] return global.GVA_DB.Table(table).Create(&l).Error } return global.GVA_DB.Create(&l).Error } func (s *LogicalLog) errLogAdd(l *log.Loging) error { return global.GVA_DB.Table("err_log").Create(&l).Error } func (s *LogicalLog) ReportPointsLog(ctx context.Context, request1 request.AddLogRequest) (err error) { key := reportPointsKey + request1.LogUuid status, err := global.GVA_REDIS.Exists(ctx, key).Result() if err != nil { return } var reportPointsData []*request.ReportPointsData if status == 0 { reportData := new(request.ReportPointsData) reportData.Data = request1.ReportPointsData reportData.Date = time.Now().Format("2006-01-02 15:04:05") reportPointsData = append(reportPointsData, reportData) str, _ := json.Marshal(reportPointsData) global.GVA_REDIS.Set(ctx, key, str, time.Minute*20) } else { str, _ := global.GVA_REDIS.Get(ctx, key).Result() err := json.Unmarshal([]byte(str), &reportPointsData) if err != nil { return err } reportData := new(request.ReportPointsData) reportData.Data = request1.ReportPointsData reportData.Date = time.Now().Format("2006-01-02 15:04:05") reportPointsData = append(reportPointsData, reportData) str1, _ := json.Marshal(reportPointsData) global.GVA_REDIS.Set(ctx, key, str1, time.Minute*20) } return err } func (s *LogicalLog) ReportPointsLogAdd(ctx context.Context, request1 request.AddLogRequest, status int) (err error) { key := reportPointsKey + request1.LogUuid isNull, err := global.GVA_REDIS.Exists(ctx, key).Result() if err != nil { return } if isNull == 0 { return } else { str, err := global.GVA_REDIS.Get(ctx, key).Result() if err != nil { return err } logIfo, _ := s.GetAccountGameIdCache(ctx, s.CurrentDate(), request1.LogUuid) var reportPointsData []*request.ReportPointsData err = json.Unmarshal([]byte(str), &reportPointsData) if err != nil { return err } reportPoints := new(log.ReportPointsLog) reportPoints.ReportPointsData = str reportPoints.LogUuid = request1.LogUuid reportPoints.Status = status reportPoints.Account = logIfo.Account reportPoints.GameId = logIfo.GameId reportPoints.Coding = logIfo.Coding reportPoints.ReportPointsNum = len(reportPointsData) reportPoints.CreateDate = time.Now().Format("2006-01-02") reportPoints.CreateTime = time.Now().Format("2006-01-02 15:04:05") err = global.GVA_DB.Create(&reportPoints).Error if err != nil { return err } global.GVA_REDIS.Del(ctx, key) err = s.DelAccountGameIdCache(ctx, s.CurrentDate(), logIfo.LogUuid) } return } func (s *LogicalLog) ExistsKey(ctx context.Context, key string) (bool, error) { isNull, err := global.GVA_REDIS.Exists(ctx, key).Result() if err != nil { return false, err } if isNull == 0 { return false, nil } else { return true, nil } } func (s *LogicalLog) ExistsHsKey(ctx context.Context, key string, field string) (bool, error) { isNull, err := global.GVA_REDIS.HExists(ctx, key, field).Result() if err != nil { return false, err } return isNull, err } // 前3个编号 func (s *LogicalLog) NodeLogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) { node := code[:3] err = s.LogSetNum(ctx, date, gameId, node, status, taskType) return } // 前5个编号 func (s *LogicalLog) TypeLogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) { node := code[:5] err = s.LogSetNum(ctx, date, gameId, node, status, taskType) return } // 整个编号 func (s *LogicalLog) CodeLogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) { err = s.LogSetNum(ctx, date, gameId, code, status, taskType) return } // 前5个编号和整个编号缓存 func (s *LogicalLog) PartTypeLogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) { node := code[:5] err = s.LogSetNum(ctx, date, gameId, node, status, taskType) if err != nil { return } err = s.LogSetNum(ctx, date, gameId, code, status, taskType) return } func (s *LogicalLog) LogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) { strGameId := strconv.Itoa(gameId) nodeGameCacheKey := fmt.Sprintf(NodeGameCacheKey, date) key := nodeGameCacheKey + ":" + strGameId + ":" + strconv.Itoa(taskType) + ":" + status + ":" + code err = s.SetCacheNum(ctx, key) return } func (s *LogicalLog) SetCacheNum(ctx context.Context, key string) (err error) { bl, err := s.ExistsKey(ctx, key) if err != nil { return err } if !bl { err = global.GVA_REDIS.Set(ctx, key, 1, time.Hour*48).Err() return err } else { err = global.GVA_REDIS.Incr(ctx, key).Err() return err } } func (s *LogicalLog) NodeLogGetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (num int, err error) { node := code[:3] num, err = s.LogGetNum(ctx, date, gameId, node, status, taskType) return } func (s *LogicalLog) TypeLogGetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (num int, err error) { node := code[:5] num, err = s.LogGetNum(ctx, date, gameId, node, status, taskType) return } func (s *LogicalLog) CodeLogGetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (num int, err error) { num, err = s.LogGetNum(ctx, date, gameId, code, status, taskType) return } func (s *LogicalLog) LogGetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (num int, err error) { strGameId := strconv.Itoa(gameId) nodeGameCacheKey := fmt.Sprintf(NodeGameCacheKey, date) key := nodeGameCacheKey + ":" + strGameId + ":" + strconv.Itoa(taskType) + ":" + status + ":" + code num, err = s.GetCacheNum(ctx, key) return } func (s *LogicalLog) GetCacheNum(ctx context.Context, key string) (num int, err error) { num, err = global.GVA_REDIS.Get(ctx, key).Int() if err != nil { if err == redis.Nil { return 0, nil } return 0, err } return num, err } func (s *LogicalLog) SetGameCache(ctx context.Context, date string, gameId int) (err error) { key := fmt.Sprintf(GameCacheKey, date) _, err = global.GVA_REDIS.HSet(ctx, key, gameId, 1).Result() if err != nil { return err } return err } func (s *LogicalLog) GetGameCache(ctx context.Context, date string) (mps map[string]string, err error) { key := fmt.Sprintf(GameCacheKey, date) mps, err = global.GVA_REDIS.HGetAll(ctx, key).Result() if err != nil { return mps, err } return mps, err } func (s *LogicalLog) SetAccountGameIdCache(ctx context.Context, date string, logInfo *log.Loging) (err error) { key := fmt.Sprintf(logUuidAccountGameId, date) value, _ := json.Marshal(logInfo) _, err = global.GVA_REDIS.HGet(ctx, key, logInfo.LogUuid).Result() if err != nil { if err == redis.Nil { _, err = global.GVA_REDIS.HSet(ctx, key, logInfo.LogUuid, value).Result() if err != nil { return err } } else { return err } } return err } func (s *LogicalLog) GetAccountGameIdCache(ctx context.Context, date string, uuid string) (logInfo log.Loging, err error) { key := fmt.Sprintf(logUuidAccountGameId, date) data, err := global.GVA_REDIS.HGet(ctx, key, uuid).Result() if err != nil { if err == redis.Nil { logInfo, err = s.getLogByUuid(uuid) return logInfo, nil } return logInfo, err } err = json.Unmarshal([]byte(data), &logInfo) return } func (s *LogicalLog) DelAccountGameIdCache(ctx context.Context, date string, uuid string) (err error) { key := fmt.Sprintf(logUuidAccountGameId, date) _, err = global.GVA_REDIS.HDel(ctx, key, uuid).Result() return } func (s *LogicalLog) GetGameStatisticsCacheKeys(ctx context.Context, date string, gameId int, taskType int) (keys []string, err error) { strGameId := strconv.Itoa(gameId) nodeGameCacheKey := fmt.Sprintf(NodeGameCacheKey, date) key := nodeGameCacheKey + ":" + strGameId + ":" + strconv.Itoa(taskType) + ":*" keys, err = global.GVA_REDIS.Keys(ctx, key).Result() return } func (s *LogicalLog) ByCacheKeyGetEndNode(key string) (code string) { str := strings.Split(key, ":") code = str[len(str)-1] return } func (s *LogicalLog) SetUuidCodeCache(ctx context.Context, date string, uuid string, code int, gameId int) (err error) { key := fmt.Sprintf(reportPointsKey, date) + strconv.Itoa(gameId) + ":" + uuid _, err = global.GVA_REDIS.HSet(ctx, key, code, 1).Result() if err != nil { return err } return err } func (s *LogicalLog) ExistsUuidCodeCache(ctx context.Context, date string, uuid string, code int, gameId int) (b bool, err error) { key := fmt.Sprintf(reportPointsKey, date) + strconv.Itoa(gameId) + ":" + uuid b, err = s.ExistsHsKey(ctx, key, strconv.Itoa(code)) if err != nil { return false, err } return } func (s *LogicalLog) GetCacheKeys(ctx context.Context, keys string) (key []string, err error) { key, err = global.GVA_REDIS.Keys(ctx, keys).Result() return } func (s *LogicalLog) DelStatisticsNumCache(ctx context.Context, date string, gameId int) (err error) { key1FailStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "1" + ":" + FailStatus + ":*" key1NoLogStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "1" + ":" + NoLogStatus + ":*" key1OkStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "1" + ":" + OkStatus + ":*" key0FailStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "0" + ":" + FailStatus + ":*" key0NoLogStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "0" + ":" + NoLogStatus + ":*" key0OkStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "0" + ":" + OkStatus + ":*" fmt.Println(key1FailStatus) fmt.Println(key1NoLogStatus) fmt.Println(key1OkStatus) fmt.Println(key0FailStatus) fmt.Println(key0NoLogStatus) fmt.Println(key0OkStatus) err = s.cache.DelBatheCache(ctx, key1FailStatus) err = s.cache.DelBatheCache(ctx, key1NoLogStatus) err = s.cache.DelBatheCache(ctx, key1OkStatus) err = s.cache.DelBatheCache(ctx, key0FailStatus) err = s.cache.DelBatheCache(ctx, key0NoLogStatus) err = s.cache.DelBatheCache(ctx, key0OkStatus) if err != nil { return err } return } func (s *LogicalLog) DelUuidCodeCache(ctx context.Context, date string, gameId int) (err error) { key := fmt.Sprintf(reportPointsKey, date) + strconv.Itoa(gameId) + ":*" keys, err := global.GVA_REDIS.Keys(ctx, key).Result() for _, v := range keys { err = s.cache.DelBatheHsCache(ctx, v) } if err != nil { return err } return err } // 统计电脑使用的记录 func (s *LogicalLog) SetComputerCache(ctx context.Context, date string, pcCode, operator string) (err error) { key := fmt.Sprintf(ComputerCacheKey, date) _, err = global.GVA_REDIS.HSet(ctx, key, pcCode, operator).Result() if err != nil { return err } return err } // 获取统计电脑使用的记录 func (s *LogicalLog) GetComputerCache(ctx context.Context, date string) (mps map[string]string, err error) { key := fmt.Sprintf(ComputerCacheKey, date) mps, err = global.GVA_REDIS.HGetAll(ctx, key).Result() if err != nil { return mps, err } return mps, err } // 统计电脑拉取账号的数量 func (s *LogicalLog) SetComputerPullAccountNumCache(ctx context.Context, date string, pcCode string, gameId int) (err error) { key := fmt.Sprintf(ComputerPullAccountCacheKey, date, pcCode) + strconv.Itoa(gameId) err = s.cache.SetCacheNum(ctx, key) return err } // 获取统计电脑拉取账号的数量 func (s *LogicalLog) GetComputerPullAccountNumCache(ctx context.Context, date string, pcCode string) (mps map[string]int, err error) { key := fmt.Sprintf(ComputerPullAccountCacheKey, date, pcCode) mps, err = s.GetByPcCodeGameNumCache(ctx, key) if len(mps) == 0 { return } return } // 统计电脑进入主线的数量 func (s *LogicalLog) SetComputerEnterMainNumCache(ctx context.Context, date string, pcCode string, gameId int) (err error) { key := fmt.Sprintf(ComputerEnterMainCacheKey, date, pcCode) + strconv.Itoa(gameId) err = s.cache.SetCacheNum(ctx, key) return err } // 获取统计电脑进入主线的数量 func (s *LogicalLog) GetComputerEnterMainNumCache(ctx context.Context, date string, pcCode string) (mps map[string]int, err error) { key := fmt.Sprintf(ComputerEnterMainCacheKey, date, pcCode) mps, err = s.GetByPcCodeGameNumCache(ctx, key) if len(mps) == 0 { return } return } // 统计电脑任务成功的数量 func (s *LogicalLog) SetComputerTaskSuccessNumCache(ctx context.Context, date string, pcCode string, gameId int) (err error) { key := fmt.Sprintf(ComputerTaskSuccessCacheKey, date, pcCode) + strconv.Itoa(gameId) err = s.cache.SetCacheNum(ctx, key) return err } // 获取统计电脑任务成功的数量 func (s *LogicalLog) GetComputerTaskSuccessNumCache(ctx context.Context, date string, pcCode string) (mps map[string]int, err error) { key := fmt.Sprintf(ComputerTaskSuccessCacheKey, date, pcCode) mps, err = s.GetByPcCodeGameNumCache(ctx, key) if len(mps) == 0 { return } return } // 通过电脑code获取游戏缓存的数量 func (s *LogicalLog) GetByPcCodeGameNumCache(ctx context.Context, key string) (mps map[string]int, err error) { key += "*" data, err := s.cache.GetCacheKeys(ctx, key) if len(data) == 0 { return } mps = make(map[string]int) for _, k := range data { gameId := s.ByCacheKeyGetEndNode(k) num, _ := s.cache.GetCacheNum(ctx, k) mps[gameId] = num } return } // 统计游戏付费效率的预埋数据 func (s *LogicalLog) StatisticsFeeRateData(ctx context.Context, gameId int, logUuid string) { key := fmt.Sprintf(GameFeeRateCacheKey, s.CurrentDate(), gameId) z := &redis.Z{ Member: logUuid, Score: float64(time.Now().UnixNano() / 1e6), } global.GVA_REDIS.ZAdd(ctx, key, z) } // 获取统计游戏付费效率的数量 func (s *LogicalLog) GetStatisticsFeeRate(ctx context.Context, gameId int) (num int) { key := fmt.Sprintf(GameFeeRateCacheKey, s.CurrentDate(), gameId) z, err := global.GVA_REDIS.ZRangeWithScores(ctx, key, -1, -1).Result() if err != nil { return } if len(z) == 0 { return } end := z[0].Score start := z[0].Score - 30*60*1000 op := redis.ZRangeBy{ Min: strconv.Itoa(int(start)), Max: strconv.Itoa(int(end)), } i, err := global.GVA_REDIS.ZRangeByScore(ctx, key, &op).Result() if err != nil { return } num = len(i) return } // 获取统计单台电脑单个游戏付费效率的预埋数据 func (s *LogicalLog) GetStatisticsPcFeeRate(ctx context.Context, pcCode string, gameId int) (num int) { key := fmt.Sprintf(GamePcFeeRateCacheKey, s.CurrentDate(), gameId, pcCode) z, err := global.GVA_REDIS.ZRangeWithScores(ctx, key, -1, -1).Result() if err != nil { return } if len(z) == 0 { return } end := z[0].Score start := z[0].Score - 30*60*1000 op := redis.ZRangeBy{ Min: strconv.Itoa(int(start)), Max: strconv.Itoa(int(end)), } i, err := global.GVA_REDIS.ZRangeByScore(ctx, key, &op).Result() if err != nil { return } num = len(i) return } // 统计单台电脑单个游戏付费效率的预埋数据 func (s *LogicalLog) StatisticsPcFeeRateData(ctx context.Context, pcCode string, logUuid string, gameId int) { key := fmt.Sprintf(GamePcFeeRateCacheKey, s.CurrentDate(), gameId, pcCode) z := &redis.Z{ Member: logUuid, Score: float64(time.Now().UnixNano() / 1e6), } global.GVA_REDIS.ZAdd(ctx, key, z) } // 统计电脑效率的预埋数据 func (s *LogicalLog) StatisticsComputerRateData(ctx context.Context, pcCode string, gameId int) (err error) { hour := time.Now().Hour() key := fmt.Sprintf(GameComputerRateCacheKey, s.CurrentDate(), pcCode) key = key + strconv.Itoa(hour) err = s.cache.SetCacheStr(ctx, key, gameId) return } // 统计电脑效率的预埋数据 func (s *LogicalLog) GetStatisticsComputerRate(ctx context.Context, pcCode string) (num int) { key := fmt.Sprintf(GameComputerRateCacheKey, s.CurrentDate(), pcCode) key = key + "*" data, _ := s.cache.GetCacheKeys(ctx, key) h := map[string]int{ "2": 1, "3": 1, "4": 1, "5": 1, "6": 1, "7": 1, "8": 1, } var i = 0 for _, v := range data { key := strings.Split(v, ":") hr := key[len(key)-1:][0] if _, ok := h[hr]; ok { continue } i++ } hour := time.Now().Hour() //num = hour + 1 - len(data) if hour >= 8 { num = hour + 1 - 7 - i } else { notCalculated := hour - 2 num = hour + 1 - notCalculated - i } return } // 请求机房任务数据 func (s *LogicalLog) RequestJfRoom() (result []byte, err error) { today := time.Now().Format("2006-01-02") jfurl := "http://xjf.lianyou.fun:8099/v1/task_statistics" jfparams := map[string]string{ "query": "date:" + today + ",type:machine", } result, err = utils.HttpGet(jfurl, jfparams) return } // 请求机房数据接口数据 func (s *LogicalLog) RequestTaskData() (result []byte, err error) { today := time.Now().Format("2006-01-02") jfurl := "http://xjf.lianyou.fun:8118/data/taskDateLog" jfparams := map[string]string{ "date": today, } result, err = utils.HttpGet(jfurl, jfparams) return } // 统计在线电脑数据 func (s *LogicalLog) SetOnlineComputerNumCache(ctx context.Context, date string, pcCode string, operator string) (err error) { key := fmt.Sprintf(OnLineComputerNum, date) key += pcCode err = s.cache.SetCacheStr(ctx, key, operator) return err } // 获取在线电脑数据 func (s *LogicalLog) GetOnlineComputerNumCache(ctx context.Context, date string) (mps map[string]string, err error) { key := fmt.Sprintf(OnLineComputerNum, date) mps, err = s.GetAllOnlineComputerNumCache(ctx, key) if len(mps) == 0 { return } return } // 获取所有在线电脑 func (s *LogicalLog) GetAllOnlineComputerNumCache(ctx context.Context, key string) (mps map[string]string, err error) { key += "*" data, err := s.cache.GetCacheKeys(ctx, key) if len(data) == 0 { return } mps = make(map[string]string) for _, k := range data { pcCode := s.ByCacheKeyGetEndNode(k) operator, _ := s.cache.GetCacheStr(ctx, k) mps[pcCode] = operator } return } // 获取电脑2小时内上报的次数 func (s *LogicalLog) GetPcReportingLog(ctx context.Context, pcCode string) (num int) { key := fmt.Sprintf(PcReportingLog, s.CurrentDate(), pcCode) z, err := global.GVA_REDIS.ZRangeWithScores(ctx, key, -1, -1).Result() if err != nil { return } if len(z) == 0 { return } end := time.Now().UnixNano()/1e6 + 60*1000*2 start := end - (60*60*1000 + 60*1000*4) op := redis.ZRangeBy{ Min: strconv.Itoa(int(start)), Max: strconv.Itoa(int(end)), } i, err := global.GVA_REDIS.ZRangeByScore(ctx, key, &op).Result() if err != nil { return } num = len(i) return } // 添加电脑定时上报记录 func (s *LogicalLog) SetPcReportingLog(ctx context.Context, pcCode string, operator string) { key := fmt.Sprintf(PcReportingLog, s.CurrentDate(), pcCode) z := &redis.Z{ Member: operator, Score: float64(time.Now().UnixNano() / 1e6), } global.GVA_REDIS.ZAdd(ctx, key, z) } // 删除hash缓存数据 func (s *LogicalLog) DelHashKey(ctx context.Context, date string) { gameKeys := fmt.Sprintf(GameCacheKey, date) data, _ := global.GVA_REDIS.HKeys(ctx, gameKeys).Result() global.GVA_REDIS.HDel(ctx, gameKeys, data...) logUuidAccountGameId := fmt.Sprintf(logUuidAccountGameId, date) logUuid, _ := global.GVA_REDIS.HKeys(ctx, logUuidAccountGameId).Result() global.GVA_REDIS.HDel(ctx, logUuidAccountGameId, logUuid...) taskStatisticsKey := fmt.Sprintf("%s:taskStatistics", date) taskStatistics, _ := global.GVA_REDIS.HKeys(ctx, taskStatisticsKey).Result() global.GVA_REDIS.HDel(ctx, taskStatisticsKey, taskStatistics...) computerListKey := fmt.Sprintf(ComputerCacheKey, date) computerListKeys, _ := global.GVA_REDIS.HKeys(ctx, computerListKey).Result() global.GVA_REDIS.HDel(ctx, computerListKey, computerListKeys...) } // 删除ZSet缓存数据 func (s *LogicalLog) DelZSetKey(ctx context.Context, date string) { gamePcKey := fmt.Sprintf("%s:gamePc:gameFeeRate*", date) gameKey := fmt.Sprintf("%s:game:gameFeeRate*", date) pcReportingKey := fmt.Sprintf("%s:pc:Reporting:*", date) beginTime, _ := time.ParseInLocation("2006-01-02", date, time.Local) beginTimeI := strconv.Itoa(int(beginTime.Unix() * 1000)) endTime := strconv.Itoa(int(beginTime.Unix()*1000 + 24*60*60*1000)) gamePcKeys, _ := global.GVA_REDIS.Keys(ctx, gamePcKey).Result() gameKeys, _ := global.GVA_REDIS.Keys(ctx, gameKey).Result() pcReportingKeys, _ := global.GVA_REDIS.Keys(ctx, pcReportingKey).Result() for _, k := range gamePcKeys { global.GVA_REDIS.ZRemRangeByScore(ctx, k, beginTimeI, endTime) } for _, k := range gameKeys { global.GVA_REDIS.ZRemRangeByScore(ctx, k, beginTimeI, endTime) } for _, k := range pcReportingKeys { global.GVA_REDIS.ZRemRangeByScore(ctx, k, beginTimeI, endTime) } } // 删除hashUuid缓存数据 func (s *LogicalLog) DelHashUuidKey(ctx context.Context, date string) { gameIds, _ := s.GetGameCache(ctx, date) for id, _ := range gameIds { gamekeys := fmt.Sprintf("%s:logUuid:%s:*", date, id) data, _ := global.GVA_REDIS.Keys(ctx, gamekeys).Result() for _, hkey := range data { value, _ := global.GVA_REDIS.HKeys(ctx, hkey).Result() global.GVA_REDIS.HDel(ctx, hkey, value...) } } } // 记录扫码订单号信息 func (s *LogicalLog) AddCodeLog(request request.AddLogRequest, status int) { if request.Remarks == "" { return } logSC := new(log.LogScanningCode) logSC.GameId = request.GameId logSC.LogUuid = request.LogUuid logSC.TaskType = request.TaskType logSC.Status = status if logSC.Status == 0 { logSC.Status = -1 } codeSupplier := strings.Split(request.Remarks, "|") logSC.OrderNum = codeSupplier[0] logSC.Supplier = codeSupplier[1] logSC.CreateDate = time.Now() err := global.GVA_DB.Omit("create_time").Create(&logSC).Error if err != nil { global.GVA_LOG.Error("create LogScanningCode fail", zap.Error(err)) } } // 记录ip信息 func (s *LogicalLog) AddIpLog(request request.AddLogRequest) { if request.SimulatorIp == "" { return } logSC := new(log.IpLog) logSC.GameId = request.GameId logSC.LogUuid = request.LogUuid logSC.Ip = request.SimulatorIp logSC.Account = request.Account logSC.PcCode = request.PcCode logSC.CreateDate = time.Now().Format("2006-01-02") err := global.GVA_DB.Omit("create_time").Create(&logSC).Error if err != nil { global.GVA_LOG.Error("create LogScanningCode fail", zap.Error(err)) } } // 进入游戏ip信息 func (s *LogicalLog) UpdateIpLogStatus(logUuid string, createDate string) { err := global.GVA_DB.Table("ip_log").Where("create_date = ?", createDate).Where("log_uuid = ?", logUuid).Update("status", 2).Error if err != nil { global.GVA_LOG.Error("create LogScanningCode fail", zap.Error(err)) } } // 记录设备信息 func (s *LogicalLog) AddDeviceLog(request request.AddLogRequest) { if request.DeviceId == "" { return } logSC := new(log.DeviceLog) logSC.GameId = request.GameId logSC.LogUuid = request.LogUuid logSC.Account = request.Account logSC.DeviceId = request.DeviceId logSC.DeviceImei = request.DeviceImei logSC.DeviceIp = request.DeviceIp logSC.DeviceMac = request.DeviceMac logSC.DeviceManufacturer = request.DeviceManufacturer logSC.DeviceModel = request.DeviceModel logSC.DeviceSdk = request.DeviceSdk logSC.DeviceNumber = request.DeviceNumber logSC.SimulatorCode = request.SimulatorCode logSC.IsErr = -1 logSC.ErrStatus = 1 logSC.DeviceHex = s.DeviceHexLog(request) logSC.AccountHex = s.AccountHexLog(request) logSC.CreateDate = time.Now().Format("2006-01-02") ctx := context.Background() if request.TaskType == 1 { var deviceLog = log.DeviceLog{} if !errors.Is(global.GVA_DB.Where("account = ?", request.Account).Where("game_id = ?", request.GameId).Order("id desc").First(&deviceLog).Error, gorm.ErrRecordNotFound) { if deviceLog.AccountHex != logSC.AccountHex { logSC.IsErr = 1 logSC.ErrStatus = 3 gameDeviceErrKey := fmt.Sprintf(GameDeviceErrKey, logSC.CreateDate, request.GameId) s.cache.SetCacheNum(ctx, gameDeviceErrKey) } } } else { var deviceLogs []log.DeviceLog if !errors.Is(global.GVA_DB.Where("create_date = ?", logSC.CreateDate).Where("game_id = ?", request.GameId).Where("device_hex = ?", logSC.DeviceHex).Order("id desc").Limit(10).Find(&deviceLogs).Error, gorm.ErrRecordNotFound) { mps := make(map[string]int, 10) for _, dl := range deviceLogs { mps[dl.Account] = 1 } if len(mps) > 2 { logSC.IsErr = 1 logSC.ErrStatus = 2 gameDeviceAccountErrKey := fmt.Sprintf(GameDeviceAccountErrKey, logSC.CreateDate, request.GameId) s.cache.SetCacheNum(ctx, gameDeviceAccountErrKey) } } } logSC.CreateDate = time.Now().Format("2006-01-02") err := global.GVA_DB.Omit("create_time").Create(&logSC).Error if err != nil { global.GVA_LOG.Error("create LogScanningCode fail", zap.Error(err)) } } func (s *LogicalLog) DeviceHexLog(request request.AddLogRequest) string { logSC := new(log.DeviceHex) logSC.GameId = request.GameId logSC.DeviceId = request.DeviceId logSC.DeviceImei = request.DeviceImei logSC.DeviceMac = request.DeviceMac logSC.DeviceManufacturer = request.DeviceManufacturer logSC.DeviceModel = request.DeviceModel logSC.DeviceSdk = request.DeviceSdk logSC.DeviceNumber = request.DeviceNumber dh, _ := json.Marshal(logSC) return utils.ByteToHex(dh) } func (s *LogicalLog) AccountHexLog(request request.AddLogRequest) string { logSC := new(log.AccountHex) logSC.GameId = request.GameId logSC.DeviceId = request.DeviceId logSC.DeviceImei = request.DeviceImei logSC.DeviceMac = request.DeviceMac logSC.DeviceManufacturer = request.DeviceManufacturer logSC.DeviceModel = request.DeviceModel logSC.DeviceSdk = request.DeviceSdk logSC.DeviceNumber = request.DeviceNumber logSC.Account = request.Account dh, _ := json.Marshal(logSC) return utils.ByteToHex(dh) } // 修改设备信息状态 func (s *LogicalLog) UpdateDeviceLogStatus(logUuid string, createDate string) { err := global.GVA_DB.Table("ip_log").Where("create_date = ?", createDate).Where("log_uuid = ?", logUuid).Update("status", 2).Error if err != nil { global.GVA_LOG.Error("create LogScanningCode fail", zap.Error(err)) } }