log_statistics.go 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. //statisticsLog.NewHaveRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604004", loging2.NoLogStatus, tt) // 新增有角色
  216. //statisticsLog.RetainedNotRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4604003", loging2.NoLogStatus, tt) // 留存无角色
  217. statisticsLog.PullAccountOk = pullAccountOk
  218. statisticsLog.PullAccountFail = pullAccountFail
  219. statisticsLog.StartProxyFail = StartProxyFail
  220. statisticsLog.StartProxyOk = StartProxyOk
  221. statisticsLog.SimulatorStartFail = SimulatorStartFail
  222. statisticsLog.SimulatorStartOk = SimulatorStartOk
  223. statisticsLog.NetworkCheckFail = NetworkCheckFail
  224. statisticsLog.NetworkCheckOk = NetworkCheckOk
  225. statisticsLog.EnterMainFail = EnterMainFail
  226. statisticsLog.EnterMainOk = EnterMainOk
  227. statisticsLog.EnterGameFail = EnterGameFail
  228. statisticsLog.EnterGameOk = EnterGameOk
  229. statisticsLog.GameId = gameIdInt
  230. statisticsLog.ConstituencyFail = ConstituencyFail
  231. statisticsLog.ConstituencyOk = ConstituencyOk
  232. statisticsLog.FeeFail = FeeFail
  233. statisticsLog.FeeOk = FeeOk
  234. statisticsLog.GameStartFail = GameStartFail
  235. statisticsLog.GameStartOk = GameStartOk
  236. statisticsLog.LoginFail = LoginFail
  237. statisticsLog.LoginOk = LoginOk
  238. statisticsLog.XmyLoginFail = xmyLoginFail
  239. statisticsLog.XmyLoginOk = xmyLoginOk
  240. statisticsLog.WxLoginFail = wxLoginFail
  241. statisticsLog.WxLoginOk = wxLoginOk
  242. statisticsLog.MzLoginFail = mzLoginFail
  243. statisticsLog.MzLoginOk = mzLoginOk
  244. statisticsLog.ScriptStartFail = ScriptStartFail
  245. statisticsLog.ScriptStartOk = ScriptStartOk
  246. statisticsLog.AuthenticationFail = AuthenticationFail
  247. statisticsLog.AuthenticationOk = AuthenticationOk
  248. statisticsLog.EnterStartGame = EnterStartGame
  249. statisticsLog.EnterAuthentication = EnterAuthentication
  250. statisticsLog.EnterConstituency = EnterConstituency
  251. statisticsLog.EnterGame = EnterGame
  252. statisticsLog.EnterMain = EnterMain
  253. statisticsLog.EnterFee = EnterFee
  254. statisticsLog.EnterLogin = EnterLogin
  255. statisticsLog.BanOff = BanOff
  256. statisticsLog.Freeze = Freeze
  257. statisticsLog.Type = tt
  258. statisticsLog.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  259. statisticsLog.CreateDate = date
  260. if tt == 1 {
  261. statisticsLog.NotRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4609901", loging2.NoLogStatus, tt)
  262. statisticsLog.HasRole = 0
  263. statisticsLog.IssuedAccount = taskStatistics.RetainedPullAccount
  264. statisticsLog.TargetNum = taskStatistics.RetainedTarget
  265. statisticsLog.TargetCompleteNum = taskStatistics.RetainedComplete
  266. statisticsLog.ScanningSuccessRate = 0
  267. } else {
  268. statisticsLog.NotRole = 0
  269. statisticsLog.HasRole, _ = s.LogicalLog.CodeLogGetNum(ctx, date, gameIdInt, "4609902", loging2.NoLogStatus, tt)
  270. statisticsLog.IssuedAccount = taskStatistics.NewPullAccount
  271. statisticsLog.TargetCompleteNum = taskStatistics.NewComplete
  272. statisticsLog.TargetNum = taskStatistics.NewTarget
  273. statisticsLog.NewScanningCode = taskStatistics.NewScanningCode
  274. if statisticsLog.NewScanningCode != 0 {
  275. statisticsLog.ScanningSuccessRate = float64(statisticsLog.TargetNum) / float64(statisticsLog.NewScanningCode) * 100
  276. }
  277. }
  278. statisticsLog.PayComplete = taskStatistics.PayComplete
  279. statisticsLog.PayTarget = taskStatistics.PayTarget
  280. statisticsLog.OrderCreate = taskStatistics.OrderCreate
  281. statisticsLog.OrderSuccess = taskStatistics.OrderSuccess
  282. statisticsLog.RetainedAccountNum = taskStatistics.RetainedAccountNum
  283. statisticsLog.FeeAccountNum = taskStatistics.FeeAccountNum
  284. statisticsLog.Operator = taskStatistics.Remark
  285. statisticsLog.GameName = taskStatistics.GameName
  286. if statisticsLog.IssuedAccount != 0 {
  287. statisticsLog.PullSuccessRate = float64(statisticsLog.PullAccountOk) / float64(statisticsLog.IssuedAccount) * 100
  288. }
  289. if statisticsLog.PullAccountOk != 0 {
  290. statisticsLog.StartSuccessRate = float64(statisticsLog.SimulatorStartOk) / float64(statisticsLog.PullAccountOk) * 100
  291. }
  292. if statisticsLog.GameStartOk != 0 {
  293. statisticsLog.MainSuccessRate = float64(statisticsLog.EnterMain) / float64(statisticsLog.GameStartOk) * 100
  294. }
  295. if statisticsLog.TargetNum != 0 {
  296. statisticsLog.TaskSuccessRate = float64(statisticsLog.EnterMain) / float64(statisticsLog.TargetNum) * 100
  297. }
  298. if statisticsLog.OrderCreate != 0 {
  299. statisticsLog.LocalOrderSuccessRate = float64(statisticsLog.FeeOk) / float64(statisticsLog.OrderCreate) * 100
  300. }
  301. return statisticsLog
  302. }
  303. func (s *ServiceStatisticsLog) OtherStatisticsLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  304. date := api.CreateDate
  305. if date == "" {
  306. date = s.CurrentDate()
  307. }
  308. db := global.GVA_DB.Model(&log.StatisticsLog{})
  309. if api.GameId != 0 {
  310. db = db.Where("game_id = ?", api.GameId)
  311. }
  312. db = db.Where("create_date = ?", date)
  313. var total int64
  314. err := db.Count(&total).Error
  315. if err != nil {
  316. return nil, 0, err
  317. }
  318. limit := info.PageSize
  319. offset := info.PageSize * (info.Page - 1)
  320. var statisticsLogs []*log.StatisticsLog
  321. db = db.Limit(limit).Offset(offset)
  322. if order != "" {
  323. var OrderStr string
  324. // 设置有效排序key 防止sql注入
  325. // 感谢 Tom4t0 提交漏洞信息
  326. orderMap := make(map[string]bool, 7)
  327. orderMap["pull_success_rate"] = true
  328. orderMap["start_success_rate"] = true
  329. orderMap["main_success_rate"] = true
  330. orderMap["task_success_rate"] = true
  331. orderMap["scanning_success_rate"] = true
  332. orderMap["local_order_success_rate"] = true
  333. orderMap["game_id"] = true
  334. if orderMap[order] {
  335. if desc {
  336. OrderStr = order + " desc"
  337. } else {
  338. OrderStr = order
  339. }
  340. } else { // didn't matched any order key in `orderMap`
  341. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  342. return statisticsLogs, total, err
  343. }
  344. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  345. } else {
  346. err = db.Order("id").Find(&statisticsLogs).Error
  347. }
  348. if err != nil {
  349. return nil, 0, err
  350. }
  351. for _, statisticsLog := range statisticsLogs {
  352. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  353. }
  354. return statisticsLogs, total, err
  355. }
  356. func (s *ServiceStatisticsLog) StatisticsNodeLogList(ctx context.Context, api log.StatisticsLog, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  357. date := api.CreateDate
  358. if date == "" || date == s.CurrentDate() {
  359. apilist, total, err := s.TodayStatisticsLogList(ctx, api, info)
  360. return apilist, total, err
  361. }
  362. db := global.GVA_DB.Model(&log.StatisticsLog{})
  363. if api.GameId != 0 {
  364. db = db.Where("game_id = ?", api.GameId)
  365. }
  366. db = db.Where("create_date = ?", date)
  367. var total int64
  368. err := db.Count(&total).Error
  369. if err != nil {
  370. return nil, 0, err
  371. }
  372. limit := info.PageSize
  373. offset := info.PageSize * (info.Page - 1)
  374. var statisticsLogs []*log.StatisticsLog
  375. db = db.Limit(limit).Offset(offset)
  376. err = db.Order("id").Find(&statisticsLogs).Error
  377. if err != nil {
  378. return nil, 0, err
  379. }
  380. for _, statisticsLog := range statisticsLogs {
  381. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  382. }
  383. return statisticsLogs, total, err
  384. }
  385. //func (s *ServiceStatisticsLog) ResetStatisticsLog(ctx context.Context, gameId int, date string) {
  386. // var total int64
  387. // key := date + ":" + strconv.Itoa(gameId) + ":reset"
  388. // global.GVA_REDIS.Set(ctx, key, 1, time.Minute*15)
  389. // db := global.GVA_DB.Table("loging")
  390. // db = db.Where("game_id = ?", gameId)
  391. // err := db.Count(&total).Error
  392. // if err != nil {
  393. // global.GVA_LOG.Error("重置统计数据总数报错", zap.Error(err))
  394. // return
  395. // }
  396. // limit := 100
  397. // num := int(math.Ceil(float64(total) / float64(limit)))
  398. // offset := 0
  399. // err = s.LogicalLog.DelStatisticsNumCache(ctx, date, gameId)
  400. // if err != nil {
  401. // global.GVA_LOG.Error("重置统计数据删除缓存报错", zap.Error(err))
  402. // return
  403. // }
  404. // err = s.LogicalLog.DelUuidCodeCache(ctx, date, gameId)
  405. // if err != nil {
  406. // global.GVA_LOG.Error("重置统计数据删除缓存报错2", zap.Error(err))
  407. // return
  408. // }
  409. // global.GVA_LOG.Info("num = " + strconv.Itoa(num))
  410. // for i := 0; i < num; i++ {
  411. // global.GVA_LOG.Info("offset = " + strconv.Itoa(offset))
  412. // var statisticsLogs []request.AddLogRequest
  413. // db2 := global.GVA_DB.Table("loging")
  414. // db2 = db2.Where("create_date = ?", date)
  415. // db2 = db2.Where("game_id = ?", gameId)
  416. // db2 = db2.Limit(limit).Offset(offset)
  417. // err = db2.Order("id").Find(&statisticsLogs).Error
  418. // for _, statisticsLog := range statisticsLogs {
  419. //
  420. // s.ResetStatisticsCache(ctx, statisticsLog, date)
  421. // }
  422. // offset += limit
  423. // }
  424. // global.GVA_LOG.Info("重置统计数据完成")
  425. //}
  426. //
  427. //func (s *ServiceStatisticsLog) ResetStatisticsCache(c context.Context, api request.AddLogRequest, date string) {
  428. // coding := strconv.Itoa(api.Coding)
  429. // nodeCoding := coding[:3]
  430. // status := coding[5:]
  431. // noLogStatus := coding[3:5]
  432. // var logical ServiceResetLoging
  433. // switch nodeCoding {
  434. // case "410":
  435. // logical = new(loging2.ResetLog)
  436. // break
  437. // case "430":
  438. // logical = new(loging2.ResetLog)
  439. // break
  440. // default:
  441. // logical = new(loging2.ResetOtherLog)
  442. // break
  443. // }
  444. // var err error
  445. // if status == "99" {
  446. // err = logical.SuccessLog(c, api, date)
  447. // } else if noLogStatus == "99" {
  448. // err = logical.NoLogStatusData(c, api, date)
  449. // } else {
  450. // err = logical.FailLog(c, api, date)
  451. // }
  452. // if err != nil {
  453. // global.GVA_LOG.Error("创建失败!", zap.Error(err))
  454. // }
  455. //}
  456. // 每天凌晨reset统计数据
  457. //func (s *ServiceStatisticsLog) EveryDayResetStatisticsCache() {
  458. // date := s.YesterdayDate()
  459. // ctx := context.Background()
  460. // gameMps, err := s.LogicalLog.GetGameCache(ctx, date)
  461. // if err != nil {
  462. // global.GVA_LOG.Error("获取redis game list失败!", zap.Error(err))
  463. // return
  464. // }
  465. // if len(gameMps) == 0 {
  466. // global.GVA_LOG.Info("获取redis game list没有数据!")
  467. // return
  468. // }
  469. // for gameId, _ := range gameMps {
  470. // id, _ := strconv.Atoi(gameId)
  471. // s.ResetStatisticsLog(ctx, id, date)
  472. // }
  473. // global.GVA_LOG.Info("游戏重置缓存完成!")
  474. //}
  475. // 添加和更新电脑指标数据
  476. func (s *ServiceStatisticsLog) CreateComputerStatisticsData() {
  477. ctx := context.Background()
  478. codeMps, err := s.LogicalLog.GetComputerCache(ctx, s.LogicalLog.CurrentDate())
  479. if err != nil {
  480. return
  481. }
  482. if len(codeMps) == 0 {
  483. return
  484. }
  485. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  486. onlineComputerMpa, _ := s.LogicalLog.GetOnlineComputerNumCache(ctx, s.LogicalLog.CurrentDate())
  487. var computer log.Computer
  488. computerData, _ := computer.OnlinePcCodeCache()
  489. var csReplys []*log.LogComputer
  490. for code, r := range codeMps {
  491. accountMps, err := s.LogicalLog.GetComputerPullAccountNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  492. if err != nil {
  493. continue
  494. }
  495. taskMps, err := s.LogicalLog.GetComputerTaskSuccessNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  496. if err != nil {
  497. continue
  498. }
  499. enterMainMps, err := s.LogicalLog.GetComputerEnterMainNumCache(ctx, s.LogicalLog.CurrentDate(), code)
  500. if err != nil {
  501. continue
  502. }
  503. delete(computerData, code)
  504. for gameId, num := range accountMps {
  505. csReply := new(log.LogComputer)
  506. csReply.PcCode = code
  507. if _, ok := onlineComputerMpa[csReply.PcCode]; ok {
  508. delete(onlineComputerMpa, csReply.PcCode)
  509. }
  510. csReply.Operator = r
  511. data, err := global.GVA_REDIS.HGet(ctx, key, gameId).Result()
  512. if err != nil {
  513. if err == redis.Nil {
  514. global.GVA_LOG.Info("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  515. } else {
  516. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  517. return
  518. }
  519. }
  520. var taskStatistics request.TaskStatistics
  521. _ = json.Unmarshal([]byte(data), &taskStatistics)
  522. csReply.GameId, _ = strconv.Atoi(gameId)
  523. csReply.PullAccountNum = num
  524. csReply.TaskSuccessNum = taskMps[gameId]
  525. csReply.EnterMain = enterMainMps[gameId]
  526. csReply.CreateDate = time.Now().Format("2006-01-02")
  527. csReply.TargetNum = taskStatistics.NewTarget + taskStatistics.RetainedTarget
  528. csReply.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, csReply.PcCode, csReply.GameId)
  529. csReply.ComputerFreeTime = s.GetStatisticsComputerRate(ctx, csReply.PcCode)
  530. csReply.GameFeeRate = s.GetStatisticsFeeRate(ctx, csReply.GameId)
  531. runTime := time.Now().Hour() + 1 - csReply.ComputerFreeTime
  532. csReply.ComputerHourAverageRate = csReply.TaskSuccessNum / runTime
  533. csReply.Status = 2
  534. global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Where("game_id = ?", 0).Delete(&log.LogComputer{})
  535. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  536. // 已存在,更新
  537. mp := make(map[string]interface{})
  538. if csReply.Operator != "" {
  539. mp["operator"] = csReply.Operator
  540. }
  541. mp["target_num"] = taskStatistics.NewTarget + taskStatistics.RetainedTarget
  542. mp["pull_account_num"] = num
  543. mp["task_success_num"] = taskMps[gameId]
  544. mp["computer_fee_rate"] = csReply.ComputerFeeRate
  545. mp["computer_free_time"] = csReply.ComputerFreeTime
  546. mp["game_fee_rate"] = csReply.GameFeeRate
  547. mp["computer_hour_average_rate"] = csReply.ComputerHourAverageRate
  548. mp["enter_main"] = csReply.EnterMain
  549. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Updates(mp).Error
  550. if err != nil {
  551. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  552. }
  553. continue
  554. }
  555. csReplys = append(csReplys, csReply)
  556. }
  557. }
  558. if len(csReplys) != 0 {
  559. err = global.GVA_DB.Table("log_computer").Create(csReplys).Error
  560. if err != nil {
  561. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  562. }
  563. }
  564. // 在线电脑没有跑数据记录
  565. if len(onlineComputerMpa) != 0 {
  566. var onlineComputer []*log.LogComputer
  567. for pcCode, op := range onlineComputerMpa {
  568. delete(computerData, pcCode)
  569. csReply := new(log.LogComputer)
  570. csReply.PcCode = pcCode
  571. csReply.Operator = op
  572. csReply.CreateDate = time.Now().Format("2006-01-02")
  573. csReply.Status = 1
  574. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  575. 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) {
  576. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Update("status", 1).Error
  577. if err != nil {
  578. global.GVA_LOG.Error("更新数据失败onlineComputerMpa", zap.Error(err))
  579. }
  580. }
  581. continue
  582. }
  583. onlineComputer = append(onlineComputer, csReply)
  584. }
  585. if len(onlineComputer) != 0 {
  586. err = global.GVA_DB.Table("log_computer").Create(onlineComputer).Error
  587. if err != nil {
  588. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  589. }
  590. }
  591. }
  592. // 没记录电脑数据
  593. if len(computerData) != 0 {
  594. var onlineComputer []*log.LogComputer
  595. for pcCode, op := range computerData {
  596. csReply := new(log.LogComputer)
  597. csReply.PcCode = pcCode
  598. csReply.Operator = op
  599. csReply.CreateDate = time.Now().Format("2006-01-02")
  600. csReply.Status = -1
  601. if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  602. continue
  603. }
  604. onlineComputer = append(onlineComputer, csReply)
  605. }
  606. if len(onlineComputer) != 0 {
  607. err = global.GVA_DB.Table("log_computer").Create(onlineComputer).Error
  608. if err != nil {
  609. global.GVA_LOG.Error("更新数据失败", zap.Error(err))
  610. }
  611. }
  612. }
  613. return
  614. }
  615. func (s *ServiceStatisticsLog) ComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
  616. date := api.CreateDate
  617. isCurrentDate := false
  618. if date == "" {
  619. date = s.CurrentDate()
  620. isCurrentDate = true
  621. }
  622. db := global.GVA_DB.Model(&log.LogComputer{})
  623. var total int64
  624. db = db.Where("create_date = ?", date)
  625. if api.Operator != "" {
  626. db = db.Where("operator = ?", api.Operator)
  627. }
  628. if api.PcCode != "" {
  629. db = db.Where("pc_code = ?", api.PcCode)
  630. }
  631. if api.GameId != 0 {
  632. db = db.Where("game_id = ?", api.GameId)
  633. }
  634. err := db.Count(&total).Error
  635. if err != nil {
  636. return nil, 0, err
  637. }
  638. limit := info.PageSize
  639. offset := info.PageSize * (info.Page - 1)
  640. var statisticsLogs []*log.LogComputer
  641. db = db.Limit(limit).Offset(offset)
  642. if order != "" {
  643. var OrderStr string
  644. // 设置有效排序key 防止sql注入
  645. // 感谢 Tom4t0 提交漏洞信息
  646. orderMap := make(map[string]bool, 3)
  647. orderMap["pc_code"] = true
  648. orderMap["game_id"] = true
  649. orderMap["operator"] = true
  650. if orderMap[order] {
  651. if desc {
  652. OrderStr = order + " desc"
  653. } else {
  654. OrderStr = order
  655. }
  656. } else { // didn't matched any order key in `orderMap`
  657. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  658. return statisticsLogs, total, err
  659. }
  660. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  661. } else {
  662. err = db.Order("id").Find(&statisticsLogs).Error
  663. }
  664. if err != nil {
  665. return nil, 0, err
  666. }
  667. var statisticsLogsComputer []*response.ComputerStatisticsReply1
  668. for _, statisticsLog := range statisticsLogs {
  669. /*var gameInfo []*response.GameInfo
  670. err = json.Unmarshal([]byte(statisticsLog.GameInfo), &gameInfo)
  671. for i, _ := range gameInfo {
  672. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  673. statisticsLogComputer.GameInfo = gameInfo
  674. statisticsLogComputer.PcCode = statisticsLog.PcCode
  675. statisticsLogComputer.Operator = statisticsLog.Operator
  676. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  677. statisticsLogComputer.GameId = gameInfo[i].GameId
  678. statisticsLogComputer.PullAccountNum = gameInfo[i].PullAccountNum
  679. statisticsLogComputer.TaskSuccessNum = gameInfo[i].TaskSuccessNum
  680. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  681. }*/
  682. //gameIdStr:=strconv.Itoa(statisticsLog.GameId)
  683. //taskStatistics := s.GameTargetInfo(ctx,s.LogicalLog.CurrentDate(),gameIdStr)
  684. statisticsLogComputer := new(response.ComputerStatisticsReply1)
  685. statisticsLogComputer.PcCode = statisticsLog.PcCode
  686. statisticsLogComputer.Operator = statisticsLog.Operator
  687. statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
  688. statisticsLogComputer.GameId = statisticsLog.GameId
  689. statisticsLogComputer.PullAccountNum = statisticsLog.PullAccountNum
  690. statisticsLogComputer.TaskSuccessNum = statisticsLog.TaskSuccessNum
  691. statisticsLogComputer.TargetNum = statisticsLog.TargetNum
  692. statisticsLogComputer.ComputerFreeTime = statisticsLog.ComputerFreeTime
  693. statisticsLogComputer.ComputerFeeRate = statisticsLog.ComputerFeeRate
  694. statisticsLogComputer.EnterMain = statisticsLog.EnterMain
  695. if isCurrentDate {
  696. statisticsLogComputer.ComputerFeeRate = s.GetStatisticsPcFeeRate(ctx, statisticsLog.PcCode, statisticsLog.GameId)
  697. }
  698. statisticsLogComputer.ComputerHourAverageRate = statisticsLog.ComputerHourAverageRate
  699. statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
  700. }
  701. return statisticsLogsComputer, total, err
  702. }
  703. // 在线电脑
  704. func (s *ServiceStatisticsLog) OnlineComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) ([]*response.ComputerUseLogReply, int64, error) {
  705. date := api.CreateDate
  706. if date == "" {
  707. date = s.CurrentDate()
  708. }
  709. db := global.GVA_DB.Model(&log.LogComputer{})
  710. var total int64
  711. db = db.Select("SUM(pull_account_num) pull_account_total,SUM(enter_main) enter_main_total,operator,pc_code,status,create_date,remarks")
  712. db = db.Where("create_date = ?", date)
  713. if api.Operator != "" {
  714. db = db.Where("operator = ?", api.Operator)
  715. }
  716. if api.PcCode != "" {
  717. db = db.Where("pc_code = ?", api.PcCode)
  718. }
  719. if api.Status != 0 {
  720. db = db.Where("status = ?", api.Status)
  721. }
  722. db = db.Group("pc_code")
  723. err := db.Count(&total).Error
  724. if err != nil {
  725. return nil, 0, err
  726. }
  727. limit := info.PageSize
  728. offset := info.PageSize * (info.Page - 1)
  729. var statisticsLogs []*response.ComputerUseLogReply
  730. db = db.Limit(limit).Offset(offset)
  731. if order != "" {
  732. var OrderStr string
  733. // 设置有效排序key 防止sql注入
  734. // 感谢 Tom4t0 提交漏洞信息
  735. orderMap := make(map[string]bool, 3)
  736. orderMap["pc_code"] = true
  737. orderMap["operator"] = true
  738. if orderMap[order] {
  739. if desc {
  740. OrderStr = order + " desc"
  741. } else {
  742. OrderStr = order
  743. }
  744. } else { // didn't matched any order key in `orderMap`
  745. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  746. return statisticsLogs, total, err
  747. }
  748. err = db.Order(OrderStr).Find(&statisticsLogs).Error
  749. } else {
  750. err = db.Order("id").Find(&statisticsLogs).Error
  751. }
  752. if err != nil {
  753. return nil, 0, err
  754. }
  755. for _, statisticsLog := range statisticsLogs {
  756. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  757. }
  758. return statisticsLogs, total, err
  759. }
  760. //根据游戏id查询数据
  761. func (s *ServiceStatisticsLog) GameStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo) (interface{}, int64, error) {
  762. date := api.CreateDate
  763. isCurrentDate := false
  764. if date == "" {
  765. date = s.CurrentDate()
  766. isCurrentDate = true
  767. }
  768. db := global.GVA_DB.Model(&log.LogComputer{})
  769. var total int64
  770. db = db.Where("create_date = ?", date)
  771. if api.Operator != "" {
  772. db = db.Where("operator = ?", api.Operator)
  773. }
  774. if api.PcCode != "" {
  775. db = db.Where("pc_code = ?", api.PcCode)
  776. }
  777. if api.GameId != 0 {
  778. db = db.Where("game_id = ?", api.GameId)
  779. }
  780. 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")
  781. err := db.Count(&total).Error
  782. if err != nil {
  783. return nil, 0, err
  784. }
  785. limit := info.PageSize
  786. offset := info.PageSize * (info.Page - 1)
  787. var statisticsLogsByGameId []*response.GameIdStatisticsReply
  788. db = db.Limit(limit).Offset(offset)
  789. err = db.Order("id").Find(&statisticsLogsByGameId).Error
  790. if err != nil {
  791. return nil, 0, err
  792. }
  793. for _, statisticsLog := range statisticsLogsByGameId {
  794. statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
  795. statisticsLog.OneComputerAverageNum = statisticsLog.TaskSuccessTotal / statisticsLog.PcCodeTotal
  796. if isCurrentDate {
  797. statisticsLog.GameFeeRate = s.GetStatisticsFeeRate(ctx, statisticsLog.GameId)
  798. }
  799. }
  800. return statisticsLogsByGameId, total, err
  801. }
  802. func (s *ServiceStatisticsLog) GameTargetInfo(ctx context.Context, date string, gameId string) (taskStatistics1 request.TaskStatistics) {
  803. key := fmt.Sprintf(taskStatistics, date)
  804. data, err := global.GVA_REDIS.HGet(ctx, key, gameId).Result()
  805. if err != nil {
  806. if err == redis.Nil {
  807. global.GVA_LOG.Info("TaskStatisticsDataCache"+key+gameId, zap.Error(err))
  808. } else {
  809. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache"+key, zap.Error(err))
  810. return
  811. }
  812. }
  813. _ = json.Unmarshal([]byte(data), &taskStatistics1)
  814. return
  815. }
  816. // 同步群控任务数据到缓存
  817. func (s *ServiceStatisticsLog) TaskStatisticsDataCache() {
  818. ctx := context.Background()
  819. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  820. data, err := s.LogicalLog.RequestJfRoom()
  821. if err != nil {
  822. global.GVA_LOG.Error("获取机房数据失败TaskStatisticsDataCache", zap.Error(err))
  823. return
  824. }
  825. dataTask, err := s.LogicalLog.RequestTaskData()
  826. if err != nil {
  827. global.GVA_LOG.Error("RequestTaskData", zap.Error(err))
  828. return
  829. }
  830. var taskData []request.TaskData
  831. var taskStatistics []request.TaskStatistics
  832. _ = json.Unmarshal(data, &taskStatistics)
  833. _ = json.Unmarshal(dataTask, &taskData)
  834. mps := map[int]request.TaskData{}
  835. for _, td := range taskData {
  836. mps[td.GameId] = td
  837. }
  838. for _, data := range taskStatistics {
  839. var id int
  840. id = data.GameId
  841. if _, ok := mps[id]; ok {
  842. data.NewScanningCode = mps[id].NewScanningCode
  843. data.RetainedPullAccount = mps[id].RetainedPullAccount
  844. data.NewPullAccount = mps[id].NewPullAccount
  845. data.FeeAccountNum = mps[id].FeeAccountNum
  846. data.RetainedAccountNum = mps[id].RetainedAccountNum
  847. }
  848. bd, _ := json.Marshal(data)
  849. err = global.GVA_REDIS.HSet(ctx, key, id, bd).Err()
  850. if err != nil {
  851. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  852. return
  853. }
  854. }
  855. }
  856. // 同步群控任务数据到缓存
  857. func (s *ServiceStatisticsLog) TaskStatisticsDataCache1() {
  858. ctx := context.Background()
  859. key := fmt.Sprintf(taskStatistics, s.LogicalLog.CurrentDate())
  860. data, err := s.LogicalLog.RequestJfRoom()
  861. if err != nil {
  862. global.GVA_LOG.Error("获取机房数据失败TaskStatisticsDataCache", zap.Error(err))
  863. return
  864. }
  865. dataTask, err := s.LogicalLog.RequestTaskData()
  866. if err != nil {
  867. global.GVA_LOG.Error("RequestTaskData", zap.Error(err))
  868. return
  869. }
  870. var taskData []request.TaskData
  871. var taskStatistics []request.TaskStatistics
  872. _ = json.Unmarshal(data, &taskStatistics)
  873. _ = json.Unmarshal(dataTask, &taskData)
  874. mps := map[int]request.TaskData{}
  875. for _, td := range taskData {
  876. mps[td.GameId] = td
  877. }
  878. for _, data := range taskStatistics {
  879. var id int
  880. if data.GameIdXmy != "" {
  881. if data.GameIdXmy == "0" {
  882. id = data.GameId
  883. } else {
  884. id, _ = strconv.Atoi(data.GameIdXmy)
  885. }
  886. } else {
  887. id = data.GameId
  888. }
  889. if _, ok := mps[id]; ok {
  890. data.NewScanningCode = mps[id].NewScanningCode
  891. data.RetainedPullAccount = mps[id].RetainedPullAccount
  892. data.NewPullAccount = mps[id].NewPullAccount
  893. data.FeeAccountNum = mps[id].FeeAccountNum
  894. data.RetainedAccountNum = mps[id].RetainedAccountNum
  895. }
  896. bd, _ := json.Marshal(data)
  897. err = global.GVA_REDIS.HSet(ctx, key, id, bd).Err()
  898. if err != nil {
  899. global.GVA_LOG.Error("添加缓存数据失败TaskStatisticsDataCache", zap.Error(err))
  900. return
  901. }
  902. }
  903. }
  904. // 同步群控任务数据到缓存
  905. func (s *ServiceStatisticsLog) GetComputerNum(date string) int64 {
  906. var total int64
  907. if date == "" {
  908. date = s.CurrentDate()
  909. }
  910. db := global.GVA_DB.Model(&log.LogComputer{})
  911. db = db.Distinct("pc_code").Where("create_date = ?", date)
  912. _ = db.Count(&total).Error
  913. return total
  914. }
  915. // 在线电脑接口
  916. func (s *ServiceStatisticsLog) ComputerHeartbeat(c context.Context, onlineComputer request.OnlineComputerRequest) error {
  917. err := s.LogicalLog.SetOnlineComputerNumCache(c, s.LogicalLog.CurrentDate(), onlineComputer.PcCode, onlineComputer.Operator)
  918. if err != nil {
  919. return err
  920. }
  921. s.LogicalLog.SetPcReportingLog(c, onlineComputer.PcCode, onlineComputer.Operator)
  922. return err
  923. }
  924. // 在线电脑接口测试
  925. func (s *ServiceStatisticsLog) ComputerTest(c context.Context) (interface{}, error) {
  926. mps, err := s.LogicalLog.GetOnlineComputerNumCache(c, s.LogicalLog.CurrentDate())
  927. if err != nil {
  928. return mps, err
  929. }
  930. return mps, err
  931. }
  932. // 定时检查电脑情况
  933. func (s *ServiceStatisticsLog) RegularCheckPc1() {
  934. ctx := context.Background()
  935. date := s.LogicalLog.CurrentDate()
  936. // 获取当天已上报的电脑数据
  937. onlineComputerMpa, _ := s.LogicalLog.GetOnlineComputerNumCache(ctx, date)
  938. // 获取数据库的数据
  939. var computer log.Computer
  940. computers, err := computer.OnlinePcCodeCache()
  941. if err != nil {
  942. global.GVA_LOG.Error("获取租机表数据失败", zap.Error(err))
  943. }
  944. // 查询两小时内上报的数据
  945. var noReportingPc []string
  946. for pc, _ := range onlineComputerMpa {
  947. num := s.LogicalLog.GetPcReportingLog(ctx, pc)
  948. delete(computers, pc)
  949. if num == 0 {
  950. noReportingPc = append(noReportingPc, pc)
  951. }
  952. }
  953. // 未上报逻辑
  954. var errReportingPc []string
  955. for pc, name := range computers {
  956. if name == "备用" {
  957. continue
  958. }
  959. errReportingPc = append(errReportingPc, pc)
  960. }
  961. var content string
  962. if len(errReportingPc) != 0 {
  963. // 发信息数据群里
  964. content += fmt.Sprintf("异常租机数据<font color=\"warning\">%d台</font>: %s", len(errReportingPc), errReportingPc)
  965. }
  966. if len(noReportingPc) != 0 {
  967. // 发信息数据群里
  968. content += "\n"
  969. content += fmt.Sprintf("两小时内未检测到中控数据<font color=\"warning\">%d台</font>: %s", len(noReportingPc), noReportingPc)
  970. }
  971. if content != "" {
  972. var sendMsg SendMsg
  973. sendMsg.MsgType = "markdown"
  974. sendMsg.Markdown.Content = content
  975. //c ,_ := json.Marshal(sendMsg)
  976. url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
  977. //url := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b"
  978. _, err = s.SendMsgData(url, sendMsg)
  979. }
  980. }
  981. // 定时检查电脑情况
  982. func (s *ServiceStatisticsLog) RegularCheckPc() {
  983. ctx := context.Background()
  984. var computer log.Computer
  985. computers, err := computer.OnlinePcCodeCache()
  986. //computersF, err := computer.OnlinePcCodeCache()
  987. if err != nil {
  988. global.GVA_LOG.Error("获取租机表数据失败", zap.Error(err))
  989. }
  990. // 查询两小时内上报的数据
  991. var computersF = map[string]string{}
  992. var noReportingPc []string
  993. for pc, name := range computers {
  994. computersF[pc] = name
  995. if strings.Contains(name, "备用") {
  996. delete(computers, pc)
  997. continue
  998. }
  999. num := s.LogicalLog.GetPcReportingLog(ctx, pc)
  1000. delete(computers, pc)
  1001. if num == 0 {
  1002. noReportingPc = append(noReportingPc, pc)
  1003. }
  1004. }
  1005. var content string
  1006. if len(noReportingPc) != 0 {
  1007. content += fmt.Sprintf("两小时内未检测到中控数据<font color=\"warning\">%d台</font>:", len(noReportingPc))
  1008. for _, pc := range noReportingPc {
  1009. content += "\n"
  1010. name := computersF[pc]
  1011. content += name + " : " + pc
  1012. }
  1013. }
  1014. if content != "" {
  1015. var sendMsg SendMsg
  1016. sendMsg.MsgType = "markdown"
  1017. sendMsg.Markdown.Content = content
  1018. //c ,_ := json.Marshal(sendMsg)
  1019. url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
  1020. //url := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b"
  1021. _, err = s.SendMsgData(url, sendMsg)
  1022. }
  1023. }
  1024. type SendMsg struct {
  1025. MsgType string `json:"msgtype"`
  1026. Markdown struct {
  1027. Content string `json:"content"`
  1028. } `json:"markdown"`
  1029. }
  1030. func (s *ServiceStatisticsLog) SendMsgData(url string, params interface{}) (result []byte, err error) {
  1031. result, err = utils.HttpPost(url, params)
  1032. return
  1033. }
  1034. var statusMps = map[int]string{
  1035. -1: "未上报",
  1036. 1: "中控上报",
  1037. 2: "任务上报",
  1038. }
  1039. func (exa *ServiceStatisticsLog) ParseInfoList2Excel(infoList []*response.ComputerUseLogReply, filePath string) error {
  1040. excel := excelize.NewFile()
  1041. excel.SetSheetRow("Sheet1", "A1", &[]string{"电脑编号", "使用者", "日期", "拉取账号", "进入主线", "主线成功率", "使用状态", "备注"})
  1042. for i, statisticsLog := range infoList {
  1043. axis := fmt.Sprintf("A%d", i+2)
  1044. var r interface{}
  1045. if statisticsLog.EnterMainTotal != 0 && statisticsLog.PullAccountTotal != 0 {
  1046. r = fmt.Sprintf("%.2f", float64(statisticsLog.EnterMainTotal)/float64(statisticsLog.PullAccountTotal)*100)
  1047. } else {
  1048. r = "0.00"
  1049. }
  1050. excel.SetSheetRow("Sheet1", axis, &[]interface{}{
  1051. statisticsLog.PcCode,
  1052. statisticsLog.Operator,
  1053. statisticsLog.CreateDate[:10],
  1054. statisticsLog.PullAccountTotal,
  1055. statisticsLog.EnterMainTotal,
  1056. r,
  1057. statusMps[statisticsLog.Status],
  1058. statisticsLog.Remarks,
  1059. })
  1060. }
  1061. err := excel.SaveAs(filePath)
  1062. return err
  1063. }
  1064. // 删除两天前的缓存数据
  1065. func (s *ServiceStatisticsLog) RegularDelCheckData() {
  1066. ctx := context.Background()
  1067. date := time.Now().Add(-time.Hour * 48).Format("2006-01-02")
  1068. s.LogicalLog.DelHashUuidKey(ctx, date)
  1069. s.LogicalLog.DelZSetKey(ctx, date)
  1070. s.LogicalLog.DelHashKey(ctx, date)
  1071. }
  1072. //@author: [piexlmax](https://github.com/piexlmax)
  1073. //@function: UpdatePc
  1074. //@description: 根据id更新pc
  1075. //@param: Computer log.Computer
  1076. //@return: err error
  1077. func (a *ServiceStatisticsLog) UpdateComputerUseLog(c log.ComputerUseRemarks) (err error) {
  1078. if !errors.Is(global.GVA_DB.Where("create_date = ?", c.CreateDate).Where("pc_code = ?", c.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
  1079. var updateInfo = make(map[string]interface{})
  1080. updateInfo["remarks"] = c.Remarks
  1081. updateInfo["remarks_update_time"] = time.Now().Format("2006-01-02 15:04:05")
  1082. err = global.GVA_DB.Table("log_computer").Where("create_date = ?", c.CreateDate).Where("pc_code = ?", c.PcCode).Updates(updateInfo).Error
  1083. }
  1084. return
  1085. }