log_statistics.go 48 KB

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