log_statistics.go 49 KB

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