log_statistics.go 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. package log
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/go-redis/redis/v8"
  8. "github.com/xuri/excelize/v2"
  9. "go.uber.org/zap"
  10. "gorm.io/gorm"
  11. "log-server/global"
  12. "log-server/model/log"
  13. "log-server/model/log/request"
  14. "log-server/model/log/response"
  15. "log-server/model/task"
  16. "log-server/model/typeManage"
  17. loging2 "log-server/service/log/loging"
  18. "log-server/utils"
  19. "strconv"
  20. "strings"
  21. "time"
  22. )
  23. type ServiceStatisticsLog struct {
  24. loging2.LogicalLog
  25. Person typeManage.ResponsiblePerson
  26. GameTargetComplete task.GameTargetComplete
  27. }
  28. var StatisticsCompletedKey = "%s:StatisticsCompleted"
  29. var taskStatistics = "%s:taskStatistics"
  30. var ComputerNum = "%s:ComputerNum"
  31. var TaskType = []int{0, 1}
  32. func (s *ServiceStatisticsLog) CreateStatisticsLog() {
  33. key := fmt.Sprintf(StatisticsCompletedKey, s.LogicalLog.CurrentDate())
  34. ctx := context.Background()
  35. b, err := s.LogicalLog.ExistsKey(ctx, key)
  36. if err != nil {
  37. global.GVA_LOG.Error("获取redis key失败!"+key, zap.Error(err))
  38. return
  39. }
  40. if b {
  41. global.GVA_LOG.Info("统计数据已完成!"+key, zap.Error(err))
  42. return
  43. }
  44. gameMps, err := s.LogicalLog.GetGameCache(context.Background(), s.YesterdayDate())
  45. if err != nil {
  46. global.GVA_LOG.Error("获取redis game list失败!", zap.Error(err))
  47. return
  48. }
  49. if len(gameMps) == 0 {
  50. global.GVA_LOG.Info("获取redis game list没有数据!")
  51. return
  52. }
  53. var statisticsLogs []*log.StatisticsLog
  54. for gameId, _ := range gameMps {
  55. for _, tt := range TaskType {
  56. gameIdInt, _ := strconv.Atoi(gameId)
  57. statisticsLogNew := new(log.StatisticsLog)
  58. statisticsLog := s.statisticsData(ctx, gameIdInt, tt, s.LogicalLog.YesterdayDate(), statisticsLogNew)
  59. if !errors.Is(global.GVA_DB.Where("create_date = ?", s.LogicalLog.YesterdayDate()).Where("game_id = ?", gameIdInt).First(&log.StatisticsLog{}).Error, gorm.ErrRecordNotFound) {
  60. // 已存在,跳过
  61. err = global.GVA_DB.Where("game_id", gameIdInt).Where("create_date = ?", s.LogicalLog.YesterdayDate()).Where("type = ?", tt).Updates(statisticsLog).Error
  62. if err != nil {
  63. global.GVA_LOG.Error("update StatisticsLogs失败!", zap.Error(err))
  64. continue
  65. }
  66. continue
  67. }
  68. statisticsLogs = append(statisticsLogs, statisticsLog)
  69. }
  70. }
  71. if len(statisticsLogs) == 0 {
  72. global.GVA_LOG.Info("statisticsLogsGameInfo没有数据!", zap.Error(err))
  73. global.GVA_REDIS.Set(ctx, key, 1, 24*time.Hour)
  74. return
  75. }
  76. err = global.GVA_DB.Create(statisticsLogs).Error
  77. if err != nil {
  78. global.GVA_LOG.Error("添加statisticsLogsGameInfo失败!", zap.Error(err))
  79. return
  80. }
  81. global.GVA_REDIS.Set(ctx, key, 1, 24*time.Hour)
  82. return
  83. }
  84. func (s *ServiceStatisticsLog) TodayCreateStatisticsGameInfoLog() {
  85. date := s.LogicalLog.CurrentDate()
  86. ctx := context.Background()
  87. gameMps, err := s.LogicalLog.GetGameCache(context.Background(), date)
  88. if err != nil {
  89. global.GVA_LOG.Error("获取redis game list失败!", zap.Error(err))
  90. return
  91. }
  92. if len(gameMps) == 0 {
  93. global.GVA_LOG.Info("获取redis game list没有数据!")
  94. return
  95. }
  96. completeTaskData, err := s.CompleteTaskData(date)
  97. if err != nil {
  98. global.GVA_LOG.Error("CompleteTaskData get data fail", zap.Error(err))
  99. return
  100. }
  101. var statisticsLogs []*log.StatisticsLog
  102. for gameId, _ := range gameMps {
  103. for _, tt := range TaskType {
  104. gameIdInt, _ := strconv.Atoi(gameId)
  105. statisticsLogNew := new(log.StatisticsLog)
  106. statisticsLog := s.statisticsData(ctx, gameIdInt, tt, date, statisticsLogNew)
  107. if tt == 0 {
  108. statisticsLog.TargetComplete = completeTaskData[gameIdInt].NewComplete + completeTaskData[gameIdInt].HandNewComplete
  109. } else {
  110. statisticsLog.TargetComplete = completeTaskData[gameIdInt].RetainedComplete + completeTaskData[gameIdInt].HandRetainedComplete - completeTaskData[gameIdInt].NewComplete - completeTaskData[gameIdInt].HandNewComplete
  111. }
  112. if !errors.Is(global.GVA_DB.Where("create_date = ?", date).Where("game_id = ?", gameIdInt).First(&log.StatisticsLog{}).Error, gorm.ErrRecordNotFound) {
  113. // 已存在,跳过
  114. err = global.GVA_DB.Where("game_id", gameIdInt).Where("create_date = ?", date).Where("type = ?", tt).Updates(statisticsLog).Error
  115. if err != nil {
  116. global.GVA_LOG.Error("update StatisticsLogs失败!", zap.Error(err))
  117. continue
  118. }
  119. continue
  120. }
  121. statisticsLogs = append(statisticsLogs, statisticsLog)
  122. if len(statisticsLogs) == 100 {
  123. err = global.GVA_DB.Create(statisticsLogs).Error
  124. if err != nil {
  125. global.GVA_LOG.Error("添加statisticsLogs失败!", zap.Error(err))
  126. return
  127. }
  128. statisticsLogs = []*log.StatisticsLog{}
  129. }
  130. }
  131. }
  132. if len(statisticsLogs) == 0 {
  133. global.GVA_LOG.Info("statisticsLogsGameInfo没有数据!", zap.Error(err))
  134. return
  135. }
  136. err = global.GVA_DB.Create(statisticsLogs).Error
  137. if err != nil {
  138. global.GVA_LOG.Error("添加statisticsLogsGameInfo失败!", zap.Error(err))
  139. return
  140. }
  141. return
  142. }
  143. // 获取任务完成数据
  144. func (s *ServiceStatisticsLog) CompleteTaskData(date string) (mps map[int]task.GameTargetComplete, err error) {
  145. db := global.GVA_DB.Table("game_target_complete")
  146. db = db.Where("create_date = ?", date)
  147. var apiList []task.GameTargetComplete
  148. mps = map[int]task.GameTargetComplete{}
  149. err = db.Order("id desc").Find(&apiList).Error
  150. for _, api := range apiList {
  151. mps[api.TaskId] = api
  152. }
  153. return
  154. }
  155. func (s *ServiceStatisticsLog) TodayStatisticsLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo) (interface{}, int64, error) {
  156. db := global.GVA_DB.Model(&log.StatisticsLog{})
  157. if api.GameId != 0 {
  158. db = db.Where("game_id = ?", api.GameId)
  159. }
  160. db = db.Where("create_date = ?", s.CurrentDate())
  161. var total int64
  162. err := db.Count(&total).Error
  163. if err != nil {
  164. return nil, 0, err
  165. }
  166. limit := info.PageSize
  167. offset := info.PageSize * (info.Page - 1)
  168. var statisticsLogs []*log.StatisticsLog
  169. var statisticsLogs2 []*log.StatisticsLog
  170. db = db.Limit(limit).Offset(offset)
  171. err = db.Order("id").Find(&statisticsLogs).Error
  172. for _, gameInfo := range statisticsLogs {
  173. tt := gameInfo.Type
  174. gameIdInt := gameInfo.GameId
  175. statisticsLogNew := new(log.StatisticsLog)
  176. statisticsLog := s.statisticsData(ctx, gameIdInt, tt, s.LogicalLog.CurrentDate(), statisticsLogNew)
  177. statisticsLogs2 = append(statisticsLogs2, statisticsLog)
  178. }
  179. return statisticsLogs2, total, err
  180. }
  181. func (s *ServiceStatisticsLog) statisticsData(ctx context.Context, gameIdInt, tt int, date string, statisticsLog *log.StatisticsLog) *log.StatisticsLog {
  182. key := fmt.Sprintf(taskStatistics, date)
  183. gameIdStr := strconv.Itoa(gameIdInt)
  184. data, err := global.GVA_REDIS.HGet(ctx, key, gameIdStr).Result()
  185. if err != nil {
  186. if err == redis.Nil {
  187. global.GVA_LOG.Info("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  188. } else {
  189. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  190. return nil
  191. }
  192. }
  193. var taskStatistics request.TaskStatistics
  194. _ = json.Unmarshal([]byte(data), &taskStatistics)
  195. pullAccountOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4100000", loging2.OkStatus, tt)
  196. pullAccountFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4100000", loging2.FailStatus, tt)
  197. ConstituencyFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4604000", loging2.FailStatus, tt)
  198. ConstituencyOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4604000", loging2.OkStatus, tt)
  199. StartProxyFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4200000", loging2.FailStatus, tt)
  200. StartProxyOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4200000", loging2.OkStatus, tt)
  201. SimulatorStartFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.FailStatus, tt)
  202. SimulatorStartOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.OkStatus, tt)
  203. NetworkCheckFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4400000", loging2.FailStatus, tt)
  204. NetworkCheckOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4400000", loging2.OkStatus, tt)
  205. GameStartFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4500000", loging2.FailStatus, tt)
  206. GameStartOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4500000", loging2.OkStatus, tt)
  207. xmyLoginFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4601000", loging2.FailStatus, tt)
  208. xmyLoginOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4601000", loging2.OkStatus, tt)
  209. wxLoginFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4602000", loging2.FailStatus, tt)
  210. wxLoginOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4602000", loging2.OkStatus, tt)
  211. mzLoginFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4603000", loging2.FailStatus, tt)
  212. mzLoginOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4603000", loging2.OkStatus, tt)
  213. LoginFail := xmyLoginFail + wxLoginFail + mzLoginFail
  214. LoginOk := xmyLoginOk + wxLoginOk + mzLoginOk
  215. EnterMainFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4700000", loging2.FailStatus, tt)
  216. EnterMainOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4700000", loging2.OkStatus, tt)
  217. FeeFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4800000", loging2.FailStatus, tt)
  218. FeeOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4800000", loging2.OkStatus, tt)
  219. ScriptStartFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.FailStatus, tt)
  220. ScriptStartOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.OkStatus, tt)
  221. EnterGameFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4605000", loging2.FailStatus, tt)
  222. EnterGameOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4605000", loging2.OkStatus, tt)
  223. AuthenticationFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4607000", loging2.FailStatus, tt)
  224. AuthenticationOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4607000", loging2.OkStatus, tt)
  225. EnterStartGame, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4501000", loging2.NoLogStatus, tt)
  226. EnterAuthentication, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4607000", loging2.NoLogStatus, tt)
  227. EnterConstituency, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604000", loging2.NoLogStatus, tt)
  228. EnterGame, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4605000", loging2.NoLogStatus, tt)
  229. EnterMain, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4701000", loging2.NoLogStatus, tt)
  230. EnterFee, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4800000", loging2.NoLogStatus, tt)
  231. EnterXmyLogin, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4601000", loging2.NoLogStatus, tt)
  232. EnterWxLogin, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4602000", loging2.NoLogStatus, tt)
  233. EnterMzLogin, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4603000", loging2.NoLogStatus, tt)
  234. BanOff1, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4601006", loging2.FailStatus, tt) // 小绵羊登录封号
  235. BanOff2, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4602008", loging2.FailStatus, tt) // 微信登录封号
  236. BanOff3, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4603006", loging2.FailStatus, tt) // 魅族登录封号
  237. BanOff4, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4701003", loging2.FailStatus, tt) // 主线封号
  238. Freeze, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4809904", loging2.NoLogStatus, tt) // 冻结
  239. BanOff := BanOff1 + BanOff2 + BanOff3 + BanOff4
  240. EnterLogin := EnterXmyLogin + EnterMzLogin + EnterWxLogin
  241. EnterScanningCode, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4608000", loging2.NoLogStatus, tt)
  242. TranscodingFail, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4608001", loging2.FailStatus, tt)
  243. ThirdPartyFail, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4608002", loging2.FailStatus, tt)
  244. ScanningCodeSuccess, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4608099", loging2.OkStatus, tt)
  245. //statisticsLog.NewHaveRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604004", loging2.NoLogStatus, tt) // 新增有角色
  246. //statisticsLog.RetainedNotRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604003", loging2.NoLogStatus, tt) // 留存无角色
  247. statisticsLog.PullAccountOk = pullAccountOk
  248. statisticsLog.PullAccountFail = pullAccountFail
  249. statisticsLog.StartProxyFail = StartProxyFail
  250. statisticsLog.StartProxyOk = StartProxyOk
  251. statisticsLog.SimulatorStartFail = SimulatorStartFail
  252. statisticsLog.SimulatorStartOk = SimulatorStartOk
  253. statisticsLog.NetworkCheckFail = NetworkCheckFail
  254. statisticsLog.NetworkCheckOk = NetworkCheckOk
  255. statisticsLog.EnterMainFail = EnterMainFail
  256. statisticsLog.EnterMainOk = EnterMainOk
  257. statisticsLog.EnterGameFail = EnterGameFail
  258. statisticsLog.EnterGameOk = EnterGameOk
  259. statisticsLog.GameId = gameIdInt
  260. statisticsLog.ConstituencyFail = ConstituencyFail
  261. statisticsLog.ConstituencyOk = ConstituencyOk
  262. statisticsLog.FeeFail = FeeFail
  263. statisticsLog.FeeOk = FeeOk
  264. statisticsLog.GameStartFail = GameStartFail
  265. statisticsLog.GameStartOk = GameStartOk
  266. statisticsLog.LoginFail = LoginFail
  267. statisticsLog.LoginOk = LoginOk
  268. statisticsLog.XmyLoginFail = xmyLoginFail
  269. statisticsLog.XmyLoginOk = xmyLoginOk
  270. statisticsLog.WxLoginFail = wxLoginFail
  271. statisticsLog.WxLoginOk = wxLoginOk
  272. statisticsLog.MzLoginFail = mzLoginFail
  273. statisticsLog.MzLoginOk = mzLoginOk
  274. statisticsLog.ScriptStartFail = ScriptStartFail
  275. statisticsLog.ScriptStartOk = ScriptStartOk
  276. statisticsLog.AuthenticationFail = AuthenticationFail
  277. statisticsLog.AuthenticationOk = AuthenticationOk
  278. statisticsLog.EnterStartGame = EnterStartGame
  279. statisticsLog.EnterAuthentication = EnterAuthentication
  280. statisticsLog.EnterConstituency = EnterConstituency
  281. statisticsLog.EnterGame = EnterGame
  282. statisticsLog.EnterMain = EnterMain
  283. statisticsLog.EnterFee = EnterFee
  284. statisticsLog.EnterLogin = EnterLogin
  285. statisticsLog.BanOff = BanOff
  286. statisticsLog.Freeze = Freeze
  287. statisticsLog.Type = tt
  288. statisticsLog.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  289. statisticsLog.CreateDate = date
  290. statisticsLog.EnterScanningCode = EnterScanningCode
  291. statisticsLog.ThirdPartyFail = ThirdPartyFail
  292. statisticsLog.TranscodingFail = TranscodingFail
  293. statisticsLog.ScanningCodeSuccess = ScanningCodeSuccess
  294. if tt == 1 {
  295. statisticsLog.NotRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4609901", loging2.NoLogStatus, tt)
  296. statisticsLog.HasRole = 0
  297. statisticsLog.IssuedAccount = taskStatistics.RetainedPullAccount
  298. statisticsLog.TargetNum = taskStatistics.RetainedTarget
  299. statisticsLog.TargetCompleteNum = taskStatistics.RetainedComplete
  300. //statisticsLog.ScanningSuccessRate = 0
  301. } else {
  302. statisticsLog.NotRole = 0
  303. statisticsLog.HasRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4609902", loging2.NoLogStatus, tt)
  304. statisticsLog.IssuedAccount = taskStatistics.NewPullAccount
  305. statisticsLog.TargetCompleteNum = taskStatistics.NewComplete
  306. statisticsLog.TargetNum = taskStatistics.NewTarget
  307. statisticsLog.NewScanningCode = taskStatistics.NewScanningCode
  308. //if statisticsLog.NewScanningCode != 0 {
  309. // statisticsLog.ScanningSuccessRate = float64(statisticsLog.TargetNum) / float64(statisticsLog.NewScanningCode) * 100
  310. //}
  311. }
  312. if statisticsLog.ScanningCodeSuccess != 0 && statisticsLog.EnterScanningCode != 0 {
  313. statisticsLog.ScanningSuccessRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", float64(statisticsLog.ScanningCodeSuccess)/float64(statisticsLog.EnterScanningCode)*100), 64)
  314. }
  315. statisticsLog.PayComplete = taskStatistics.PayComplete
  316. statisticsLog.PayTarget = taskStatistics.PayTarget
  317. statisticsLog.OrderCreate = taskStatistics.OrderCreate
  318. statisticsLog.OrderSuccess = taskStatistics.OrderSuccess
  319. statisticsLog.RetainedAccountNum = taskStatistics.RetainedAccountNum
  320. statisticsLog.FeeAccountNum = taskStatistics.FeeAccountNum
  321. statisticsLog.Operator = taskStatistics.Remark
  322. statisticsLog.GameName = taskStatistics.GameName
  323. if statisticsLog.IssuedAccount != 0 {
  324. statisticsLog.PullSuccessRate = float64(statisticsLog.PullAccountOk) / float64(statisticsLog.IssuedAccount) * 100
  325. }
  326. if statisticsLog.PullAccountOk != 0 {
  327. statisticsLog.StartSuccessRate = float64(statisticsLog.SimulatorStartOk) / float64(statisticsLog.PullAccountOk) * 100
  328. }
  329. if statisticsLog.GameStartOk != 0 {
  330. statisticsLog.MainSuccessRate = float64(statisticsLog.EnterMain) / float64(statisticsLog.GameStartOk) * 100
  331. }
  332. if statisticsLog.TargetNum != 0 {
  333. statisticsLog.TaskSuccessRate = float64(statisticsLog.EnterMain) / float64(statisticsLog.TargetNum) * 100
  334. }
  335. if statisticsLog.OrderCreate != 0 {
  336. statisticsLog.LocalOrderSuccessRate = float64(statisticsLog.FeeOk) / float64(statisticsLog.OrderCreate) * 100
  337. }
  338. return statisticsLog
  339. }
  340. func (s *ServiceStatisticsLog) OtherStatisticsLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo, order string, desc bool, gamePortId int) (interface{}, int64, error) {
  341. date := api.CreateDate
  342. if date == "" {
  343. date = s.CurrentDate()
  344. }
  345. db := global.GVA_DB.Table("log_statistics gtc")
  346. db = db.Joins("left join game_task gt on gt.task_id = gtc.game_id")
  347. if api.GameId != 0 {
  348. db = db.Where("gtc.game_id = ?", api.GameId)
  349. }
  350. db = db.Where("gtc.create_date = ?", date)
  351. if gamePortId != 0 {
  352. db = db.Where("gt.game_port_id = ?", gamePortId)
  353. }
  354. var total int64
  355. err := db.Count(&total).Error
  356. db = db.Select("gtc.*")
  357. if err != nil {
  358. return nil, 0, err
  359. }
  360. limit := info.PageSize
  361. offset := info.PageSize * (info.Page - 1)
  362. var statisticsLogs []*log.StatisticsLog
  363. db = db.Limit(limit).Offset(offset)
  364. if order != "" {
  365. var OrderStr string
  366. // 设置有效排序key 防止sql注入
  367. // 感谢 Tom4t0 提交漏洞信息
  368. orderMap := make(map[string]bool, 7)
  369. orderMap["pull_success_rate"] = true
  370. orderMap["start_success_rate"] = true
  371. orderMap["main_success_rate"] = true
  372. orderMap["task_success_rate"] = true
  373. orderMap["scanning_success_rate"] = true
  374. orderMap["local_order_success_rate"] = true
  375. orderMap["game_id"] = true
  376. if orderMap[order] {
  377. if desc {
  378. OrderStr = "gtc." + order + " desc"
  379. } else {
  380. OrderStr = "gtc." + order
  381. }
  382. } else { // didn't matched any order key in `orderMap`
  383. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  384. return statisticsLogs, total, err
  385. }
  386. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  387. } else {
  388. err = db.Order("gtc.id").Find(&statisticsLogs).Error
  389. }
  390. if err != nil {
  391. return nil, 0, err
  392. }
  393. for _, statisticsLog := range statisticsLogs {
  394. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  395. }
  396. return statisticsLogs, total, err
  397. }
  398. func (s *ServiceStatisticsLog) StatisticsNodeLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  399. date := api.CreateDate
  400. if date == "" || date == s.CurrentDate() {
  401. apilist, total, err := s.TodayStatisticsLogList(ctx, api, info)
  402. return apilist, total, err
  403. }
  404. db := global.GVA_DB.Model(&log.StatisticsLog{})
  405. if api.GameId != 0 {
  406. db = db.Where("game_id = ?", api.GameId)
  407. }
  408. db = db.Where("create_date = ?", date)
  409. var total int64
  410. err := db.Count(&total).Error
  411. if err != nil {
  412. return nil, 0, err
  413. }
  414. limit := info.PageSize
  415. offset := info.PageSize * (info.Page - 1)
  416. var statisticsLogs []*log.StatisticsLog
  417. db = db.Limit(limit).Offset(offset)
  418. err = db.Order("id").Find(&statisticsLogs).Error
  419. if err != nil {
  420. return nil, 0, err
  421. }
  422. for _, statisticsLog := range statisticsLogs {
  423. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  424. }
  425. return statisticsLogs, total, err
  426. }
  427. //func (s *ServiceStatisticsLog) ResetStatisticsLog(ctx context.Context, gameId int, date string) {
  428. // var total int64
  429. // key := date + ":" + strconv.Itoa(gameId) + ":reset"
  430. // global.GVA_REDIS.Set(ctx, key, 1, time.Minute*15)
  431. // db := global.GVA_DB.Table("loging")
  432. // db = db.Where("game_id = ?", gameId)
  433. // err := db.Count(&total).Error
  434. // if err != nil {
  435. // global.GVA_LOG.Error("重置统计数据总数报错", zap.Error(err))
  436. // return
  437. // }
  438. // limit := 100
  439. // num := int(math.Ceil(float64(total) / float64(limit)))
  440. // offset := 0
  441. // err = s.LogicalLog.DelStatisticsNumCache(ctx, date, gameId)
  442. // if err != nil {
  443. // global.GVA_LOG.Error("重置统计数据删除缓存报错", zap.Error(err))
  444. // return
  445. // }
  446. // err = s.LogicalLog.DelUuidCodeCache(ctx, date, gameId)
  447. // if err != nil {
  448. // global.GVA_LOG.Error("重置统计数据删除缓存报错2", zap.Error(err))
  449. // return
  450. // }
  451. // global.GVA_LOG.Info("num = " + strconv.Itoa(num))
  452. // for i := 0; i < num; i++ {
  453. // global.GVA_LOG.Info("offset = " + strconv.Itoa(offset))
  454. // var statisticsLogs []request.AddLogRequest
  455. // db2 := global.GVA_DB.Table("loging")
  456. // db2 = db2.Where("create_date = ?", date)
  457. // db2 = db2.Where("game_id = ?", gameId)
  458. // db2 = db2.Limit(limit).Offset(offset)
  459. // err = db2.Order("id").Find(&statisticsLogs).Error
  460. // for _, statisticsLog := range statisticsLogs {
  461. //
  462. // s.ResetStatisticsCache(ctx, statisticsLog, date)
  463. // }
  464. // offset += limit
  465. // }
  466. // global.GVA_LOG.Info("重置统计数据完成")
  467. //}
  468. //
  469. //func (s *ServiceStatisticsLog) ResetStatisticsCache(c context.Context, api request.AddLogRequest, date string) {
  470. // coding := strconv.Itoa(api.Coding)
  471. // nodeCoding := coding[:3]
  472. // status := coding[5:]
  473. // noLogStatus := coding[3:5]
  474. // var logical ServiceResetLoging
  475. // switch nodeCoding {
  476. // case "410":
  477. // logical = new(loging2.ResetLog)
  478. // break
  479. // case "430":
  480. // logical = new(loging2.ResetLog)
  481. // break
  482. // default:
  483. // logical = new(loging2.ResetOtherLog)
  484. // break
  485. // }
  486. // var err error
  487. // if status == "99" {
  488. // err = logical.SuccessLog(c, api, date)
  489. // } else if noLogStatus == "99" {
  490. // err = logical.NoLogStatusData(c, api, date)
  491. // } else {
  492. // err = logical.FailLog(c, api, date)
  493. // }
  494. // if err != nil {
  495. // global.GVA_LOG.Error("创建失败!", zap.Error(err))
  496. // }
  497. //}
  498. // 每天凌晨reset统计数据
  499. //func (s *ServiceStatisticsLog) EveryDayResetStatisticsCache() {
  500. // date := s.YesterdayDate()
  501. // ctx := context.Background()
  502. // gameMps, err := s.LogicalLog.GetGameCache(ctx, date)
  503. // if err != nil {
  504. // global.GVA_LOG.Error("获取redis game list失败!", zap.Error(err))
  505. // return
  506. // }
  507. // if len(gameMps) == 0 {
  508. // global.GVA_LOG.Info("获取redis game list没有数据!")
  509. // return
  510. // }
  511. // for gameId, _ := range gameMps {
  512. // id, _ := strconv.Atoi(gameId)
  513. // s.ResetStatisticsLog(ctx, id, date)
  514. // }
  515. // global.GVA_LOG.Info("游戏重置缓存完成!")
  516. //}
  517. // 添加和更新电脑指标数据
  518. func (s *ServiceStatisticsLog) CreateComputerStatisticsData() {
  519. ctx := context.Background()
  520. codeMps, err := s.LogicalLog.GetComputerCache(ctx, s.LogicalLog.CurrentDate())
  521. if err != nil {
  522. return
  523. }
  524. if len(codeMps) == 0 {
  525. return
  526. }
  527. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  528. onlineComputerMpa, _ := s.LogicalLog.GetOnlineComputerNumCache(ctx, s.LogicalLog.CurrentDate())
  529. var computer log.Computer
  530. computerData, _ := computer.OnlinePcCodeCache()
  531. if computerData == nil {
  532. global.GVA_LOG.Warn("电脑缓存数据获取失败TaskStatisticsDataCache")
  533. return
  534. }
  535. var csReplys []*log.LogComputer
  536. for code, _ := range codeMps {
  537. // 不统计不在电脑列表里的
  538. if _, ok := computerData[code]; !ok {
  539. continue
  540. }
  541. // 负责人为空的不统计
  542. if computerData[code] == "" {
  543. continue
  544. }
  545. accountMps, err := s.LogicalLog.GetComputerPullAccountNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  546. if err != nil {
  547. continue
  548. }
  549. taskMps, err := s.LogicalLog.GetComputerTaskSuccessNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  550. if err != nil {
  551. continue
  552. }
  553. enterMainMps, err := s.LogicalLog.GetComputerEnterMainNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  554. if err != nil {
  555. continue
  556. }
  557. operator := computerData[code]
  558. delete(computerData, code)
  559. for gameId, num := range accountMps {
  560. csReply := new(log.LogComputer)
  561. csReply.PcCode = code
  562. if _, ok := onlineComputerMpa[csReply.PcCode]; ok {
  563. delete(onlineComputerMpa, csReply.PcCode)
  564. }
  565. csReply.Operator = operator
  566. data, err := global.GVA_REDIS.HGet(ctx, key, gameId).Result()
  567. if err != nil {
  568. if err == redis.Nil {
  569. global.GVA_LOG.Info("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  570. } else {
  571. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  572. return
  573. }
  574. }
  575. var taskStatistics request.TaskStatistics
  576. _ = json.Unmarshal([]byte(data), &taskStatistics)
  577. csReply.GameId, _ = strconv.Atoi(gameId)
  578. csReply.PullAccountNum = num
  579. csReply.TaskSuccessNum = taskMps[gameId]
  580. csReply.EnterMain = enterMainMps[gameId]
  581. csReply.CreateDate = time.Now().Format("2006-01-02")
  582. csReply.TargetNum = taskStatistics.NewTarget + taskStatistics.RetainedTarget
  583. csReply.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, csReply.PcCode, csReply.GameId)
  584. csReply.ComputerFreeTime = s.GetStatisticsComputerRate(ctx, csReply.PcCode)
  585. csReply.GameFeeRate = s.GetStatisticsFeeRate(ctx, csReply.GameId)
  586. runTime := time.Now().Hour() + 1 - csReply.ComputerFreeTime
  587. csReply.ComputerHourAverageRate = csReply.TaskSuccessNum / runTime
  588. csReply.Status = 2
  589. global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Where("game_id = ?", 0).Delete(&log.LogComputer{})
  590. 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) {
  591. // 已存在,更新
  592. mp := make(map[string]interface{})
  593. if csReply.Operator != "" {
  594. mp["operator"] = csReply.Operator
  595. }
  596. mp["target_num"] = taskStatistics.NewTarget + taskStatistics.RetainedTarget
  597. mp["pull_account_num"] = num
  598. mp["task_success_num"] = taskMps[gameId]
  599. mp["computer_fee_rate"] = csReply.ComputerFeeRate
  600. mp["computer_free_time"] = csReply.ComputerFreeTime
  601. mp["game_fee_rate"] = csReply.GameFeeRate
  602. mp["computer_hour_average_rate"] = csReply.ComputerHourAverageRate
  603. mp["enter_main"] = csReply.EnterMain
  604. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Where("game_id = ?", gameId).Updates(mp).Error
  605. if err != nil {
  606. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  607. }
  608. continue
  609. }
  610. csReplys = append(csReplys, csReply)
  611. }
  612. }
  613. if len(csReplys) != 0 {
  614. err = global.GVA_DB.Table("log_computer").Create(csReplys).Error
  615. if err != nil {
  616. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  617. }
  618. }
  619. // 在线电脑没有跑数据记录
  620. if len(onlineComputerMpa) != 0 {
  621. var onlineComputer []*log.LogComputer
  622. for pcCode, op := range onlineComputerMpa {
  623. delete(computerData, pcCode)
  624. csReply := new(log.LogComputer)
  625. csReply.PcCode = pcCode
  626. csReply.Operator = op
  627. csReply.CreateDate = time.Now().Format("2006-01-02")
  628. csReply.Status = 1
  629. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  630. 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) {
  631. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Update("status", 1).Error
  632. if err != nil {
  633. global.GVA_LOG.Error("更新数据失败onlineComputerMpa", zap.Error(err))
  634. }
  635. }
  636. continue
  637. }
  638. onlineComputer = append(onlineComputer, csReply)
  639. }
  640. if len(onlineComputer) != 0 {
  641. err = global.GVA_DB.Table("log_computer").Create(onlineComputer).Error
  642. if err != nil {
  643. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  644. }
  645. }
  646. }
  647. // 没记录电脑数据
  648. if len(computerData) != 0 {
  649. var onlineComputer []*log.LogComputer
  650. for pcCode, op := range computerData {
  651. csReply := new(log.LogComputer)
  652. csReply.PcCode = pcCode
  653. csReply.Operator = op
  654. csReply.CreateDate = time.Now().Format("2006-01-02")
  655. csReply.Status = -1
  656. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  657. continue
  658. }
  659. onlineComputer = append(onlineComputer, csReply)
  660. }
  661. if len(onlineComputer) != 0 {
  662. err = global.GVA_DB.Table("log_computer").Create(onlineComputer).Error
  663. if err != nil {
  664. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  665. }
  666. }
  667. }
  668. return
  669. }
  670. func (s *ServiceStatisticsLog) ComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) (statisticsLogsComputer []*response.ComputerStatisticsReply1, total int64, err error) {
  671. date := api.CreateDate
  672. isCurrentDate := false
  673. if date == "" {
  674. date = s.CurrentDate()
  675. isCurrentDate = true
  676. }
  677. db := global.GVA_DB.Model(&log.LogComputer{})
  678. db = db.Where("create_date = ?", date)
  679. if api.Operator != "" {
  680. db = db.Where("operator = ?", api.Operator)
  681. }
  682. if api.PcCode != "" {
  683. db = db.Where("pc_code = ?", api.PcCode)
  684. }
  685. if api.GameId != 0 {
  686. db = db.Where("game_id = ?", api.GameId)
  687. }
  688. err = db.Count(&total).Error
  689. if err != nil {
  690. return nil, 0, err
  691. }
  692. limit := info.PageSize
  693. offset := info.PageSize * (info.Page - 1)
  694. var statisticsLogs []*log.LogComputer
  695. db = db.Limit(limit).Offset(offset)
  696. if order != "" {
  697. var OrderStr string
  698. // 设置有效排序key 防止sql注入
  699. // 感谢 Tom4t0 提交漏洞信息
  700. orderMap := make(map[string]bool, 3)
  701. orderMap["pc_code"] = true
  702. orderMap["game_id"] = true
  703. orderMap["operator"] = true
  704. if orderMap[order] {
  705. if desc {
  706. OrderStr = order + " desc"
  707. } else {
  708. OrderStr = order
  709. }
  710. } else { // didn't matched any order key in `orderMap`
  711. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  712. return statisticsLogsComputer, total, err
  713. }
  714. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  715. } else {
  716. err = db.Order("id").Find(&statisticsLogs).Error
  717. }
  718. if err != nil {
  719. return nil, 0, err
  720. }
  721. for _, statisticsLog := range statisticsLogs {
  722. /*var gameInfo []*response.GameInfo
  723. err = json.Unmarshal([]byte(statisticsLog.GameInfo), &gameInfo)
  724. for i, _ := range gameInfo {
  725. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  726. statisticsLogComputer.GameInfo = gameInfo
  727. statisticsLogComputer.PcCode = statisticsLog.PcCode
  728. statisticsLogComputer.Operator = statisticsLog.Operator
  729. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  730. statisticsLogComputer.GameId = gameInfo[i].GameId
  731. statisticsLogComputer.PullAccountNum = gameInfo[i].PullAccountNum
  732. statisticsLogComputer.TaskSuccessNum = gameInfo[i].TaskSuccessNum
  733. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  734. }*/
  735. //gameIdStr:=strconv.Itoa(statisticsLog.GameId)
  736. //taskStatistics := s.GameTargetInfo(ctx,s.LogicalLog.CurrentDate(),gameIdStr)
  737. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  738. statisticsLogComputer.PcCode = statisticsLog.PcCode
  739. statisticsLogComputer.Operator = statisticsLog.Operator
  740. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  741. statisticsLogComputer.GameId = statisticsLog.GameId
  742. statisticsLogComputer.PullAccountNum = statisticsLog.PullAccountNum
  743. statisticsLogComputer.TaskSuccessNum = statisticsLog.TaskSuccessNum
  744. statisticsLogComputer.TargetNum = statisticsLog.TargetNum
  745. statisticsLogComputer.ComputerFreeTime = statisticsLog.ComputerFreeTime
  746. statisticsLogComputer.ComputerFeeRate = statisticsLog.ComputerFeeRate
  747. statisticsLogComputer.EnterMain = statisticsLog.EnterMain
  748. if isCurrentDate {
  749. statisticsLogComputer.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, statisticsLog.PcCode, statisticsLog.GameId)
  750. }
  751. statisticsLogComputer.ComputerHourAverageRate = statisticsLog.ComputerHourAverageRate
  752. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  753. }
  754. return statisticsLogsComputer, total, err
  755. }
  756. // OnlineComputerStatistics 在线电脑
  757. func (s *ServiceStatisticsLog) OnlineComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) ([]*response.ComputerUseLogReply, int64, error) {
  758. date := api.CreateDate
  759. if date == "" {
  760. date = s.CurrentDate()
  761. }
  762. db := global.GVA_DB.Model(&log.LogComputer{})
  763. var total int64
  764. db = db.Select("SUM(pull_account_num) pull_account_total,SUM(enter_main) enter_main_total,operator,pc_code,status,create_date,remarks")
  765. db = db.Where("create_date = ?", date)
  766. if api.Operator != "" {
  767. db = db.Where("operator = ?", api.Operator)
  768. }
  769. if api.PcCode != "" {
  770. db = db.Where("pc_code = ?", api.PcCode)
  771. }
  772. if api.Status != 0 {
  773. db = db.Where("status = ?", api.Status)
  774. }
  775. db = db.Group("pc_code")
  776. err := db.Count(&total).Error
  777. if err != nil {
  778. return nil, 0, err
  779. }
  780. limit := info.PageSize
  781. offset := info.PageSize * (info.Page - 1)
  782. var statisticsLogs []*response.ComputerUseLogReply
  783. db = db.Limit(limit).Offset(offset)
  784. if order != "" {
  785. var OrderStr string
  786. // 设置有效排序key 防止sql注入
  787. // 感谢 Tom4t0 提交漏洞信息
  788. orderMap := make(map[string]bool, 3)
  789. orderMap["pc_code"] = true
  790. orderMap["operator"] = true
  791. if orderMap[order] {
  792. if desc {
  793. OrderStr = order + " desc"
  794. } else {
  795. OrderStr = order
  796. }
  797. } else { // didn't matched any order key in `orderMap`
  798. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  799. return statisticsLogs, total, err
  800. }
  801. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  802. } else {
  803. err = db.Order("id").Find(&statisticsLogs).Error
  804. }
  805. if err != nil {
  806. return nil, 0, err
  807. }
  808. for _, statisticsLog := range statisticsLogs {
  809. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  810. }
  811. return statisticsLogs, total, err
  812. }
  813. // GameStatistics 根据游戏id查询数据
  814. func (s *ServiceStatisticsLog) GameStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo) (interface{}, int64, error) {
  815. date := api.CreateDate
  816. isCurrentDate := false
  817. if date == "" {
  818. date = s.CurrentDate()
  819. isCurrentDate = true
  820. }
  821. db := global.GVA_DB.Model(&log.LogComputer{})
  822. var total int64
  823. db = db.Where("create_date = ?", date)
  824. if api.Operator != "" {
  825. db = db.Where("operator = ?", api.Operator)
  826. }
  827. if api.PcCode != "" {
  828. db = db.Where("pc_code = ?", api.PcCode)
  829. }
  830. if api.GameId != 0 {
  831. db = db.Where("game_id = ?", api.GameId)
  832. }
  833. 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")
  834. err := db.Count(&total).Error
  835. if err != nil {
  836. return nil, 0, err
  837. }
  838. limit := info.PageSize
  839. offset := info.PageSize * (info.Page - 1)
  840. var statisticsLogsByGameId []*response.GameIdStatisticsReply
  841. db = db.Limit(limit).Offset(offset)
  842. err = db.Order("id").Find(&statisticsLogsByGameId).Error
  843. if err != nil {
  844. return nil, 0, err
  845. }
  846. for _, statisticsLog := range statisticsLogsByGameId {
  847. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  848. statisticsLog.OneComputerAverageNum = statisticsLog.TaskSuccessTotal / statisticsLog.PcCodeTotal
  849. if isCurrentDate {
  850. statisticsLog.GameFeeRate = s.GetStatisticsFeeRate(ctx, statisticsLog.GameId)
  851. }
  852. }
  853. return statisticsLogsByGameId, total, err
  854. }
  855. func (s *ServiceStatisticsLog) GameTargetInfo(ctx context.Context, date string, gameId string) (taskStatistics1 request.TaskStatistics) {
  856. key := fmt.Sprintf(taskStatistics, date)
  857. data, err := global.GVA_REDIS.HGet(ctx, key, gameId).Result()
  858. if err != nil {
  859. if err == redis.Nil {
  860. global.GVA_LOG.Info("TaskStatisticsDataCache"+key+gameId, zap.Error(err))
  861. } else {
  862. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache"+key, zap.Error(err))
  863. return
  864. }
  865. }
  866. _ = json.Unmarshal([]byte(data), &taskStatistics1)
  867. return
  868. }
  869. // TaskStatisticsDataCache 同步群控任务数据到缓存
  870. func (s *ServiceStatisticsLog) TaskStatisticsDataCache() {
  871. ctx := context.Background()
  872. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  873. data, err := s.LogicalLog.RequestJfRoom()
  874. if err != nil {
  875. global.GVA_LOG.Error("获取机房数据失败TaskStatisticsDataCache", zap.Error(err))
  876. return
  877. }
  878. dataTask, err := s.LogicalLog.RequestTaskData()
  879. if err != nil {
  880. global.GVA_LOG.Error("RequestTaskData", zap.Error(err))
  881. return
  882. }
  883. var taskData []request.TaskData
  884. var taskStatistics []request.TaskStatistics
  885. _ = json.Unmarshal(data, &taskStatistics)
  886. _ = json.Unmarshal(dataTask, &taskData)
  887. mps := map[int]request.TaskData{}
  888. for _, td := range taskData {
  889. mps[td.GameId] = td
  890. }
  891. for _, data := range taskStatistics {
  892. var id int
  893. id = data.GameId
  894. if _, ok := mps[id]; ok {
  895. data.NewScanningCode = mps[id].NewScanningCode
  896. data.RetainedPullAccount = mps[id].RetainedPullAccount
  897. data.NewPullAccount = mps[id].NewPullAccount
  898. data.FeeAccountNum = mps[id].FeeAccountNum
  899. data.RetainedAccountNum = mps[id].RetainedAccountNum
  900. }
  901. bd, _ := json.Marshal(data)
  902. err = global.GVA_REDIS.HSet(ctx, key, id, bd).Err()
  903. if err != nil {
  904. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  905. return
  906. }
  907. }
  908. }
  909. // TaskStatisticsDataCache1 同步群控任务数据到缓存
  910. func (s *ServiceStatisticsLog) TaskStatisticsDataCache1() {
  911. ctx := context.Background()
  912. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  913. data, err := s.LogicalLog.RequestJfRoom()
  914. if err != nil {
  915. global.GVA_LOG.Error("获取机房数据失败TaskStatisticsDataCache", zap.Error(err))
  916. return
  917. }
  918. dataTask, err := s.LogicalLog.RequestTaskData()
  919. if err != nil {
  920. global.GVA_LOG.Error("RequestTaskData", zap.Error(err))
  921. return
  922. }
  923. var taskData []request.TaskData
  924. var taskStatistics []request.TaskStatistics
  925. _ = json.Unmarshal(data, &taskStatistics)
  926. _ = json.Unmarshal(dataTask, &taskData)
  927. mps := map[int]request.TaskData{}
  928. for _, td := range taskData {
  929. mps[td.GameId] = td
  930. }
  931. for _, data := range taskStatistics {
  932. var id int
  933. if data.GameIdXmy != "" {
  934. if data.GameIdXmy == "0" {
  935. id = data.GameId
  936. } else {
  937. id, _ = strconv.Atoi(data.GameIdXmy)
  938. }
  939. } else {
  940. id = data.GameId
  941. }
  942. if _, ok := mps[id]; ok {
  943. data.NewScanningCode = mps[id].NewScanningCode
  944. data.RetainedPullAccount = mps[id].RetainedPullAccount
  945. data.NewPullAccount = mps[id].NewPullAccount
  946. data.FeeAccountNum = mps[id].FeeAccountNum
  947. data.RetainedAccountNum = mps[id].RetainedAccountNum
  948. }
  949. bd, _ := json.Marshal(data)
  950. err = global.GVA_REDIS.HSet(ctx, key, id, bd).Err()
  951. if err != nil {
  952. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  953. return
  954. }
  955. }
  956. }
  957. // GetComputerNum 同步群控任务数据到缓存
  958. func (s *ServiceStatisticsLog) GetComputerNum(date string) int64 {
  959. var total int64
  960. if date == "" {
  961. date = s.CurrentDate()
  962. }
  963. db := global.GVA_DB.Model(&log.LogComputer{})
  964. db = db.Distinct("pc_code").Where("create_date = ?", date)
  965. _ = db.Count(&total).Error
  966. return total
  967. }
  968. // ComputerHeartbeat 在线电脑接口
  969. func (s *ServiceStatisticsLog) ComputerHeartbeat(c context.Context, onlineComputer request.OnlineComputerRequest) error {
  970. err := s.LogicalLog.SetOnlineComputerNumCache(c, s.LogicalLog.CurrentDate(), onlineComputer.PcCode, onlineComputer.Operator)
  971. if err != nil {
  972. return err
  973. }
  974. s.LogicalLog.SetPcReportingLog(c, onlineComputer.PcCode, onlineComputer.Operator)
  975. return err
  976. }
  977. // ComputerTest 在线电脑接口测试
  978. func (s *ServiceStatisticsLog) ComputerTest(c context.Context) (interface{}, error) {
  979. mps, err := s.LogicalLog.GetOnlineComputerNumCache(c, s.LogicalLog.CurrentDate())
  980. if err != nil {
  981. return mps, err
  982. }
  983. return mps, err
  984. }
  985. // 定时检查电脑情况
  986. func (s *ServiceStatisticsLog) RegularCheckPc() {
  987. ctx := context.Background()
  988. var computer log.Computer
  989. computers, err := computer.OnlinePcCodeCache()
  990. //computersF, err := computer.OnlinePcCodeCache()
  991. if err != nil {
  992. global.GVA_LOG.Error("获取租机表数据失败", zap.Error(err))
  993. return
  994. }
  995. if computers == nil {
  996. global.GVA_LOG.Warn("电脑缓存数据获取失败TaskStatisticsDataCache")
  997. return
  998. }
  999. // 查询两小时内上报的数据
  1000. var computersF = map[string]string{}
  1001. var noReportingPc []string
  1002. for pc, name := range computers {
  1003. computersF[pc] = name
  1004. if strings.Contains(name, "备用") {
  1005. delete(computers, pc)
  1006. continue
  1007. }
  1008. if name == "" {
  1009. delete(computers, pc)
  1010. continue
  1011. }
  1012. num := s.LogicalLog.GetPcReportingLog(ctx, pc)
  1013. delete(computers, pc)
  1014. if num == 0 {
  1015. noReportingPc = append(noReportingPc, pc)
  1016. }
  1017. }
  1018. mpsPerson, _ := s.Person.GetUserInfoData()
  1019. var mobile []string
  1020. var content string
  1021. nameMps := map[string]string{}
  1022. if len(noReportingPc) != 0 {
  1023. content += fmt.Sprintf("一小时内未检测到中控数据<font color=\"warning\">%d台</font>:", len(noReportingPc))
  1024. for _, pc := range noReportingPc {
  1025. content += "\n"
  1026. name := computersF[pc]
  1027. content += name + " : " + pc
  1028. if nameMps[name] == "" {
  1029. nameMps[name] = pc
  1030. } else {
  1031. nameMps[name] += ","
  1032. nameMps[name] += pc
  1033. }
  1034. }
  1035. }
  1036. c := "# 未检测到电脑数据"
  1037. c += "\n"
  1038. if len(nameMps) != 0 {
  1039. for name, ct := range nameMps {
  1040. if _, ok := mpsPerson[name]; ok {
  1041. mobile = append(mobile, mpsPerson[name].MobilePhoneNumber)
  1042. }
  1043. ct = fmt.Sprintf("<font color=\"warning\">%s</font>", ct)
  1044. ct = c + ct
  1045. go s.SendContent(ct, mpsPerson[name].Url)
  1046. }
  1047. }
  1048. if content != "" {
  1049. var sendMsg SendMsg
  1050. sendMsg.MsgType = "markdown"
  1051. sendMsg.Markdown.Content = content
  1052. //c ,_ := json.Marshal(sendMsg)
  1053. url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
  1054. //url := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b"
  1055. _, err = s.SendMsgData(url, sendMsg)
  1056. if len(mobile) != 0 {
  1057. var sendTextData SendTextMsg
  1058. sendTextData.MsgType = "text"
  1059. sendTextData.Text.MentionedMobileList = mobile
  1060. _, _ = s.SendMsgData(url, sendTextData)
  1061. }
  1062. }
  1063. }
  1064. type SendTextMsg struct {
  1065. MsgType string `json:"msgtype"`
  1066. Text struct {
  1067. MentionedMobileList []string `json:"mentioned_mobile_list"`
  1068. } `json:"text"`
  1069. }
  1070. type SendMsg struct {
  1071. MsgType string `json:"msgtype"`
  1072. Markdown struct {
  1073. Content string `json:"content"`
  1074. } `json:"markdown"`
  1075. }
  1076. func (s *ServiceStatisticsLog) SendContent(content, url string) {
  1077. var sendMsg SendMsg
  1078. sendMsg.MsgType = "markdown"
  1079. sendMsg.Markdown.Content = content
  1080. _, _ = s.SendMsgData(url, sendMsg)
  1081. }
  1082. func (s *ServiceStatisticsLog) SendMsgData(url string, params interface{}) (result []byte, err error) {
  1083. result, err = utils.HttpPost(url, params)
  1084. return
  1085. }
  1086. var statusMps = map[int]string{
  1087. -1: "未上报",
  1088. 1: "中控上报",
  1089. 2: "任务上报",
  1090. }
  1091. func (exa *ServiceStatisticsLog) ParseInfoList2Excel(infoList []*response.ComputerUseLogReply, filePath string) error {
  1092. excel := excelize.NewFile()
  1093. excel.SetSheetRow("Sheet1", "A1", &[]string{"电脑编号", "使用者", "日期", "拉取账号", "进入主线", "主线成功率", "使用状态", "备注"})
  1094. for i, statisticsLog := range infoList {
  1095. axis := fmt.Sprintf("A%d", i+2)
  1096. var r interface{}
  1097. if statisticsLog.EnterMainTotal != 0 && statisticsLog.PullAccountTotal != 0 {
  1098. r = fmt.Sprintf("%.2f", float64(statisticsLog.EnterMainTotal)/float64(statisticsLog.PullAccountTotal)*100)
  1099. } else {
  1100. r = "0.00"
  1101. }
  1102. excel.SetSheetRow("Sheet1", axis, &[]interface{}{
  1103. statisticsLog.PcCode,
  1104. statisticsLog.Operator,
  1105. statisticsLog.CreateDate[:10],
  1106. statisticsLog.PullAccountTotal,
  1107. statisticsLog.EnterMainTotal,
  1108. r,
  1109. statusMps[statisticsLog.Status],
  1110. statisticsLog.Remarks,
  1111. })
  1112. }
  1113. err := excel.SaveAs(filePath)
  1114. return err
  1115. }
  1116. // 删除两天前的缓存数据
  1117. func (s *ServiceStatisticsLog) RegularDelCheckData() {
  1118. ctx := context.Background()
  1119. date := time.Now().Add(-time.Hour * 48).Format("2006-01-02")
  1120. s.LogicalLog.DelHashUuidKey(ctx, date)
  1121. s.LogicalLog.DelZSetKey(ctx, date)
  1122. s.LogicalLog.DelHashKey(ctx, date)
  1123. s.DelListKey(ctx, date)
  1124. }
  1125. // 删除list缓存数据
  1126. func (s *ServiceStatisticsLog) DelListKey(ctx context.Context, date string) {
  1127. key := fmt.Sprintf(failCacheKey, date)
  1128. global.GVA_REDIS.Del(ctx, key)
  1129. }
  1130. // @author: [piexlmax](https://github.com/piexlmax)
  1131. // @function: UpdatePc
  1132. // @description: 根据id更新pc
  1133. // @param: Computer log.Computer
  1134. // @return: err error
  1135. func (a *ServiceStatisticsLog) UpdateComputerUseLog(c log.ComputerUseRemarks) (err error) {
  1136. if !errors.Is(global.GVA_DB.Where("create_date = ?", c.CreateDate).Where("pc_code = ?", c.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  1137. var updateInfo = make(map[string]interface{})
  1138. updateInfo["remarks"] = c.Remarks
  1139. updateInfo["remarks_update_time"] = time.Now().Format("2006-01-02 15:04:05")
  1140. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", c.CreateDate).Where("pc_code = ?", c.PcCode).Updates(updateInfo).Error
  1141. }
  1142. return
  1143. }
  1144. // 统计上报的订单
  1145. //@function: GetScanningInfoList
  1146. //@description: 分页获取数据,
  1147. //@param: card card.Card, info request.PageInfo, order string, desc bool
  1148. //@return: list interface{}, total int64, err error
  1149. func (apiService *ServiceStatisticsLog) GetScanningInfoList(api request.LogScanningRequest, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
  1150. limit := info.PageSize
  1151. offset := info.PageSize * (info.Page - 1)
  1152. db := global.GVA_DB.Model(&log.LogScanningCode{})
  1153. var apiList []log.LogScanningCode
  1154. if api.CreateDate == "" {
  1155. api.CreateDate = time.Now().Format("2006-01-02")
  1156. }
  1157. db = db.Where("create_date = ?", api.CreateDate)
  1158. if api.GameId != 0 {
  1159. db = db.Where("game_id = ?", api.GameId)
  1160. }
  1161. if api.Status != 0 {
  1162. db = db.Where("status = ?", api.Status)
  1163. }
  1164. if api.TaskType != 0 {
  1165. if api.TaskType == -1 {
  1166. db = db.Where("task_type = ?", 0)
  1167. } else {
  1168. db = db.Where("task_type = ?", api.TaskType)
  1169. }
  1170. }
  1171. err = db.Count(&total).Error
  1172. if err != nil {
  1173. return apiList, total, err
  1174. } else {
  1175. db = db.Limit(limit).Offset(offset)
  1176. if order != "" {
  1177. var OrderStr string
  1178. // 设置有效排序key 防止sql注入
  1179. // 感谢 Tom4t0 提交漏洞信息
  1180. orderMap := make(map[string]bool, 4)
  1181. orderMap["id"] = true
  1182. orderMap["create_date"] = true
  1183. orderMap["game_id"] = true
  1184. orderMap["status"] = true
  1185. if orderMap[order] {
  1186. if desc {
  1187. OrderStr = order + " desc"
  1188. } else {
  1189. OrderStr = order
  1190. }
  1191. } else { // didn't matched any order key in `orderMap`
  1192. err = fmt.Errorf("非法的排序字段: %v", order)
  1193. return apiList, total, err
  1194. }
  1195. err = db.Order(OrderStr).Find(&apiList).Error
  1196. } else {
  1197. err = db.Order("id desc").Find(&apiList).Error
  1198. }
  1199. }
  1200. var apisReply []response.LogScanningReply
  1201. if len(apiList) != 0 {
  1202. for _, apiInfo := range apiList {
  1203. var apiReply = response.LogScanningReply{}
  1204. apiReply.Status = apiInfo.Status
  1205. apiReply.TaskType = apiInfo.TaskType
  1206. apiReply.GameId = apiInfo.GameId
  1207. apiReply.Supplier = apiInfo.Supplier
  1208. apiReply.OrderNum = apiInfo.OrderNum
  1209. apiReply.CreateDate = apiInfo.CreateTime.Format("2006-01-02")
  1210. apiReply.CreateTime = apiInfo.CreateTime.Format("2006-01-02 15:04:05")
  1211. apisReply = append(apisReply, apiReply)
  1212. }
  1213. }
  1214. return apisReply, total, err
  1215. }
  1216. func (exa *ServiceStatisticsLog) ComputeRateList2Excel(infoList []*response.ComputerStatisticsReply1, filePath string) error {
  1217. excel := excelize.NewFile()
  1218. excel.SetSheetRow("Sheet1", "A1", &[]string{"电脑编号", "使用者", "日期", "游戏id", "目标数量", "拉取账号", "进入主线", "主线成功", "半小时付费", "任务完成效率", "空闲时间"})
  1219. for i, statisticsLog := range infoList {
  1220. axis := fmt.Sprintf("A%d", i+2)
  1221. excel.SetSheetRow("Sheet1", axis, &[]interface{}{
  1222. statisticsLog.PcCode,
  1223. statisticsLog.Operator,
  1224. statisticsLog.CreateDate[:10],
  1225. statisticsLog.GameId,
  1226. statisticsLog.TargetNum,
  1227. statisticsLog.PullAccountNum,
  1228. statisticsLog.EnterMain,
  1229. statisticsLog.TaskSuccessNum,
  1230. statisticsLog.ComputerFeeRate,
  1231. statisticsLog.ComputerHourAverageRate,
  1232. statisticsLog.ComputerFreeTime,
  1233. })
  1234. }
  1235. err := excel.SaveAs(filePath)
  1236. return err
  1237. }
  1238. // 电脑效率七天数据查询
  1239. func (s *ServiceStatisticsLog) ComputerSevenStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) (statisticsLogsComputer []*response.ComputerStatisticsReply1, total int64, err error) {
  1240. date := api.CreateDate
  1241. endDate := ""
  1242. startDate := ""
  1243. if date == "" {
  1244. endDate = time.Now().Add(-time.Hour * 24).Format("2006-01-02")
  1245. startDate = time.Now().Add(-time.Hour * 24 * 8).Format("2006-01-02")
  1246. } else {
  1247. formatTime, _ := time.Parse("2006-01-02", date)
  1248. endDate = date
  1249. startDate = formatTime.Add(-time.Hour * 24 * 7).Format("2006-01-02")
  1250. }
  1251. db := global.GVA_DB.Model(&log.LogComputer{})
  1252. db = db.Where("create_date >= ? and create_date <= ?", startDate, endDate)
  1253. db = db.Where("pc_code = ?", api.PcCode)
  1254. err = db.Count(&total).Error
  1255. if err != nil {
  1256. return nil, 0, err
  1257. }
  1258. limit := info.PageSize
  1259. offset := info.PageSize * (info.Page - 1)
  1260. var statisticsLogs []*log.LogComputer
  1261. db = db.Limit(limit).Offset(offset)
  1262. db.Group("create_date")
  1263. if order != "" {
  1264. var OrderStr string
  1265. // 设置有效排序key 防止sql注入
  1266. // 感谢 Tom4t0 提交漏洞信息
  1267. orderMap := make(map[string]bool, 3)
  1268. orderMap["pc_code"] = true
  1269. orderMap["game_id"] = true
  1270. orderMap["operator"] = true
  1271. if orderMap[order] {
  1272. if desc {
  1273. OrderStr = order + " desc"
  1274. } else {
  1275. OrderStr = order
  1276. }
  1277. } else { // didn't matched any order key in `orderMap`
  1278. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  1279. return statisticsLogsComputer, total, err
  1280. }
  1281. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  1282. } else {
  1283. err = db.Order("id").Find(&statisticsLogs).Error
  1284. }
  1285. if err != nil {
  1286. return nil, 0, err
  1287. }
  1288. for _, statisticsLog := range statisticsLogs {
  1289. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  1290. statisticsLogComputer.PcCode = statisticsLog.PcCode
  1291. statisticsLogComputer.Operator = statisticsLog.Operator
  1292. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  1293. statisticsLogComputer.GameId = statisticsLog.GameId
  1294. statisticsLogComputer.PullAccountNum = statisticsLog.PullAccountNum
  1295. statisticsLogComputer.TaskSuccessNum = statisticsLog.TaskSuccessNum
  1296. statisticsLogComputer.TargetNum = statisticsLog.TargetNum
  1297. statisticsLogComputer.ComputerFreeTime = statisticsLog.ComputerFreeTime
  1298. statisticsLogComputer.ComputerFeeRate = statisticsLog.ComputerFeeRate
  1299. statisticsLogComputer.EnterMain = statisticsLog.EnterMain
  1300. statisticsLogComputer.ComputerFeeRate = statisticsLog.ComputerFeeRate
  1301. statisticsLogComputer.ComputerHourAverageRate = statisticsLog.ComputerHourAverageRate
  1302. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  1303. }
  1304. return statisticsLogsComputer, total, err
  1305. }
  1306. // 定时统计设备信息
  1307. func (s *ServiceStatisticsLog) DeviceStatistics() {
  1308. var devices []log.DeviceLog
  1309. date := time.Now().Format("2006-01-02")
  1310. db := global.GVA_DB.Model(&log.DeviceLog{})
  1311. db.Where("create_date = ?", date)
  1312. db.Where("is_err = ?", 1)
  1313. db.Group("game_id")
  1314. db.Select("game_id")
  1315. db.Find(&devices)
  1316. if len(devices) == 0 {
  1317. return
  1318. }
  1319. completeData, _ := s.GameTargetComplete.CompleteTaskData(date)
  1320. ctx := context.Background()
  1321. for _, game := range devices {
  1322. deviceLog := new(log.DeviceStatistics)
  1323. deviceLog.GameId = game.GameId
  1324. gameDeviceAccountErrKey := fmt.Sprintf(loging2.GameDeviceAccountErrKey, date, game.GameId)
  1325. gameDeviceErrKey := fmt.Sprintf(loging2.GameDeviceErrKey, date, game.GameId)
  1326. deviceLog.AccountErrNum, _ = s.GetCacheNum(ctx, gameDeviceAccountErrKey)
  1327. deviceLog.DeviceErrNum, _ = s.GetCacheNum(ctx, gameDeviceErrKey)
  1328. n, _ := s.LogicalLog.NodeLogGetNum(ctx, date, game.GameId, "4300000", loging2.OkStatus, 0)
  1329. r, _ := s.LogicalLog.NodeLogGetNum(ctx, date, game.GameId, "4300000", loging2.OkStatus, 1)
  1330. deviceLog.SimulatorStartNum = n + r
  1331. deviceLog.NewComplete = completeData[game.GameId].NewComplete
  1332. deviceLog.RetainedComplete = completeData[game.GameId].RetainedComplete
  1333. if deviceLog.DeviceErrNum != 0 {
  1334. deviceLog.DeviceErrRate = utils.Decimal(float64(deviceLog.DeviceErrNum) / float64(deviceLog.RetainedComplete) * 100)
  1335. }
  1336. if deviceLog.AccountErrNum != 0 {
  1337. deviceLog.AccountErrRate = utils.Decimal(float64(deviceLog.AccountErrNum) / float64(deviceLog.NewComplete) * 100)
  1338. }
  1339. deviceLog.CreateDate = date
  1340. if !errors.Is(global.GVA_DB.Where("create_date = ?", date).Where("game_id = ?", game.GameId).First(&log.DeviceStatistics{}).Error, gorm.ErrRecordNotFound) {
  1341. global.GVA_DB.Where("create_date = ?", date).Where("game_id = ?", game.GameId).Omit("create_date,game_id").Updates(deviceLog)
  1342. continue
  1343. }
  1344. global.GVA_DB.Save(deviceLog)
  1345. }
  1346. }
  1347. func (s *ServiceStatisticsLog) GetDeviceStatistics(ctx context.Context, api log.DeviceStatistics, info request.PageInfo, order string, desc bool) (statistics []*log.DeviceStatistics, total int64, err error) {
  1348. if api.CreateDate == "" {
  1349. api.CreateDate = time.Now().Format("2006-01-02")
  1350. }
  1351. db := global.GVA_DB.Model(&log.DeviceStatistics{})
  1352. db = db.Where("create_date = ?", api.CreateDate)
  1353. if api.GameId != 0 {
  1354. db = db.Where("game_id = ?", api.GameId)
  1355. }
  1356. err = db.Count(&total).Error
  1357. if err != nil {
  1358. return nil, 0, err
  1359. }
  1360. limit := info.PageSize
  1361. offset := info.PageSize * (info.Page - 1)
  1362. db = db.Limit(limit).Offset(offset)
  1363. if order != "" {
  1364. var OrderStr string
  1365. orderMap := make(map[string]bool, 3)
  1366. orderMap["device_err_num"] = true
  1367. orderMap["game_id"] = true
  1368. orderMap["account_err_num"] = true
  1369. if orderMap[order] {
  1370. if desc {
  1371. OrderStr = order + " desc"
  1372. } else {
  1373. OrderStr = order
  1374. }
  1375. } else { // didn't matched any order key in `orderMap`
  1376. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  1377. return statistics, total, err
  1378. }
  1379. err = db.Order(OrderStr).Find(&statistics).Error
  1380. } else {
  1381. err = db.Order("id").Find(&statistics).Error
  1382. }
  1383. for _, statistic := range statistics {
  1384. statistic.CreateDate = api.CreateDate
  1385. }
  1386. return
  1387. }
  1388. func (s *ServiceStatisticsLog) GetDeviceInfo(ctx context.Context, api log.DeviceLog, info request.PageInfo, order string, desc bool) (deviceLogs []*log.DeviceLog, total int64, err error) {
  1389. if api.CreateDate == "" {
  1390. api.CreateDate = time.Now().Format("2006-01-02")
  1391. }
  1392. db := global.GVA_DB.Model(&log.DeviceLog{})
  1393. if api.GameId != 0 {
  1394. db = db.Where("game_id = ?", api.GameId)
  1395. } else {
  1396. return
  1397. }
  1398. db = db.Where("create_date = ?", api.CreateDate)
  1399. if api.IsErr != 0 {
  1400. db = db.Where("is_err = ?", api.IsErr)
  1401. }
  1402. err = db.Count(&total).Error
  1403. if err != nil {
  1404. return nil, 0, err
  1405. }
  1406. limit := info.PageSize
  1407. offset := info.PageSize * (info.Page - 1)
  1408. db = db.Limit(limit).Offset(offset)
  1409. if order != "" {
  1410. var OrderStr string
  1411. // 设置有效排序key 防止sql注入
  1412. // 感谢 Tom4t0 提交漏洞信息
  1413. orderMap := make(map[string]bool, 3)
  1414. orderMap["game_id"] = true
  1415. if orderMap[order] {
  1416. if desc {
  1417. OrderStr = order + " desc"
  1418. } else {
  1419. OrderStr = order
  1420. }
  1421. } else { // didn't matched any order key in `orderMap`
  1422. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  1423. return deviceLogs, total, err
  1424. }
  1425. err = db.Order(OrderStr).Find(&deviceLogs).Error
  1426. } else {
  1427. err = db.Order("id").Find(&deviceLogs).Error
  1428. }
  1429. for _, statistic := range deviceLogs {
  1430. statistic.CreateDate = api.CreateDate
  1431. }
  1432. return
  1433. }
  1434. func (s *ServiceStatisticsLog) GetDeviceContrastInfo(ctx context.Context, api log.DeviceLog) (deviceLogs []log.DeviceLog, err error) {
  1435. db := global.GVA_DB.Model(&log.DeviceLog{})
  1436. var device log.DeviceLog
  1437. err = db.Where("id = ?", api.Id).First(&device).Error
  1438. if err != nil {
  1439. return nil, err
  1440. }
  1441. if device.ErrStatus == 3 {
  1442. err = global.GVA_DB.Where("game_id = ?", device.GameId).Where("account = ?", device.Account).Where("id < ?", device.Id).Order("id desc").First(&deviceLogs).Error
  1443. if err != nil {
  1444. return nil, err
  1445. }
  1446. } else {
  1447. err = global.GVA_DB.Where("create_date = ?", device.CreateDate).Where("game_id = ?", device.GameId).Where("device_hex = ?", device.DeviceHex).Where("id < ?", device.Id).Order("id desc").Limit(10).Find(&deviceLogs).Error
  1448. }
  1449. deviceLogs = append(deviceLogs, device)
  1450. for _, statistic := range deviceLogs {
  1451. statistic.CreateDate = api.CreateDate
  1452. }
  1453. return
  1454. }