package loging

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/go-redis/redis/v8"
	"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"
	PcGameFeeRateCacheKey       = "%s:PcGame:FeeRate:%s"
	OnLineComputerNum           = "%s:OnLineComputerNum:"
)

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)
	}*/
	err = s.SetGameCache(context.Background(), s.CurrentDate(), logInfo.GameId)
	if err != nil {
		return
	}
	err = s.SetAccountGameIdCache(context.Background(), s.CurrentDate(), logInfo)
	if err != nil {
		return
	}
	if logInfo.PcCode != "" {
		err = s.SetComputerCache(context.Background(), s.CurrentDate(), logInfo.PcCode, logInfo.Operator)
		_ = s.StatisticsComputerRateData(context.Background(), logInfo.PcCode, logInfo.GameId)
	}
	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) {
	db := global.GVA_DB.Table("loging")
	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) {
	db := global.GVA_DB.Table("loging")
	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.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 {
	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)
	hour := time.Now().Hour()
	num = hour + 1 - len(data)
	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
}
