log_statistics.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. package log
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/go-redis/redis/v8"
  8. "go.uber.org/zap"
  9. "gorm.io/gorm"
  10. "log-server/global"
  11. "log-server/model/log"
  12. "log-server/model/log/request"
  13. "log-server/model/log/response"
  14. loging2 "log-server/service/log/loging"
  15. "math"
  16. "strconv"
  17. "time"
  18. )
  19. type ServiceStatisticsLog struct {
  20. loging2.LogicalLog
  21. }
  22. var StatisticsCompletedKey = "%s:StatisticsCompleted"
  23. var taskStatistics = "%s:taskStatistics"
  24. var ComputerNum = "%s:ComputerNum"
  25. var TaskType = []int{0, 1}
  26. func (s *ServiceStatisticsLog) CreateStatisticsLog() {
  27. key := fmt.Sprintf(StatisticsCompletedKey, s.LogicalLog.CurrentDate())
  28. ctx := context.Background()
  29. b, err := s.LogicalLog.ExistsKey(ctx, key)
  30. if err != nil {
  31. global.GVA_LOG.Error("获取redis key失败!"+key, zap.Error(err))
  32. return
  33. }
  34. if b {
  35. global.GVA_LOG.Info("统计数据已完成!"+key, zap.Error(err))
  36. return
  37. }
  38. gameMps, err := s.LogicalLog.GetGameCache(context.Background(), s.YesterdayDate())
  39. if err != nil {
  40. global.GVA_LOG.Error("获取redis game list失败!", zap.Error(err))
  41. return
  42. }
  43. if len(gameMps) == 0 {
  44. global.GVA_LOG.Info("获取redis game list没有数据!")
  45. return
  46. }
  47. var statisticsLogs []*log.StatisticsLog
  48. for gameId, _ := range gameMps {
  49. for _, tt := range TaskType {
  50. gameIdInt, _ := strconv.Atoi(gameId)
  51. statisticsLogNew := new(log.StatisticsLog)
  52. statisticsLog := s.statisticsData(ctx, gameIdInt, tt, s.LogicalLog.YesterdayDate(), statisticsLogNew)
  53. if !errors.Is(global.GVA_DB.Where("create_date = ?", s.LogicalLog.YesterdayDate()).Where("game_id = ?", gameIdInt).First(&log.StatisticsLog{}).Error, gorm.ErrRecordNotFound) {
  54. // 已存在,跳过
  55. err = global.GVA_DB.Where("game_id", gameIdInt).Where("create_date = ?", s.LogicalLog.YesterdayDate()).Where("type = ?", tt).Updates(statisticsLog).Error
  56. if err != nil {
  57. global.GVA_LOG.Error("update StatisticsLogs失败!", zap.Error(err))
  58. continue
  59. }
  60. continue
  61. }
  62. statisticsLogs = append(statisticsLogs, statisticsLog)
  63. }
  64. }
  65. if len(statisticsLogs) == 0 {
  66. global.GVA_LOG.Info("statisticsLogsGameInfo没有数据!", zap.Error(err))
  67. global.GVA_REDIS.Set(ctx, key, 1, 24*time.Hour)
  68. return
  69. }
  70. err = global.GVA_DB.Create(statisticsLogs).Error
  71. if err != nil {
  72. global.GVA_LOG.Error("添加statisticsLogsGameInfo失败!", zap.Error(err))
  73. return
  74. }
  75. global.GVA_REDIS.Set(ctx, key, 1, 24*time.Hour)
  76. return
  77. }
  78. func (s *ServiceStatisticsLog) TodayCreateStatisticsGameInfoLog() {
  79. date := s.LogicalLog.CurrentDate()
  80. ctx := context.Background()
  81. gameMps, err := s.LogicalLog.GetGameCache(context.Background(), date)
  82. if err != nil {
  83. global.GVA_LOG.Error("获取redis game list失败!", zap.Error(err))
  84. return
  85. }
  86. if len(gameMps) == 0 {
  87. global.GVA_LOG.Info("获取redis game list没有数据!")
  88. return
  89. }
  90. var statisticsLogs []*log.StatisticsLog
  91. for gameId, _ := range gameMps {
  92. for _, tt := range TaskType {
  93. gameIdInt, _ := strconv.Atoi(gameId)
  94. statisticsLogNew := new(log.StatisticsLog)
  95. statisticsLog := s.statisticsData(ctx, gameIdInt, tt, date, statisticsLogNew)
  96. if !errors.Is(global.GVA_DB.Where("create_date = ?", date).Where("game_id = ?", gameIdInt).First(&log.StatisticsLog{}).Error, gorm.ErrRecordNotFound) {
  97. // 已存在,跳过
  98. err = global.GVA_DB.Where("game_id", gameIdInt).Where("create_date = ?", date).Where("type = ?", tt).Updates(statisticsLog).Error
  99. if err != nil {
  100. global.GVA_LOG.Error("update StatisticsLogs失败!", zap.Error(err))
  101. continue
  102. }
  103. continue
  104. }
  105. statisticsLogs = append(statisticsLogs, statisticsLog)
  106. if len(statisticsLogs) == 100 {
  107. err = global.GVA_DB.Create(statisticsLogs).Error
  108. if err != nil {
  109. global.GVA_LOG.Error("添加statisticsLogs失败!", zap.Error(err))
  110. return
  111. }
  112. statisticsLogs = []*log.StatisticsLog{}
  113. }
  114. }
  115. }
  116. if len(statisticsLogs) == 0 {
  117. global.GVA_LOG.Info("statisticsLogsGameInfo没有数据!", zap.Error(err))
  118. return
  119. }
  120. err = global.GVA_DB.Create(statisticsLogs).Error
  121. if err != nil {
  122. global.GVA_LOG.Error("添加statisticsLogsGameInfo失败!", zap.Error(err))
  123. return
  124. }
  125. return
  126. }
  127. func (s *ServiceStatisticsLog) TodayStatisticsLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo) (interface{}, int64, error) {
  128. db := global.GVA_DB.Model(&log.StatisticsLog{})
  129. if api.GameId != 0 {
  130. db = db.Where("game_id = ?", api.GameId)
  131. }
  132. db = db.Where("create_date = ?", s.CurrentDate())
  133. var total int64
  134. err := db.Count(&total).Error
  135. if err != nil {
  136. return nil, 0, err
  137. }
  138. limit := info.PageSize
  139. offset := info.PageSize * (info.Page - 1)
  140. var statisticsLogs []*log.StatisticsLog
  141. var statisticsLogs2 []*log.StatisticsLog
  142. db = db.Limit(limit).Offset(offset)
  143. err = db.Order("id").Find(&statisticsLogs).Error
  144. for _, gameInfo := range statisticsLogs {
  145. tt := gameInfo.Type
  146. gameIdInt := gameInfo.GameId
  147. statisticsLogNew := new(log.StatisticsLog)
  148. statisticsLog := s.statisticsData(ctx, gameIdInt, tt, s.LogicalLog.CurrentDate(), statisticsLogNew)
  149. statisticsLogs2 = append(statisticsLogs2, statisticsLog)
  150. }
  151. return statisticsLogs2, total, err
  152. }
  153. func (s *ServiceStatisticsLog) statisticsData(ctx context.Context, gameIdInt, tt int, date string, statisticsLog *log.StatisticsLog) *log.StatisticsLog {
  154. key := fmt.Sprintf(taskStatistics, date)
  155. gameIdStr := strconv.Itoa(gameIdInt)
  156. data, err := global.GVA_REDIS.HGet(ctx, key, gameIdStr).Result()
  157. if err != nil {
  158. if err == redis.Nil {
  159. global.GVA_LOG.Info("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  160. } else {
  161. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  162. return nil
  163. }
  164. }
  165. var taskStatistics request.TaskStatistics
  166. _ = json.Unmarshal([]byte(data), &taskStatistics)
  167. pullAccountOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4100000", loging2.OkStatus, tt)
  168. pullAccountFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4100000", loging2.FailStatus, tt)
  169. ConstituencyFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4604000", loging2.FailStatus, tt)
  170. ConstituencyOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4604000", loging2.OkStatus, tt)
  171. StartProxyFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4200000", loging2.FailStatus, tt)
  172. StartProxyOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4200000", loging2.OkStatus, tt)
  173. SimulatorStartFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.FailStatus, tt)
  174. SimulatorStartOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.OkStatus, tt)
  175. NetworkCheckFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4400000", loging2.FailStatus, tt)
  176. NetworkCheckOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4400000", loging2.OkStatus, tt)
  177. GameStartFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4500000", loging2.FailStatus, tt)
  178. GameStartOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4500000", loging2.OkStatus, tt)
  179. xmyLoginFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4601000", loging2.FailStatus, tt)
  180. xmyLoginOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4601000", loging2.OkStatus, tt)
  181. wxLoginFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4602000", loging2.FailStatus, tt)
  182. wxLoginOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4602000", loging2.OkStatus, tt)
  183. mzLoginFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4603000", loging2.FailStatus, tt)
  184. mzLoginOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4603000", loging2.OkStatus, tt)
  185. LoginFail := xmyLoginFail + wxLoginFail + mzLoginFail
  186. LoginOk := xmyLoginOk + wxLoginOk + mzLoginOk
  187. EnterMainFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4700000", loging2.FailStatus, tt)
  188. EnterMainOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4700000", loging2.OkStatus, tt)
  189. FeeFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4800000", loging2.FailStatus, tt)
  190. FeeOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4800000", loging2.OkStatus, tt)
  191. ScriptStartFail, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.FailStatus, tt)
  192. ScriptStartOk, _ := s.LogicalLog.NodeLogGetNum(ctx, date, gameIdInt, "4300000", loging2.OkStatus, tt)
  193. EnterGameFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4605000", loging2.FailStatus, tt)
  194. EnterGameOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4605000", loging2.OkStatus, tt)
  195. AuthenticationFail, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4607000", loging2.FailStatus, tt)
  196. AuthenticationOk, _ := s.LogicalLog.TypeLogGetNum(ctx, date, gameIdInt, "4607000", loging2.OkStatus, tt)
  197. EnterStartGame, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4501000", loging2.NoLogStatus, tt)
  198. EnterAuthentication, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4607000", loging2.NoLogStatus, tt)
  199. EnterConstituency, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604000", loging2.NoLogStatus, tt)
  200. EnterGame, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4605000", loging2.NoLogStatus, tt)
  201. EnterMain, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4701000", loging2.NoLogStatus, tt)
  202. EnterFee, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4800000", loging2.NoLogStatus, tt)
  203. EnterXmyLogin, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4601000", loging2.NoLogStatus, tt)
  204. EnterWxLogin, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4602000", loging2.NoLogStatus, tt)
  205. EnterMzLogin, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4603000", loging2.NoLogStatus, tt)
  206. BanOff1, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4601006", loging2.FailStatus, tt) // 小绵羊登录封号
  207. BanOff2, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4602008", loging2.FailStatus, tt) // 微信登录封号
  208. BanOff3, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4603006", loging2.FailStatus, tt) // 魅族登录封号
  209. BanOff4, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4701003", loging2.FailStatus, tt) // 主线封号
  210. Freeze, _ := s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4809904", loging2.NoLogStatus, tt) // 冻结
  211. BanOff := BanOff1 + BanOff2 + BanOff3 + BanOff4
  212. EnterLogin := EnterXmyLogin + EnterMzLogin + EnterWxLogin
  213. //statisticsLog.NewHaveRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604004", loging2.NoLogStatus, tt) // 新增有角色
  214. //statisticsLog.RetainedNotRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604003", loging2.NoLogStatus, tt) // 留存无角色
  215. statisticsLog.PullAccountOk = pullAccountOk
  216. statisticsLog.PullAccountFail = pullAccountFail
  217. statisticsLog.StartProxyFail = StartProxyFail
  218. statisticsLog.StartProxyOk = StartProxyOk
  219. statisticsLog.SimulatorStartFail = SimulatorStartFail
  220. statisticsLog.SimulatorStartOk = SimulatorStartOk
  221. statisticsLog.NetworkCheckFail = NetworkCheckFail
  222. statisticsLog.NetworkCheckOk = NetworkCheckOk
  223. statisticsLog.EnterMainFail = EnterMainFail
  224. statisticsLog.EnterMainOk = EnterMainOk
  225. statisticsLog.EnterGameFail = EnterGameFail
  226. statisticsLog.EnterGameOk = EnterGameOk
  227. statisticsLog.GameId = gameIdInt
  228. statisticsLog.ConstituencyFail = ConstituencyFail
  229. statisticsLog.ConstituencyOk = ConstituencyOk
  230. statisticsLog.FeeFail = FeeFail
  231. statisticsLog.FeeOk = FeeOk
  232. statisticsLog.GameStartFail = GameStartFail
  233. statisticsLog.GameStartOk = GameStartOk
  234. statisticsLog.LoginFail = LoginFail
  235. statisticsLog.LoginOk = LoginOk
  236. statisticsLog.XmyLoginFail = xmyLoginFail
  237. statisticsLog.XmyLoginOk = xmyLoginOk
  238. statisticsLog.WxLoginFail = wxLoginFail
  239. statisticsLog.WxLoginOk = wxLoginOk
  240. statisticsLog.MzLoginFail = mzLoginFail
  241. statisticsLog.MzLoginOk = mzLoginOk
  242. statisticsLog.ScriptStartFail = ScriptStartFail
  243. statisticsLog.ScriptStartOk = ScriptStartOk
  244. statisticsLog.AuthenticationFail = AuthenticationFail
  245. statisticsLog.AuthenticationOk = AuthenticationOk
  246. statisticsLog.EnterStartGame = EnterStartGame
  247. statisticsLog.EnterAuthentication = EnterAuthentication
  248. statisticsLog.EnterConstituency = EnterConstituency
  249. statisticsLog.EnterGame = EnterGame
  250. statisticsLog.EnterMain = EnterMain
  251. statisticsLog.EnterFee = EnterFee
  252. statisticsLog.EnterLogin = EnterLogin
  253. statisticsLog.BanOff = BanOff
  254. statisticsLog.Freeze = Freeze
  255. statisticsLog.Type = tt
  256. statisticsLog.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  257. statisticsLog.CreateDate = date
  258. if tt == 1 {
  259. statisticsLog.NotRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4609901", loging2.NoLogStatus, tt)
  260. statisticsLog.HasRole = 0
  261. statisticsLog.IssuedAccount = taskStatistics.RetainedPullAccount
  262. statisticsLog.TargetNum = taskStatistics.RetainedTarget
  263. statisticsLog.TargetCompleteNum = taskStatistics.RetainedComplete
  264. statisticsLog.ScanningSuccessRate = 0
  265. } else {
  266. statisticsLog.NotRole = 0
  267. statisticsLog.HasRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4609902", loging2.NoLogStatus, tt)
  268. statisticsLog.IssuedAccount = taskStatistics.NewPullAccount
  269. statisticsLog.TargetCompleteNum = taskStatistics.NewComplete
  270. statisticsLog.TargetNum = taskStatistics.NewTarget
  271. statisticsLog.NewScanningCode = taskStatistics.NewScanningCode
  272. if statisticsLog.NewScanningCode != 0 {
  273. statisticsLog.ScanningSuccessRate = float64(statisticsLog.TargetNum) / float64(statisticsLog.NewScanningCode) * 100
  274. }
  275. }
  276. statisticsLog.PayComplete = taskStatistics.PayComplete
  277. statisticsLog.PayTarget = taskStatistics.PayTarget
  278. statisticsLog.OrderCreate = taskStatistics.OrderCreate
  279. statisticsLog.OrderSuccess = taskStatistics.OrderSuccess
  280. statisticsLog.RetainedAccountNum = taskStatistics.RetainedAccountNum
  281. statisticsLog.FeeAccountNum = taskStatistics.FeeAccountNum
  282. statisticsLog.Operator = taskStatistics.Remark
  283. statisticsLog.GameName = taskStatistics.GameName
  284. if statisticsLog.IssuedAccount != 0 {
  285. statisticsLog.PullSuccessRate = float64(statisticsLog.PullAccountOk) / float64(statisticsLog.IssuedAccount) * 100
  286. }
  287. if statisticsLog.PullAccountOk != 0 {
  288. statisticsLog.StartSuccessRate = float64(statisticsLog.SimulatorStartOk) / float64(statisticsLog.PullAccountOk) * 100
  289. }
  290. if statisticsLog.GameStartOk != 0 {
  291. statisticsLog.MainSuccessRate = float64(statisticsLog.EnterMain) / float64(statisticsLog.GameStartOk) * 100
  292. }
  293. if statisticsLog.TargetNum != 0 {
  294. statisticsLog.TaskSuccessRate = float64(statisticsLog.EnterMain) / float64(statisticsLog.TargetNum) * 100
  295. }
  296. if statisticsLog.OrderCreate != 0 {
  297. statisticsLog.LocalOrderSuccessRate = float64(statisticsLog.FeeOk) / float64(statisticsLog.OrderCreate) * 100
  298. }
  299. return statisticsLog
  300. }
  301. func (s *ServiceStatisticsLog) OtherStatisticsLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  302. date := api.CreateDate
  303. if date == "" {
  304. date = s.CurrentDate()
  305. }
  306. db := global.GVA_DB.Model(&log.StatisticsLog{})
  307. if api.GameId != 0 {
  308. db = db.Where("game_id = ?", api.GameId)
  309. }
  310. db = db.Where("create_date = ?", date)
  311. var total int64
  312. err := db.Count(&total).Error
  313. if err != nil {
  314. return nil, 0, err
  315. }
  316. limit := info.PageSize
  317. offset := info.PageSize * (info.Page - 1)
  318. var statisticsLogs []*log.StatisticsLog
  319. db = db.Limit(limit).Offset(offset)
  320. if order != "" {
  321. var OrderStr string
  322. // 设置有效排序key 防止sql注入
  323. // 感谢 Tom4t0 提交漏洞信息
  324. orderMap := make(map[string]bool, 7)
  325. orderMap["pull_success_rate"] = true
  326. orderMap["start_success_rate"] = true
  327. orderMap["main_success_rate"] = true
  328. orderMap["task_success_rate"] = true
  329. orderMap["scanning_success_rate"] = true
  330. orderMap["local_order_success_rate"] = true
  331. orderMap["game_id"] = true
  332. if orderMap[order] {
  333. if desc {
  334. OrderStr = order + " desc"
  335. } else {
  336. OrderStr = order
  337. }
  338. } else { // didn't matched any order key in `orderMap`
  339. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  340. return statisticsLogs, total, err
  341. }
  342. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  343. } else {
  344. err = db.Order("id").Find(&statisticsLogs).Error
  345. }
  346. if err != nil {
  347. return nil, 0, err
  348. }
  349. for _, statisticsLog := range statisticsLogs {
  350. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  351. }
  352. return statisticsLogs, total, err
  353. }
  354. func (s *ServiceStatisticsLog) StatisticsNodeLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  355. date := api.CreateDate
  356. if date == "" || date == s.CurrentDate() {
  357. apilist, total, err := s.TodayStatisticsLogList(ctx, api, info)
  358. return apilist, total, err
  359. }
  360. db := global.GVA_DB.Model(&log.StatisticsLog{})
  361. if api.GameId != 0 {
  362. db = db.Where("game_id = ?", api.GameId)
  363. }
  364. db = db.Where("create_date = ?", date)
  365. var total int64
  366. err := db.Count(&total).Error
  367. if err != nil {
  368. return nil, 0, err
  369. }
  370. limit := info.PageSize
  371. offset := info.PageSize * (info.Page - 1)
  372. var statisticsLogs []*log.StatisticsLog
  373. db = db.Limit(limit).Offset(offset)
  374. err = db.Order("id").Find(&statisticsLogs).Error
  375. if err != nil {
  376. return nil, 0, err
  377. }
  378. for _, statisticsLog := range statisticsLogs {
  379. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  380. }
  381. return statisticsLogs, total, err
  382. }
  383. func (s *ServiceStatisticsLog) ResetStatisticsLog(ctx context.Context, gameId int, date string) {
  384. var total int64
  385. key := date + ":" + strconv.Itoa(gameId) + ":reset"
  386. global.GVA_REDIS.Set(ctx, key, 1, time.Minute*15)
  387. db := global.GVA_DB.Table("loging")
  388. db = db.Where("game_id = ?", gameId)
  389. err := db.Count(&total).Error
  390. if err != nil {
  391. global.GVA_LOG.Error("重置统计数据总数报错", zap.Error(err))
  392. return
  393. }
  394. limit := 100
  395. num := int(math.Ceil(float64(total) / float64(limit)))
  396. offset := 0
  397. err = s.LogicalLog.DelStatisticsNumCache(ctx, date, gameId)
  398. if err != nil {
  399. global.GVA_LOG.Error("重置统计数据删除缓存报错", zap.Error(err))
  400. return
  401. }
  402. err = s.LogicalLog.DelUuidCodeCache(ctx, date, gameId)
  403. if err != nil {
  404. global.GVA_LOG.Error("重置统计数据删除缓存报错2", zap.Error(err))
  405. return
  406. }
  407. global.GVA_LOG.Info("num = " + strconv.Itoa(num))
  408. for i := 0; i < num; i++ {
  409. global.GVA_LOG.Info("offset = " + strconv.Itoa(offset))
  410. var statisticsLogs []request.AddLogRequest
  411. db2 := global.GVA_DB.Table("loging")
  412. db2 = db2.Where("create_date = ?", date)
  413. db2 = db2.Where("game_id = ?", gameId)
  414. db2 = db2.Limit(limit).Offset(offset)
  415. err = db2.Order("id").Find(&statisticsLogs).Error
  416. for _, statisticsLog := range statisticsLogs {
  417. s.ResetStatisticsCache(ctx, statisticsLog, date)
  418. }
  419. offset += limit
  420. }
  421. global.GVA_LOG.Info("重置统计数据完成")
  422. }
  423. func (s *ServiceStatisticsLog) ResetStatisticsCache(c context.Context, api request.AddLogRequest, date string) {
  424. coding := strconv.Itoa(api.Coding)
  425. nodeCoding := coding[:3]
  426. status := coding[5:]
  427. noLogStatus := coding[3:5]
  428. var logical ServiceResetLoging
  429. switch nodeCoding {
  430. case "410":
  431. logical = new(loging2.ResetLog)
  432. break
  433. case "430":
  434. logical = new(loging2.ResetLog)
  435. break
  436. default:
  437. logical = new(loging2.ResetOtherLog)
  438. break
  439. }
  440. var err error
  441. if status == "99" {
  442. err = logical.SuccessLog(c, api, date)
  443. } else if noLogStatus == "99" {
  444. err = logical.NoLogStatusData(c, api, date)
  445. } else {
  446. err = logical.FailLog(c, api, date)
  447. }
  448. if err != nil {
  449. global.GVA_LOG.Error("创建失败!", zap.Error(err))
  450. }
  451. }
  452. // 每天凌晨reset统计数据
  453. func (s *ServiceStatisticsLog) EveryDayResetStatisticsCache() {
  454. date := s.YesterdayDate()
  455. ctx := context.Background()
  456. gameMps, err := s.LogicalLog.GetGameCache(ctx, date)
  457. if err != nil {
  458. global.GVA_LOG.Error("获取redis game list失败!", zap.Error(err))
  459. return
  460. }
  461. if len(gameMps) == 0 {
  462. global.GVA_LOG.Info("获取redis game list没有数据!")
  463. return
  464. }
  465. for gameId, _ := range gameMps {
  466. id, _ := strconv.Atoi(gameId)
  467. s.ResetStatisticsLog(ctx, id, date)
  468. }
  469. global.GVA_LOG.Info("游戏重置缓存完成!")
  470. }
  471. func (s *ServiceStatisticsLog) CreateComputerStatisticsData() {
  472. ctx := context.Background()
  473. codeMps, err := s.LogicalLog.GetComputerCache(ctx, s.LogicalLog.CurrentDate())
  474. if err != nil {
  475. return
  476. }
  477. if len(codeMps) == 0 {
  478. return
  479. }
  480. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  481. onlineComputerMpa, _ := s.LogicalLog.GetOnlineComputerNumCache(ctx, s.LogicalLog.CurrentDate())
  482. fmt.Println(onlineComputerMpa)
  483. var csReplys []*log.LogComputer
  484. for code, r := range codeMps {
  485. accountMps, err := s.LogicalLog.GetComputerPullAccountNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  486. if err != nil {
  487. continue
  488. }
  489. taskMps, err := s.LogicalLog.GetComputerTaskSuccessNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  490. if err != nil {
  491. continue
  492. }
  493. enterMainMps, err := s.LogicalLog.GetComputerEnterMainNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  494. if err != nil {
  495. continue
  496. }
  497. for gameId, num := range accountMps {
  498. csReply := new(log.LogComputer)
  499. if _, ok := onlineComputerMpa[csReply.PcCode]; ok {
  500. delete(onlineComputerMpa, csReply.PcCode)
  501. }
  502. csReply.PcCode = code
  503. csReply.Operator = r
  504. data, err := global.GVA_REDIS.HGet(ctx, key, gameId).Result()
  505. if err != nil {
  506. if err == redis.Nil {
  507. global.GVA_LOG.Info("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  508. } else {
  509. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  510. return
  511. }
  512. }
  513. var taskStatistics request.TaskStatistics
  514. _ = json.Unmarshal([]byte(data), &taskStatistics)
  515. csReply.GameId, _ = strconv.Atoi(gameId)
  516. csReply.PullAccountNum = num
  517. csReply.TaskSuccessNum = taskMps[gameId]
  518. csReply.EnterMain = enterMainMps[gameId]
  519. csReply.CreateDate = time.Now().Format("2006-01-02")
  520. csReply.TargetNum = taskStatistics.NewTarget + taskStatistics.RetainedTarget
  521. csReply.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, csReply.PcCode, csReply.GameId)
  522. csReply.ComputerFreeTime = s.GetStatisticsComputerRate(ctx, csReply.PcCode)
  523. csReply.GameFeeRate = s.GetStatisticsFeeRate(ctx, csReply.GameId)
  524. runTime := time.Now().Hour() + 1 - csReply.ComputerFreeTime
  525. csReply.ComputerHourAverageRate = csReply.TaskSuccessNum / runTime
  526. global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Where("game_id = ?", 0).Delete(&log.LogComputer{})
  527. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  528. // 已存在,更新
  529. mp := make(map[string]interface{})
  530. if csReply.Operator != "" {
  531. mp["operator"] = csReply.Operator
  532. }
  533. mp["target_num"] = taskStatistics.NewTarget + taskStatistics.RetainedTarget
  534. mp["pull_account_num"] = num
  535. mp["task_success_num"] = taskMps[gameId]
  536. mp["computer_fee_rate"] = csReply.ComputerFeeRate
  537. mp["computer_free_time"] = csReply.ComputerFreeTime
  538. mp["game_fee_rate"] = csReply.GameFeeRate
  539. mp["computer_hour_average_rate"] = csReply.ComputerHourAverageRate
  540. mp["enter_main"] = csReply.EnterMain
  541. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Updates(mp).Error
  542. if err != nil {
  543. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  544. }
  545. continue
  546. }
  547. csReplys = append(csReplys, csReply)
  548. }
  549. }
  550. if len(csReplys) != 0 {
  551. err = global.GVA_DB.Table("log_computer").Create(csReplys).Error
  552. if err != nil {
  553. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  554. }
  555. }
  556. // 在线电脑没有跑数据记录
  557. if len(onlineComputerMpa) != 0 {
  558. var onlineComputer []*log.LogComputer
  559. for pcCode, op := range onlineComputerMpa {
  560. csReply := new(log.LogComputer)
  561. csReply.PcCode = pcCode
  562. csReply.Operator = op
  563. csReply.CreateDate = time.Now().Format("2006-01-02")
  564. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  565. continue
  566. }
  567. onlineComputer = append(onlineComputer, csReply)
  568. }
  569. if len(onlineComputer) != 0 {
  570. err = global.GVA_DB.Table("log_computer").Create(onlineComputer).Error
  571. if err != nil {
  572. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  573. }
  574. }
  575. }
  576. return
  577. }
  578. func (s *ServiceStatisticsLog) ComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  579. date := api.CreateDate
  580. isCurrentDate := false
  581. if date == "" {
  582. date = s.CurrentDate()
  583. isCurrentDate = true
  584. }
  585. db := global.GVA_DB.Model(&log.LogComputer{})
  586. var total int64
  587. db = db.Where("create_date = ?", date)
  588. if api.Operator != "" {
  589. db = db.Where("operator = ?", api.Operator)
  590. }
  591. if api.PcCode != "" {
  592. db = db.Where("pc_code = ?", api.PcCode)
  593. }
  594. if api.GameId != 0 {
  595. db = db.Where("game_id = ?", api.GameId)
  596. }
  597. err := db.Count(&total).Error
  598. if err != nil {
  599. return nil, 0, err
  600. }
  601. limit := info.PageSize
  602. offset := info.PageSize * (info.Page - 1)
  603. var statisticsLogs []*log.LogComputer
  604. db = db.Limit(limit).Offset(offset)
  605. if order != "" {
  606. var OrderStr string
  607. // 设置有效排序key 防止sql注入
  608. // 感谢 Tom4t0 提交漏洞信息
  609. orderMap := make(map[string]bool, 3)
  610. orderMap["pc_code"] = true
  611. orderMap["game_id"] = true
  612. orderMap["operator"] = true
  613. if orderMap[order] {
  614. if desc {
  615. OrderStr = order + " desc"
  616. } else {
  617. OrderStr = order
  618. }
  619. } else { // didn't matched any order key in `orderMap`
  620. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  621. return statisticsLogs, total, err
  622. }
  623. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  624. } else {
  625. err = db.Order("id").Find(&statisticsLogs).Error
  626. }
  627. if err != nil {
  628. return nil, 0, err
  629. }
  630. var statisticsLogsComputer []*response.ComputerStatisticsReply1
  631. for _, statisticsLog := range statisticsLogs {
  632. /*var gameInfo []*response.GameInfo
  633. err = json.Unmarshal([]byte(statisticsLog.GameInfo), &gameInfo)
  634. for i, _ := range gameInfo {
  635. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  636. statisticsLogComputer.GameInfo = gameInfo
  637. statisticsLogComputer.PcCode = statisticsLog.PcCode
  638. statisticsLogComputer.Operator = statisticsLog.Operator
  639. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  640. statisticsLogComputer.GameId = gameInfo[i].GameId
  641. statisticsLogComputer.PullAccountNum = gameInfo[i].PullAccountNum
  642. statisticsLogComputer.TaskSuccessNum = gameInfo[i].TaskSuccessNum
  643. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  644. }*/
  645. //gameIdStr:=strconv.Itoa(statisticsLog.GameId)
  646. //taskStatistics := s.GameTargetInfo(ctx,s.LogicalLog.CurrentDate(),gameIdStr)
  647. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  648. statisticsLogComputer.PcCode = statisticsLog.PcCode
  649. statisticsLogComputer.Operator = statisticsLog.Operator
  650. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  651. statisticsLogComputer.GameId = statisticsLog.GameId
  652. statisticsLogComputer.PullAccountNum = statisticsLog.PullAccountNum
  653. statisticsLogComputer.TaskSuccessNum = statisticsLog.TaskSuccessNum
  654. statisticsLogComputer.TargetNum = statisticsLog.TargetNum
  655. statisticsLogComputer.ComputerFreeTime = statisticsLog.ComputerFreeTime
  656. statisticsLogComputer.ComputerFeeRate = statisticsLog.ComputerFeeRate
  657. statisticsLogComputer.EnterMain = statisticsLog.EnterMain
  658. if isCurrentDate {
  659. statisticsLogComputer.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, statisticsLog.PcCode, statisticsLog.GameId)
  660. }
  661. statisticsLogComputer.ComputerHourAverageRate = statisticsLog.ComputerHourAverageRate
  662. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  663. }
  664. return statisticsLogsComputer, total, err
  665. }
  666. // 在线电脑
  667. func (s *ServiceStatisticsLog) OnlineComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  668. date := api.CreateDate
  669. if date == "" {
  670. date = s.CurrentDate()
  671. }
  672. db := global.GVA_DB.Model(&log.LogComputer{})
  673. var total int64
  674. db = db.Where("create_date = ?", date)
  675. if api.Operator != "" {
  676. db = db.Where("operator = ?", api.Operator)
  677. }
  678. if api.PcCode != "" {
  679. db = db.Where("pc_code = ?", api.PcCode)
  680. }
  681. db = db.Group("pc_code")
  682. err := db.Count(&total).Error
  683. if err != nil {
  684. return nil, 0, err
  685. }
  686. limit := info.PageSize
  687. offset := info.PageSize * (info.Page - 1)
  688. var statisticsLogs []*log.LogComputer
  689. db = db.Limit(limit).Offset(offset)
  690. if order != "" {
  691. var OrderStr string
  692. // 设置有效排序key 防止sql注入
  693. // 感谢 Tom4t0 提交漏洞信息
  694. orderMap := make(map[string]bool, 3)
  695. orderMap["pc_code"] = 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 statisticsLogs, 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. var statisticsLogsComputer []*response.ComputerStatisticsReply1
  715. for _, statisticsLog := range statisticsLogs {
  716. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  717. statisticsLogComputer.PcCode = statisticsLog.PcCode
  718. statisticsLogComputer.Operator = statisticsLog.Operator
  719. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  720. statisticsLogComputer.GameId = statisticsLog.GameId
  721. statisticsLogComputer.PullAccountNum = statisticsLog.PullAccountNum
  722. statisticsLogComputer.TaskSuccessNum = statisticsLog.TaskSuccessNum
  723. statisticsLogComputer.TargetNum = statisticsLog.TargetNum
  724. statisticsLogComputer.ComputerFreeTime = statisticsLog.ComputerFreeTime
  725. statisticsLogComputer.ComputerFeeRate = statisticsLog.ComputerFeeRate
  726. statisticsLogComputer.EnterMain = statisticsLog.EnterMain
  727. statisticsLogComputer.ComputerHourAverageRate = statisticsLog.ComputerHourAverageRate
  728. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  729. }
  730. return statisticsLogsComputer, total, err
  731. }
  732. //根据游戏id查询数据
  733. func (s *ServiceStatisticsLog) GameStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo) (interface{}, int64, error) {
  734. date := api.CreateDate
  735. isCurrentDate := false
  736. if date == "" {
  737. date = s.CurrentDate()
  738. isCurrentDate = true
  739. }
  740. db := global.GVA_DB.Model(&log.LogComputer{})
  741. var total int64
  742. db = db.Where("create_date = ?", date)
  743. if api.Operator != "" {
  744. db = db.Where("operator = ?", api.Operator)
  745. }
  746. if api.PcCode != "" {
  747. db = db.Where("pc_code = ?", api.PcCode)
  748. }
  749. if api.GameId != 0 {
  750. db = db.Where("game_id = ?", api.GameId)
  751. }
  752. 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")
  753. err := db.Count(&total).Error
  754. if err != nil {
  755. return nil, 0, err
  756. }
  757. limit := info.PageSize
  758. offset := info.PageSize * (info.Page - 1)
  759. var statisticsLogsByGameId []*response.GameIdStatisticsReply
  760. db = db.Limit(limit).Offset(offset)
  761. err = db.Order("id").Find(&statisticsLogsByGameId).Error
  762. if err != nil {
  763. return nil, 0, err
  764. }
  765. for _, statisticsLog := range statisticsLogsByGameId {
  766. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  767. statisticsLog.OneComputerAverageNum = statisticsLog.TaskSuccessTotal / statisticsLog.PcCodeTotal
  768. if isCurrentDate {
  769. statisticsLog.GameFeeRate = s.GetStatisticsFeeRate(ctx, statisticsLog.GameId)
  770. }
  771. }
  772. return statisticsLogsByGameId, total, err
  773. }
  774. func (s *ServiceStatisticsLog) GameTargetInfo(ctx context.Context, date string, gameId string) (taskStatistics1 request.TaskStatistics) {
  775. key := fmt.Sprintf(taskStatistics, date)
  776. data, err := global.GVA_REDIS.HGet(ctx, key, gameId).Result()
  777. if err != nil {
  778. if err == redis.Nil {
  779. global.GVA_LOG.Info("TaskStatisticsDataCache"+key+gameId, zap.Error(err))
  780. } else {
  781. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache"+key, zap.Error(err))
  782. return
  783. }
  784. }
  785. _ = json.Unmarshal([]byte(data), &taskStatistics1)
  786. return
  787. }
  788. // 同步群控任务数据到缓存
  789. func (s *ServiceStatisticsLog) TaskStatisticsDataCache() {
  790. ctx := context.Background()
  791. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  792. data, err := s.LogicalLog.RequestJfRoom()
  793. if err != nil {
  794. global.GVA_LOG.Error("获取机房数据失败TaskStatisticsDataCache", zap.Error(err))
  795. return
  796. }
  797. dataTask, err := s.LogicalLog.RequestTaskData()
  798. if err != nil {
  799. global.GVA_LOG.Error("RequestTaskData", zap.Error(err))
  800. return
  801. }
  802. var taskData []request.TaskData
  803. var taskStatistics []request.TaskStatistics
  804. _ = json.Unmarshal(data, &taskStatistics)
  805. _ = json.Unmarshal(dataTask, &taskData)
  806. mps := map[int]request.TaskData{}
  807. for _, td := range taskData {
  808. mps[td.GameId] = td
  809. }
  810. for _, data := range taskStatistics {
  811. var id int
  812. id = data.GameId
  813. if _, ok := mps[id]; ok {
  814. data.NewScanningCode = mps[id].NewScanningCode
  815. data.RetainedPullAccount = mps[id].RetainedPullAccount
  816. data.NewPullAccount = mps[id].NewPullAccount
  817. data.FeeAccountNum = mps[id].FeeAccountNum
  818. data.RetainedAccountNum = mps[id].RetainedAccountNum
  819. }
  820. bd, _ := json.Marshal(data)
  821. err = global.GVA_REDIS.HSet(ctx, key, id, bd).Err()
  822. if err != nil {
  823. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  824. return
  825. }
  826. }
  827. }
  828. // 同步群控任务数据到缓存
  829. func (s *ServiceStatisticsLog) TaskStatisticsDataCache1() {
  830. ctx := context.Background()
  831. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  832. data, err := s.LogicalLog.RequestJfRoom()
  833. if err != nil {
  834. global.GVA_LOG.Error("获取机房数据失败TaskStatisticsDataCache", zap.Error(err))
  835. return
  836. }
  837. dataTask, err := s.LogicalLog.RequestTaskData()
  838. if err != nil {
  839. global.GVA_LOG.Error("RequestTaskData", zap.Error(err))
  840. return
  841. }
  842. var taskData []request.TaskData
  843. var taskStatistics []request.TaskStatistics
  844. _ = json.Unmarshal(data, &taskStatistics)
  845. _ = json.Unmarshal(dataTask, &taskData)
  846. mps := map[int]request.TaskData{}
  847. for _, td := range taskData {
  848. mps[td.GameId] = td
  849. }
  850. for _, data := range taskStatistics {
  851. var id int
  852. if data.GameIdXmy != "" {
  853. if data.GameIdXmy == "0" {
  854. id = data.GameId
  855. } else {
  856. id, _ = strconv.Atoi(data.GameIdXmy)
  857. }
  858. } else {
  859. id = data.GameId
  860. }
  861. if _, ok := mps[id]; ok {
  862. data.NewScanningCode = mps[id].NewScanningCode
  863. data.RetainedPullAccount = mps[id].RetainedPullAccount
  864. data.NewPullAccount = mps[id].NewPullAccount
  865. data.FeeAccountNum = mps[id].FeeAccountNum
  866. data.RetainedAccountNum = mps[id].RetainedAccountNum
  867. }
  868. bd, _ := json.Marshal(data)
  869. err = global.GVA_REDIS.HSet(ctx, key, id, bd).Err()
  870. if err != nil {
  871. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  872. return
  873. }
  874. }
  875. }
  876. // 同步群控任务数据到缓存
  877. func (s *ServiceStatisticsLog) GetComputerNum(date string) int64 {
  878. var total int64
  879. if date == "" {
  880. date = s.CurrentDate()
  881. }
  882. db := global.GVA_DB.Model(&log.LogComputer{})
  883. db = db.Distinct("pc_code").Where("create_date = ?", date)
  884. _ = db.Count(&total).Error
  885. return total
  886. }
  887. // 在线电脑接口
  888. func (s *ServiceStatisticsLog) ComputerHeartbeat(c context.Context, onlineComputer request.OnlineComputerRequest) error {
  889. err := s.LogicalLog.SetOnlineComputerNumCache(c, s.LogicalLog.CurrentDate(), onlineComputer.PcCode, onlineComputer.Operator)
  890. return err
  891. }
  892. // 在线电脑接口测试
  893. func (s *ServiceStatisticsLog) ComputerTest(c context.Context) (interface{}, error) {
  894. mps, err := s.LogicalLog.GetOnlineComputerNumCache(c, s.LogicalLog.CurrentDate())
  895. if err != nil {
  896. return mps, err
  897. }
  898. return mps, err
  899. }