log_statistics.go 49 KB

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