log_statistics.go 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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. loging2 "log-server/service/log/loging"
  16. "log-server/utils"
  17. "math"
  18. "strconv"
  19. "time"
  20. )
  21. type ServiceStatisticsLog struct {
  22. loging2.LogicalLog
  23. }
  24. var StatisticsCompletedKey = "%s:StatisticsCompleted"
  25. var taskStatistics = "%s:taskStatistics"
  26. var ComputerNum = "%s:ComputerNum"
  27. var TaskType = []int{0, 1}
  28. func (s *ServiceStatisticsLog) CreateStatisticsLog() {
  29. key := fmt.Sprintf(StatisticsCompletedKey, s.LogicalLog.CurrentDate())
  30. ctx := context.Background()
  31. b, err := s.LogicalLog.ExistsKey(ctx, key)
  32. if err != nil {
  33. global.GVA_LOG.Error("获取redis key失败!"+key, zap.Error(err))
  34. return
  35. }
  36. if b {
  37. global.GVA_LOG.Info("统计数据已完成!"+key, zap.Error(err))
  38. return
  39. }
  40. gameMps, err := s.LogicalLog.GetGameCache(context.Background(), s.YesterdayDate())
  41. if err != nil {
  42. global.GVA_LOG.Error("获取redis game list失败!", zap.Error(err))
  43. return
  44. }
  45. if len(gameMps) == 0 {
  46. global.GVA_LOG.Info("获取redis game list没有数据!")
  47. return
  48. }
  49. var statisticsLogs []*log.StatisticsLog
  50. for gameId, _ := range gameMps {
  51. for _, tt := range TaskType {
  52. gameIdInt, _ := strconv.Atoi(gameId)
  53. statisticsLogNew := new(log.StatisticsLog)
  54. statisticsLog := s.statisticsData(ctx, gameIdInt, tt, s.LogicalLog.YesterdayDate(), statisticsLogNew)
  55. if !errors.Is(global.GVA_DB.Where("create_date = ?", s.LogicalLog.YesterdayDate()).Where("game_id = ?", gameIdInt).First(&log.StatisticsLog{}).Error, gorm.ErrRecordNotFound) {
  56. // 已存在,跳过
  57. err = global.GVA_DB.Where("game_id", gameIdInt).Where("create_date = ?", s.LogicalLog.YesterdayDate()).Where("type = ?", tt).Updates(statisticsLog).Error
  58. if err != nil {
  59. global.GVA_LOG.Error("update StatisticsLogs失败!", zap.Error(err))
  60. continue
  61. }
  62. continue
  63. }
  64. statisticsLogs = append(statisticsLogs, statisticsLog)
  65. }
  66. }
  67. if len(statisticsLogs) == 0 {
  68. global.GVA_LOG.Info("statisticsLogsGameInfo没有数据!", zap.Error(err))
  69. global.GVA_REDIS.Set(ctx, key, 1, 24*time.Hour)
  70. return
  71. }
  72. err = global.GVA_DB.Create(statisticsLogs).Error
  73. if err != nil {
  74. global.GVA_LOG.Error("添加statisticsLogsGameInfo失败!", zap.Error(err))
  75. return
  76. }
  77. global.GVA_REDIS.Set(ctx, key, 1, 24*time.Hour)
  78. return
  79. }
  80. func (s *ServiceStatisticsLog) TodayCreateStatisticsGameInfoLog() {
  81. date := s.LogicalLog.CurrentDate()
  82. ctx := context.Background()
  83. gameMps, err := s.LogicalLog.GetGameCache(context.Background(), date)
  84. if err != nil {
  85. global.GVA_LOG.Error("获取redis game list失败!", zap.Error(err))
  86. return
  87. }
  88. if len(gameMps) == 0 {
  89. global.GVA_LOG.Info("获取redis game list没有数据!")
  90. return
  91. }
  92. var statisticsLogs []*log.StatisticsLog
  93. for gameId, _ := range gameMps {
  94. for _, tt := range TaskType {
  95. gameIdInt, _ := strconv.Atoi(gameId)
  96. statisticsLogNew := new(log.StatisticsLog)
  97. statisticsLog := s.statisticsData(ctx, gameIdInt, tt, date, statisticsLogNew)
  98. if !errors.Is(global.GVA_DB.Where("create_date = ?", date).Where("game_id = ?", gameIdInt).First(&log.StatisticsLog{}).Error, gorm.ErrRecordNotFound) {
  99. // 已存在,跳过
  100. err = global.GVA_DB.Where("game_id", gameIdInt).Where("create_date = ?", date).Where("type = ?", tt).Updates(statisticsLog).Error
  101. if err != nil {
  102. global.GVA_LOG.Error("update StatisticsLogs失败!", zap.Error(err))
  103. continue
  104. }
  105. continue
  106. }
  107. statisticsLogs = append(statisticsLogs, statisticsLog)
  108. if len(statisticsLogs) == 100 {
  109. err = global.GVA_DB.Create(statisticsLogs).Error
  110. if err != nil {
  111. global.GVA_LOG.Error("添加statisticsLogs失败!", zap.Error(err))
  112. return
  113. }
  114. statisticsLogs = []*log.StatisticsLog{}
  115. }
  116. }
  117. }
  118. if len(statisticsLogs) == 0 {
  119. global.GVA_LOG.Info("statisticsLogsGameInfo没有数据!", zap.Error(err))
  120. return
  121. }
  122. err = global.GVA_DB.Create(statisticsLogs).Error
  123. if err != nil {
  124. global.GVA_LOG.Error("添加statisticsLogsGameInfo失败!", zap.Error(err))
  125. return
  126. }
  127. return
  128. }
  129. func (s *ServiceStatisticsLog) TodayStatisticsLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo) (interface{}, int64, error) {
  130. db := global.GVA_DB.Model(&log.StatisticsLog{})
  131. if api.GameId != 0 {
  132. db = db.Where("game_id = ?", api.GameId)
  133. }
  134. db = db.Where("create_date = ?", s.CurrentDate())
  135. var total int64
  136. err := db.Count(&total).Error
  137. if err != nil {
  138. return nil, 0, err
  139. }
  140. limit := info.PageSize
  141. offset := info.PageSize * (info.Page - 1)
  142. var statisticsLogs []*log.StatisticsLog
  143. var statisticsLogs2 []*log.StatisticsLog
  144. db = db.Limit(limit).Offset(offset)
  145. err = db.Order("id").Find(&statisticsLogs).Error
  146. for _, gameInfo := range statisticsLogs {
  147. tt := gameInfo.Type
  148. gameIdInt := gameInfo.GameId
  149. statisticsLogNew := new(log.StatisticsLog)
  150. statisticsLog := s.statisticsData(ctx, gameIdInt, tt, s.LogicalLog.CurrentDate(), statisticsLogNew)
  151. statisticsLogs2 = append(statisticsLogs2, statisticsLog)
  152. }
  153. return statisticsLogs2, total, err
  154. }
  155. func (s *ServiceStatisticsLog) statisticsData(ctx context.Context, gameIdInt, tt int, date string, statisticsLog *log.StatisticsLog) *log.StatisticsLog {
  156. key := fmt.Sprintf(taskStatistics, date)
  157. gameIdStr := strconv.Itoa(gameIdInt)
  158. data, err := global.GVA_REDIS.HGet(ctx, key, gameIdStr).Result()
  159. if err != nil {
  160. if err == redis.Nil {
  161. global.GVA_LOG.Info("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  162. } else {
  163. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  164. return nil
  165. }
  166. }
  167. var taskStatistics request.TaskStatistics
  168. _ = json.Unmarshal([]byte(data), &taskStatistics)
  169. pullAccountOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4100000", loging2.OkStatus, tt)
  170. pullAccountFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4100000", loging2.FailStatus, tt)
  171. ConstituencyFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4604000", loging2.FailStatus, tt)
  172. ConstituencyOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4604000", loging2.OkStatus, tt)
  173. StartProxyFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4200000", loging2.FailStatus, tt)
  174. StartProxyOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4200000", loging2.OkStatus, tt)
  175. SimulatorStartFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.FailStatus, tt)
  176. SimulatorStartOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.OkStatus, tt)
  177. NetworkCheckFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4400000", loging2.FailStatus, tt)
  178. NetworkCheckOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4400000", loging2.OkStatus, tt)
  179. GameStartFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4500000", loging2.FailStatus, tt)
  180. GameStartOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4500000", loging2.OkStatus, tt)
  181. xmyLoginFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4601000", loging2.FailStatus, tt)
  182. xmyLoginOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4601000", loging2.OkStatus, tt)
  183. wxLoginFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4602000", loging2.FailStatus, tt)
  184. wxLoginOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4602000", loging2.OkStatus, tt)
  185. mzLoginFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4603000", loging2.FailStatus, tt)
  186. mzLoginOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4603000", loging2.OkStatus, tt)
  187. LoginFail := xmyLoginFail + wxLoginFail + mzLoginFail
  188. LoginOk := xmyLoginOk + wxLoginOk + mzLoginOk
  189. EnterMainFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4700000", loging2.FailStatus, tt)
  190. EnterMainOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4700000", loging2.OkStatus, tt)
  191. FeeFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4800000", loging2.FailStatus, tt)
  192. FeeOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4800000", loging2.OkStatus, tt)
  193. ScriptStartFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.FailStatus, tt)
  194. ScriptStartOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.OkStatus, tt)
  195. EnterGameFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4605000", loging2.FailStatus, tt)
  196. EnterGameOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4605000", loging2.OkStatus, tt)
  197. AuthenticationFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4607000", loging2.FailStatus, tt)
  198. AuthenticationOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4607000", loging2.OkStatus, tt)
  199. EnterStartGame, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4501000", loging2.NoLogStatus, tt)
  200. EnterAuthentication, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4607000", loging2.NoLogStatus, tt)
  201. EnterConstituency, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604000", loging2.NoLogStatus, tt)
  202. EnterGame, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4605000", loging2.NoLogStatus, tt)
  203. EnterMain, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4701000", loging2.NoLogStatus, tt)
  204. EnterFee, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4800000", loging2.NoLogStatus, tt)
  205. EnterXmyLogin, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4601000", loging2.NoLogStatus, tt)
  206. EnterWxLogin, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4602000", loging2.NoLogStatus, tt)
  207. EnterMzLogin, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4603000", loging2.NoLogStatus, tt)
  208. BanOff1, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4601006", loging2.FailStatus, tt) // 小绵羊登录封号
  209. BanOff2, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4602008", loging2.FailStatus, tt) // 微信登录封号
  210. BanOff3, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4603006", loging2.FailStatus, tt) // 魅族登录封号
  211. BanOff4, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4701003", loging2.FailStatus, tt) // 主线封号
  212. Freeze, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4809904", loging2.NoLogStatus, tt) // 冻结
  213. BanOff := BanOff1 + BanOff2 + BanOff3 + BanOff4
  214. EnterLogin := EnterXmyLogin + EnterMzLogin + EnterWxLogin
  215. //statisticsLog.NewHaveRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604004", loging2.NoLogStatus, tt) // 新增有角色
  216. //statisticsLog.RetainedNotRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604003", loging2.NoLogStatus, tt) // 留存无角色
  217. statisticsLog.PullAccountOk = pullAccountOk
  218. statisticsLog.PullAccountFail = pullAccountFail
  219. statisticsLog.StartProxyFail = StartProxyFail
  220. statisticsLog.StartProxyOk = StartProxyOk
  221. statisticsLog.SimulatorStartFail = SimulatorStartFail
  222. statisticsLog.SimulatorStartOk = SimulatorStartOk
  223. statisticsLog.NetworkCheckFail = NetworkCheckFail
  224. statisticsLog.NetworkCheckOk = NetworkCheckOk
  225. statisticsLog.EnterMainFail = EnterMainFail
  226. statisticsLog.EnterMainOk = EnterMainOk
  227. statisticsLog.EnterGameFail = EnterGameFail
  228. statisticsLog.EnterGameOk = EnterGameOk
  229. statisticsLog.GameId = gameIdInt
  230. statisticsLog.ConstituencyFail = ConstituencyFail
  231. statisticsLog.ConstituencyOk = ConstituencyOk
  232. statisticsLog.FeeFail = FeeFail
  233. statisticsLog.FeeOk = FeeOk
  234. statisticsLog.GameStartFail = GameStartFail
  235. statisticsLog.GameStartOk = GameStartOk
  236. statisticsLog.LoginFail = LoginFail
  237. statisticsLog.LoginOk = LoginOk
  238. statisticsLog.XmyLoginFail = xmyLoginFail
  239. statisticsLog.XmyLoginOk = xmyLoginOk
  240. statisticsLog.WxLoginFail = wxLoginFail
  241. statisticsLog.WxLoginOk = wxLoginOk
  242. statisticsLog.MzLoginFail = mzLoginFail
  243. statisticsLog.MzLoginOk = mzLoginOk
  244. statisticsLog.ScriptStartFail = ScriptStartFail
  245. statisticsLog.ScriptStartOk = ScriptStartOk
  246. statisticsLog.AuthenticationFail = AuthenticationFail
  247. statisticsLog.AuthenticationOk = AuthenticationOk
  248. statisticsLog.EnterStartGame = EnterStartGame
  249. statisticsLog.EnterAuthentication = EnterAuthentication
  250. statisticsLog.EnterConstituency = EnterConstituency
  251. statisticsLog.EnterGame = EnterGame
  252. statisticsLog.EnterMain = EnterMain
  253. statisticsLog.EnterFee = EnterFee
  254. statisticsLog.EnterLogin = EnterLogin
  255. statisticsLog.BanOff = BanOff
  256. statisticsLog.Freeze = Freeze
  257. statisticsLog.Type = tt
  258. statisticsLog.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  259. statisticsLog.CreateDate = date
  260. if tt == 1 {
  261. statisticsLog.NotRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4609901", loging2.NoLogStatus, tt)
  262. statisticsLog.HasRole = 0
  263. statisticsLog.IssuedAccount = taskStatistics.RetainedPullAccount
  264. statisticsLog.TargetNum = taskStatistics.RetainedTarget
  265. statisticsLog.TargetCompleteNum = taskStatistics.RetainedComplete
  266. statisticsLog.ScanningSuccessRate = 0
  267. } else {
  268. statisticsLog.NotRole = 0
  269. statisticsLog.HasRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4609902", loging2.NoLogStatus, tt)
  270. statisticsLog.IssuedAccount = taskStatistics.NewPullAccount
  271. statisticsLog.TargetCompleteNum = taskStatistics.NewComplete
  272. statisticsLog.TargetNum = taskStatistics.NewTarget
  273. statisticsLog.NewScanningCode = taskStatistics.NewScanningCode
  274. if statisticsLog.NewScanningCode != 0 {
  275. statisticsLog.ScanningSuccessRate = float64(statisticsLog.TargetNum) / float64(statisticsLog.NewScanningCode) * 100
  276. }
  277. }
  278. statisticsLog.PayComplete = taskStatistics.PayComplete
  279. statisticsLog.PayTarget = taskStatistics.PayTarget
  280. statisticsLog.OrderCreate = taskStatistics.OrderCreate
  281. statisticsLog.OrderSuccess = taskStatistics.OrderSuccess
  282. statisticsLog.RetainedAccountNum = taskStatistics.RetainedAccountNum
  283. statisticsLog.FeeAccountNum = taskStatistics.FeeAccountNum
  284. statisticsLog.Operator = taskStatistics.Remark
  285. statisticsLog.GameName = taskStatistics.GameName
  286. if statisticsLog.IssuedAccount != 0 {
  287. statisticsLog.PullSuccessRate = float64(statisticsLog.PullAccountOk) / float64(statisticsLog.IssuedAccount) * 100
  288. }
  289. if statisticsLog.PullAccountOk != 0 {
  290. statisticsLog.StartSuccessRate = float64(statisticsLog.SimulatorStartOk) / float64(statisticsLog.PullAccountOk) * 100
  291. }
  292. if statisticsLog.GameStartOk != 0 {
  293. statisticsLog.MainSuccessRate = float64(statisticsLog.EnterMain) / float64(statisticsLog.GameStartOk) * 100
  294. }
  295. if statisticsLog.TargetNum != 0 {
  296. statisticsLog.TaskSuccessRate = float64(statisticsLog.EnterMain) / float64(statisticsLog.TargetNum) * 100
  297. }
  298. if statisticsLog.OrderCreate != 0 {
  299. statisticsLog.LocalOrderSuccessRate = float64(statisticsLog.FeeOk) / float64(statisticsLog.OrderCreate) * 100
  300. }
  301. return statisticsLog
  302. }
  303. func (s *ServiceStatisticsLog) OtherStatisticsLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  304. date := api.CreateDate
  305. if date == "" {
  306. date = s.CurrentDate()
  307. }
  308. db := global.GVA_DB.Model(&log.StatisticsLog{})
  309. if api.GameId != 0 {
  310. db = db.Where("game_id = ?", api.GameId)
  311. }
  312. db = db.Where("create_date = ?", date)
  313. var total int64
  314. err := db.Count(&total).Error
  315. if err != nil {
  316. return nil, 0, err
  317. }
  318. limit := info.PageSize
  319. offset := info.PageSize * (info.Page - 1)
  320. var statisticsLogs []*log.StatisticsLog
  321. db = db.Limit(limit).Offset(offset)
  322. if order != "" {
  323. var OrderStr string
  324. // 设置有效排序key 防止sql注入
  325. // 感谢 Tom4t0 提交漏洞信息
  326. orderMap := make(map[string]bool, 7)
  327. orderMap["pull_success_rate"] = true
  328. orderMap["start_success_rate"] = true
  329. orderMap["main_success_rate"] = true
  330. orderMap["task_success_rate"] = true
  331. orderMap["scanning_success_rate"] = true
  332. orderMap["local_order_success_rate"] = true
  333. orderMap["game_id"] = true
  334. if orderMap[order] {
  335. if desc {
  336. OrderStr = order + " desc"
  337. } else {
  338. OrderStr = order
  339. }
  340. } else { // didn't matched any order key in `orderMap`
  341. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  342. return statisticsLogs, total, err
  343. }
  344. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  345. } else {
  346. err = db.Order("id").Find(&statisticsLogs).Error
  347. }
  348. if err != nil {
  349. return nil, 0, err
  350. }
  351. for _, statisticsLog := range statisticsLogs {
  352. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  353. }
  354. return statisticsLogs, total, err
  355. }
  356. func (s *ServiceStatisticsLog) StatisticsNodeLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  357. date := api.CreateDate
  358. if date == "" || date == s.CurrentDate() {
  359. apilist, total, err := s.TodayStatisticsLogList(ctx, api, info)
  360. return apilist, total, err
  361. }
  362. db := global.GVA_DB.Model(&log.StatisticsLog{})
  363. if api.GameId != 0 {
  364. db = db.Where("game_id = ?", api.GameId)
  365. }
  366. db = db.Where("create_date = ?", date)
  367. var total int64
  368. err := db.Count(&total).Error
  369. if err != nil {
  370. return nil, 0, err
  371. }
  372. limit := info.PageSize
  373. offset := info.PageSize * (info.Page - 1)
  374. var statisticsLogs []*log.StatisticsLog
  375. db = db.Limit(limit).Offset(offset)
  376. err = db.Order("id").Find(&statisticsLogs).Error
  377. if err != nil {
  378. return nil, 0, err
  379. }
  380. for _, statisticsLog := range statisticsLogs {
  381. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  382. }
  383. return statisticsLogs, total, err
  384. }
  385. func (s *ServiceStatisticsLog) ResetStatisticsLog(ctx context.Context, gameId int, date string) {
  386. var total int64
  387. key := date + ":" + strconv.Itoa(gameId) + ":reset"
  388. global.GVA_REDIS.Set(ctx, key, 1, time.Minute*15)
  389. db := global.GVA_DB.Table("loging")
  390. db = db.Where("game_id = ?", gameId)
  391. err := db.Count(&total).Error
  392. if err != nil {
  393. global.GVA_LOG.Error("重置统计数据总数报错", zap.Error(err))
  394. return
  395. }
  396. limit := 100
  397. num := int(math.Ceil(float64(total) / float64(limit)))
  398. offset := 0
  399. err = s.LogicalLog.DelStatisticsNumCache(ctx, date, gameId)
  400. if err != nil {
  401. global.GVA_LOG.Error("重置统计数据删除缓存报错", zap.Error(err))
  402. return
  403. }
  404. err = s.LogicalLog.DelUuidCodeCache(ctx, date, gameId)
  405. if err != nil {
  406. global.GVA_LOG.Error("重置统计数据删除缓存报错2", zap.Error(err))
  407. return
  408. }
  409. global.GVA_LOG.Info("num = " + strconv.Itoa(num))
  410. for i := 0; i < num; i++ {
  411. global.GVA_LOG.Info("offset = " + strconv.Itoa(offset))
  412. var statisticsLogs []request.AddLogRequest
  413. db2 := global.GVA_DB.Table("loging")
  414. db2 = db2.Where("create_date = ?", date)
  415. db2 = db2.Where("game_id = ?", gameId)
  416. db2 = db2.Limit(limit).Offset(offset)
  417. err = db2.Order("id").Find(&statisticsLogs).Error
  418. for _, statisticsLog := range statisticsLogs {
  419. s.ResetStatisticsCache(ctx, statisticsLog, date)
  420. }
  421. offset += limit
  422. }
  423. global.GVA_LOG.Info("重置统计数据完成")
  424. }
  425. func (s *ServiceStatisticsLog) ResetStatisticsCache(c context.Context, api request.AddLogRequest, date string) {
  426. coding := strconv.Itoa(api.Coding)
  427. nodeCoding := coding[:3]
  428. status := coding[5:]
  429. noLogStatus := coding[3:5]
  430. var logical ServiceResetLoging
  431. switch nodeCoding {
  432. case "410":
  433. logical = new(loging2.ResetLog)
  434. break
  435. case "430":
  436. logical = new(loging2.ResetLog)
  437. break
  438. default:
  439. logical = new(loging2.ResetOtherLog)
  440. break
  441. }
  442. var err error
  443. if status == "99" {
  444. err = logical.SuccessLog(c, api, date)
  445. } else if noLogStatus == "99" {
  446. err = logical.NoLogStatusData(c, api, date)
  447. } else {
  448. err = logical.FailLog(c, api, date)
  449. }
  450. if err != nil {
  451. global.GVA_LOG.Error("创建失败!", zap.Error(err))
  452. }
  453. }
  454. // 每天凌晨reset统计数据
  455. func (s *ServiceStatisticsLog) EveryDayResetStatisticsCache() {
  456. date := s.YesterdayDate()
  457. ctx := context.Background()
  458. gameMps, err := s.LogicalLog.GetGameCache(ctx, date)
  459. if err != nil {
  460. global.GVA_LOG.Error("获取redis game list失败!", zap.Error(err))
  461. return
  462. }
  463. if len(gameMps) == 0 {
  464. global.GVA_LOG.Info("获取redis game list没有数据!")
  465. return
  466. }
  467. for gameId, _ := range gameMps {
  468. id, _ := strconv.Atoi(gameId)
  469. s.ResetStatisticsLog(ctx, id, date)
  470. }
  471. global.GVA_LOG.Info("游戏重置缓存完成!")
  472. }
  473. // 添加和更新电脑指标数据
  474. func (s *ServiceStatisticsLog) CreateComputerStatisticsData() {
  475. ctx := context.Background()
  476. codeMps, err := s.LogicalLog.GetComputerCache(ctx, s.LogicalLog.CurrentDate())
  477. if err != nil {
  478. return
  479. }
  480. if len(codeMps) == 0 {
  481. return
  482. }
  483. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  484. onlineComputerMpa, _ := s.LogicalLog.GetOnlineComputerNumCache(ctx, s.LogicalLog.CurrentDate())
  485. var computer log.Computer
  486. computerData, _ := computer.OnlinePcCodeCache()
  487. var csReplys []*log.LogComputer
  488. for code, r := range codeMps {
  489. accountMps, err := s.LogicalLog.GetComputerPullAccountNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  490. if err != nil {
  491. continue
  492. }
  493. taskMps, err := s.LogicalLog.GetComputerTaskSuccessNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  494. if err != nil {
  495. continue
  496. }
  497. enterMainMps, err := s.LogicalLog.GetComputerEnterMainNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  498. if err != nil {
  499. continue
  500. }
  501. delete(computerData, code)
  502. for gameId, num := range accountMps {
  503. csReply := new(log.LogComputer)
  504. csReply.PcCode = code
  505. if _, ok := onlineComputerMpa[csReply.PcCode]; ok {
  506. delete(onlineComputerMpa, csReply.PcCode)
  507. }
  508. csReply.Operator = r
  509. data, err := global.GVA_REDIS.HGet(ctx, key, gameId).Result()
  510. if err != nil {
  511. if err == redis.Nil {
  512. global.GVA_LOG.Info("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  513. } else {
  514. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  515. return
  516. }
  517. }
  518. var taskStatistics request.TaskStatistics
  519. _ = json.Unmarshal([]byte(data), &taskStatistics)
  520. csReply.GameId, _ = strconv.Atoi(gameId)
  521. csReply.PullAccountNum = num
  522. csReply.TaskSuccessNum = taskMps[gameId]
  523. csReply.EnterMain = enterMainMps[gameId]
  524. csReply.CreateDate = time.Now().Format("2006-01-02")
  525. csReply.TargetNum = taskStatistics.NewTarget + taskStatistics.RetainedTarget
  526. csReply.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, csReply.PcCode, csReply.GameId)
  527. csReply.ComputerFreeTime = s.GetStatisticsComputerRate(ctx, csReply.PcCode)
  528. csReply.GameFeeRate = s.GetStatisticsFeeRate(ctx, csReply.GameId)
  529. runTime := time.Now().Hour() + 1 - csReply.ComputerFreeTime
  530. csReply.ComputerHourAverageRate = csReply.TaskSuccessNum / runTime
  531. csReply.Status = 2
  532. global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Where("game_id = ?", 0).Delete(&log.LogComputer{})
  533. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  534. // 已存在,更新
  535. mp := make(map[string]interface{})
  536. if csReply.Operator != "" {
  537. mp["operator"] = csReply.Operator
  538. }
  539. mp["target_num"] = taskStatistics.NewTarget + taskStatistics.RetainedTarget
  540. mp["pull_account_num"] = num
  541. mp["task_success_num"] = taskMps[gameId]
  542. mp["computer_fee_rate"] = csReply.ComputerFeeRate
  543. mp["computer_free_time"] = csReply.ComputerFreeTime
  544. mp["game_fee_rate"] = csReply.GameFeeRate
  545. mp["computer_hour_average_rate"] = csReply.ComputerHourAverageRate
  546. mp["enter_main"] = csReply.EnterMain
  547. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Updates(mp).Error
  548. if err != nil {
  549. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  550. }
  551. continue
  552. }
  553. csReplys = append(csReplys, csReply)
  554. }
  555. }
  556. if len(csReplys) != 0 {
  557. err = global.GVA_DB.Table("log_computer").Create(csReplys).Error
  558. if err != nil {
  559. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  560. }
  561. }
  562. // 在线电脑没有跑数据记录
  563. if len(onlineComputerMpa) != 0 {
  564. var onlineComputer []*log.LogComputer
  565. for pcCode, op := range onlineComputerMpa {
  566. delete(computerData, pcCode)
  567. csReply := new(log.LogComputer)
  568. csReply.PcCode = pcCode
  569. csReply.Operator = op
  570. csReply.CreateDate = time.Now().Format("2006-01-02")
  571. csReply.Status = 1
  572. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  573. 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) {
  574. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Update("status", 1).Error
  575. if err != nil {
  576. global.GVA_LOG.Error("更新数据失败onlineComputerMpa", zap.Error(err))
  577. }
  578. }
  579. continue
  580. }
  581. onlineComputer = append(onlineComputer, csReply)
  582. }
  583. if len(onlineComputer) != 0 {
  584. err = global.GVA_DB.Table("log_computer").Create(onlineComputer).Error
  585. if err != nil {
  586. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  587. }
  588. }
  589. }
  590. // 没记录电脑数据
  591. if len(computerData) != 0 {
  592. var onlineComputer []*log.LogComputer
  593. for pcCode, op := range computerData {
  594. csReply := new(log.LogComputer)
  595. csReply.PcCode = pcCode
  596. csReply.Operator = op
  597. csReply.CreateDate = time.Now().Format("2006-01-02")
  598. csReply.Status = -1
  599. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  600. continue
  601. }
  602. onlineComputer = append(onlineComputer, csReply)
  603. }
  604. if len(onlineComputer) != 0 {
  605. err = global.GVA_DB.Table("log_computer").Create(onlineComputer).Error
  606. if err != nil {
  607. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  608. }
  609. }
  610. }
  611. return
  612. }
  613. func (s *ServiceStatisticsLog) ComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  614. date := api.CreateDate
  615. isCurrentDate := false
  616. if date == "" {
  617. date = s.CurrentDate()
  618. isCurrentDate = true
  619. }
  620. db := global.GVA_DB.Model(&log.LogComputer{})
  621. var total int64
  622. db = db.Where("create_date = ?", date)
  623. if api.Operator != "" {
  624. db = db.Where("operator = ?", api.Operator)
  625. }
  626. if api.PcCode != "" {
  627. db = db.Where("pc_code = ?", api.PcCode)
  628. }
  629. if api.GameId != 0 {
  630. db = db.Where("game_id = ?", api.GameId)
  631. }
  632. err := db.Count(&total).Error
  633. if err != nil {
  634. return nil, 0, err
  635. }
  636. limit := info.PageSize
  637. offset := info.PageSize * (info.Page - 1)
  638. var statisticsLogs []*log.LogComputer
  639. db = db.Limit(limit).Offset(offset)
  640. if order != "" {
  641. var OrderStr string
  642. // 设置有效排序key 防止sql注入
  643. // 感谢 Tom4t0 提交漏洞信息
  644. orderMap := make(map[string]bool, 3)
  645. orderMap["pc_code"] = true
  646. orderMap["game_id"] = true
  647. orderMap["operator"] = true
  648. if orderMap[order] {
  649. if desc {
  650. OrderStr = order + " desc"
  651. } else {
  652. OrderStr = order
  653. }
  654. } else { // didn't matched any order key in `orderMap`
  655. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  656. return statisticsLogs, total, err
  657. }
  658. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  659. } else {
  660. err = db.Order("id").Find(&statisticsLogs).Error
  661. }
  662. if err != nil {
  663. return nil, 0, err
  664. }
  665. var statisticsLogsComputer []*response.ComputerStatisticsReply1
  666. for _, statisticsLog := range statisticsLogs {
  667. /*var gameInfo []*response.GameInfo
  668. err = json.Unmarshal([]byte(statisticsLog.GameInfo), &gameInfo)
  669. for i, _ := range gameInfo {
  670. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  671. statisticsLogComputer.GameInfo = gameInfo
  672. statisticsLogComputer.PcCode = statisticsLog.PcCode
  673. statisticsLogComputer.Operator = statisticsLog.Operator
  674. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  675. statisticsLogComputer.GameId = gameInfo[i].GameId
  676. statisticsLogComputer.PullAccountNum = gameInfo[i].PullAccountNum
  677. statisticsLogComputer.TaskSuccessNum = gameInfo[i].TaskSuccessNum
  678. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  679. }*/
  680. //gameIdStr:=strconv.Itoa(statisticsLog.GameId)
  681. //taskStatistics := s.GameTargetInfo(ctx,s.LogicalLog.CurrentDate(),gameIdStr)
  682. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  683. statisticsLogComputer.PcCode = statisticsLog.PcCode
  684. statisticsLogComputer.Operator = statisticsLog.Operator
  685. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  686. statisticsLogComputer.GameId = statisticsLog.GameId
  687. statisticsLogComputer.PullAccountNum = statisticsLog.PullAccountNum
  688. statisticsLogComputer.TaskSuccessNum = statisticsLog.TaskSuccessNum
  689. statisticsLogComputer.TargetNum = statisticsLog.TargetNum
  690. statisticsLogComputer.ComputerFreeTime = statisticsLog.ComputerFreeTime
  691. statisticsLogComputer.ComputerFeeRate = statisticsLog.ComputerFeeRate
  692. statisticsLogComputer.EnterMain = statisticsLog.EnterMain
  693. if isCurrentDate {
  694. statisticsLogComputer.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, statisticsLog.PcCode, statisticsLog.GameId)
  695. }
  696. statisticsLogComputer.ComputerHourAverageRate = statisticsLog.ComputerHourAverageRate
  697. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  698. }
  699. return statisticsLogsComputer, total, err
  700. }
  701. // 在线电脑
  702. func (s *ServiceStatisticsLog) OnlineComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) ([]*response.ComputerUseLogReply, int64, error) {
  703. date := api.CreateDate
  704. if date == "" {
  705. date = s.CurrentDate()
  706. }
  707. db := global.GVA_DB.Model(&log.LogComputer{})
  708. var total int64
  709. db = db.Select("SUM(pull_account_num) pull_account_total,SUM(enter_main) enter_main_total,operator,pc_code,status,create_date,remarks")
  710. db = db.Where("create_date = ?", date)
  711. if api.Operator != "" {
  712. db = db.Where("operator = ?", api.Operator)
  713. }
  714. if api.PcCode != "" {
  715. db = db.Where("pc_code = ?", api.PcCode)
  716. }
  717. if api.Status != 0 {
  718. db = db.Where("status = ?", api.Status)
  719. }
  720. db = db.Group("pc_code")
  721. err := db.Count(&total).Error
  722. if err != nil {
  723. return nil, 0, err
  724. }
  725. limit := info.PageSize
  726. offset := info.PageSize * (info.Page - 1)
  727. var statisticsLogs []*response.ComputerUseLogReply
  728. db = db.Limit(limit).Offset(offset)
  729. if order != "" {
  730. var OrderStr string
  731. // 设置有效排序key 防止sql注入
  732. // 感谢 Tom4t0 提交漏洞信息
  733. orderMap := make(map[string]bool, 3)
  734. orderMap["pc_code"] = true
  735. orderMap["operator"] = true
  736. if orderMap[order] {
  737. if desc {
  738. OrderStr = order + " desc"
  739. } else {
  740. OrderStr = order
  741. }
  742. } else { // didn't matched any order key in `orderMap`
  743. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  744. return statisticsLogs, total, err
  745. }
  746. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  747. } else {
  748. err = db.Order("id").Find(&statisticsLogs).Error
  749. }
  750. if err != nil {
  751. return nil, 0, err
  752. }
  753. for _, statisticsLog := range statisticsLogs {
  754. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  755. }
  756. return statisticsLogs, total, err
  757. }
  758. //根据游戏id查询数据
  759. func (s *ServiceStatisticsLog) GameStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo) (interface{}, int64, error) {
  760. date := api.CreateDate
  761. isCurrentDate := false
  762. if date == "" {
  763. date = s.CurrentDate()
  764. isCurrentDate = true
  765. }
  766. db := global.GVA_DB.Model(&log.LogComputer{})
  767. var total int64
  768. db = db.Where("create_date = ?", date)
  769. if api.Operator != "" {
  770. db = db.Where("operator = ?", api.Operator)
  771. }
  772. if api.PcCode != "" {
  773. db = db.Where("pc_code = ?", api.PcCode)
  774. }
  775. if api.GameId != 0 {
  776. db = db.Where("game_id = ?", api.GameId)
  777. }
  778. 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")
  779. err := db.Count(&total).Error
  780. if err != nil {
  781. return nil, 0, err
  782. }
  783. limit := info.PageSize
  784. offset := info.PageSize * (info.Page - 1)
  785. var statisticsLogsByGameId []*response.GameIdStatisticsReply
  786. db = db.Limit(limit).Offset(offset)
  787. err = db.Order("id").Find(&statisticsLogsByGameId).Error
  788. if err != nil {
  789. return nil, 0, err
  790. }
  791. for _, statisticsLog := range statisticsLogsByGameId {
  792. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  793. statisticsLog.OneComputerAverageNum = statisticsLog.TaskSuccessTotal / statisticsLog.PcCodeTotal
  794. if isCurrentDate {
  795. statisticsLog.GameFeeRate = s.GetStatisticsFeeRate(ctx, statisticsLog.GameId)
  796. }
  797. }
  798. return statisticsLogsByGameId, total, err
  799. }
  800. func (s *ServiceStatisticsLog) GameTargetInfo(ctx context.Context, date string, gameId string) (taskStatistics1 request.TaskStatistics) {
  801. key := fmt.Sprintf(taskStatistics, date)
  802. data, err := global.GVA_REDIS.HGet(ctx, key, gameId).Result()
  803. if err != nil {
  804. if err == redis.Nil {
  805. global.GVA_LOG.Info("TaskStatisticsDataCache"+key+gameId, zap.Error(err))
  806. } else {
  807. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache"+key, zap.Error(err))
  808. return
  809. }
  810. }
  811. _ = json.Unmarshal([]byte(data), &taskStatistics1)
  812. return
  813. }
  814. // 同步群控任务数据到缓存
  815. func (s *ServiceStatisticsLog) TaskStatisticsDataCache() {
  816. ctx := context.Background()
  817. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  818. data, err := s.LogicalLog.RequestJfRoom()
  819. if err != nil {
  820. global.GVA_LOG.Error("获取机房数据失败TaskStatisticsDataCache", zap.Error(err))
  821. return
  822. }
  823. dataTask, err := s.LogicalLog.RequestTaskData()
  824. if err != nil {
  825. global.GVA_LOG.Error("RequestTaskData", zap.Error(err))
  826. return
  827. }
  828. var taskData []request.TaskData
  829. var taskStatistics []request.TaskStatistics
  830. _ = json.Unmarshal(data, &taskStatistics)
  831. _ = json.Unmarshal(dataTask, &taskData)
  832. mps := map[int]request.TaskData{}
  833. for _, td := range taskData {
  834. mps[td.GameId] = td
  835. }
  836. for _, data := range taskStatistics {
  837. var id int
  838. id = data.GameId
  839. if _, ok := mps[id]; ok {
  840. data.NewScanningCode = mps[id].NewScanningCode
  841. data.RetainedPullAccount = mps[id].RetainedPullAccount
  842. data.NewPullAccount = mps[id].NewPullAccount
  843. data.FeeAccountNum = mps[id].FeeAccountNum
  844. data.RetainedAccountNum = mps[id].RetainedAccountNum
  845. }
  846. bd, _ := json.Marshal(data)
  847. err = global.GVA_REDIS.HSet(ctx, key, id, bd).Err()
  848. if err != nil {
  849. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  850. return
  851. }
  852. }
  853. }
  854. // 同步群控任务数据到缓存
  855. func (s *ServiceStatisticsLog) TaskStatisticsDataCache1() {
  856. ctx := context.Background()
  857. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  858. data, err := s.LogicalLog.RequestJfRoom()
  859. if err != nil {
  860. global.GVA_LOG.Error("获取机房数据失败TaskStatisticsDataCache", zap.Error(err))
  861. return
  862. }
  863. dataTask, err := s.LogicalLog.RequestTaskData()
  864. if err != nil {
  865. global.GVA_LOG.Error("RequestTaskData", zap.Error(err))
  866. return
  867. }
  868. var taskData []request.TaskData
  869. var taskStatistics []request.TaskStatistics
  870. _ = json.Unmarshal(data, &taskStatistics)
  871. _ = json.Unmarshal(dataTask, &taskData)
  872. mps := map[int]request.TaskData{}
  873. for _, td := range taskData {
  874. mps[td.GameId] = td
  875. }
  876. for _, data := range taskStatistics {
  877. var id int
  878. if data.GameIdXmy != "" {
  879. if data.GameIdXmy == "0" {
  880. id = data.GameId
  881. } else {
  882. id, _ = strconv.Atoi(data.GameIdXmy)
  883. }
  884. } else {
  885. id = data.GameId
  886. }
  887. if _, ok := mps[id]; ok {
  888. data.NewScanningCode = mps[id].NewScanningCode
  889. data.RetainedPullAccount = mps[id].RetainedPullAccount
  890. data.NewPullAccount = mps[id].NewPullAccount
  891. data.FeeAccountNum = mps[id].FeeAccountNum
  892. data.RetainedAccountNum = mps[id].RetainedAccountNum
  893. }
  894. bd, _ := json.Marshal(data)
  895. err = global.GVA_REDIS.HSet(ctx, key, id, bd).Err()
  896. if err != nil {
  897. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  898. return
  899. }
  900. }
  901. }
  902. // 同步群控任务数据到缓存
  903. func (s *ServiceStatisticsLog) GetComputerNum(date string) int64 {
  904. var total int64
  905. if date == "" {
  906. date = s.CurrentDate()
  907. }
  908. db := global.GVA_DB.Model(&log.LogComputer{})
  909. db = db.Distinct("pc_code").Where("create_date = ?", date)
  910. _ = db.Count(&total).Error
  911. return total
  912. }
  913. // 在线电脑接口
  914. func (s *ServiceStatisticsLog) ComputerHeartbeat(c context.Context, onlineComputer request.OnlineComputerRequest) error {
  915. err := s.LogicalLog.SetOnlineComputerNumCache(c, s.LogicalLog.CurrentDate(), onlineComputer.PcCode, onlineComputer.Operator)
  916. if err != nil {
  917. return err
  918. }
  919. s.LogicalLog.SetPcReportingLog(c, onlineComputer.PcCode, onlineComputer.Operator)
  920. return err
  921. }
  922. // 在线电脑接口测试
  923. func (s *ServiceStatisticsLog) ComputerTest(c context.Context) (interface{}, error) {
  924. mps, err := s.LogicalLog.GetOnlineComputerNumCache(c, s.LogicalLog.CurrentDate())
  925. if err != nil {
  926. return mps, err
  927. }
  928. return mps, err
  929. }
  930. // 定时检查电脑情况
  931. func (s *ServiceStatisticsLog) RegularCheckPc1() {
  932. ctx := context.Background()
  933. date := s.LogicalLog.CurrentDate()
  934. // 获取当天已上报的电脑数据
  935. onlineComputerMpa, _ := s.LogicalLog.GetOnlineComputerNumCache(ctx, date)
  936. // 获取数据库的数据
  937. var computer log.Computer
  938. computers, err := computer.OnlinePcCodeCache()
  939. if err != nil {
  940. global.GVA_LOG.Error("获取租机表数据失败", zap.Error(err))
  941. }
  942. // 查询两小时内上报的数据
  943. var noReportingPc []string
  944. for pc, _ := range onlineComputerMpa {
  945. num := s.LogicalLog.GetPcReportingLog(ctx, pc)
  946. delete(computers, pc)
  947. if num == 0 {
  948. noReportingPc = append(noReportingPc, pc)
  949. }
  950. }
  951. // 未上报逻辑
  952. var errReportingPc []string
  953. for pc, name := range computers {
  954. if name == "备用" {
  955. continue
  956. }
  957. errReportingPc = append(errReportingPc, pc)
  958. }
  959. var content string
  960. if len(errReportingPc) != 0 {
  961. // 发信息数据群里
  962. content += fmt.Sprintf("异常租机数据<font color=\"warning\">%d台</font>: %s", len(errReportingPc), errReportingPc)
  963. }
  964. if len(noReportingPc) != 0 {
  965. // 发信息数据群里
  966. content += "\n"
  967. content += fmt.Sprintf("两小时内未检测到中控数据<font color=\"warning\">%d台</font>: %s", len(noReportingPc), noReportingPc)
  968. }
  969. if content != "" {
  970. var sendMsg SendMsg
  971. sendMsg.MsgType = "markdown"
  972. sendMsg.Markdown.Content = content
  973. //c ,_ := json.Marshal(sendMsg)
  974. url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
  975. //url := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b"
  976. _, err = s.SendMsgData(url, sendMsg)
  977. }
  978. }
  979. // 定时检查电脑情况
  980. func (s *ServiceStatisticsLog) RegularCheckPc() {
  981. ctx := context.Background()
  982. var computer log.Computer
  983. computers, err := computer.OnlinePcCodeCache()
  984. //computersF, err := computer.OnlinePcCodeCache()
  985. if err != nil {
  986. global.GVA_LOG.Error("获取租机表数据失败", zap.Error(err))
  987. }
  988. // 查询两小时内上报的数据
  989. var computersF = map[string]string{}
  990. var noReportingPc []string
  991. for pc, name := range computers {
  992. computersF[pc] = name
  993. if name == "备用" {
  994. delete(computers, pc)
  995. continue
  996. }
  997. num := s.LogicalLog.GetPcReportingLog(ctx, pc)
  998. delete(computers, pc)
  999. if num == 0 {
  1000. noReportingPc = append(noReportingPc, pc)
  1001. }
  1002. }
  1003. var content string
  1004. if len(noReportingPc) != 0 {
  1005. content += fmt.Sprintf("两小时内未检测到中控数据<font color=\"warning\">%d台</font>:", len(noReportingPc))
  1006. for _, pc := range noReportingPc {
  1007. content += "\n"
  1008. name := computersF[pc]
  1009. content += name + " : " + pc
  1010. }
  1011. }
  1012. if content != "" {
  1013. var sendMsg SendMsg
  1014. sendMsg.MsgType = "markdown"
  1015. sendMsg.Markdown.Content = content
  1016. //c ,_ := json.Marshal(sendMsg)
  1017. url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
  1018. //url := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b"
  1019. _, err = s.SendMsgData(url, sendMsg)
  1020. }
  1021. }
  1022. type SendMsg struct {
  1023. MsgType string `json:"msgtype"`
  1024. Markdown struct {
  1025. Content string `json:"content"`
  1026. } `json:"markdown"`
  1027. }
  1028. func (s *ServiceStatisticsLog) SendMsgData(url string, params interface{}) (result []byte, err error) {
  1029. result, err = utils.HttpPost(url, params)
  1030. return
  1031. }
  1032. var statusMps = map[int]string{
  1033. -1: "未上报",
  1034. 1: "中控上报",
  1035. 2: "任务上报",
  1036. }
  1037. func (exa *ServiceStatisticsLog) ParseInfoList2Excel(infoList []*response.ComputerUseLogReply, filePath string) error {
  1038. excel := excelize.NewFile()
  1039. excel.SetSheetRow("Sheet1", "A1", &[]string{"电脑编号", "使用者", "日期", "拉取账号", "进入主线", "主线成功率", "使用状态", "备注"})
  1040. for i, statisticsLog := range infoList {
  1041. axis := fmt.Sprintf("A%d", i+2)
  1042. var r interface{}
  1043. if statisticsLog.EnterMainTotal != 0 && statisticsLog.PullAccountTotal != 0 {
  1044. r = fmt.Sprintf("%.2f", float64(statisticsLog.EnterMainTotal)/float64(statisticsLog.PullAccountTotal)*100)
  1045. } else {
  1046. r = "0.00"
  1047. }
  1048. excel.SetSheetRow("Sheet1", axis, &[]interface{}{
  1049. statisticsLog.PcCode,
  1050. statisticsLog.Operator,
  1051. statisticsLog.CreateDate[:10],
  1052. statisticsLog.PullAccountTotal,
  1053. statisticsLog.EnterMainTotal,
  1054. r,
  1055. statusMps[statisticsLog.Status],
  1056. statisticsLog.Remarks,
  1057. })
  1058. }
  1059. err := excel.SaveAs(filePath)
  1060. return err
  1061. }
  1062. // 删除两天前的缓存数据
  1063. func (s *ServiceStatisticsLog) RegularDelCheckData() {
  1064. ctx := context.Background()
  1065. date := time.Now().Add(-time.Hour * 48).Format("2006-01-02")
  1066. s.LogicalLog.DelHashUuidKey(ctx, date)
  1067. s.LogicalLog.DelZSetKey(ctx, date)
  1068. s.LogicalLog.DelHashKey(ctx, date)
  1069. }
  1070. //@author: [piexlmax](https://github.com/piexlmax)
  1071. //@function: UpdatePc
  1072. //@description: 根据id更新pc
  1073. //@param: Computer log.Computer
  1074. //@return: err error
  1075. func (a *ServiceStatisticsLog) UpdateComputerUseLog(c log.ComputerUseRemarks) (err error) {
  1076. if !errors.Is(global.GVA_DB.Where("create_date = ?", c.CreateDate).Where("pc_code = ?", c.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  1077. var updateInfo = make(map[string]interface{})
  1078. updateInfo["remarks"] = c.Remarks
  1079. updateInfo["remarks_update_time"] = time.Now().Format("2006-01-02 15:04:05")
  1080. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", c.CreateDate).Where("pc_code = ?", c.PcCode).Updates(updateInfo).Error
  1081. }
  1082. return
  1083. }