| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356 |
- package log
- import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/go-redis/redis/v8"
- "github.com/xuri/excelize/v2"
- "go.uber.org/zap"
- "gorm.io/gorm"
- "log-server/global"
- "log-server/model/log"
- "log-server/model/log/request"
- "log-server/model/log/response"
- "log-server/model/task"
- "log-server/model/typeManage"
- loging2 "log-server/service/log/loging"
- "log-server/utils"
- "strconv"
- "strings"
- "time"
- )
- type ServiceStatisticsLog struct {
- loging2.LogicalLog
- Person typeManage.ResponsiblePerson
- }
- var StatisticsCompletedKey = "%s:StatisticsCompleted"
- var taskStatistics = "%s:taskStatistics"
- var ComputerNum = "%s:ComputerNum"
- var TaskType = []int{0, 1}
- func (s *ServiceStatisticsLog) CreateStatisticsLog() {
- key := fmt.Sprintf(StatisticsCompletedKey, s.LogicalLog.CurrentDate())
- ctx := context.Background()
- b, err := s.LogicalLog.ExistsKey(ctx, key)
- if err != nil {
- global.GVA_LOG.Error("获取redis key失败!"+key, zap.Error(err))
- return
- }
- if b {
- global.GVA_LOG.Info("统计数据已完成!"+key, zap.Error(err))
- return
- }
- gameMps, err := s.LogicalLog.GetGameCache(context.Background(), s.YesterdayDate())
- if err != nil {
- global.GVA_LOG.Error("获取redis game list失败!", zap.Error(err))
- return
- }
- if len(gameMps) == 0 {
- global.GVA_LOG.Info("获取redis game list没有数据!")
- return
- }
- var statisticsLogs []*log.StatisticsLog
- for gameId, _ := range gameMps {
- for _, tt := range TaskType {
- gameIdInt, _ := strconv.Atoi(gameId)
- statisticsLogNew := new(log.StatisticsLog)
- statisticsLog := s.statisticsData(ctx, gameIdInt, tt, s.LogicalLog.YesterdayDate(), statisticsLogNew)
- if !errors.Is(global.GVA_DB.Where("create_date = ?", s.LogicalLog.YesterdayDate()).Where("game_id = ?", gameIdInt).First(&log.StatisticsLog{}).Error, gorm.ErrRecordNotFound) {
- // 已存在,跳过
- err = global.GVA_DB.Where("game_id", gameIdInt).Where("create_date = ?", s.LogicalLog.YesterdayDate()).Where("type = ?", tt).Updates(statisticsLog).Error
- if err != nil {
- global.GVA_LOG.Error("update StatisticsLogs失败!", zap.Error(err))
- continue
- }
- continue
- }
- statisticsLogs = append(statisticsLogs, statisticsLog)
- }
- }
- if len(statisticsLogs) == 0 {
- global.GVA_LOG.Info("statisticsLogsGameInfo没有数据!", zap.Error(err))
- global.GVA_REDIS.Set(ctx, key, 1, 24*time.Hour)
- return
- }
- err = global.GVA_DB.Create(statisticsLogs).Error
- if err != nil {
- global.GVA_LOG.Error("添加statisticsLogsGameInfo失败!", zap.Error(err))
- return
- }
- global.GVA_REDIS.Set(ctx, key, 1, 24*time.Hour)
- return
- }
- func (s *ServiceStatisticsLog) TodayCreateStatisticsGameInfoLog() {
- date := s.LogicalLog.CurrentDate()
- ctx := context.Background()
- gameMps, err := s.LogicalLog.GetGameCache(context.Background(), date)
- if err != nil {
- global.GVA_LOG.Error("获取redis game list失败!", zap.Error(err))
- return
- }
- if len(gameMps) == 0 {
- global.GVA_LOG.Info("获取redis game list没有数据!")
- return
- }
- completeTaskData, err := s.CompleteTaskData(date)
- if err != nil {
- global.GVA_LOG.Error("CompleteTaskData get data fail", zap.Error(err))
- return
- }
- var statisticsLogs []*log.StatisticsLog
- for gameId, _ := range gameMps {
- for _, tt := range TaskType {
- gameIdInt, _ := strconv.Atoi(gameId)
- statisticsLogNew := new(log.StatisticsLog)
- statisticsLog := s.statisticsData(ctx, gameIdInt, tt, date, statisticsLogNew)
- if tt == 0 {
- statisticsLog.TargetComplete = completeTaskData[gameIdInt].NewComplete + completeTaskData[gameIdInt].HandNewComplete
- } else {
- statisticsLog.TargetComplete = completeTaskData[gameIdInt].RetainedComplete + completeTaskData[gameIdInt].HandRetainedComplete - completeTaskData[gameIdInt].NewComplete - completeTaskData[gameIdInt].HandNewComplete
- }
- if !errors.Is(global.GVA_DB.Where("create_date = ?", date).Where("game_id = ?", gameIdInt).First(&log.StatisticsLog{}).Error, gorm.ErrRecordNotFound) {
- // 已存在,跳过
- err = global.GVA_DB.Where("game_id", gameIdInt).Where("create_date = ?", date).Where("type = ?", tt).Updates(statisticsLog).Error
- if err != nil {
- global.GVA_LOG.Error("update StatisticsLogs失败!", zap.Error(err))
- continue
- }
- continue
- }
- statisticsLogs = append(statisticsLogs, statisticsLog)
- if len(statisticsLogs) == 100 {
- err = global.GVA_DB.Create(statisticsLogs).Error
- if err != nil {
- global.GVA_LOG.Error("添加statisticsLogs失败!", zap.Error(err))
- return
- }
- statisticsLogs = []*log.StatisticsLog{}
- }
- }
- }
- if len(statisticsLogs) == 0 {
- global.GVA_LOG.Info("statisticsLogsGameInfo没有数据!", zap.Error(err))
- return
- }
- err = global.GVA_DB.Create(statisticsLogs).Error
- if err != nil {
- global.GVA_LOG.Error("添加statisticsLogsGameInfo失败!", zap.Error(err))
- return
- }
- return
- }
- // 获取任务完成数据
- func (s *ServiceStatisticsLog) CompleteTaskData(date string) (mps map[int]task.GameTargetComplete, err error) {
- db := global.GVA_DB.Table("game_target_complete")
- db = db.Where("create_date = ?", date)
- var apiList []task.GameTargetComplete
- mps = map[int]task.GameTargetComplete{}
- err = db.Order("id desc").Find(&apiList).Error
- for _, api := range apiList {
- mps[api.TaskId] = api
- }
- return
- }
- func (s *ServiceStatisticsLog) TodayStatisticsLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo) (interface{}, int64, error) {
- db := global.GVA_DB.Model(&log.StatisticsLog{})
- if api.GameId != 0 {
- db = db.Where("game_id = ?", api.GameId)
- }
- db = db.Where("create_date = ?", s.CurrentDate())
- var total int64
- err := db.Count(&total).Error
- if err != nil {
- return nil, 0, err
- }
- limit := info.PageSize
- offset := info.PageSize * (info.Page - 1)
- var statisticsLogs []*log.StatisticsLog
- var statisticsLogs2 []*log.StatisticsLog
- db = db.Limit(limit).Offset(offset)
- err = db.Order("id").Find(&statisticsLogs).Error
- for _, gameInfo := range statisticsLogs {
- tt := gameInfo.Type
- gameIdInt := gameInfo.GameId
- statisticsLogNew := new(log.StatisticsLog)
- statisticsLog := s.statisticsData(ctx, gameIdInt, tt, s.LogicalLog.CurrentDate(), statisticsLogNew)
- statisticsLogs2 = append(statisticsLogs2, statisticsLog)
- }
- return statisticsLogs2, total, err
- }
- func (s *ServiceStatisticsLog) statisticsData(ctx context.Context, gameIdInt, tt int, date string, statisticsLog *log.StatisticsLog) *log.StatisticsLog {
- key := fmt.Sprintf(taskStatistics, date)
- gameIdStr := strconv.Itoa(gameIdInt)
- data, err := global.GVA_REDIS.HGet(ctx, key, gameIdStr).Result()
- if err != nil {
- if err == redis.Nil {
- global.GVA_LOG.Info("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
- } else {
- global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
- return nil
- }
- }
- var taskStatistics request.TaskStatistics
- _ = json.Unmarshal([]byte(data), &taskStatistics)
- pullAccountOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4100000", loging2.OkStatus, tt)
- pullAccountFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4100000", loging2.FailStatus, tt)
- ConstituencyFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4604000", loging2.FailStatus, tt)
- ConstituencyOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4604000", loging2.OkStatus, tt)
- StartProxyFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4200000", loging2.FailStatus, tt)
- StartProxyOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4200000", loging2.OkStatus, tt)
- SimulatorStartFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.FailStatus, tt)
- SimulatorStartOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.OkStatus, tt)
- NetworkCheckFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4400000", loging2.FailStatus, tt)
- NetworkCheckOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4400000", loging2.OkStatus, tt)
- GameStartFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4500000", loging2.FailStatus, tt)
- GameStartOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4500000", loging2.OkStatus, tt)
- xmyLoginFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4601000", loging2.FailStatus, tt)
- xmyLoginOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4601000", loging2.OkStatus, tt)
- wxLoginFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4602000", loging2.FailStatus, tt)
- wxLoginOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4602000", loging2.OkStatus, tt)
- mzLoginFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4603000", loging2.FailStatus, tt)
- mzLoginOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4603000", loging2.OkStatus, tt)
- LoginFail := xmyLoginFail + wxLoginFail + mzLoginFail
- LoginOk := xmyLoginOk + wxLoginOk + mzLoginOk
- EnterMainFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4700000", loging2.FailStatus, tt)
- EnterMainOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4700000", loging2.OkStatus, tt)
- FeeFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4800000", loging2.FailStatus, tt)
- FeeOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4800000", loging2.OkStatus, tt)
- ScriptStartFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.FailStatus, tt)
- ScriptStartOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.OkStatus, tt)
- EnterGameFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4605000", loging2.FailStatus, tt)
- EnterGameOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4605000", loging2.OkStatus, tt)
- AuthenticationFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4607000", loging2.FailStatus, tt)
- AuthenticationOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4607000", loging2.OkStatus, tt)
- EnterStartGame, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4501000", loging2.NoLogStatus, tt)
- EnterAuthentication, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4607000", loging2.NoLogStatus, tt)
- EnterConstituency, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604000", loging2.NoLogStatus, tt)
- EnterGame, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4605000", loging2.NoLogStatus, tt)
- EnterMain, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4701000", loging2.NoLogStatus, tt)
- EnterFee, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4800000", loging2.NoLogStatus, tt)
- EnterXmyLogin, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4601000", loging2.NoLogStatus, tt)
- EnterWxLogin, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4602000", loging2.NoLogStatus, tt)
- EnterMzLogin, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4603000", loging2.NoLogStatus, tt)
- BanOff1, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4601006", loging2.FailStatus, tt) // 小绵羊登录封号
- BanOff2, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4602008", loging2.FailStatus, tt) // 微信登录封号
- BanOff3, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4603006", loging2.FailStatus, tt) // 魅族登录封号
- BanOff4, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4701003", loging2.FailStatus, tt) // 主线封号
- Freeze, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4809904", loging2.NoLogStatus, tt) // 冻结
- BanOff := BanOff1 + BanOff2 + BanOff3 + BanOff4
- EnterLogin := EnterXmyLogin + EnterMzLogin + EnterWxLogin
- EnterScanningCode, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4608000", loging2.NoLogStatus, tt)
- TranscodingFail, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4608001", loging2.FailStatus, tt)
- ThirdPartyFail, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4608002", loging2.FailStatus, tt)
- ScanningCodeSuccess, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4608099", loging2.OkStatus, tt)
- //statisticsLog.NewHaveRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604004", loging2.NoLogStatus, tt) // 新增有角色
- //statisticsLog.RetainedNotRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604003", loging2.NoLogStatus, tt) // 留存无角色
- statisticsLog.PullAccountOk = pullAccountOk
- statisticsLog.PullAccountFail = pullAccountFail
- statisticsLog.StartProxyFail = StartProxyFail
- statisticsLog.StartProxyOk = StartProxyOk
- statisticsLog.SimulatorStartFail = SimulatorStartFail
- statisticsLog.SimulatorStartOk = SimulatorStartOk
- statisticsLog.NetworkCheckFail = NetworkCheckFail
- statisticsLog.NetworkCheckOk = NetworkCheckOk
- statisticsLog.EnterMainFail = EnterMainFail
- statisticsLog.EnterMainOk = EnterMainOk
- statisticsLog.EnterGameFail = EnterGameFail
- statisticsLog.EnterGameOk = EnterGameOk
- statisticsLog.GameId = gameIdInt
- statisticsLog.ConstituencyFail = ConstituencyFail
- statisticsLog.ConstituencyOk = ConstituencyOk
- statisticsLog.FeeFail = FeeFail
- statisticsLog.FeeOk = FeeOk
- statisticsLog.GameStartFail = GameStartFail
- statisticsLog.GameStartOk = GameStartOk
- statisticsLog.LoginFail = LoginFail
- statisticsLog.LoginOk = LoginOk
- statisticsLog.XmyLoginFail = xmyLoginFail
- statisticsLog.XmyLoginOk = xmyLoginOk
- statisticsLog.WxLoginFail = wxLoginFail
- statisticsLog.WxLoginOk = wxLoginOk
- statisticsLog.MzLoginFail = mzLoginFail
- statisticsLog.MzLoginOk = mzLoginOk
- statisticsLog.ScriptStartFail = ScriptStartFail
- statisticsLog.ScriptStartOk = ScriptStartOk
- statisticsLog.AuthenticationFail = AuthenticationFail
- statisticsLog.AuthenticationOk = AuthenticationOk
- statisticsLog.EnterStartGame = EnterStartGame
- statisticsLog.EnterAuthentication = EnterAuthentication
- statisticsLog.EnterConstituency = EnterConstituency
- statisticsLog.EnterGame = EnterGame
- statisticsLog.EnterMain = EnterMain
- statisticsLog.EnterFee = EnterFee
- statisticsLog.EnterLogin = EnterLogin
- statisticsLog.BanOff = BanOff
- statisticsLog.Freeze = Freeze
- statisticsLog.Type = tt
- statisticsLog.CreateTime = time.Now().Format("2006-01-02 15:04:05")
- statisticsLog.CreateDate = date
- statisticsLog.EnterScanningCode = EnterScanningCode
- statisticsLog.ThirdPartyFail = ThirdPartyFail
- statisticsLog.TranscodingFail = TranscodingFail
- statisticsLog.ScanningCodeSuccess = ScanningCodeSuccess
- if tt == 1 {
- statisticsLog.NotRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4609901", loging2.NoLogStatus, tt)
- statisticsLog.HasRole = 0
- statisticsLog.IssuedAccount = taskStatistics.RetainedPullAccount
- statisticsLog.TargetNum = taskStatistics.RetainedTarget
- statisticsLog.TargetCompleteNum = taskStatistics.RetainedComplete
- //statisticsLog.ScanningSuccessRate = 0
- } else {
- statisticsLog.NotRole = 0
- statisticsLog.HasRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4609902", loging2.NoLogStatus, tt)
- statisticsLog.IssuedAccount = taskStatistics.NewPullAccount
- statisticsLog.TargetCompleteNum = taskStatistics.NewComplete
- statisticsLog.TargetNum = taskStatistics.NewTarget
- statisticsLog.NewScanningCode = taskStatistics.NewScanningCode
- //if statisticsLog.NewScanningCode != 0 {
- // statisticsLog.ScanningSuccessRate = float64(statisticsLog.TargetNum) / float64(statisticsLog.NewScanningCode) * 100
- //}
- }
- if statisticsLog.ScanningCodeSuccess != 0 && statisticsLog.EnterScanningCode != 0 {
- statisticsLog.ScanningSuccessRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", float64(statisticsLog.ScanningCodeSuccess)/float64(statisticsLog.EnterScanningCode)*100), 64)
- }
- statisticsLog.PayComplete = taskStatistics.PayComplete
- statisticsLog.PayTarget = taskStatistics.PayTarget
- statisticsLog.OrderCreate = taskStatistics.OrderCreate
- statisticsLog.OrderSuccess = taskStatistics.OrderSuccess
- statisticsLog.RetainedAccountNum = taskStatistics.RetainedAccountNum
- statisticsLog.FeeAccountNum = taskStatistics.FeeAccountNum
- statisticsLog.Operator = taskStatistics.Remark
- statisticsLog.GameName = taskStatistics.GameName
- if statisticsLog.IssuedAccount != 0 {
- statisticsLog.PullSuccessRate = float64(statisticsLog.PullAccountOk) / float64(statisticsLog.IssuedAccount) * 100
- }
- if statisticsLog.PullAccountOk != 0 {
- statisticsLog.StartSuccessRate = float64(statisticsLog.SimulatorStartOk) / float64(statisticsLog.PullAccountOk) * 100
- }
- if statisticsLog.GameStartOk != 0 {
- statisticsLog.MainSuccessRate = float64(statisticsLog.EnterMain) / float64(statisticsLog.GameStartOk) * 100
- }
- if statisticsLog.TargetNum != 0 {
- statisticsLog.TaskSuccessRate = float64(statisticsLog.EnterMain) / float64(statisticsLog.TargetNum) * 100
- }
- if statisticsLog.OrderCreate != 0 {
- statisticsLog.LocalOrderSuccessRate = float64(statisticsLog.FeeOk) / float64(statisticsLog.OrderCreate) * 100
- }
- return statisticsLog
- }
- func (s *ServiceStatisticsLog) OtherStatisticsLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo, order string, desc bool, gamePortId int) (interface{}, int64, error) {
- date := api.CreateDate
- if date == "" {
- date = s.CurrentDate()
- }
- db := global.GVA_DB.Table("log_statistics gtc")
- db = db.Joins("left join game_task gt on gt.task_id = gtc.game_id")
- if api.GameId != 0 {
- db = db.Where("gtc.game_id = ?", api.GameId)
- }
- db = db.Where("gtc.create_date = ?", date)
- if gamePortId != 0 {
- db = db.Where("gt.game_port_id = ?", gamePortId)
- }
- var total int64
- err := db.Count(&total).Error
- db = db.Select("gtc.*")
- if err != nil {
- return nil, 0, err
- }
- limit := info.PageSize
- offset := info.PageSize * (info.Page - 1)
- var statisticsLogs []*log.StatisticsLog
- db = db.Limit(limit).Offset(offset)
- if order != "" {
- var OrderStr string
- // 设置有效排序key 防止sql注入
- // 感谢 Tom4t0 提交漏洞信息
- orderMap := make(map[string]bool, 7)
- orderMap["pull_success_rate"] = true
- orderMap["start_success_rate"] = true
- orderMap["main_success_rate"] = true
- orderMap["task_success_rate"] = true
- orderMap["scanning_success_rate"] = true
- orderMap["local_order_success_rate"] = true
- orderMap["game_id"] = true
- if orderMap[order] {
- if desc {
- OrderStr = "gtc." + order + " desc"
- } else {
- OrderStr = "gtc." + order
- }
- } else { // didn't matched any order key in `orderMap`
- global.GVA_LOG.Error("获取失败!", zap.Error(err))
- return statisticsLogs, total, err
- }
- err = db.Order(OrderStr).Find(&statisticsLogs).Error
- } else {
- err = db.Order("gtc.id").Find(&statisticsLogs).Error
- }
- if err != nil {
- return nil, 0, err
- }
- for _, statisticsLog := range statisticsLogs {
- statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
- }
- return statisticsLogs, total, err
- }
- func (s *ServiceStatisticsLog) StatisticsNodeLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
- date := api.CreateDate
- if date == "" || date == s.CurrentDate() {
- apilist, total, err := s.TodayStatisticsLogList(ctx, api, info)
- return apilist, total, err
- }
- db := global.GVA_DB.Model(&log.StatisticsLog{})
- if api.GameId != 0 {
- db = db.Where("game_id = ?", api.GameId)
- }
- db = db.Where("create_date = ?", date)
- var total int64
- err := db.Count(&total).Error
- if err != nil {
- return nil, 0, err
- }
- limit := info.PageSize
- offset := info.PageSize * (info.Page - 1)
- var statisticsLogs []*log.StatisticsLog
- db = db.Limit(limit).Offset(offset)
- err = db.Order("id").Find(&statisticsLogs).Error
- if err != nil {
- return nil, 0, err
- }
- for _, statisticsLog := range statisticsLogs {
- statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
- }
- return statisticsLogs, total, err
- }
- //func (s *ServiceStatisticsLog) ResetStatisticsLog(ctx context.Context, gameId int, date string) {
- // var total int64
- // key := date + ":" + strconv.Itoa(gameId) + ":reset"
- // global.GVA_REDIS.Set(ctx, key, 1, time.Minute*15)
- // db := global.GVA_DB.Table("loging")
- // db = db.Where("game_id = ?", gameId)
- // err := db.Count(&total).Error
- // if err != nil {
- // global.GVA_LOG.Error("重置统计数据总数报错", zap.Error(err))
- // return
- // }
- // limit := 100
- // num := int(math.Ceil(float64(total) / float64(limit)))
- // offset := 0
- // err = s.LogicalLog.DelStatisticsNumCache(ctx, date, gameId)
- // if err != nil {
- // global.GVA_LOG.Error("重置统计数据删除缓存报错", zap.Error(err))
- // return
- // }
- // err = s.LogicalLog.DelUuidCodeCache(ctx, date, gameId)
- // if err != nil {
- // global.GVA_LOG.Error("重置统计数据删除缓存报错2", zap.Error(err))
- // return
- // }
- // global.GVA_LOG.Info("num = " + strconv.Itoa(num))
- // for i := 0; i < num; i++ {
- // global.GVA_LOG.Info("offset = " + strconv.Itoa(offset))
- // var statisticsLogs []request.AddLogRequest
- // db2 := global.GVA_DB.Table("loging")
- // db2 = db2.Where("create_date = ?", date)
- // db2 = db2.Where("game_id = ?", gameId)
- // db2 = db2.Limit(limit).Offset(offset)
- // err = db2.Order("id").Find(&statisticsLogs).Error
- // for _, statisticsLog := range statisticsLogs {
- //
- // s.ResetStatisticsCache(ctx, statisticsLog, date)
- // }
- // offset += limit
- // }
- // global.GVA_LOG.Info("重置统计数据完成")
- //}
- //
- //func (s *ServiceStatisticsLog) ResetStatisticsCache(c context.Context, api request.AddLogRequest, date string) {
- // coding := strconv.Itoa(api.Coding)
- // nodeCoding := coding[:3]
- // status := coding[5:]
- // noLogStatus := coding[3:5]
- // var logical ServiceResetLoging
- // switch nodeCoding {
- // case "410":
- // logical = new(loging2.ResetLog)
- // break
- // case "430":
- // logical = new(loging2.ResetLog)
- // break
- // default:
- // logical = new(loging2.ResetOtherLog)
- // break
- // }
- // var err error
- // if status == "99" {
- // err = logical.SuccessLog(c, api, date)
- // } else if noLogStatus == "99" {
- // err = logical.NoLogStatusData(c, api, date)
- // } else {
- // err = logical.FailLog(c, api, date)
- // }
- // if err != nil {
- // global.GVA_LOG.Error("创建失败!", zap.Error(err))
- // }
- //}
- // 每天凌晨reset统计数据
- //func (s *ServiceStatisticsLog) EveryDayResetStatisticsCache() {
- // date := s.YesterdayDate()
- // ctx := context.Background()
- // gameMps, err := s.LogicalLog.GetGameCache(ctx, date)
- // if err != nil {
- // global.GVA_LOG.Error("获取redis game list失败!", zap.Error(err))
- // return
- // }
- // if len(gameMps) == 0 {
- // global.GVA_LOG.Info("获取redis game list没有数据!")
- // return
- // }
- // for gameId, _ := range gameMps {
- // id, _ := strconv.Atoi(gameId)
- // s.ResetStatisticsLog(ctx, id, date)
- // }
- // global.GVA_LOG.Info("游戏重置缓存完成!")
- //}
- // 添加和更新电脑指标数据
- func (s *ServiceStatisticsLog) CreateComputerStatisticsData() {
- ctx := context.Background()
- codeMps, err := s.LogicalLog.GetComputerCache(ctx, s.LogicalLog.CurrentDate())
- if err != nil {
- return
- }
- if len(codeMps) == 0 {
- return
- }
- key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
- onlineComputerMpa, _ := s.LogicalLog.GetOnlineComputerNumCache(ctx, s.LogicalLog.CurrentDate())
- var computer log.Computer
- computerData, _ := computer.OnlinePcCodeCache()
- if computerData == nil {
- global.GVA_LOG.Warn("电脑缓存数据获取失败TaskStatisticsDataCache")
- return
- }
- var csReplys []*log.LogComputer
- for code, r := range codeMps {
- // 不统计不在电脑列表里的
- if _, ok := computerData[code]; !ok {
- continue
- }
- // 负责人为空的不统计
- if computerData[code] == "" {
- continue
- }
- accountMps, err := s.LogicalLog.GetComputerPullAccountNumCache(ctx, s.LogicalLog.CurrentDate(), code)
- if err != nil {
- continue
- }
- taskMps, err := s.LogicalLog.GetComputerTaskSuccessNumCache(ctx, s.LogicalLog.CurrentDate(), code)
- if err != nil {
- continue
- }
- enterMainMps, err := s.LogicalLog.GetComputerEnterMainNumCache(ctx, s.LogicalLog.CurrentDate(), code)
- if err != nil {
- continue
- }
- delete(computerData, code)
- for gameId, num := range accountMps {
- csReply := new(log.LogComputer)
- csReply.PcCode = code
- if _, ok := onlineComputerMpa[csReply.PcCode]; ok {
- delete(onlineComputerMpa, csReply.PcCode)
- }
- csReply.Operator = r
- data, err := global.GVA_REDIS.HGet(ctx, key, gameId).Result()
- if err != nil {
- if err == redis.Nil {
- global.GVA_LOG.Info("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
- } else {
- global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
- return
- }
- }
- var taskStatistics request.TaskStatistics
- _ = json.Unmarshal([]byte(data), &taskStatistics)
- csReply.GameId, _ = strconv.Atoi(gameId)
- csReply.PullAccountNum = num
- csReply.TaskSuccessNum = taskMps[gameId]
- csReply.EnterMain = enterMainMps[gameId]
- csReply.CreateDate = time.Now().Format("2006-01-02")
- csReply.TargetNum = taskStatistics.NewTarget + taskStatistics.RetainedTarget
- csReply.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, csReply.PcCode, csReply.GameId)
- csReply.ComputerFreeTime = s.GetStatisticsComputerRate(ctx, csReply.PcCode)
- csReply.GameFeeRate = s.GetStatisticsFeeRate(ctx, csReply.GameId)
- runTime := time.Now().Hour() + 1 - csReply.ComputerFreeTime
- csReply.ComputerHourAverageRate = csReply.TaskSuccessNum / runTime
- csReply.Status = 2
- global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Where("game_id = ?", 0).Delete(&log.LogComputer{})
- if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Where("game_id = ?", gameId).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
- // 已存在,更新
- mp := make(map[string]interface{})
- if csReply.Operator != "" {
- mp["operator"] = csReply.Operator
- }
- mp["target_num"] = taskStatistics.NewTarget + taskStatistics.RetainedTarget
- mp["pull_account_num"] = num
- mp["task_success_num"] = taskMps[gameId]
- mp["computer_fee_rate"] = csReply.ComputerFeeRate
- mp["computer_free_time"] = csReply.ComputerFreeTime
- mp["game_fee_rate"] = csReply.GameFeeRate
- mp["computer_hour_average_rate"] = csReply.ComputerHourAverageRate
- mp["enter_main"] = csReply.EnterMain
- err = global.GVA_DB.Table("log_computer").Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Where("game_id = ?", gameId).Updates(mp).Error
- if err != nil {
- global.GVA_LOG.Error("更新数据失败", zap.Error(err))
- }
- continue
- }
- csReplys = append(csReplys, csReply)
- }
- }
- if len(csReplys) != 0 {
- err = global.GVA_DB.Table("log_computer").Create(csReplys).Error
- if err != nil {
- global.GVA_LOG.Error("更新数据失败", zap.Error(err))
- }
- }
- // 在线电脑没有跑数据记录
- if len(onlineComputerMpa) != 0 {
- var onlineComputer []*log.LogComputer
- for pcCode, op := range onlineComputerMpa {
- delete(computerData, pcCode)
- csReply := new(log.LogComputer)
- csReply.PcCode = pcCode
- csReply.Operator = op
- csReply.CreateDate = time.Now().Format("2006-01-02")
- csReply.Status = 1
- if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
- if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Where("status = ?", -1).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
- err = global.GVA_DB.Table("log_computer").Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Update("status", 1).Error
- if err != nil {
- global.GVA_LOG.Error("更新数据失败onlineComputerMpa", zap.Error(err))
- }
- }
- continue
- }
- onlineComputer = append(onlineComputer, csReply)
- }
- if len(onlineComputer) != 0 {
- err = global.GVA_DB.Table("log_computer").Create(onlineComputer).Error
- if err != nil {
- global.GVA_LOG.Error("更新数据失败", zap.Error(err))
- }
- }
- }
- // 没记录电脑数据
- if len(computerData) != 0 {
- var onlineComputer []*log.LogComputer
- for pcCode, op := range computerData {
- csReply := new(log.LogComputer)
- csReply.PcCode = pcCode
- csReply.Operator = op
- csReply.CreateDate = time.Now().Format("2006-01-02")
- csReply.Status = -1
- if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
- continue
- }
- onlineComputer = append(onlineComputer, csReply)
- }
- if len(onlineComputer) != 0 {
- err = global.GVA_DB.Table("log_computer").Create(onlineComputer).Error
- if err != nil {
- global.GVA_LOG.Error("更新数据失败", zap.Error(err))
- }
- }
- }
- return
- }
- func (s *ServiceStatisticsLog) ComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) (statisticsLogsComputer []*response.ComputerStatisticsReply1, total int64, err error) {
- date := api.CreateDate
- isCurrentDate := false
- if date == "" {
- date = s.CurrentDate()
- isCurrentDate = true
- }
- db := global.GVA_DB.Model(&log.LogComputer{})
- db = db.Where("create_date = ?", date)
- if api.Operator != "" {
- db = db.Where("operator = ?", api.Operator)
- }
- if api.PcCode != "" {
- db = db.Where("pc_code = ?", api.PcCode)
- }
- if api.GameId != 0 {
- db = db.Where("game_id = ?", api.GameId)
- }
- err = db.Count(&total).Error
- if err != nil {
- return nil, 0, err
- }
- limit := info.PageSize
- offset := info.PageSize * (info.Page - 1)
- var statisticsLogs []*log.LogComputer
- db = db.Limit(limit).Offset(offset)
- if order != "" {
- var OrderStr string
- // 设置有效排序key 防止sql注入
- // 感谢 Tom4t0 提交漏洞信息
- orderMap := make(map[string]bool, 3)
- orderMap["pc_code"] = true
- orderMap["game_id"] = true
- orderMap["operator"] = 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 statisticsLogsComputer, total, err
- }
- err = db.Order(OrderStr).Find(&statisticsLogs).Error
- } else {
- err = db.Order("id").Find(&statisticsLogs).Error
- }
- if err != nil {
- return nil, 0, err
- }
- for _, statisticsLog := range statisticsLogs {
- /*var gameInfo []*response.GameInfo
- err = json.Unmarshal([]byte(statisticsLog.GameInfo), &gameInfo)
- for i, _ := range gameInfo {
- statisticsLogComputer := new(response.ComputerStatisticsReply1)
- statisticsLogComputer.GameInfo = gameInfo
- statisticsLogComputer.PcCode = statisticsLog.PcCode
- statisticsLogComputer.Operator = statisticsLog.Operator
- statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
- statisticsLogComputer.GameId = gameInfo[i].GameId
- statisticsLogComputer.PullAccountNum = gameInfo[i].PullAccountNum
- statisticsLogComputer.TaskSuccessNum = gameInfo[i].TaskSuccessNum
- statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
- }*/
- //gameIdStr:=strconv.Itoa(statisticsLog.GameId)
- //taskStatistics := s.GameTargetInfo(ctx,s.LogicalLog.CurrentDate(),gameIdStr)
- statisticsLogComputer := new(response.ComputerStatisticsReply1)
- statisticsLogComputer.PcCode = statisticsLog.PcCode
- statisticsLogComputer.Operator = statisticsLog.Operator
- statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
- statisticsLogComputer.GameId = statisticsLog.GameId
- statisticsLogComputer.PullAccountNum = statisticsLog.PullAccountNum
- statisticsLogComputer.TaskSuccessNum = statisticsLog.TaskSuccessNum
- statisticsLogComputer.TargetNum = statisticsLog.TargetNum
- statisticsLogComputer.ComputerFreeTime = statisticsLog.ComputerFreeTime
- statisticsLogComputer.ComputerFeeRate = statisticsLog.ComputerFeeRate
- statisticsLogComputer.EnterMain = statisticsLog.EnterMain
- if isCurrentDate {
- statisticsLogComputer.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, statisticsLog.PcCode, statisticsLog.GameId)
- }
- statisticsLogComputer.ComputerHourAverageRate = statisticsLog.ComputerHourAverageRate
- statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
- }
- return statisticsLogsComputer, total, err
- }
- // OnlineComputerStatistics 在线电脑
- func (s *ServiceStatisticsLog) OnlineComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) ([]*response.ComputerUseLogReply, int64, error) {
- date := api.CreateDate
- if date == "" {
- date = s.CurrentDate()
- }
- db := global.GVA_DB.Model(&log.LogComputer{})
- var total int64
- db = db.Select("SUM(pull_account_num) pull_account_total,SUM(enter_main) enter_main_total,operator,pc_code,status,create_date,remarks")
- db = db.Where("create_date = ?", date)
- if api.Operator != "" {
- db = db.Where("operator = ?", api.Operator)
- }
- if api.PcCode != "" {
- db = db.Where("pc_code = ?", api.PcCode)
- }
- if api.Status != 0 {
- db = db.Where("status = ?", api.Status)
- }
- db = db.Group("pc_code")
- err := db.Count(&total).Error
- if err != nil {
- return nil, 0, err
- }
- limit := info.PageSize
- offset := info.PageSize * (info.Page - 1)
- var statisticsLogs []*response.ComputerUseLogReply
- db = db.Limit(limit).Offset(offset)
- if order != "" {
- var OrderStr string
- // 设置有效排序key 防止sql注入
- // 感谢 Tom4t0 提交漏洞信息
- orderMap := make(map[string]bool, 3)
- orderMap["pc_code"] = true
- orderMap["operator"] = 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 statisticsLogs, total, err
- }
- err = db.Order(OrderStr).Find(&statisticsLogs).Error
- } else {
- err = db.Order("id").Find(&statisticsLogs).Error
- }
- if err != nil {
- return nil, 0, err
- }
- for _, statisticsLog := range statisticsLogs {
- statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
- }
- return statisticsLogs, total, err
- }
- // GameStatistics 根据游戏id查询数据
- func (s *ServiceStatisticsLog) GameStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo) (interface{}, int64, error) {
- date := api.CreateDate
- isCurrentDate := false
- if date == "" {
- date = s.CurrentDate()
- isCurrentDate = true
- }
- db := global.GVA_DB.Model(&log.LogComputer{})
- var total int64
- db = db.Where("create_date = ?", date)
- if api.Operator != "" {
- db = db.Where("operator = ?", api.Operator)
- }
- if api.PcCode != "" {
- db = db.Where("pc_code = ?", api.PcCode)
- }
- if api.GameId != 0 {
- db = db.Where("game_id = ?", api.GameId)
- }
- db = db.Select("SUM(task_success_num) task_success_total,count(pc_code) pc_code_total,game_id,create_date,SUM(computer_free_time) computer_free_time_total,SUM(computer_hour_average_rate) computer_hour_average_total,SUM(enter_main) enter_main_total,game_fee_rate,target_num").Group("game_id")
- err := db.Count(&total).Error
- if err != nil {
- return nil, 0, err
- }
- limit := info.PageSize
- offset := info.PageSize * (info.Page - 1)
- var statisticsLogsByGameId []*response.GameIdStatisticsReply
- db = db.Limit(limit).Offset(offset)
- err = db.Order("id").Find(&statisticsLogsByGameId).Error
- if err != nil {
- return nil, 0, err
- }
- for _, statisticsLog := range statisticsLogsByGameId {
- statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
- statisticsLog.OneComputerAverageNum = statisticsLog.TaskSuccessTotal / statisticsLog.PcCodeTotal
- if isCurrentDate {
- statisticsLog.GameFeeRate = s.GetStatisticsFeeRate(ctx, statisticsLog.GameId)
- }
- }
- return statisticsLogsByGameId, total, err
- }
- func (s *ServiceStatisticsLog) GameTargetInfo(ctx context.Context, date string, gameId string) (taskStatistics1 request.TaskStatistics) {
- key := fmt.Sprintf(taskStatistics, date)
- data, err := global.GVA_REDIS.HGet(ctx, key, gameId).Result()
- if err != nil {
- if err == redis.Nil {
- global.GVA_LOG.Info("TaskStatisticsDataCache"+key+gameId, zap.Error(err))
- } else {
- global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache"+key, zap.Error(err))
- return
- }
- }
- _ = json.Unmarshal([]byte(data), &taskStatistics1)
- return
- }
- // TaskStatisticsDataCache 同步群控任务数据到缓存
- func (s *ServiceStatisticsLog) TaskStatisticsDataCache() {
- ctx := context.Background()
- key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
- data, err := s.LogicalLog.RequestJfRoom()
- if err != nil {
- global.GVA_LOG.Error("获取机房数据失败TaskStatisticsDataCache", zap.Error(err))
- return
- }
- dataTask, err := s.LogicalLog.RequestTaskData()
- if err != nil {
- global.GVA_LOG.Error("RequestTaskData", zap.Error(err))
- return
- }
- var taskData []request.TaskData
- var taskStatistics []request.TaskStatistics
- _ = json.Unmarshal(data, &taskStatistics)
- _ = json.Unmarshal(dataTask, &taskData)
- mps := map[int]request.TaskData{}
- for _, td := range taskData {
- mps[td.GameId] = td
- }
- for _, data := range taskStatistics {
- var id int
- id = data.GameId
- if _, ok := mps[id]; ok {
- data.NewScanningCode = mps[id].NewScanningCode
- data.RetainedPullAccount = mps[id].RetainedPullAccount
- data.NewPullAccount = mps[id].NewPullAccount
- data.FeeAccountNum = mps[id].FeeAccountNum
- data.RetainedAccountNum = mps[id].RetainedAccountNum
- }
- bd, _ := json.Marshal(data)
- err = global.GVA_REDIS.HSet(ctx, key, id, bd).Err()
- if err != nil {
- global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
- return
- }
- }
- }
- // TaskStatisticsDataCache1 同步群控任务数据到缓存
- func (s *ServiceStatisticsLog) TaskStatisticsDataCache1() {
- ctx := context.Background()
- key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
- data, err := s.LogicalLog.RequestJfRoom()
- if err != nil {
- global.GVA_LOG.Error("获取机房数据失败TaskStatisticsDataCache", zap.Error(err))
- return
- }
- dataTask, err := s.LogicalLog.RequestTaskData()
- if err != nil {
- global.GVA_LOG.Error("RequestTaskData", zap.Error(err))
- return
- }
- var taskData []request.TaskData
- var taskStatistics []request.TaskStatistics
- _ = json.Unmarshal(data, &taskStatistics)
- _ = json.Unmarshal(dataTask, &taskData)
- mps := map[int]request.TaskData{}
- for _, td := range taskData {
- mps[td.GameId] = td
- }
- for _, data := range taskStatistics {
- var id int
- if data.GameIdXmy != "" {
- if data.GameIdXmy == "0" {
- id = data.GameId
- } else {
- id, _ = strconv.Atoi(data.GameIdXmy)
- }
- } else {
- id = data.GameId
- }
- if _, ok := mps[id]; ok {
- data.NewScanningCode = mps[id].NewScanningCode
- data.RetainedPullAccount = mps[id].RetainedPullAccount
- data.NewPullAccount = mps[id].NewPullAccount
- data.FeeAccountNum = mps[id].FeeAccountNum
- data.RetainedAccountNum = mps[id].RetainedAccountNum
- }
- bd, _ := json.Marshal(data)
- err = global.GVA_REDIS.HSet(ctx, key, id, bd).Err()
- if err != nil {
- global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
- return
- }
- }
- }
- // GetComputerNum 同步群控任务数据到缓存
- func (s *ServiceStatisticsLog) GetComputerNum(date string) int64 {
- var total int64
- if date == "" {
- date = s.CurrentDate()
- }
- db := global.GVA_DB.Model(&log.LogComputer{})
- db = db.Distinct("pc_code").Where("create_date = ?", date)
- _ = db.Count(&total).Error
- return total
- }
- // ComputerHeartbeat 在线电脑接口
- func (s *ServiceStatisticsLog) ComputerHeartbeat(c context.Context, onlineComputer request.OnlineComputerRequest) error {
- err := s.LogicalLog.SetOnlineComputerNumCache(c, s.LogicalLog.CurrentDate(), onlineComputer.PcCode, onlineComputer.Operator)
- if err != nil {
- return err
- }
- s.LogicalLog.SetPcReportingLog(c, onlineComputer.PcCode, onlineComputer.Operator)
- return err
- }
- // ComputerTest 在线电脑接口测试
- func (s *ServiceStatisticsLog) ComputerTest(c context.Context) (interface{}, error) {
- mps, err := s.LogicalLog.GetOnlineComputerNumCache(c, s.LogicalLog.CurrentDate())
- if err != nil {
- return mps, err
- }
- return mps, err
- }
- // 定时检查电脑情况
- func (s *ServiceStatisticsLog) RegularCheckPc() {
- ctx := context.Background()
- var computer log.Computer
- computers, err := computer.OnlinePcCodeCache()
- //computersF, err := computer.OnlinePcCodeCache()
- if err != nil {
- global.GVA_LOG.Error("获取租机表数据失败", zap.Error(err))
- return
- }
- if computers == nil {
- global.GVA_LOG.Warn("电脑缓存数据获取失败TaskStatisticsDataCache")
- return
- }
- // 查询两小时内上报的数据
- var computersF = map[string]string{}
- var noReportingPc []string
- for pc, name := range computers {
- computersF[pc] = name
- if strings.Contains(name, "备用") {
- delete(computers, pc)
- continue
- }
- if name == "" {
- delete(computers, pc)
- continue
- }
- num := s.LogicalLog.GetPcReportingLog(ctx, pc)
- delete(computers, pc)
- if num == 0 {
- noReportingPc = append(noReportingPc, pc)
- }
- }
- mpsPerson, _ := s.Person.GetUserInfoData()
- var mobile []string
- var content string
- nameMps := map[string]string{}
- if len(noReportingPc) != 0 {
- content += fmt.Sprintf("一小时内未检测到中控数据<font color=\"warning\">%d台</font>:", len(noReportingPc))
- for _, pc := range noReportingPc {
- content += "\n"
- name := computersF[pc]
- content += name + " : " + pc
- if nameMps[name] == "" {
- nameMps[name] = pc
- } else {
- nameMps[name] += ","
- nameMps[name] += pc
- }
- }
- }
- c := "# 未检测到电脑数据"
- c += "\n"
- if len(nameMps) != 0 {
- for name, ct := range nameMps {
- if _, ok := mpsPerson[name]; ok {
- mobile = append(mobile, mpsPerson[name].MobilePhoneNumber)
- }
- ct = fmt.Sprintf("<font color=\"warning\">%s</font>", ct)
- ct = c + ct
- //go s.SendContent(ct, mpsPerson[name].Url)
- }
- }
- if content != "" {
- var sendMsg SendMsg
- sendMsg.MsgType = "markdown"
- sendMsg.Markdown.Content = content
- //c ,_ := json.Marshal(sendMsg)
- url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
- //url := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b"
- _, err = s.SendMsgData(url, sendMsg)
- if len(mobile) != 0 {
- var sendTextData SendTextMsg
- sendTextData.MsgType = "text"
- sendTextData.Text.MentionedMobileList = mobile
- _, _ = s.SendMsgData(url, sendTextData)
- }
- }
- }
- type SendTextMsg struct {
- MsgType string `json:"msgtype"`
- Text struct {
- MentionedMobileList []string `json:"mentioned_mobile_list"`
- } `json:"text"`
- }
- type SendMsg struct {
- MsgType string `json:"msgtype"`
- Markdown struct {
- Content string `json:"content"`
- } `json:"markdown"`
- }
- func (s *ServiceStatisticsLog) SendContent(content, url string) {
- var sendMsg SendMsg
- sendMsg.MsgType = "markdown"
- sendMsg.Markdown.Content = content
- _, _ = s.SendMsgData(url, sendMsg)
- }
- func (s *ServiceStatisticsLog) SendMsgData(url string, params interface{}) (result []byte, err error) {
- result, err = utils.HttpPost(url, params)
- return
- }
- var statusMps = map[int]string{
- -1: "未上报",
- 1: "中控上报",
- 2: "任务上报",
- }
- func (exa *ServiceStatisticsLog) ParseInfoList2Excel(infoList []*response.ComputerUseLogReply, filePath string) error {
- excel := excelize.NewFile()
- excel.SetSheetRow("Sheet1", "A1", &[]string{"电脑编号", "使用者", "日期", "拉取账号", "进入主线", "主线成功率", "使用状态", "备注"})
- for i, statisticsLog := range infoList {
- axis := fmt.Sprintf("A%d", i+2)
- var r interface{}
- if statisticsLog.EnterMainTotal != 0 && statisticsLog.PullAccountTotal != 0 {
- r = fmt.Sprintf("%.2f", float64(statisticsLog.EnterMainTotal)/float64(statisticsLog.PullAccountTotal)*100)
- } else {
- r = "0.00"
- }
- excel.SetSheetRow("Sheet1", axis, &[]interface{}{
- statisticsLog.PcCode,
- statisticsLog.Operator,
- statisticsLog.CreateDate[:10],
- statisticsLog.PullAccountTotal,
- statisticsLog.EnterMainTotal,
- r,
- statusMps[statisticsLog.Status],
- statisticsLog.Remarks,
- })
- }
- err := excel.SaveAs(filePath)
- return err
- }
- // 删除两天前的缓存数据
- func (s *ServiceStatisticsLog) RegularDelCheckData() {
- ctx := context.Background()
- date := time.Now().Add(-time.Hour * 48).Format("2006-01-02")
- s.LogicalLog.DelHashUuidKey(ctx, date)
- s.LogicalLog.DelZSetKey(ctx, date)
- s.LogicalLog.DelHashKey(ctx, date)
- }
- // @author: [piexlmax](https://github.com/piexlmax)
- // @function: UpdatePc
- // @description: 根据id更新pc
- // @param: Computer log.Computer
- // @return: err error
- func (a *ServiceStatisticsLog) UpdateComputerUseLog(c log.ComputerUseRemarks) (err error) {
- if !errors.Is(global.GVA_DB.Where("create_date = ?", c.CreateDate).Where("pc_code = ?", c.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
- var updateInfo = make(map[string]interface{})
- updateInfo["remarks"] = c.Remarks
- updateInfo["remarks_update_time"] = time.Now().Format("2006-01-02 15:04:05")
- err = global.GVA_DB.Table("log_computer").Where("create_date = ?", c.CreateDate).Where("pc_code = ?", c.PcCode).Updates(updateInfo).Error
- }
- return
- }
- // 统计上报的订单
- //@function: GetScanningInfoList
- //@description: 分页获取数据,
- //@param: card card.Card, info request.PageInfo, order string, desc bool
- //@return: list interface{}, total int64, err error
- func (apiService *ServiceStatisticsLog) GetScanningInfoList(api request.LogScanningRequest, 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.LogScanningCode{})
- var apiList []log.LogScanningCode
- if api.CreateDate == "" {
- api.CreateDate = time.Now().Format("2006-01-02")
- }
- db = db.Where("create_date = ?", api.CreateDate)
- if api.GameId != 0 {
- db = db.Where("game_id = ?", api.GameId)
- }
- if api.Status != 0 {
- db = db.Where("status = ?", api.Status)
- }
- if api.TaskType != 0 {
- if api.TaskType == -1 {
- db = db.Where("task_type = ?", 0)
- } else {
- db = db.Where("task_type = ?", api.TaskType)
- }
- }
- 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, 4)
- orderMap["id"] = true
- orderMap["create_date"] = true
- orderMap["game_id"] = true
- orderMap["status"] = true
- if orderMap[order] {
- if desc {
- OrderStr = order + " desc"
- } else {
- OrderStr = order
- }
- } else { // didn't matched any order key in `orderMap`
- err = fmt.Errorf("非法的排序字段: %v", order)
- return apiList, total, err
- }
- err = db.Order(OrderStr).Find(&apiList).Error
- } else {
- err = db.Order("id desc").Find(&apiList).Error
- }
- }
- var apisReply []response.LogScanningReply
- if len(apiList) != 0 {
- for _, apiInfo := range apiList {
- var apiReply = response.LogScanningReply{}
- apiReply.Status = apiInfo.Status
- apiReply.TaskType = apiInfo.TaskType
- apiReply.GameId = apiInfo.GameId
- apiReply.Supplier = apiInfo.Supplier
- apiReply.OrderNum = apiInfo.OrderNum
- apiReply.CreateDate = apiInfo.CreateTime.Format("2006-01-02")
- apiReply.CreateTime = apiInfo.CreateTime.Format("2006-01-02 15:04:05")
- apisReply = append(apisReply, apiReply)
- }
- }
- return apisReply, total, err
- }
- func (exa *ServiceStatisticsLog) ComputeRateList2Excel(infoList []*response.ComputerStatisticsReply1, filePath string) error {
- excel := excelize.NewFile()
- excel.SetSheetRow("Sheet1", "A1", &[]string{"电脑编号", "使用者", "日期", "游戏id", "目标数量", "拉取账号", "进入主线", "主线成功", "半小时付费", "任务完成效率", "空闲时间"})
- for i, statisticsLog := range infoList {
- axis := fmt.Sprintf("A%d", i+2)
- excel.SetSheetRow("Sheet1", axis, &[]interface{}{
- statisticsLog.PcCode,
- statisticsLog.Operator,
- statisticsLog.CreateDate[:10],
- statisticsLog.GameId,
- statisticsLog.TargetNum,
- statisticsLog.PullAccountNum,
- statisticsLog.EnterMain,
- statisticsLog.TaskSuccessNum,
- statisticsLog.ComputerFeeRate,
- statisticsLog.ComputerHourAverageRate,
- statisticsLog.ComputerFreeTime,
- })
- }
- err := excel.SaveAs(filePath)
- return err
- }
- // 电脑效率七天数据查询
- func (s *ServiceStatisticsLog) ComputerSevenStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) (statisticsLogsComputer []*response.ComputerStatisticsReply1, total int64, err error) {
- date := api.CreateDate
- endDate := ""
- startDate := ""
- if date == "" {
- endDate = time.Now().Add(-time.Hour * 24).Format("2006-01-02")
- startDate = time.Now().Add(-time.Hour * 24 * 8).Format("2006-01-02")
- } else {
- formatTime, _ := time.Parse("2006-01-02", date)
- endDate = date
- startDate = formatTime.Add(-time.Hour * 24 * 7).Format("2006-01-02")
- }
- db := global.GVA_DB.Model(&log.LogComputer{})
- db = db.Where("create_date >= ? and create_date <= ?", startDate, endDate)
- db = db.Where("pc_code = ?", api.PcCode)
- err = db.Count(&total).Error
- if err != nil {
- return nil, 0, err
- }
- limit := info.PageSize
- offset := info.PageSize * (info.Page - 1)
- var statisticsLogs []*log.LogComputer
- db = db.Limit(limit).Offset(offset)
- db.Group("create_date")
- if order != "" {
- var OrderStr string
- // 设置有效排序key 防止sql注入
- // 感谢 Tom4t0 提交漏洞信息
- orderMap := make(map[string]bool, 3)
- orderMap["pc_code"] = true
- orderMap["game_id"] = true
- orderMap["operator"] = 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 statisticsLogsComputer, total, err
- }
- err = db.Order(OrderStr).Find(&statisticsLogs).Error
- } else {
- err = db.Order("id").Find(&statisticsLogs).Error
- }
- if err != nil {
- return nil, 0, err
- }
- for _, statisticsLog := range statisticsLogs {
- statisticsLogComputer := new(response.ComputerStatisticsReply1)
- statisticsLogComputer.PcCode = statisticsLog.PcCode
- statisticsLogComputer.Operator = statisticsLog.Operator
- statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
- statisticsLogComputer.GameId = statisticsLog.GameId
- statisticsLogComputer.PullAccountNum = statisticsLog.PullAccountNum
- statisticsLogComputer.TaskSuccessNum = statisticsLog.TaskSuccessNum
- statisticsLogComputer.TargetNum = statisticsLog.TargetNum
- statisticsLogComputer.ComputerFreeTime = statisticsLog.ComputerFreeTime
- statisticsLogComputer.ComputerFeeRate = statisticsLog.ComputerFeeRate
- statisticsLogComputer.EnterMain = statisticsLog.EnterMain
- statisticsLogComputer.ComputerFeeRate = statisticsLog.ComputerFeeRate
- statisticsLogComputer.ComputerHourAverageRate = statisticsLog.ComputerHourAverageRate
- statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
- }
- return statisticsLogsComputer, total, err
- }
|