log_statistics.go 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. "strconv"
  18. "strings"
  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. EnterScanningCode, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4608000", loging2.NoLogStatus, tt)
  216. TranscodingFail, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4608001", loging2.FailStatus, tt)
  217. ThirdPartyFail, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4608002", loging2.FailStatus, tt)
  218. ScanningCodeSuccess, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4608099", loging2.OkStatus, tt)
  219. //statisticsLog.NewHaveRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604004", loging2.NoLogStatus, tt) // 新增有角色
  220. //statisticsLog.RetainedNotRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604003", loging2.NoLogStatus, tt) // 留存无角色
  221. statisticsLog.PullAccountOk = pullAccountOk
  222. statisticsLog.PullAccountFail = pullAccountFail
  223. statisticsLog.StartProxyFail = StartProxyFail
  224. statisticsLog.StartProxyOk = StartProxyOk
  225. statisticsLog.SimulatorStartFail = SimulatorStartFail
  226. statisticsLog.SimulatorStartOk = SimulatorStartOk
  227. statisticsLog.NetworkCheckFail = NetworkCheckFail
  228. statisticsLog.NetworkCheckOk = NetworkCheckOk
  229. statisticsLog.EnterMainFail = EnterMainFail
  230. statisticsLog.EnterMainOk = EnterMainOk
  231. statisticsLog.EnterGameFail = EnterGameFail
  232. statisticsLog.EnterGameOk = EnterGameOk
  233. statisticsLog.GameId = gameIdInt
  234. statisticsLog.ConstituencyFail = ConstituencyFail
  235. statisticsLog.ConstituencyOk = ConstituencyOk
  236. statisticsLog.FeeFail = FeeFail
  237. statisticsLog.FeeOk = FeeOk
  238. statisticsLog.GameStartFail = GameStartFail
  239. statisticsLog.GameStartOk = GameStartOk
  240. statisticsLog.LoginFail = LoginFail
  241. statisticsLog.LoginOk = LoginOk
  242. statisticsLog.XmyLoginFail = xmyLoginFail
  243. statisticsLog.XmyLoginOk = xmyLoginOk
  244. statisticsLog.WxLoginFail = wxLoginFail
  245. statisticsLog.WxLoginOk = wxLoginOk
  246. statisticsLog.MzLoginFail = mzLoginFail
  247. statisticsLog.MzLoginOk = mzLoginOk
  248. statisticsLog.ScriptStartFail = ScriptStartFail
  249. statisticsLog.ScriptStartOk = ScriptStartOk
  250. statisticsLog.AuthenticationFail = AuthenticationFail
  251. statisticsLog.AuthenticationOk = AuthenticationOk
  252. statisticsLog.EnterStartGame = EnterStartGame
  253. statisticsLog.EnterAuthentication = EnterAuthentication
  254. statisticsLog.EnterConstituency = EnterConstituency
  255. statisticsLog.EnterGame = EnterGame
  256. statisticsLog.EnterMain = EnterMain
  257. statisticsLog.EnterFee = EnterFee
  258. statisticsLog.EnterLogin = EnterLogin
  259. statisticsLog.BanOff = BanOff
  260. statisticsLog.Freeze = Freeze
  261. statisticsLog.Type = tt
  262. statisticsLog.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  263. statisticsLog.CreateDate = date
  264. statisticsLog.EnterScanningCode = EnterScanningCode
  265. statisticsLog.ThirdPartyFail = ThirdPartyFail
  266. statisticsLog.TranscodingFail = TranscodingFail
  267. statisticsLog.ScanningCodeSuccess = ScanningCodeSuccess
  268. if tt == 1 {
  269. statisticsLog.NotRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4609901", loging2.NoLogStatus, tt)
  270. statisticsLog.HasRole = 0
  271. statisticsLog.IssuedAccount = taskStatistics.RetainedPullAccount
  272. statisticsLog.TargetNum = taskStatistics.RetainedTarget
  273. statisticsLog.TargetCompleteNum = taskStatistics.RetainedComplete
  274. //statisticsLog.ScanningSuccessRate = 0
  275. } else {
  276. statisticsLog.NotRole = 0
  277. statisticsLog.HasRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4609902", loging2.NoLogStatus, tt)
  278. statisticsLog.IssuedAccount = taskStatistics.NewPullAccount
  279. statisticsLog.TargetCompleteNum = taskStatistics.NewComplete
  280. statisticsLog.TargetNum = taskStatistics.NewTarget
  281. statisticsLog.NewScanningCode = taskStatistics.NewScanningCode
  282. //if statisticsLog.NewScanningCode != 0 {
  283. // statisticsLog.ScanningSuccessRate = float64(statisticsLog.TargetNum) / float64(statisticsLog.NewScanningCode) * 100
  284. //}
  285. }
  286. if statisticsLog.ScanningCodeSuccess != 0 {
  287. statisticsLog.ScanningSuccessRate = float64(statisticsLog.ScanningCodeSuccess) / float64(statisticsLog.EnterScanningCode) * 100
  288. }
  289. statisticsLog.PayComplete = taskStatistics.PayComplete
  290. statisticsLog.PayTarget = taskStatistics.PayTarget
  291. statisticsLog.OrderCreate = taskStatistics.OrderCreate
  292. statisticsLog.OrderSuccess = taskStatistics.OrderSuccess
  293. statisticsLog.RetainedAccountNum = taskStatistics.RetainedAccountNum
  294. statisticsLog.FeeAccountNum = taskStatistics.FeeAccountNum
  295. statisticsLog.Operator = taskStatistics.Remark
  296. statisticsLog.GameName = taskStatistics.GameName
  297. if statisticsLog.IssuedAccount != 0 {
  298. statisticsLog.PullSuccessRate = float64(statisticsLog.PullAccountOk) / float64(statisticsLog.IssuedAccount) * 100
  299. }
  300. if statisticsLog.PullAccountOk != 0 {
  301. statisticsLog.StartSuccessRate = float64(statisticsLog.SimulatorStartOk) / float64(statisticsLog.PullAccountOk) * 100
  302. }
  303. if statisticsLog.GameStartOk != 0 {
  304. statisticsLog.MainSuccessRate = float64(statisticsLog.EnterMain) / float64(statisticsLog.GameStartOk) * 100
  305. }
  306. if statisticsLog.TargetNum != 0 {
  307. statisticsLog.TaskSuccessRate = float64(statisticsLog.EnterMain) / float64(statisticsLog.TargetNum) * 100
  308. }
  309. if statisticsLog.OrderCreate != 0 {
  310. statisticsLog.LocalOrderSuccessRate = float64(statisticsLog.FeeOk) / float64(statisticsLog.OrderCreate) * 100
  311. }
  312. return statisticsLog
  313. }
  314. func (s *ServiceStatisticsLog) OtherStatisticsLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  315. date := api.CreateDate
  316. if date == "" {
  317. date = s.CurrentDate()
  318. }
  319. db := global.GVA_DB.Model(&log.StatisticsLog{})
  320. if api.GameId != 0 {
  321. db = db.Where("game_id = ?", api.GameId)
  322. }
  323. db = db.Where("create_date = ?", date)
  324. var total int64
  325. err := db.Count(&total).Error
  326. if err != nil {
  327. return nil, 0, err
  328. }
  329. limit := info.PageSize
  330. offset := info.PageSize * (info.Page - 1)
  331. var statisticsLogs []*log.StatisticsLog
  332. db = db.Limit(limit).Offset(offset)
  333. if order != "" {
  334. var OrderStr string
  335. // 设置有效排序key 防止sql注入
  336. // 感谢 Tom4t0 提交漏洞信息
  337. orderMap := make(map[string]bool, 7)
  338. orderMap["pull_success_rate"] = true
  339. orderMap["start_success_rate"] = true
  340. orderMap["main_success_rate"] = true
  341. orderMap["task_success_rate"] = true
  342. orderMap["scanning_success_rate"] = true
  343. orderMap["local_order_success_rate"] = true
  344. orderMap["game_id"] = true
  345. if orderMap[order] {
  346. if desc {
  347. OrderStr = order + " desc"
  348. } else {
  349. OrderStr = order
  350. }
  351. } else { // didn't matched any order key in `orderMap`
  352. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  353. return statisticsLogs, total, err
  354. }
  355. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  356. } else {
  357. err = db.Order("id").Find(&statisticsLogs).Error
  358. }
  359. if err != nil {
  360. return nil, 0, err
  361. }
  362. for _, statisticsLog := range statisticsLogs {
  363. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  364. }
  365. return statisticsLogs, total, err
  366. }
  367. func (s *ServiceStatisticsLog) StatisticsNodeLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  368. date := api.CreateDate
  369. if date == "" || date == s.CurrentDate() {
  370. apilist, total, err := s.TodayStatisticsLogList(ctx, api, info)
  371. return apilist, total, err
  372. }
  373. db := global.GVA_DB.Model(&log.StatisticsLog{})
  374. if api.GameId != 0 {
  375. db = db.Where("game_id = ?", api.GameId)
  376. }
  377. db = db.Where("create_date = ?", date)
  378. var total int64
  379. err := db.Count(&total).Error
  380. if err != nil {
  381. return nil, 0, err
  382. }
  383. limit := info.PageSize
  384. offset := info.PageSize * (info.Page - 1)
  385. var statisticsLogs []*log.StatisticsLog
  386. db = db.Limit(limit).Offset(offset)
  387. err = db.Order("id").Find(&statisticsLogs).Error
  388. if err != nil {
  389. return nil, 0, err
  390. }
  391. for _, statisticsLog := range statisticsLogs {
  392. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  393. }
  394. return statisticsLogs, total, err
  395. }
  396. //func (s *ServiceStatisticsLog) ResetStatisticsLog(ctx context.Context, gameId int, date string) {
  397. // var total int64
  398. // key := date + ":" + strconv.Itoa(gameId) + ":reset"
  399. // global.GVA_REDIS.Set(ctx, key, 1, time.Minute*15)
  400. // db := global.GVA_DB.Table("loging")
  401. // db = db.Where("game_id = ?", gameId)
  402. // err := db.Count(&total).Error
  403. // if err != nil {
  404. // global.GVA_LOG.Error("重置统计数据总数报错", zap.Error(err))
  405. // return
  406. // }
  407. // limit := 100
  408. // num := int(math.Ceil(float64(total) / float64(limit)))
  409. // offset := 0
  410. // err = s.LogicalLog.DelStatisticsNumCache(ctx, date, gameId)
  411. // if err != nil {
  412. // global.GVA_LOG.Error("重置统计数据删除缓存报错", zap.Error(err))
  413. // return
  414. // }
  415. // err = s.LogicalLog.DelUuidCodeCache(ctx, date, gameId)
  416. // if err != nil {
  417. // global.GVA_LOG.Error("重置统计数据删除缓存报错2", zap.Error(err))
  418. // return
  419. // }
  420. // global.GVA_LOG.Info("num = " + strconv.Itoa(num))
  421. // for i := 0; i < num; i++ {
  422. // global.GVA_LOG.Info("offset = " + strconv.Itoa(offset))
  423. // var statisticsLogs []request.AddLogRequest
  424. // db2 := global.GVA_DB.Table("loging")
  425. // db2 = db2.Where("create_date = ?", date)
  426. // db2 = db2.Where("game_id = ?", gameId)
  427. // db2 = db2.Limit(limit).Offset(offset)
  428. // err = db2.Order("id").Find(&statisticsLogs).Error
  429. // for _, statisticsLog := range statisticsLogs {
  430. //
  431. // s.ResetStatisticsCache(ctx, statisticsLog, date)
  432. // }
  433. // offset += limit
  434. // }
  435. // global.GVA_LOG.Info("重置统计数据完成")
  436. //}
  437. //
  438. //func (s *ServiceStatisticsLog) ResetStatisticsCache(c context.Context, api request.AddLogRequest, date string) {
  439. // coding := strconv.Itoa(api.Coding)
  440. // nodeCoding := coding[:3]
  441. // status := coding[5:]
  442. // noLogStatus := coding[3:5]
  443. // var logical ServiceResetLoging
  444. // switch nodeCoding {
  445. // case "410":
  446. // logical = new(loging2.ResetLog)
  447. // break
  448. // case "430":
  449. // logical = new(loging2.ResetLog)
  450. // break
  451. // default:
  452. // logical = new(loging2.ResetOtherLog)
  453. // break
  454. // }
  455. // var err error
  456. // if status == "99" {
  457. // err = logical.SuccessLog(c, api, date)
  458. // } else if noLogStatus == "99" {
  459. // err = logical.NoLogStatusData(c, api, date)
  460. // } else {
  461. // err = logical.FailLog(c, api, date)
  462. // }
  463. // if err != nil {
  464. // global.GVA_LOG.Error("创建失败!", zap.Error(err))
  465. // }
  466. //}
  467. // 每天凌晨reset统计数据
  468. //func (s *ServiceStatisticsLog) EveryDayResetStatisticsCache() {
  469. // date := s.YesterdayDate()
  470. // ctx := context.Background()
  471. // gameMps, err := s.LogicalLog.GetGameCache(ctx, date)
  472. // if err != nil {
  473. // global.GVA_LOG.Error("获取redis game list失败!", zap.Error(err))
  474. // return
  475. // }
  476. // if len(gameMps) == 0 {
  477. // global.GVA_LOG.Info("获取redis game list没有数据!")
  478. // return
  479. // }
  480. // for gameId, _ := range gameMps {
  481. // id, _ := strconv.Atoi(gameId)
  482. // s.ResetStatisticsLog(ctx, id, date)
  483. // }
  484. // global.GVA_LOG.Info("游戏重置缓存完成!")
  485. //}
  486. // 添加和更新电脑指标数据
  487. func (s *ServiceStatisticsLog) CreateComputerStatisticsData() {
  488. ctx := context.Background()
  489. codeMps, err := s.LogicalLog.GetComputerCache(ctx, s.LogicalLog.CurrentDate())
  490. if err != nil {
  491. return
  492. }
  493. if len(codeMps) == 0 {
  494. return
  495. }
  496. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  497. onlineComputerMpa, _ := s.LogicalLog.GetOnlineComputerNumCache(ctx, s.LogicalLog.CurrentDate())
  498. var computer log.Computer
  499. computerData, _ := computer.OnlinePcCodeCache()
  500. var csReplys []*log.LogComputer
  501. for code, r := range codeMps {
  502. accountMps, err := s.LogicalLog.GetComputerPullAccountNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  503. if err != nil {
  504. continue
  505. }
  506. taskMps, err := s.LogicalLog.GetComputerTaskSuccessNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  507. if err != nil {
  508. continue
  509. }
  510. enterMainMps, err := s.LogicalLog.GetComputerEnterMainNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  511. if err != nil {
  512. continue
  513. }
  514. delete(computerData, code)
  515. for gameId, num := range accountMps {
  516. csReply := new(log.LogComputer)
  517. csReply.PcCode = code
  518. if _, ok := onlineComputerMpa[csReply.PcCode]; ok {
  519. delete(onlineComputerMpa, csReply.PcCode)
  520. }
  521. csReply.Operator = r
  522. data, err := global.GVA_REDIS.HGet(ctx, key, gameId).Result()
  523. if err != nil {
  524. if err == redis.Nil {
  525. global.GVA_LOG.Info("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  526. } else {
  527. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  528. return
  529. }
  530. }
  531. var taskStatistics request.TaskStatistics
  532. _ = json.Unmarshal([]byte(data), &taskStatistics)
  533. csReply.GameId, _ = strconv.Atoi(gameId)
  534. csReply.PullAccountNum = num
  535. csReply.TaskSuccessNum = taskMps[gameId]
  536. csReply.EnterMain = enterMainMps[gameId]
  537. csReply.CreateDate = time.Now().Format("2006-01-02")
  538. csReply.TargetNum = taskStatistics.NewTarget + taskStatistics.RetainedTarget
  539. csReply.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, csReply.PcCode, csReply.GameId)
  540. csReply.ComputerFreeTime = s.GetStatisticsComputerRate(ctx, csReply.PcCode)
  541. csReply.GameFeeRate = s.GetStatisticsFeeRate(ctx, csReply.GameId)
  542. runTime := time.Now().Hour() + 1 - csReply.ComputerFreeTime
  543. csReply.ComputerHourAverageRate = csReply.TaskSuccessNum / runTime
  544. csReply.Status = 2
  545. global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Where("game_id = ?", 0).Delete(&log.LogComputer{})
  546. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  547. // 已存在,更新
  548. mp := make(map[string]interface{})
  549. if csReply.Operator != "" {
  550. mp["operator"] = csReply.Operator
  551. }
  552. mp["target_num"] = taskStatistics.NewTarget + taskStatistics.RetainedTarget
  553. mp["pull_account_num"] = num
  554. mp["task_success_num"] = taskMps[gameId]
  555. mp["computer_fee_rate"] = csReply.ComputerFeeRate
  556. mp["computer_free_time"] = csReply.ComputerFreeTime
  557. mp["game_fee_rate"] = csReply.GameFeeRate
  558. mp["computer_hour_average_rate"] = csReply.ComputerHourAverageRate
  559. mp["enter_main"] = csReply.EnterMain
  560. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Updates(mp).Error
  561. if err != nil {
  562. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  563. }
  564. continue
  565. }
  566. csReplys = append(csReplys, csReply)
  567. }
  568. }
  569. if len(csReplys) != 0 {
  570. err = global.GVA_DB.Table("log_computer").Create(csReplys).Error
  571. if err != nil {
  572. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  573. }
  574. }
  575. // 在线电脑没有跑数据记录
  576. if len(onlineComputerMpa) != 0 {
  577. var onlineComputer []*log.LogComputer
  578. for pcCode, op := range onlineComputerMpa {
  579. delete(computerData, pcCode)
  580. csReply := new(log.LogComputer)
  581. csReply.PcCode = pcCode
  582. csReply.Operator = op
  583. csReply.CreateDate = time.Now().Format("2006-01-02")
  584. csReply.Status = 1
  585. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  586. 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) {
  587. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Update("status", 1).Error
  588. if err != nil {
  589. global.GVA_LOG.Error("更新数据失败onlineComputerMpa", zap.Error(err))
  590. }
  591. }
  592. continue
  593. }
  594. onlineComputer = append(onlineComputer, csReply)
  595. }
  596. if len(onlineComputer) != 0 {
  597. err = global.GVA_DB.Table("log_computer").Create(onlineComputer).Error
  598. if err != nil {
  599. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  600. }
  601. }
  602. }
  603. // 没记录电脑数据
  604. if len(computerData) != 0 {
  605. var onlineComputer []*log.LogComputer
  606. for pcCode, op := range computerData {
  607. csReply := new(log.LogComputer)
  608. csReply.PcCode = pcCode
  609. csReply.Operator = op
  610. csReply.CreateDate = time.Now().Format("2006-01-02")
  611. csReply.Status = -1
  612. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  613. continue
  614. }
  615. onlineComputer = append(onlineComputer, csReply)
  616. }
  617. if len(onlineComputer) != 0 {
  618. err = global.GVA_DB.Table("log_computer").Create(onlineComputer).Error
  619. if err != nil {
  620. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  621. }
  622. }
  623. }
  624. return
  625. }
  626. func (s *ServiceStatisticsLog) ComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  627. date := api.CreateDate
  628. isCurrentDate := false
  629. if date == "" {
  630. date = s.CurrentDate()
  631. isCurrentDate = true
  632. }
  633. db := global.GVA_DB.Model(&log.LogComputer{})
  634. var total int64
  635. db = db.Where("create_date = ?", date)
  636. if api.Operator != "" {
  637. db = db.Where("operator = ?", api.Operator)
  638. }
  639. if api.PcCode != "" {
  640. db = db.Where("pc_code = ?", api.PcCode)
  641. }
  642. if api.GameId != 0 {
  643. db = db.Where("game_id = ?", api.GameId)
  644. }
  645. err := db.Count(&total).Error
  646. if err != nil {
  647. return nil, 0, err
  648. }
  649. limit := info.PageSize
  650. offset := info.PageSize * (info.Page - 1)
  651. var statisticsLogs []*log.LogComputer
  652. db = db.Limit(limit).Offset(offset)
  653. if order != "" {
  654. var OrderStr string
  655. // 设置有效排序key 防止sql注入
  656. // 感谢 Tom4t0 提交漏洞信息
  657. orderMap := make(map[string]bool, 3)
  658. orderMap["pc_code"] = true
  659. orderMap["game_id"] = true
  660. orderMap["operator"] = true
  661. if orderMap[order] {
  662. if desc {
  663. OrderStr = order + " desc"
  664. } else {
  665. OrderStr = order
  666. }
  667. } else { // didn't matched any order key in `orderMap`
  668. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  669. return statisticsLogs, total, err
  670. }
  671. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  672. } else {
  673. err = db.Order("id").Find(&statisticsLogs).Error
  674. }
  675. if err != nil {
  676. return nil, 0, err
  677. }
  678. var statisticsLogsComputer []*response.ComputerStatisticsReply1
  679. for _, statisticsLog := range statisticsLogs {
  680. /*var gameInfo []*response.GameInfo
  681. err = json.Unmarshal([]byte(statisticsLog.GameInfo), &gameInfo)
  682. for i, _ := range gameInfo {
  683. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  684. statisticsLogComputer.GameInfo = gameInfo
  685. statisticsLogComputer.PcCode = statisticsLog.PcCode
  686. statisticsLogComputer.Operator = statisticsLog.Operator
  687. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  688. statisticsLogComputer.GameId = gameInfo[i].GameId
  689. statisticsLogComputer.PullAccountNum = gameInfo[i].PullAccountNum
  690. statisticsLogComputer.TaskSuccessNum = gameInfo[i].TaskSuccessNum
  691. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  692. }*/
  693. //gameIdStr:=strconv.Itoa(statisticsLog.GameId)
  694. //taskStatistics := s.GameTargetInfo(ctx,s.LogicalLog.CurrentDate(),gameIdStr)
  695. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  696. statisticsLogComputer.PcCode = statisticsLog.PcCode
  697. statisticsLogComputer.Operator = statisticsLog.Operator
  698. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  699. statisticsLogComputer.GameId = statisticsLog.GameId
  700. statisticsLogComputer.PullAccountNum = statisticsLog.PullAccountNum
  701. statisticsLogComputer.TaskSuccessNum = statisticsLog.TaskSuccessNum
  702. statisticsLogComputer.TargetNum = statisticsLog.TargetNum
  703. statisticsLogComputer.ComputerFreeTime = statisticsLog.ComputerFreeTime
  704. statisticsLogComputer.ComputerFeeRate = statisticsLog.ComputerFeeRate
  705. statisticsLogComputer.EnterMain = statisticsLog.EnterMain
  706. if isCurrentDate {
  707. statisticsLogComputer.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, statisticsLog.PcCode, statisticsLog.GameId)
  708. }
  709. statisticsLogComputer.ComputerHourAverageRate = statisticsLog.ComputerHourAverageRate
  710. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  711. }
  712. return statisticsLogsComputer, total, err
  713. }
  714. // 在线电脑
  715. func (s *ServiceStatisticsLog) OnlineComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) ([]*response.ComputerUseLogReply, int64, error) {
  716. date := api.CreateDate
  717. if date == "" {
  718. date = s.CurrentDate()
  719. }
  720. db := global.GVA_DB.Model(&log.LogComputer{})
  721. var total int64
  722. db = db.Select("SUM(pull_account_num) pull_account_total,SUM(enter_main) enter_main_total,operator,pc_code,status,create_date,remarks")
  723. db = db.Where("create_date = ?", date)
  724. if api.Operator != "" {
  725. db = db.Where("operator = ?", api.Operator)
  726. }
  727. if api.PcCode != "" {
  728. db = db.Where("pc_code = ?", api.PcCode)
  729. }
  730. if api.Status != 0 {
  731. db = db.Where("status = ?", api.Status)
  732. }
  733. db = db.Group("pc_code")
  734. err := db.Count(&total).Error
  735. if err != nil {
  736. return nil, 0, err
  737. }
  738. limit := info.PageSize
  739. offset := info.PageSize * (info.Page - 1)
  740. var statisticsLogs []*response.ComputerUseLogReply
  741. db = db.Limit(limit).Offset(offset)
  742. if order != "" {
  743. var OrderStr string
  744. // 设置有效排序key 防止sql注入
  745. // 感谢 Tom4t0 提交漏洞信息
  746. orderMap := make(map[string]bool, 3)
  747. orderMap["pc_code"] = true
  748. orderMap["operator"] = true
  749. if orderMap[order] {
  750. if desc {
  751. OrderStr = order + " desc"
  752. } else {
  753. OrderStr = order
  754. }
  755. } else { // didn't matched any order key in `orderMap`
  756. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  757. return statisticsLogs, total, err
  758. }
  759. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  760. } else {
  761. err = db.Order("id").Find(&statisticsLogs).Error
  762. }
  763. if err != nil {
  764. return nil, 0, err
  765. }
  766. for _, statisticsLog := range statisticsLogs {
  767. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  768. }
  769. return statisticsLogs, total, err
  770. }
  771. //根据游戏id查询数据
  772. func (s *ServiceStatisticsLog) GameStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo) (interface{}, int64, error) {
  773. date := api.CreateDate
  774. isCurrentDate := false
  775. if date == "" {
  776. date = s.CurrentDate()
  777. isCurrentDate = true
  778. }
  779. db := global.GVA_DB.Model(&log.LogComputer{})
  780. var total int64
  781. db = db.Where("create_date = ?", date)
  782. if api.Operator != "" {
  783. db = db.Where("operator = ?", api.Operator)
  784. }
  785. if api.PcCode != "" {
  786. db = db.Where("pc_code = ?", api.PcCode)
  787. }
  788. if api.GameId != 0 {
  789. db = db.Where("game_id = ?", api.GameId)
  790. }
  791. 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")
  792. err := db.Count(&total).Error
  793. if err != nil {
  794. return nil, 0, err
  795. }
  796. limit := info.PageSize
  797. offset := info.PageSize * (info.Page - 1)
  798. var statisticsLogsByGameId []*response.GameIdStatisticsReply
  799. db = db.Limit(limit).Offset(offset)
  800. err = db.Order("id").Find(&statisticsLogsByGameId).Error
  801. if err != nil {
  802. return nil, 0, err
  803. }
  804. for _, statisticsLog := range statisticsLogsByGameId {
  805. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  806. statisticsLog.OneComputerAverageNum = statisticsLog.TaskSuccessTotal / statisticsLog.PcCodeTotal
  807. if isCurrentDate {
  808. statisticsLog.GameFeeRate = s.GetStatisticsFeeRate(ctx, statisticsLog.GameId)
  809. }
  810. }
  811. return statisticsLogsByGameId, total, err
  812. }
  813. func (s *ServiceStatisticsLog) GameTargetInfo(ctx context.Context, date string, gameId string) (taskStatistics1 request.TaskStatistics) {
  814. key := fmt.Sprintf(taskStatistics, date)
  815. data, err := global.GVA_REDIS.HGet(ctx, key, gameId).Result()
  816. if err != nil {
  817. if err == redis.Nil {
  818. global.GVA_LOG.Info("TaskStatisticsDataCache"+key+gameId, zap.Error(err))
  819. } else {
  820. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache"+key, zap.Error(err))
  821. return
  822. }
  823. }
  824. _ = json.Unmarshal([]byte(data), &taskStatistics1)
  825. return
  826. }
  827. // 同步群控任务数据到缓存
  828. func (s *ServiceStatisticsLog) TaskStatisticsDataCache() {
  829. ctx := context.Background()
  830. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  831. data, err := s.LogicalLog.RequestJfRoom()
  832. if err != nil {
  833. global.GVA_LOG.Error("获取机房数据失败TaskStatisticsDataCache", zap.Error(err))
  834. return
  835. }
  836. dataTask, err := s.LogicalLog.RequestTaskData()
  837. if err != nil {
  838. global.GVA_LOG.Error("RequestTaskData", zap.Error(err))
  839. return
  840. }
  841. var taskData []request.TaskData
  842. var taskStatistics []request.TaskStatistics
  843. _ = json.Unmarshal(data, &taskStatistics)
  844. _ = json.Unmarshal(dataTask, &taskData)
  845. mps := map[int]request.TaskData{}
  846. for _, td := range taskData {
  847. mps[td.GameId] = td
  848. }
  849. for _, data := range taskStatistics {
  850. var id int
  851. id = data.GameId
  852. if _, ok := mps[id]; ok {
  853. data.NewScanningCode = mps[id].NewScanningCode
  854. data.RetainedPullAccount = mps[id].RetainedPullAccount
  855. data.NewPullAccount = mps[id].NewPullAccount
  856. data.FeeAccountNum = mps[id].FeeAccountNum
  857. data.RetainedAccountNum = mps[id].RetainedAccountNum
  858. }
  859. bd, _ := json.Marshal(data)
  860. err = global.GVA_REDIS.HSet(ctx, key, id, bd).Err()
  861. if err != nil {
  862. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  863. return
  864. }
  865. }
  866. }
  867. // 同步群控任务数据到缓存
  868. func (s *ServiceStatisticsLog) TaskStatisticsDataCache1() {
  869. ctx := context.Background()
  870. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  871. data, err := s.LogicalLog.RequestJfRoom()
  872. if err != nil {
  873. global.GVA_LOG.Error("获取机房数据失败TaskStatisticsDataCache", zap.Error(err))
  874. return
  875. }
  876. dataTask, err := s.LogicalLog.RequestTaskData()
  877. if err != nil {
  878. global.GVA_LOG.Error("RequestTaskData", zap.Error(err))
  879. return
  880. }
  881. var taskData []request.TaskData
  882. var taskStatistics []request.TaskStatistics
  883. _ = json.Unmarshal(data, &taskStatistics)
  884. _ = json.Unmarshal(dataTask, &taskData)
  885. mps := map[int]request.TaskData{}
  886. for _, td := range taskData {
  887. mps[td.GameId] = td
  888. }
  889. for _, data := range taskStatistics {
  890. var id int
  891. if data.GameIdXmy != "" {
  892. if data.GameIdXmy == "0" {
  893. id = data.GameId
  894. } else {
  895. id, _ = strconv.Atoi(data.GameIdXmy)
  896. }
  897. } else {
  898. id = data.GameId
  899. }
  900. if _, ok := mps[id]; ok {
  901. data.NewScanningCode = mps[id].NewScanningCode
  902. data.RetainedPullAccount = mps[id].RetainedPullAccount
  903. data.NewPullAccount = mps[id].NewPullAccount
  904. data.FeeAccountNum = mps[id].FeeAccountNum
  905. data.RetainedAccountNum = mps[id].RetainedAccountNum
  906. }
  907. bd, _ := json.Marshal(data)
  908. err = global.GVA_REDIS.HSet(ctx, key, id, bd).Err()
  909. if err != nil {
  910. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  911. return
  912. }
  913. }
  914. }
  915. // 同步群控任务数据到缓存
  916. func (s *ServiceStatisticsLog) GetComputerNum(date string) int64 {
  917. var total int64
  918. if date == "" {
  919. date = s.CurrentDate()
  920. }
  921. db := global.GVA_DB.Model(&log.LogComputer{})
  922. db = db.Distinct("pc_code").Where("create_date = ?", date)
  923. _ = db.Count(&total).Error
  924. return total
  925. }
  926. // 在线电脑接口
  927. func (s *ServiceStatisticsLog) ComputerHeartbeat(c context.Context, onlineComputer request.OnlineComputerRequest) error {
  928. err := s.LogicalLog.SetOnlineComputerNumCache(c, s.LogicalLog.CurrentDate(), onlineComputer.PcCode, onlineComputer.Operator)
  929. if err != nil {
  930. return err
  931. }
  932. s.LogicalLog.SetPcReportingLog(c, onlineComputer.PcCode, onlineComputer.Operator)
  933. return err
  934. }
  935. // 在线电脑接口测试
  936. func (s *ServiceStatisticsLog) ComputerTest(c context.Context) (interface{}, error) {
  937. mps, err := s.LogicalLog.GetOnlineComputerNumCache(c, s.LogicalLog.CurrentDate())
  938. if err != nil {
  939. return mps, err
  940. }
  941. return mps, err
  942. }
  943. // 定时检查电脑情况
  944. func (s *ServiceStatisticsLog) RegularCheckPc1() {
  945. ctx := context.Background()
  946. date := s.LogicalLog.CurrentDate()
  947. // 获取当天已上报的电脑数据
  948. onlineComputerMpa, _ := s.LogicalLog.GetOnlineComputerNumCache(ctx, date)
  949. // 获取数据库的数据
  950. var computer log.Computer
  951. computers, err := computer.OnlinePcCodeCache()
  952. if err != nil {
  953. global.GVA_LOG.Error("获取租机表数据失败", zap.Error(err))
  954. }
  955. // 查询两小时内上报的数据
  956. var noReportingPc []string
  957. for pc, _ := range onlineComputerMpa {
  958. num := s.LogicalLog.GetPcReportingLog(ctx, pc)
  959. delete(computers, pc)
  960. if num == 0 {
  961. noReportingPc = append(noReportingPc, pc)
  962. }
  963. }
  964. // 未上报逻辑
  965. var errReportingPc []string
  966. for pc, name := range computers {
  967. if name == "备用" {
  968. continue
  969. }
  970. errReportingPc = append(errReportingPc, pc)
  971. }
  972. var content string
  973. if len(errReportingPc) != 0 {
  974. // 发信息数据群里
  975. content += fmt.Sprintf("异常租机数据<font color=\"warning\">%d台</font>: %s", len(errReportingPc), errReportingPc)
  976. }
  977. if len(noReportingPc) != 0 {
  978. // 发信息数据群里
  979. content += "\n"
  980. content += fmt.Sprintf("两小时内未检测到中控数据<font color=\"warning\">%d台</font>: %s", len(noReportingPc), noReportingPc)
  981. }
  982. if content != "" {
  983. var sendMsg SendMsg
  984. sendMsg.MsgType = "markdown"
  985. sendMsg.Markdown.Content = content
  986. //c ,_ := json.Marshal(sendMsg)
  987. url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
  988. //url := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b"
  989. _, err = s.SendMsgData(url, sendMsg)
  990. }
  991. }
  992. // 定时检查电脑情况
  993. func (s *ServiceStatisticsLog) RegularCheckPc() {
  994. ctx := context.Background()
  995. var computer log.Computer
  996. computers, err := computer.OnlinePcCodeCache()
  997. //computersF, err := computer.OnlinePcCodeCache()
  998. if err != nil {
  999. global.GVA_LOG.Error("获取租机表数据失败", zap.Error(err))
  1000. }
  1001. // 查询两小时内上报的数据
  1002. var computersF = map[string]string{}
  1003. var noReportingPc []string
  1004. for pc, name := range computers {
  1005. computersF[pc] = name
  1006. if strings.Contains(name, "备用") {
  1007. delete(computers, pc)
  1008. continue
  1009. }
  1010. num := s.LogicalLog.GetPcReportingLog(ctx, pc)
  1011. delete(computers, pc)
  1012. if num == 0 {
  1013. noReportingPc = append(noReportingPc, pc)
  1014. }
  1015. }
  1016. var content string
  1017. if len(noReportingPc) != 0 {
  1018. content += fmt.Sprintf("两小时内未检测到中控数据<font color=\"warning\">%d台</font>:", len(noReportingPc))
  1019. for _, pc := range noReportingPc {
  1020. content += "\n"
  1021. name := computersF[pc]
  1022. content += name + " : " + pc
  1023. }
  1024. }
  1025. if content != "" {
  1026. var sendMsg SendMsg
  1027. sendMsg.MsgType = "markdown"
  1028. sendMsg.Markdown.Content = content
  1029. //c ,_ := json.Marshal(sendMsg)
  1030. url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
  1031. //url := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b"
  1032. _, err = s.SendMsgData(url, sendMsg)
  1033. }
  1034. }
  1035. type SendMsg struct {
  1036. MsgType string `json:"msgtype"`
  1037. Markdown struct {
  1038. Content string `json:"content"`
  1039. } `json:"markdown"`
  1040. }
  1041. func (s *ServiceStatisticsLog) SendMsgData(url string, params interface{}) (result []byte, err error) {
  1042. result, err = utils.HttpPost(url, params)
  1043. return
  1044. }
  1045. var statusMps = map[int]string{
  1046. -1: "未上报",
  1047. 1: "中控上报",
  1048. 2: "任务上报",
  1049. }
  1050. func (exa *ServiceStatisticsLog) ParseInfoList2Excel(infoList []*response.ComputerUseLogReply, filePath string) error {
  1051. excel := excelize.NewFile()
  1052. excel.SetSheetRow("Sheet1", "A1", &[]string{"电脑编号", "使用者", "日期", "拉取账号", "进入主线", "主线成功率", "使用状态", "备注"})
  1053. for i, statisticsLog := range infoList {
  1054. axis := fmt.Sprintf("A%d", i+2)
  1055. var r interface{}
  1056. if statisticsLog.EnterMainTotal != 0 && statisticsLog.PullAccountTotal != 0 {
  1057. r = fmt.Sprintf("%.2f", float64(statisticsLog.EnterMainTotal)/float64(statisticsLog.PullAccountTotal)*100)
  1058. } else {
  1059. r = "0.00"
  1060. }
  1061. excel.SetSheetRow("Sheet1", axis, &[]interface{}{
  1062. statisticsLog.PcCode,
  1063. statisticsLog.Operator,
  1064. statisticsLog.CreateDate[:10],
  1065. statisticsLog.PullAccountTotal,
  1066. statisticsLog.EnterMainTotal,
  1067. r,
  1068. statusMps[statisticsLog.Status],
  1069. statisticsLog.Remarks,
  1070. })
  1071. }
  1072. err := excel.SaveAs(filePath)
  1073. return err
  1074. }
  1075. // 删除两天前的缓存数据
  1076. func (s *ServiceStatisticsLog) RegularDelCheckData() {
  1077. ctx := context.Background()
  1078. date := time.Now().Add(-time.Hour * 48).Format("2006-01-02")
  1079. s.LogicalLog.DelHashUuidKey(ctx, date)
  1080. s.LogicalLog.DelZSetKey(ctx, date)
  1081. s.LogicalLog.DelHashKey(ctx, date)
  1082. }
  1083. //@author: [piexlmax](https://github.com/piexlmax)
  1084. //@function: UpdatePc
  1085. //@description: 根据id更新pc
  1086. //@param: Computer log.Computer
  1087. //@return: err error
  1088. func (a *ServiceStatisticsLog) UpdateComputerUseLog(c log.ComputerUseRemarks) (err error) {
  1089. if !errors.Is(global.GVA_DB.Where("create_date = ?", c.CreateDate).Where("pc_code = ?", c.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  1090. var updateInfo = make(map[string]interface{})
  1091. updateInfo["remarks"] = c.Remarks
  1092. updateInfo["remarks_update_time"] = time.Now().Format("2006-01-02 15:04:05")
  1093. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", c.CreateDate).Where("pc_code = ?", c.PcCode).Updates(updateInfo).Error
  1094. }
  1095. return
  1096. }
  1097. // 统计上报的订单
  1098. //@function: GetScanningInfoList
  1099. //@description: 分页获取数据,
  1100. //@param: card card.Card, info request.PageInfo, order string, desc bool
  1101. //@return: list interface{}, total int64, err error
  1102. func (apiService *ServiceStatisticsLog) GetScanningInfoList(api request.LogScanningRequest, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
  1103. limit := info.PageSize
  1104. offset := info.PageSize * (info.Page - 1)
  1105. db := global.GVA_DB.Model(&log.LogScanningCode{})
  1106. var apiList []log.LogScanningCode
  1107. if api.CreateDate == "" {
  1108. api.CreateDate = time.Now().Format("2006-01-02")
  1109. }
  1110. db = db.Where("create_date = ?", api.CreateDate)
  1111. if api.GameId != 0 {
  1112. db = db.Where("game_id = ?", api.GameId)
  1113. }
  1114. if api.Status != 0 {
  1115. db = db.Where("status = ?", api.Status)
  1116. }
  1117. if api.TaskType != 0 {
  1118. if api.TaskType == -1 {
  1119. db = db.Where("task_type = ?", 0)
  1120. } else {
  1121. db = db.Where("task_type = ?", api.TaskType)
  1122. }
  1123. }
  1124. err = db.Count(&total).Error
  1125. if err != nil {
  1126. return apiList, total, err
  1127. } else {
  1128. db = db.Limit(limit).Offset(offset)
  1129. if order != "" {
  1130. var OrderStr string
  1131. // 设置有效排序key 防止sql注入
  1132. // 感谢 Tom4t0 提交漏洞信息
  1133. orderMap := make(map[string]bool, 4)
  1134. orderMap["id"] = true
  1135. orderMap["create_date"] = true
  1136. orderMap["game_id"] = true
  1137. orderMap["status"] = true
  1138. if orderMap[order] {
  1139. if desc {
  1140. OrderStr = order + " desc"
  1141. } else {
  1142. OrderStr = order
  1143. }
  1144. } else { // didn't matched any order key in `orderMap`
  1145. err = fmt.Errorf("非法的排序字段: %v", order)
  1146. return apiList, total, err
  1147. }
  1148. err = db.Order(OrderStr).Find(&apiList).Error
  1149. } else {
  1150. err = db.Order("id desc").Find(&apiList).Error
  1151. }
  1152. }
  1153. var apisReply []response.LogScanningReply
  1154. if len(apiList) != 0 {
  1155. for _, apiInfo := range apiList {
  1156. var apiReply = response.LogScanningReply{}
  1157. apiReply.Status = apiInfo.Status
  1158. apiReply.TaskType = apiInfo.TaskType
  1159. apiReply.GameId = apiInfo.GameId
  1160. apiReply.Supplier = apiInfo.Supplier
  1161. apiReply.OrderNum = apiInfo.OrderNum
  1162. apiReply.CreateDate = apiInfo.CreateTime.Format("2006-01-02")
  1163. apiReply.CreateTime = apiInfo.CreateTime.Format("2006-01-02 15:04:05")
  1164. apisReply = append(apisReply, apiReply)
  1165. }
  1166. }
  1167. return apisReply, total, err
  1168. }