log_statistics.go 50 KB

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