log_statistics.go 50 KB

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