log_statistics.go 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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. var csReplys []*log.LogComputer
  503. for code, r := range codeMps {
  504. // 不统计不在电脑列表里的
  505. if _, ok := computerData[code]; !ok {
  506. continue
  507. }
  508. accountMps, err := s.LogicalLog.GetComputerPullAccountNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  509. if err != nil {
  510. continue
  511. }
  512. taskMps, err := s.LogicalLog.GetComputerTaskSuccessNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  513. if err != nil {
  514. continue
  515. }
  516. enterMainMps, err := s.LogicalLog.GetComputerEnterMainNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  517. if err != nil {
  518. continue
  519. }
  520. delete(computerData, code)
  521. for gameId, num := range accountMps {
  522. csReply := new(log.LogComputer)
  523. csReply.PcCode = code
  524. if _, ok := onlineComputerMpa[csReply.PcCode]; ok {
  525. delete(onlineComputerMpa, csReply.PcCode)
  526. }
  527. csReply.Operator = r
  528. data, err := global.GVA_REDIS.HGet(ctx, key, gameId).Result()
  529. if err != nil {
  530. if err == redis.Nil {
  531. global.GVA_LOG.Info("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  532. } else {
  533. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  534. return
  535. }
  536. }
  537. var taskStatistics request.TaskStatistics
  538. _ = json.Unmarshal([]byte(data), &taskStatistics)
  539. csReply.GameId, _ = strconv.Atoi(gameId)
  540. csReply.PullAccountNum = num
  541. csReply.TaskSuccessNum = taskMps[gameId]
  542. csReply.EnterMain = enterMainMps[gameId]
  543. csReply.CreateDate = time.Now().Format("2006-01-02")
  544. csReply.TargetNum = taskStatistics.NewTarget + taskStatistics.RetainedTarget
  545. csReply.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, csReply.PcCode, csReply.GameId)
  546. csReply.ComputerFreeTime = s.GetStatisticsComputerRate(ctx, csReply.PcCode)
  547. csReply.GameFeeRate = s.GetStatisticsFeeRate(ctx, csReply.GameId)
  548. runTime := time.Now().Hour() + 1 - csReply.ComputerFreeTime
  549. csReply.ComputerHourAverageRate = csReply.TaskSuccessNum / runTime
  550. csReply.Status = 2
  551. global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Where("game_id = ?", 0).Delete(&log.LogComputer{})
  552. 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) {
  553. // 已存在,更新
  554. mp := make(map[string]interface{})
  555. if csReply.Operator != "" {
  556. mp["operator"] = csReply.Operator
  557. }
  558. mp["target_num"] = taskStatistics.NewTarget + taskStatistics.RetainedTarget
  559. mp["pull_account_num"] = num
  560. mp["task_success_num"] = taskMps[gameId]
  561. mp["computer_fee_rate"] = csReply.ComputerFeeRate
  562. mp["computer_free_time"] = csReply.ComputerFreeTime
  563. mp["game_fee_rate"] = csReply.GameFeeRate
  564. mp["computer_hour_average_rate"] = csReply.ComputerHourAverageRate
  565. mp["enter_main"] = csReply.EnterMain
  566. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Where("game_id = ?", gameId).Updates(mp).Error
  567. if err != nil {
  568. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  569. }
  570. continue
  571. }
  572. csReplys = append(csReplys, csReply)
  573. }
  574. }
  575. if len(csReplys) != 0 {
  576. err = global.GVA_DB.Table("log_computer").Create(csReplys).Error
  577. if err != nil {
  578. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  579. }
  580. }
  581. // 在线电脑没有跑数据记录
  582. if len(onlineComputerMpa) != 0 {
  583. var onlineComputer []*log.LogComputer
  584. for pcCode, op := range onlineComputerMpa {
  585. delete(computerData, pcCode)
  586. csReply := new(log.LogComputer)
  587. csReply.PcCode = pcCode
  588. csReply.Operator = op
  589. csReply.CreateDate = time.Now().Format("2006-01-02")
  590. csReply.Status = 1
  591. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  592. 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) {
  593. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Update("status", 1).Error
  594. if err != nil {
  595. global.GVA_LOG.Error("更新数据失败onlineComputerMpa", zap.Error(err))
  596. }
  597. }
  598. continue
  599. }
  600. onlineComputer = append(onlineComputer, csReply)
  601. }
  602. if len(onlineComputer) != 0 {
  603. err = global.GVA_DB.Table("log_computer").Create(onlineComputer).Error
  604. if err != nil {
  605. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  606. }
  607. }
  608. }
  609. // 没记录电脑数据
  610. if len(computerData) != 0 {
  611. var onlineComputer []*log.LogComputer
  612. for pcCode, op := range computerData {
  613. csReply := new(log.LogComputer)
  614. csReply.PcCode = pcCode
  615. csReply.Operator = op
  616. csReply.CreateDate = time.Now().Format("2006-01-02")
  617. csReply.Status = -1
  618. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  619. continue
  620. }
  621. onlineComputer = append(onlineComputer, csReply)
  622. }
  623. if len(onlineComputer) != 0 {
  624. err = global.GVA_DB.Table("log_computer").Create(onlineComputer).Error
  625. if err != nil {
  626. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  627. }
  628. }
  629. }
  630. return
  631. }
  632. func (s *ServiceStatisticsLog) ComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) (statisticsLogsComputer []*response.ComputerStatisticsReply1, total int64, err error) {
  633. date := api.CreateDate
  634. isCurrentDate := false
  635. if date == "" {
  636. date = s.CurrentDate()
  637. isCurrentDate = true
  638. }
  639. db := global.GVA_DB.Model(&log.LogComputer{})
  640. db = db.Where("create_date = ?", date)
  641. if api.Operator != "" {
  642. db = db.Where("operator = ?", api.Operator)
  643. }
  644. if api.PcCode != "" {
  645. db = db.Where("pc_code = ?", api.PcCode)
  646. }
  647. if api.GameId != 0 {
  648. db = db.Where("game_id = ?", api.GameId)
  649. }
  650. err = db.Count(&total).Error
  651. if err != nil {
  652. return nil, 0, err
  653. }
  654. limit := info.PageSize
  655. offset := info.PageSize * (info.Page - 1)
  656. var statisticsLogs []*log.LogComputer
  657. db = db.Limit(limit).Offset(offset)
  658. if order != "" {
  659. var OrderStr string
  660. // 设置有效排序key 防止sql注入
  661. // 感谢 Tom4t0 提交漏洞信息
  662. orderMap := make(map[string]bool, 3)
  663. orderMap["pc_code"] = true
  664. orderMap["game_id"] = true
  665. orderMap["operator"] = true
  666. if orderMap[order] {
  667. if desc {
  668. OrderStr = order + " desc"
  669. } else {
  670. OrderStr = order
  671. }
  672. } else { // didn't matched any order key in `orderMap`
  673. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  674. return statisticsLogsComputer, total, err
  675. }
  676. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  677. } else {
  678. err = db.Order("id").Find(&statisticsLogs).Error
  679. }
  680. if err != nil {
  681. return nil, 0, err
  682. }
  683. for _, statisticsLog := range statisticsLogs {
  684. /*var gameInfo []*response.GameInfo
  685. err = json.Unmarshal([]byte(statisticsLog.GameInfo), &gameInfo)
  686. for i, _ := range gameInfo {
  687. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  688. statisticsLogComputer.GameInfo = gameInfo
  689. statisticsLogComputer.PcCode = statisticsLog.PcCode
  690. statisticsLogComputer.Operator = statisticsLog.Operator
  691. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  692. statisticsLogComputer.GameId = gameInfo[i].GameId
  693. statisticsLogComputer.PullAccountNum = gameInfo[i].PullAccountNum
  694. statisticsLogComputer.TaskSuccessNum = gameInfo[i].TaskSuccessNum
  695. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  696. }*/
  697. //gameIdStr:=strconv.Itoa(statisticsLog.GameId)
  698. //taskStatistics := s.GameTargetInfo(ctx,s.LogicalLog.CurrentDate(),gameIdStr)
  699. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  700. statisticsLogComputer.PcCode = statisticsLog.PcCode
  701. statisticsLogComputer.Operator = statisticsLog.Operator
  702. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  703. statisticsLogComputer.GameId = statisticsLog.GameId
  704. statisticsLogComputer.PullAccountNum = statisticsLog.PullAccountNum
  705. statisticsLogComputer.TaskSuccessNum = statisticsLog.TaskSuccessNum
  706. statisticsLogComputer.TargetNum = statisticsLog.TargetNum
  707. statisticsLogComputer.ComputerFreeTime = statisticsLog.ComputerFreeTime
  708. statisticsLogComputer.ComputerFeeRate = statisticsLog.ComputerFeeRate
  709. statisticsLogComputer.EnterMain = statisticsLog.EnterMain
  710. if isCurrentDate {
  711. statisticsLogComputer.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, statisticsLog.PcCode, statisticsLog.GameId)
  712. }
  713. statisticsLogComputer.ComputerHourAverageRate = statisticsLog.ComputerHourAverageRate
  714. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  715. }
  716. return statisticsLogsComputer, total, err
  717. }
  718. // OnlineComputerStatistics 在线电脑
  719. func (s *ServiceStatisticsLog) OnlineComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) ([]*response.ComputerUseLogReply, int64, error) {
  720. date := api.CreateDate
  721. if date == "" {
  722. date = s.CurrentDate()
  723. }
  724. db := global.GVA_DB.Model(&log.LogComputer{})
  725. var total int64
  726. db = db.Select("SUM(pull_account_num) pull_account_total,SUM(enter_main) enter_main_total,operator,pc_code,status,create_date,remarks")
  727. db = db.Where("create_date = ?", date)
  728. if api.Operator != "" {
  729. db = db.Where("operator = ?", api.Operator)
  730. }
  731. if api.PcCode != "" {
  732. db = db.Where("pc_code = ?", api.PcCode)
  733. }
  734. if api.Status != 0 {
  735. db = db.Where("status = ?", api.Status)
  736. }
  737. db = db.Group("pc_code")
  738. err := db.Count(&total).Error
  739. if err != nil {
  740. return nil, 0, err
  741. }
  742. limit := info.PageSize
  743. offset := info.PageSize * (info.Page - 1)
  744. var statisticsLogs []*response.ComputerUseLogReply
  745. db = db.Limit(limit).Offset(offset)
  746. if order != "" {
  747. var OrderStr string
  748. // 设置有效排序key 防止sql注入
  749. // 感谢 Tom4t0 提交漏洞信息
  750. orderMap := make(map[string]bool, 3)
  751. orderMap["pc_code"] = true
  752. orderMap["operator"] = true
  753. if orderMap[order] {
  754. if desc {
  755. OrderStr = order + " desc"
  756. } else {
  757. OrderStr = order
  758. }
  759. } else { // didn't matched any order key in `orderMap`
  760. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  761. return statisticsLogs, total, err
  762. }
  763. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  764. } else {
  765. err = db.Order("id").Find(&statisticsLogs).Error
  766. }
  767. if err != nil {
  768. return nil, 0, err
  769. }
  770. for _, statisticsLog := range statisticsLogs {
  771. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  772. }
  773. return statisticsLogs, total, err
  774. }
  775. // GameStatistics 根据游戏id查询数据
  776. func (s *ServiceStatisticsLog) GameStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo) (interface{}, int64, error) {
  777. date := api.CreateDate
  778. isCurrentDate := false
  779. if date == "" {
  780. date = s.CurrentDate()
  781. isCurrentDate = true
  782. }
  783. db := global.GVA_DB.Model(&log.LogComputer{})
  784. var total int64
  785. db = db.Where("create_date = ?", date)
  786. if api.Operator != "" {
  787. db = db.Where("operator = ?", api.Operator)
  788. }
  789. if api.PcCode != "" {
  790. db = db.Where("pc_code = ?", api.PcCode)
  791. }
  792. if api.GameId != 0 {
  793. db = db.Where("game_id = ?", api.GameId)
  794. }
  795. 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")
  796. err := db.Count(&total).Error
  797. if err != nil {
  798. return nil, 0, err
  799. }
  800. limit := info.PageSize
  801. offset := info.PageSize * (info.Page - 1)
  802. var statisticsLogsByGameId []*response.GameIdStatisticsReply
  803. db = db.Limit(limit).Offset(offset)
  804. err = db.Order("id").Find(&statisticsLogsByGameId).Error
  805. if err != nil {
  806. return nil, 0, err
  807. }
  808. for _, statisticsLog := range statisticsLogsByGameId {
  809. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  810. statisticsLog.OneComputerAverageNum = statisticsLog.TaskSuccessTotal / statisticsLog.PcCodeTotal
  811. if isCurrentDate {
  812. statisticsLog.GameFeeRate = s.GetStatisticsFeeRate(ctx, statisticsLog.GameId)
  813. }
  814. }
  815. return statisticsLogsByGameId, total, err
  816. }
  817. func (s *ServiceStatisticsLog) GameTargetInfo(ctx context.Context, date string, gameId string) (taskStatistics1 request.TaskStatistics) {
  818. key := fmt.Sprintf(taskStatistics, date)
  819. data, err := global.GVA_REDIS.HGet(ctx, key, gameId).Result()
  820. if err != nil {
  821. if err == redis.Nil {
  822. global.GVA_LOG.Info("TaskStatisticsDataCache"+key+gameId, zap.Error(err))
  823. } else {
  824. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache"+key, zap.Error(err))
  825. return
  826. }
  827. }
  828. _ = json.Unmarshal([]byte(data), &taskStatistics1)
  829. return
  830. }
  831. // TaskStatisticsDataCache 同步群控任务数据到缓存
  832. func (s *ServiceStatisticsLog) TaskStatisticsDataCache() {
  833. ctx := context.Background()
  834. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  835. data, err := s.LogicalLog.RequestJfRoom()
  836. if err != nil {
  837. global.GVA_LOG.Error("获取机房数据失败TaskStatisticsDataCache", zap.Error(err))
  838. return
  839. }
  840. dataTask, err := s.LogicalLog.RequestTaskData()
  841. if err != nil {
  842. global.GVA_LOG.Error("RequestTaskData", zap.Error(err))
  843. return
  844. }
  845. var taskData []request.TaskData
  846. var taskStatistics []request.TaskStatistics
  847. _ = json.Unmarshal(data, &taskStatistics)
  848. _ = json.Unmarshal(dataTask, &taskData)
  849. mps := map[int]request.TaskData{}
  850. for _, td := range taskData {
  851. mps[td.GameId] = td
  852. }
  853. for _, data := range taskStatistics {
  854. var id int
  855. id = data.GameId
  856. if _, ok := mps[id]; ok {
  857. data.NewScanningCode = mps[id].NewScanningCode
  858. data.RetainedPullAccount = mps[id].RetainedPullAccount
  859. data.NewPullAccount = mps[id].NewPullAccount
  860. data.FeeAccountNum = mps[id].FeeAccountNum
  861. data.RetainedAccountNum = mps[id].RetainedAccountNum
  862. }
  863. bd, _ := json.Marshal(data)
  864. err = global.GVA_REDIS.HSet(ctx, key, id, bd).Err()
  865. if err != nil {
  866. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  867. return
  868. }
  869. }
  870. }
  871. // TaskStatisticsDataCache1 同步群控任务数据到缓存
  872. func (s *ServiceStatisticsLog) TaskStatisticsDataCache1() {
  873. ctx := context.Background()
  874. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  875. data, err := s.LogicalLog.RequestJfRoom()
  876. if err != nil {
  877. global.GVA_LOG.Error("获取机房数据失败TaskStatisticsDataCache", zap.Error(err))
  878. return
  879. }
  880. dataTask, err := s.LogicalLog.RequestTaskData()
  881. if err != nil {
  882. global.GVA_LOG.Error("RequestTaskData", zap.Error(err))
  883. return
  884. }
  885. var taskData []request.TaskData
  886. var taskStatistics []request.TaskStatistics
  887. _ = json.Unmarshal(data, &taskStatistics)
  888. _ = json.Unmarshal(dataTask, &taskData)
  889. mps := map[int]request.TaskData{}
  890. for _, td := range taskData {
  891. mps[td.GameId] = td
  892. }
  893. for _, data := range taskStatistics {
  894. var id int
  895. if data.GameIdXmy != "" {
  896. if data.GameIdXmy == "0" {
  897. id = data.GameId
  898. } else {
  899. id, _ = strconv.Atoi(data.GameIdXmy)
  900. }
  901. } else {
  902. id = data.GameId
  903. }
  904. if _, ok := mps[id]; ok {
  905. data.NewScanningCode = mps[id].NewScanningCode
  906. data.RetainedPullAccount = mps[id].RetainedPullAccount
  907. data.NewPullAccount = mps[id].NewPullAccount
  908. data.FeeAccountNum = mps[id].FeeAccountNum
  909. data.RetainedAccountNum = mps[id].RetainedAccountNum
  910. }
  911. bd, _ := json.Marshal(data)
  912. err = global.GVA_REDIS.HSet(ctx, key, id, bd).Err()
  913. if err != nil {
  914. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  915. return
  916. }
  917. }
  918. }
  919. // GetComputerNum 同步群控任务数据到缓存
  920. func (s *ServiceStatisticsLog) GetComputerNum(date string) int64 {
  921. var total int64
  922. if date == "" {
  923. date = s.CurrentDate()
  924. }
  925. db := global.GVA_DB.Model(&log.LogComputer{})
  926. db = db.Distinct("pc_code").Where("create_date = ?", date)
  927. _ = db.Count(&total).Error
  928. return total
  929. }
  930. // ComputerHeartbeat 在线电脑接口
  931. func (s *ServiceStatisticsLog) ComputerHeartbeat(c context.Context, onlineComputer request.OnlineComputerRequest) error {
  932. err := s.LogicalLog.SetOnlineComputerNumCache(c, s.LogicalLog.CurrentDate(), onlineComputer.PcCode, onlineComputer.Operator)
  933. if err != nil {
  934. return err
  935. }
  936. s.LogicalLog.SetPcReportingLog(c, onlineComputer.PcCode, onlineComputer.Operator)
  937. return err
  938. }
  939. // ComputerTest 在线电脑接口测试
  940. func (s *ServiceStatisticsLog) ComputerTest(c context.Context) (interface{}, error) {
  941. mps, err := s.LogicalLog.GetOnlineComputerNumCache(c, s.LogicalLog.CurrentDate())
  942. if err != nil {
  943. return mps, err
  944. }
  945. return mps, err
  946. }
  947. // RegularCheckPc1 定时检查电脑情况
  948. func (s *ServiceStatisticsLog) RegularCheckPc1() {
  949. ctx := context.Background()
  950. date := s.LogicalLog.CurrentDate()
  951. // 获取当天已上报的电脑数据
  952. onlineComputerMpa, _ := s.LogicalLog.GetOnlineComputerNumCache(ctx, date)
  953. // 获取数据库的数据
  954. var computer log.Computer
  955. computers, err := computer.OnlinePcCodeCache()
  956. if err != nil {
  957. global.GVA_LOG.Error("获取租机表数据失败", zap.Error(err))
  958. }
  959. // 查询两小时内上报的数据
  960. var noReportingPc []string
  961. for pc, _ := range onlineComputerMpa {
  962. num := s.LogicalLog.GetPcReportingLog(ctx, pc)
  963. delete(computers, pc)
  964. if num == 0 {
  965. noReportingPc = append(noReportingPc, pc)
  966. }
  967. }
  968. // 未上报逻辑
  969. var errReportingPc []string
  970. for pc, name := range computers {
  971. if name == "备用" {
  972. continue
  973. }
  974. errReportingPc = append(errReportingPc, pc)
  975. }
  976. var content string
  977. if len(errReportingPc) != 0 {
  978. // 发信息数据群里
  979. content += fmt.Sprintf("异常租机数据<font color=\"warning\">%d台</font>: %s", len(errReportingPc), errReportingPc)
  980. }
  981. if len(noReportingPc) != 0 {
  982. // 发信息数据群里
  983. content += "\n"
  984. content += fmt.Sprintf("两小时内未检测到中控数据<font color=\"warning\">%d台</font>: %s", len(noReportingPc), noReportingPc)
  985. }
  986. if content != "" {
  987. var sendMsg SendMsg
  988. sendMsg.MsgType = "markdown"
  989. sendMsg.Markdown.Content = content
  990. //c ,_ := json.Marshal(sendMsg)
  991. url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
  992. //url := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b"
  993. _, err = s.SendMsgData(url, sendMsg)
  994. }
  995. }
  996. // 定时检查电脑情况
  997. func (s *ServiceStatisticsLog) RegularCheckPc() {
  998. ctx := context.Background()
  999. var computer log.Computer
  1000. computers, err := computer.OnlinePcCodeCache()
  1001. //computersF, err := computer.OnlinePcCodeCache()
  1002. if err != nil {
  1003. global.GVA_LOG.Error("获取租机表数据失败", zap.Error(err))
  1004. }
  1005. // 查询两小时内上报的数据
  1006. var computersF = map[string]string{}
  1007. var noReportingPc []string
  1008. for pc, name := range computers {
  1009. computersF[pc] = name
  1010. if strings.Contains(name, "备用") {
  1011. delete(computers, pc)
  1012. continue
  1013. }
  1014. num := s.LogicalLog.GetPcReportingLog(ctx, pc)
  1015. delete(computers, pc)
  1016. if num == 0 {
  1017. noReportingPc = append(noReportingPc, pc)
  1018. }
  1019. }
  1020. mpsPerson, _ := s.Person.GetUserInfo()
  1021. var mobile []string
  1022. var content string
  1023. nameMps := map[string]int{}
  1024. if len(noReportingPc) != 0 {
  1025. content += fmt.Sprintf("一小时内未检测到中控数据<font color=\"warning\">%d台</font>:", len(noReportingPc))
  1026. for _, pc := range noReportingPc {
  1027. content += "\n"
  1028. name := computersF[pc]
  1029. content += name + " : " + pc
  1030. nameMps[name] = 1
  1031. }
  1032. }
  1033. if len(nameMps) != 0 {
  1034. for name, _ := range nameMps {
  1035. if _, ok := mpsPerson[name]; ok {
  1036. mobile = append(mobile, mpsPerson[name])
  1037. }
  1038. }
  1039. }
  1040. if content != "" {
  1041. var sendMsg SendMsg
  1042. sendMsg.MsgType = "markdown"
  1043. sendMsg.Markdown.Content = content
  1044. //c ,_ := json.Marshal(sendMsg)
  1045. url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
  1046. //url := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b"
  1047. _, err = s.SendMsgData(url, sendMsg)
  1048. if len(mobile) != 0 {
  1049. var sendTextData SendTextMsg
  1050. sendTextData.MsgType = "text"
  1051. sendTextData.Text.MentionedMobileList = mobile
  1052. _, _ = s.SendMsgData(url, sendTextData)
  1053. }
  1054. }
  1055. }
  1056. type SendTextMsg struct {
  1057. MsgType string `json:"msgtype"`
  1058. Text struct {
  1059. MentionedMobileList []string `json:"mentioned_mobile_list"`
  1060. } `json:"text"`
  1061. }
  1062. type SendMsg struct {
  1063. MsgType string `json:"msgtype"`
  1064. Markdown struct {
  1065. Content string `json:"content"`
  1066. } `json:"markdown"`
  1067. }
  1068. func (s *ServiceStatisticsLog) SendMsgData(url string, params interface{}) (result []byte, err error) {
  1069. result, err = utils.HttpPost(url, params)
  1070. return
  1071. }
  1072. var statusMps = map[int]string{
  1073. -1: "未上报",
  1074. 1: "中控上报",
  1075. 2: "任务上报",
  1076. }
  1077. func (exa *ServiceStatisticsLog) ParseInfoList2Excel(infoList []*response.ComputerUseLogReply, filePath string) error {
  1078. excel := excelize.NewFile()
  1079. excel.SetSheetRow("Sheet1", "A1", &[]string{"电脑编号", "使用者", "日期", "拉取账号", "进入主线", "主线成功率", "使用状态", "备注"})
  1080. for i, statisticsLog := range infoList {
  1081. axis := fmt.Sprintf("A%d", i+2)
  1082. var r interface{}
  1083. if statisticsLog.EnterMainTotal != 0 && statisticsLog.PullAccountTotal != 0 {
  1084. r = fmt.Sprintf("%.2f", float64(statisticsLog.EnterMainTotal)/float64(statisticsLog.PullAccountTotal)*100)
  1085. } else {
  1086. r = "0.00"
  1087. }
  1088. excel.SetSheetRow("Sheet1", axis, &[]interface{}{
  1089. statisticsLog.PcCode,
  1090. statisticsLog.Operator,
  1091. statisticsLog.CreateDate[:10],
  1092. statisticsLog.PullAccountTotal,
  1093. statisticsLog.EnterMainTotal,
  1094. r,
  1095. statusMps[statisticsLog.Status],
  1096. statisticsLog.Remarks,
  1097. })
  1098. }
  1099. err := excel.SaveAs(filePath)
  1100. return err
  1101. }
  1102. // 删除两天前的缓存数据
  1103. func (s *ServiceStatisticsLog) RegularDelCheckData() {
  1104. ctx := context.Background()
  1105. date := time.Now().Add(-time.Hour * 48).Format("2006-01-02")
  1106. s.LogicalLog.DelHashUuidKey(ctx, date)
  1107. s.LogicalLog.DelZSetKey(ctx, date)
  1108. s.LogicalLog.DelHashKey(ctx, date)
  1109. }
  1110. // @author: [piexlmax](https://github.com/piexlmax)
  1111. // @function: UpdatePc
  1112. // @description: 根据id更新pc
  1113. // @param: Computer log.Computer
  1114. // @return: err error
  1115. func (a *ServiceStatisticsLog) UpdateComputerUseLog(c log.ComputerUseRemarks) (err error) {
  1116. if !errors.Is(global.GVA_DB.Where("create_date = ?", c.CreateDate).Where("pc_code = ?", c.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  1117. var updateInfo = make(map[string]interface{})
  1118. updateInfo["remarks"] = c.Remarks
  1119. updateInfo["remarks_update_time"] = time.Now().Format("2006-01-02 15:04:05")
  1120. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", c.CreateDate).Where("pc_code = ?", c.PcCode).Updates(updateInfo).Error
  1121. }
  1122. return
  1123. }
  1124. // 统计上报的订单
  1125. //@function: GetScanningInfoList
  1126. //@description: 分页获取数据,
  1127. //@param: card card.Card, info request.PageInfo, order string, desc bool
  1128. //@return: list interface{}, total int64, err error
  1129. func (apiService *ServiceStatisticsLog) GetScanningInfoList(api request.LogScanningRequest, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
  1130. limit := info.PageSize
  1131. offset := info.PageSize * (info.Page - 1)
  1132. db := global.GVA_DB.Model(&log.LogScanningCode{})
  1133. var apiList []log.LogScanningCode
  1134. if api.CreateDate == "" {
  1135. api.CreateDate = time.Now().Format("2006-01-02")
  1136. }
  1137. db = db.Where("create_date = ?", api.CreateDate)
  1138. if api.GameId != 0 {
  1139. db = db.Where("game_id = ?", api.GameId)
  1140. }
  1141. if api.Status != 0 {
  1142. db = db.Where("status = ?", api.Status)
  1143. }
  1144. if api.TaskType != 0 {
  1145. if api.TaskType == -1 {
  1146. db = db.Where("task_type = ?", 0)
  1147. } else {
  1148. db = db.Where("task_type = ?", api.TaskType)
  1149. }
  1150. }
  1151. err = db.Count(&total).Error
  1152. if err != nil {
  1153. return apiList, total, err
  1154. } else {
  1155. db = db.Limit(limit).Offset(offset)
  1156. if order != "" {
  1157. var OrderStr string
  1158. // 设置有效排序key 防止sql注入
  1159. // 感谢 Tom4t0 提交漏洞信息
  1160. orderMap := make(map[string]bool, 4)
  1161. orderMap["id"] = true
  1162. orderMap["create_date"] = true
  1163. orderMap["game_id"] = true
  1164. orderMap["status"] = true
  1165. if orderMap[order] {
  1166. if desc {
  1167. OrderStr = order + " desc"
  1168. } else {
  1169. OrderStr = order
  1170. }
  1171. } else { // didn't matched any order key in `orderMap`
  1172. err = fmt.Errorf("非法的排序字段: %v", order)
  1173. return apiList, total, err
  1174. }
  1175. err = db.Order(OrderStr).Find(&apiList).Error
  1176. } else {
  1177. err = db.Order("id desc").Find(&apiList).Error
  1178. }
  1179. }
  1180. var apisReply []response.LogScanningReply
  1181. if len(apiList) != 0 {
  1182. for _, apiInfo := range apiList {
  1183. var apiReply = response.LogScanningReply{}
  1184. apiReply.Status = apiInfo.Status
  1185. apiReply.TaskType = apiInfo.TaskType
  1186. apiReply.GameId = apiInfo.GameId
  1187. apiReply.Supplier = apiInfo.Supplier
  1188. apiReply.OrderNum = apiInfo.OrderNum
  1189. apiReply.CreateDate = apiInfo.CreateTime.Format("2006-01-02")
  1190. apiReply.CreateTime = apiInfo.CreateTime.Format("2006-01-02 15:04:05")
  1191. apisReply = append(apisReply, apiReply)
  1192. }
  1193. }
  1194. return apisReply, total, err
  1195. }
  1196. func (exa *ServiceStatisticsLog) ComputeRateList2Excel(infoList []*response.ComputerStatisticsReply1, filePath string) error {
  1197. excel := excelize.NewFile()
  1198. excel.SetSheetRow("Sheet1", "A1", &[]string{"电脑编号", "使用者", "日期", "游戏id", "目标数量", "拉取账号", "进入主线", "主线成功", "半小时付费", "任务完成效率", "空闲时间"})
  1199. for i, statisticsLog := range infoList {
  1200. axis := fmt.Sprintf("A%d", i+2)
  1201. excel.SetSheetRow("Sheet1", axis, &[]interface{}{
  1202. statisticsLog.PcCode,
  1203. statisticsLog.Operator,
  1204. statisticsLog.CreateDate[:10],
  1205. statisticsLog.GameId,
  1206. statisticsLog.TargetNum,
  1207. statisticsLog.PullAccountNum,
  1208. statisticsLog.EnterMain,
  1209. statisticsLog.TaskSuccessNum,
  1210. statisticsLog.ComputerFeeRate,
  1211. statisticsLog.ComputerHourAverageRate,
  1212. statisticsLog.ComputerFreeTime,
  1213. })
  1214. }
  1215. err := excel.SaveAs(filePath)
  1216. return err
  1217. }
  1218. // 电脑效率七天数据查询
  1219. func (s *ServiceStatisticsLog) ComputerSevenStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) (statisticsLogsComputer []*response.ComputerStatisticsReply1, total int64, err error) {
  1220. date := api.CreateDate
  1221. endDate := ""
  1222. startDate := ""
  1223. if date == "" {
  1224. endDate = time.Now().Add(-time.Hour * 24).Format("2006-01-02")
  1225. startDate = time.Now().Add(-time.Hour * 24 * 8).Format("2006-01-02")
  1226. } else {
  1227. formatTime, _ := time.Parse("2006-01-02", date)
  1228. endDate = date
  1229. startDate = formatTime.Add(-time.Hour * 24 * 7).Format("2006-01-02")
  1230. }
  1231. db := global.GVA_DB.Model(&log.LogComputer{})
  1232. db = db.Where("create_date >= ? and create_date <= ?", startDate, endDate)
  1233. db = db.Where("pc_code = ?", api.PcCode)
  1234. err = db.Count(&total).Error
  1235. if err != nil {
  1236. return nil, 0, err
  1237. }
  1238. limit := info.PageSize
  1239. offset := info.PageSize * (info.Page - 1)
  1240. var statisticsLogs []*log.LogComputer
  1241. db = db.Limit(limit).Offset(offset)
  1242. db.Group("create_date")
  1243. if order != "" {
  1244. var OrderStr string
  1245. // 设置有效排序key 防止sql注入
  1246. // 感谢 Tom4t0 提交漏洞信息
  1247. orderMap := make(map[string]bool, 3)
  1248. orderMap["pc_code"] = true
  1249. orderMap["game_id"] = true
  1250. orderMap["operator"] = true
  1251. if orderMap[order] {
  1252. if desc {
  1253. OrderStr = order + " desc"
  1254. } else {
  1255. OrderStr = order
  1256. }
  1257. } else { // didn't matched any order key in `orderMap`
  1258. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  1259. return statisticsLogsComputer, total, err
  1260. }
  1261. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  1262. } else {
  1263. err = db.Order("id").Find(&statisticsLogs).Error
  1264. }
  1265. if err != nil {
  1266. return nil, 0, err
  1267. }
  1268. for _, statisticsLog := range statisticsLogs {
  1269. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  1270. statisticsLogComputer.PcCode = statisticsLog.PcCode
  1271. statisticsLogComputer.Operator = statisticsLog.Operator
  1272. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  1273. statisticsLogComputer.GameId = statisticsLog.GameId
  1274. statisticsLogComputer.PullAccountNum = statisticsLog.PullAccountNum
  1275. statisticsLogComputer.TaskSuccessNum = statisticsLog.TaskSuccessNum
  1276. statisticsLogComputer.TargetNum = statisticsLog.TargetNum
  1277. statisticsLogComputer.ComputerFreeTime = statisticsLog.ComputerFreeTime
  1278. statisticsLogComputer.ComputerFeeRate = statisticsLog.ComputerFeeRate
  1279. statisticsLogComputer.EnterMain = statisticsLog.EnterMain
  1280. statisticsLogComputer.ComputerFeeRate = statisticsLog.ComputerFeeRate
  1281. statisticsLogComputer.ComputerHourAverageRate = statisticsLog.ComputerHourAverageRate
  1282. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  1283. }
  1284. return statisticsLogsComputer, total, err
  1285. }