log_statistics.go 46 KB

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