logical_log.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. package loging
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/go-redis/redis/v8"
  8. "go.uber.org/zap"
  9. "log-server/global"
  10. "log-server/model/log"
  11. "log-server/model/log/request"
  12. "log-server/service/cache"
  13. "log-server/utils"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. var (
  19. FailStatus = "fail"
  20. OkStatus = "ok"
  21. NoLogStatus = "notStatus"
  22. GameCacheKey = "%s:game"
  23. NodeGameCacheKey = "%s:log"
  24. reportPointsKey = "%s:logUuid:"
  25. logUuidAccountGameId = "%s:logUuid:account:gameId"
  26. ComputerCacheKey = "%s:computer:list"
  27. ComputerPullAccountCacheKey = "%s:computer:%s:pullAccount:"
  28. ComputerEnterMainCacheKey = "%s:computer:%s:enterMain:"
  29. ComputerTaskSuccessCacheKey = "%s:computer:%s:taskSuccess:"
  30. GameFeeRateCacheKey = "%s:game:gameFeeRate:%d"
  31. GameComputerRateCacheKey = "%s:Computer:Rate:%s:"
  32. GamePcFeeRateCacheKey = "%s:gamePc:gameFeeRate:%d:%s"
  33. OnLineComputerNum = "%s:OnLineComputerNum:"
  34. PcReportingLog = "%s:pc:Reporting:%s"
  35. )
  36. type LogicalLog struct {
  37. Status int // 状态
  38. Request request.AddLogRequest
  39. cache cache.Cache
  40. ScriptType int
  41. }
  42. func (s *LogicalLog) CurrentDate() (current string) {
  43. current = time.Now().Format("2006-01-02")
  44. return
  45. }
  46. func (s *LogicalLog) YesterdayDate() (yesterday string) {
  47. yesterday = time.Now().Add(-time.Hour * 24).Format("2006-01-02")
  48. return
  49. }
  50. func (s *LogicalLog) DataAdd() (err error) {
  51. logInfo, err := s.RepositoryData()
  52. if err != nil {
  53. return err
  54. }
  55. err = s.LogAdd(logInfo)
  56. if err != nil {
  57. return
  58. }
  59. /*if s.Status == 0 {
  60. _ = s.errLogAdd(logInfo)
  61. }*/
  62. ctx := context.Background()
  63. err = s.SetGameCache(ctx, s.CurrentDate(), logInfo.GameId)
  64. if err != nil {
  65. return
  66. }
  67. err = s.SetAccountGameIdCache(ctx, s.CurrentDate(), logInfo)
  68. if err != nil {
  69. return
  70. }
  71. if logInfo.PcCode != "" {
  72. err = s.SetComputerCache(ctx, s.CurrentDate(), logInfo.PcCode, logInfo.Operator)
  73. _ = s.StatisticsComputerRateData(ctx, logInfo.PcCode, logInfo.GameId)
  74. _ = s.SetOnlineComputerNumCache(ctx, s.CurrentDate(), logInfo.PcCode, logInfo.Operator)
  75. s.SetPcReportingLog(ctx, logInfo.PcCode, logInfo.Operator)
  76. }
  77. return
  78. }
  79. func (s *LogicalLog) NoLogStatusDataAdd(ctx context.Context) (err error) {
  80. logInfo, err := s.RepositoryData()
  81. if err != nil {
  82. return err
  83. }
  84. err = s.LogAdd(logInfo)
  85. return
  86. }
  87. func (s *LogicalLog) getLogByUuid(uuid string) (log log.Loging, err error) {
  88. createDate := time.Now().Format("2006-01")
  89. db := global.GVA_DB.Table("loging_" + createDate)
  90. db = db.Where("log_uuid = ?", uuid)
  91. db = db.Where("coding = 4101099 or coding = 4103099")
  92. db.First(&log)
  93. return
  94. }
  95. func (s *LogicalLog) GetLogByUuidCoding(uuid string, coding int, scriptType int) (log log.Loging, err error) {
  96. createDate := time.Now().Format("2006-01")
  97. db := global.GVA_DB.Table("loging_" + createDate)
  98. db = db.Where("log_uuid = ?", uuid)
  99. db = db.Where("coding = ?", coding)
  100. db = db.Where("script_type = ?", scriptType)
  101. db.First(&log)
  102. return
  103. }
  104. func (s *LogicalLog) RepositoryData() (*log.Loging, error) {
  105. nodeCode, typeCode, err := s.codeData()
  106. if err != nil {
  107. return nil, err
  108. }
  109. if s.Request.GameId == 0 {
  110. loging, err := s.GetAccountGameIdCache(context.Background(), s.CurrentDate(), s.Request.LogUuid)
  111. if err != nil {
  112. return nil, err
  113. }
  114. if loging.GameId == 0 {
  115. return nil, errors.New("没有找到数据" + s.Request.LogUuid)
  116. }
  117. s.Request.GameId = loging.GameId
  118. s.Request.Account = loging.Account
  119. s.Request.DeviceId = loging.DeviceId
  120. s.Request.ComputerType = loging.ComputerType
  121. s.Request.EnvCode = loging.EnvCode
  122. s.Request.Operator = loging.Operator
  123. s.Request.PcCode = loging.PcCode
  124. s.Request.PcIp = loging.PcIp
  125. s.Request.PcMac = loging.PcMac
  126. s.Request.SimulatorIp = loging.SimulatorIp
  127. s.Request.SimulatorMac = loging.SimulatorMac
  128. s.Request.AccountType = loging.AccountType
  129. s.Request.TaskType = loging.TaskType
  130. }
  131. logInfo := new(log.Loging)
  132. logInfo.Coding = s.Request.Coding
  133. logInfo.GameId = s.Request.GameId
  134. logInfo.Status = s.Status
  135. logInfo.Account = s.Request.Account
  136. logInfo.NodeCoding = nodeCode
  137. logInfo.TypeCoding = typeCode
  138. logInfo.DeviceId = s.Request.DeviceId
  139. logInfo.ComputerType = s.Request.ComputerType
  140. logInfo.EnvCode = s.Request.EnvCode
  141. logInfo.LogUuid = s.Request.LogUuid
  142. logInfo.Operator = s.Request.Operator
  143. logInfo.PcCode = s.Request.PcCode
  144. logInfo.PcIp = s.Request.PcIp
  145. logInfo.SimulatorIp = s.Request.SimulatorIp
  146. logInfo.SimulatorMac = s.Request.SimulatorMac
  147. logInfo.PcMac = s.Request.PcMac
  148. logInfo.AccountType = s.Request.AccountType
  149. logInfo.TaskType = s.Request.TaskType
  150. logInfo.ScriptType = s.ScriptType
  151. logInfo.Remarks = s.Request.Remarks
  152. logInfo.CreateDate = time.Now().Format("2006-01-02")
  153. logInfo.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  154. return logInfo, err
  155. }
  156. func (s *LogicalLog) codeData() (nodeCode, typeCode int, err error) {
  157. code := strconv.Itoa(s.Request.Coding)
  158. nodeCodeStr := code[:3]
  159. typeCodeStr := code[:5]
  160. nodeCode, err = strconv.Atoi(nodeCodeStr)
  161. if err != nil {
  162. return
  163. }
  164. typeCode, err = strconv.Atoi(typeCodeStr)
  165. return
  166. }
  167. func (s *LogicalLog) LogAdd(l *log.Loging) error {
  168. createDate := l.CreateDate
  169. splitTime := strings.Split(createDate, "-")
  170. date := splitTime[0] + "-" + splitTime[1]
  171. if date != "2022-11" {
  172. table := "loging_" + splitTime[0] + "-" + splitTime[1]
  173. return global.GVA_DB.Table(table).Create(&l).Error
  174. }
  175. return global.GVA_DB.Create(&l).Error
  176. }
  177. func (s *LogicalLog) errLogAdd(l *log.Loging) error {
  178. return global.GVA_DB.Table("err_log").Create(&l).Error
  179. }
  180. func (s *LogicalLog) ReportPointsLog(ctx context.Context, request1 request.AddLogRequest) (err error) {
  181. key := reportPointsKey + request1.LogUuid
  182. status, err := global.GVA_REDIS.Exists(ctx, key).Result()
  183. if err != nil {
  184. return
  185. }
  186. var reportPointsData []*request.ReportPointsData
  187. if status == 0 {
  188. reportData := new(request.ReportPointsData)
  189. reportData.Data = request1.ReportPointsData
  190. reportData.Date = time.Now().Format("2006-01-02 15:04:05")
  191. reportPointsData = append(reportPointsData, reportData)
  192. str, _ := json.Marshal(reportPointsData)
  193. global.GVA_REDIS.Set(ctx, key, str, time.Minute*20)
  194. } else {
  195. str, _ := global.GVA_REDIS.Get(ctx, key).Result()
  196. err := json.Unmarshal([]byte(str), &reportPointsData)
  197. if err != nil {
  198. return err
  199. }
  200. reportData := new(request.ReportPointsData)
  201. reportData.Data = request1.ReportPointsData
  202. reportData.Date = time.Now().Format("2006-01-02 15:04:05")
  203. reportPointsData = append(reportPointsData, reportData)
  204. str1, _ := json.Marshal(reportPointsData)
  205. global.GVA_REDIS.Set(ctx, key, str1, time.Minute*20)
  206. }
  207. return err
  208. }
  209. func (s *LogicalLog) ReportPointsLogAdd(ctx context.Context, request1 request.AddLogRequest, status int) (err error) {
  210. key := reportPointsKey + request1.LogUuid
  211. isNull, err := global.GVA_REDIS.Exists(ctx, key).Result()
  212. if err != nil {
  213. return
  214. }
  215. if isNull == 0 {
  216. return
  217. } else {
  218. str, err := global.GVA_REDIS.Get(ctx, key).Result()
  219. if err != nil {
  220. return err
  221. }
  222. logIfo, _ := s.GetAccountGameIdCache(ctx, s.CurrentDate(), request1.LogUuid)
  223. var reportPointsData []*request.ReportPointsData
  224. err = json.Unmarshal([]byte(str), &reportPointsData)
  225. if err != nil {
  226. return err
  227. }
  228. reportPoints := new(log.ReportPointsLog)
  229. reportPoints.ReportPointsData = str
  230. reportPoints.LogUuid = request1.LogUuid
  231. reportPoints.Status = status
  232. reportPoints.Account = logIfo.Account
  233. reportPoints.GameId = logIfo.GameId
  234. reportPoints.Coding = logIfo.Coding
  235. reportPoints.ReportPointsNum = len(reportPointsData)
  236. reportPoints.CreateDate = time.Now().Format("2006-01-02")
  237. reportPoints.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  238. err = global.GVA_DB.Create(&reportPoints).Error
  239. if err != nil {
  240. return err
  241. }
  242. global.GVA_REDIS.Del(ctx, key)
  243. err = s.DelAccountGameIdCache(ctx, s.CurrentDate(), logIfo.LogUuid)
  244. }
  245. return
  246. }
  247. func (s *LogicalLog) ExistsKey(ctx context.Context, key string) (bool, error) {
  248. isNull, err := global.GVA_REDIS.Exists(ctx, key).Result()
  249. if err != nil {
  250. return false, err
  251. }
  252. if isNull == 0 {
  253. return false, nil
  254. } else {
  255. return true, nil
  256. }
  257. }
  258. func (s *LogicalLog) ExistsHsKey(ctx context.Context, key string, field string) (bool, error) {
  259. isNull, err := global.GVA_REDIS.HExists(ctx, key, field).Result()
  260. if err != nil {
  261. return false, err
  262. }
  263. return isNull, err
  264. }
  265. // 前3个编号
  266. func (s *LogicalLog) NodeLogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) {
  267. node := code[:3]
  268. err = s.LogSetNum(ctx, date, gameId, node, status, taskType)
  269. return
  270. }
  271. // 前5个编号
  272. func (s *LogicalLog) TypeLogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) {
  273. node := code[:5]
  274. err = s.LogSetNum(ctx, date, gameId, node, status, taskType)
  275. return
  276. }
  277. // 整个编号
  278. func (s *LogicalLog) CodeLogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) {
  279. err = s.LogSetNum(ctx, date, gameId, code, status, taskType)
  280. return
  281. }
  282. // 前5个编号和整个编号缓存
  283. func (s *LogicalLog) PartTypeLogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) {
  284. node := code[:5]
  285. err = s.LogSetNum(ctx, date, gameId, node, status, taskType)
  286. if err != nil {
  287. return
  288. }
  289. err = s.LogSetNum(ctx, date, gameId, code, status, taskType)
  290. return
  291. }
  292. func (s *LogicalLog) LogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) {
  293. strGameId := strconv.Itoa(gameId)
  294. nodeGameCacheKey := fmt.Sprintf(NodeGameCacheKey, date)
  295. key := nodeGameCacheKey + ":" + strGameId + ":" + strconv.Itoa(taskType) + ":" + status + ":" + code
  296. err = s.SetCacheNum(ctx, key)
  297. return
  298. }
  299. func (s *LogicalLog) SetCacheNum(ctx context.Context, key string) (err error) {
  300. bl, err := s.ExistsKey(ctx, key)
  301. if err != nil {
  302. return err
  303. }
  304. if !bl {
  305. err = global.GVA_REDIS.Set(ctx, key, 1, time.Hour*48).Err()
  306. return err
  307. } else {
  308. err = global.GVA_REDIS.Incr(ctx, key).Err()
  309. return err
  310. }
  311. }
  312. func (s *LogicalLog) NodeLogGetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (num int, err error) {
  313. node := code[:3]
  314. num, err = s.LogGetNum(ctx, date, gameId, node, status, taskType)
  315. return
  316. }
  317. func (s *LogicalLog) TypeLogGetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (num int, err error) {
  318. node := code[:5]
  319. num, err = s.LogGetNum(ctx, date, gameId, node, status, taskType)
  320. return
  321. }
  322. func (s *LogicalLog) CodeLogGetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (num int, err error) {
  323. num, err = s.LogGetNum(ctx, date, gameId, code, status, taskType)
  324. return
  325. }
  326. func (s *LogicalLog) LogGetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (num int, err error) {
  327. strGameId := strconv.Itoa(gameId)
  328. nodeGameCacheKey := fmt.Sprintf(NodeGameCacheKey, date)
  329. key := nodeGameCacheKey + ":" + strGameId + ":" + strconv.Itoa(taskType) + ":" + status + ":" + code
  330. num, err = s.GetCacheNum(ctx, key)
  331. return
  332. }
  333. func (s *LogicalLog) GetCacheNum(ctx context.Context, key string) (num int, err error) {
  334. num, err = global.GVA_REDIS.Get(ctx, key).Int()
  335. if err != nil {
  336. if err == redis.Nil {
  337. return 0, nil
  338. }
  339. return 0, err
  340. }
  341. return num, err
  342. }
  343. func (s *LogicalLog) SetGameCache(ctx context.Context, date string, gameId int) (err error) {
  344. key := fmt.Sprintf(GameCacheKey, date)
  345. _, err = global.GVA_REDIS.HSet(ctx, key, gameId, 1).Result()
  346. if err != nil {
  347. return err
  348. }
  349. return err
  350. }
  351. func (s *LogicalLog) GetGameCache(ctx context.Context, date string) (mps map[string]string, err error) {
  352. key := fmt.Sprintf(GameCacheKey, date)
  353. mps, err = global.GVA_REDIS.HGetAll(ctx, key).Result()
  354. if err != nil {
  355. return mps, err
  356. }
  357. return mps, err
  358. }
  359. func (s *LogicalLog) SetAccountGameIdCache(ctx context.Context, date string, logInfo *log.Loging) (err error) {
  360. key := fmt.Sprintf(logUuidAccountGameId, date)
  361. value, _ := json.Marshal(logInfo)
  362. _, err = global.GVA_REDIS.HGet(ctx, key, logInfo.LogUuid).Result()
  363. if err != nil {
  364. if err == redis.Nil {
  365. _, err = global.GVA_REDIS.HSet(ctx, key, logInfo.LogUuid, value).Result()
  366. if err != nil {
  367. return err
  368. }
  369. } else {
  370. return err
  371. }
  372. }
  373. return err
  374. }
  375. func (s *LogicalLog) GetAccountGameIdCache(ctx context.Context, date string, uuid string) (logInfo log.Loging, err error) {
  376. key := fmt.Sprintf(logUuidAccountGameId, date)
  377. data, err := global.GVA_REDIS.HGet(ctx, key, uuid).Result()
  378. if err != nil {
  379. if err == redis.Nil {
  380. logInfo, err = s.getLogByUuid(uuid)
  381. return logInfo, nil
  382. }
  383. return logInfo, err
  384. }
  385. err = json.Unmarshal([]byte(data), &logInfo)
  386. return
  387. }
  388. func (s *LogicalLog) DelAccountGameIdCache(ctx context.Context, date string, uuid string) (err error) {
  389. key := fmt.Sprintf(logUuidAccountGameId, date)
  390. _, err = global.GVA_REDIS.HDel(ctx, key, uuid).Result()
  391. return
  392. }
  393. func (s *LogicalLog) GetGameStatisticsCacheKeys(ctx context.Context, date string, gameId int, taskType int) (keys []string, err error) {
  394. strGameId := strconv.Itoa(gameId)
  395. nodeGameCacheKey := fmt.Sprintf(NodeGameCacheKey, date)
  396. key := nodeGameCacheKey + ":" + strGameId + ":" + strconv.Itoa(taskType) + ":*"
  397. keys, err = global.GVA_REDIS.Keys(ctx, key).Result()
  398. return
  399. }
  400. func (s *LogicalLog) ByCacheKeyGetEndNode(key string) (code string) {
  401. str := strings.Split(key, ":")
  402. code = str[len(str)-1]
  403. return
  404. }
  405. func (s *LogicalLog) SetUuidCodeCache(ctx context.Context, date string, uuid string, code int, gameId int) (err error) {
  406. key := fmt.Sprintf(reportPointsKey, date) + strconv.Itoa(gameId) + ":" + uuid
  407. _, err = global.GVA_REDIS.HSet(ctx, key, code, 1).Result()
  408. if err != nil {
  409. return err
  410. }
  411. return err
  412. }
  413. func (s *LogicalLog) ExistsUuidCodeCache(ctx context.Context, date string, uuid string, code int, gameId int) (b bool, err error) {
  414. key := fmt.Sprintf(reportPointsKey, date) + strconv.Itoa(gameId) + ":" + uuid
  415. b, err = s.ExistsHsKey(ctx, key, strconv.Itoa(code))
  416. if err != nil {
  417. return false, err
  418. }
  419. return
  420. }
  421. func (s *LogicalLog) GetCacheKeys(ctx context.Context, keys string) (key []string, err error) {
  422. key, err = global.GVA_REDIS.Keys(ctx, keys).Result()
  423. return
  424. }
  425. func (s *LogicalLog) DelStatisticsNumCache(ctx context.Context, date string, gameId int) (err error) {
  426. key1FailStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "1" + ":" + FailStatus + ":*"
  427. key1NoLogStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "1" + ":" + NoLogStatus + ":*"
  428. key1OkStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "1" + ":" + OkStatus + ":*"
  429. key0FailStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "0" + ":" + FailStatus + ":*"
  430. key0NoLogStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "0" + ":" + NoLogStatus + ":*"
  431. key0OkStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "0" + ":" + OkStatus + ":*"
  432. fmt.Println(key1FailStatus)
  433. fmt.Println(key1NoLogStatus)
  434. fmt.Println(key1OkStatus)
  435. fmt.Println(key0FailStatus)
  436. fmt.Println(key0NoLogStatus)
  437. fmt.Println(key0OkStatus)
  438. err = s.cache.DelBatheCache(ctx, key1FailStatus)
  439. err = s.cache.DelBatheCache(ctx, key1NoLogStatus)
  440. err = s.cache.DelBatheCache(ctx, key1OkStatus)
  441. err = s.cache.DelBatheCache(ctx, key0FailStatus)
  442. err = s.cache.DelBatheCache(ctx, key0NoLogStatus)
  443. err = s.cache.DelBatheCache(ctx, key0OkStatus)
  444. if err != nil {
  445. return err
  446. }
  447. return
  448. }
  449. func (s *LogicalLog) DelUuidCodeCache(ctx context.Context, date string, gameId int) (err error) {
  450. key := fmt.Sprintf(reportPointsKey, date) + strconv.Itoa(gameId) + ":*"
  451. keys, err := global.GVA_REDIS.Keys(ctx, key).Result()
  452. for _, v := range keys {
  453. err = s.cache.DelBatheHsCache(ctx, v)
  454. }
  455. if err != nil {
  456. return err
  457. }
  458. return err
  459. }
  460. // 统计电脑使用的记录
  461. func (s *LogicalLog) SetComputerCache(ctx context.Context, date string, pcCode, operator string) (err error) {
  462. key := fmt.Sprintf(ComputerCacheKey, date)
  463. _, err = global.GVA_REDIS.HSet(ctx, key, pcCode, operator).Result()
  464. if err != nil {
  465. return err
  466. }
  467. return err
  468. }
  469. // 获取统计电脑使用的记录
  470. func (s *LogicalLog) GetComputerCache(ctx context.Context, date string) (mps map[string]string, err error) {
  471. key := fmt.Sprintf(ComputerCacheKey, date)
  472. mps, err = global.GVA_REDIS.HGetAll(ctx, key).Result()
  473. if err != nil {
  474. return mps, err
  475. }
  476. return mps, err
  477. }
  478. // 统计电脑拉取账号的数量
  479. func (s *LogicalLog) SetComputerPullAccountNumCache(ctx context.Context, date string, pcCode string, gameId int) (err error) {
  480. key := fmt.Sprintf(ComputerPullAccountCacheKey, date, pcCode) + strconv.Itoa(gameId)
  481. err = s.cache.SetCacheNum(ctx, key)
  482. return err
  483. }
  484. // 获取统计电脑拉取账号的数量
  485. func (s *LogicalLog) GetComputerPullAccountNumCache(ctx context.Context, date string, pcCode string) (mps map[string]int, err error) {
  486. key := fmt.Sprintf(ComputerPullAccountCacheKey, date, pcCode)
  487. mps, err = s.GetByPcCodeGameNumCache(ctx, key)
  488. if len(mps) == 0 {
  489. return
  490. }
  491. return
  492. }
  493. // 统计电脑进入主线的数量
  494. func (s *LogicalLog) SetComputerEnterMainNumCache(ctx context.Context, date string, pcCode string, gameId int) (err error) {
  495. key := fmt.Sprintf(ComputerEnterMainCacheKey, date, pcCode) + strconv.Itoa(gameId)
  496. err = s.cache.SetCacheNum(ctx, key)
  497. return err
  498. }
  499. // 获取统计电脑进入主线的数量
  500. func (s *LogicalLog) GetComputerEnterMainNumCache(ctx context.Context, date string, pcCode string) (mps map[string]int, err error) {
  501. key := fmt.Sprintf(ComputerEnterMainCacheKey, date, pcCode)
  502. mps, err = s.GetByPcCodeGameNumCache(ctx, key)
  503. if len(mps) == 0 {
  504. return
  505. }
  506. return
  507. }
  508. // 统计电脑任务成功的数量
  509. func (s *LogicalLog) SetComputerTaskSuccessNumCache(ctx context.Context, date string, pcCode string, gameId int) (err error) {
  510. key := fmt.Sprintf(ComputerTaskSuccessCacheKey, date, pcCode) + strconv.Itoa(gameId)
  511. err = s.cache.SetCacheNum(ctx, key)
  512. return err
  513. }
  514. // 获取统计电脑任务成功的数量
  515. func (s *LogicalLog) GetComputerTaskSuccessNumCache(ctx context.Context, date string, pcCode string) (mps map[string]int, err error) {
  516. key := fmt.Sprintf(ComputerTaskSuccessCacheKey, date, pcCode)
  517. mps, err = s.GetByPcCodeGameNumCache(ctx, key)
  518. if len(mps) == 0 {
  519. return
  520. }
  521. return
  522. }
  523. // 通过电脑code获取游戏缓存的数量
  524. func (s *LogicalLog) GetByPcCodeGameNumCache(ctx context.Context, key string) (mps map[string]int, err error) {
  525. key += "*"
  526. data, err := s.cache.GetCacheKeys(ctx, key)
  527. if len(data) == 0 {
  528. return
  529. }
  530. mps = make(map[string]int)
  531. for _, k := range data {
  532. gameId := s.ByCacheKeyGetEndNode(k)
  533. num, _ := s.cache.GetCacheNum(ctx, k)
  534. mps[gameId] = num
  535. }
  536. return
  537. }
  538. // 统计游戏付费效率的预埋数据
  539. func (s *LogicalLog) StatisticsFeeRateData(ctx context.Context, gameId int, logUuid string) {
  540. key := fmt.Sprintf(GameFeeRateCacheKey, s.CurrentDate(), gameId)
  541. z := &redis.Z{
  542. Member: logUuid,
  543. Score: float64(time.Now().UnixNano() / 1e6),
  544. }
  545. global.GVA_REDIS.ZAdd(ctx, key, z)
  546. }
  547. // 获取统计游戏付费效率的数量
  548. func (s *LogicalLog) GetStatisticsFeeRate(ctx context.Context, gameId int) (num int) {
  549. key := fmt.Sprintf(GameFeeRateCacheKey, s.CurrentDate(), gameId)
  550. z, err := global.GVA_REDIS.ZRangeWithScores(ctx, key, -1, -1).Result()
  551. if err != nil {
  552. return
  553. }
  554. if len(z) == 0 {
  555. return
  556. }
  557. end := z[0].Score
  558. start := z[0].Score - 30*60*1000
  559. op := redis.ZRangeBy{
  560. Min: strconv.Itoa(int(start)),
  561. Max: strconv.Itoa(int(end)),
  562. }
  563. i, err := global.GVA_REDIS.ZRangeByScore(ctx, key, &op).Result()
  564. if err != nil {
  565. return
  566. }
  567. num = len(i)
  568. return
  569. }
  570. // 获取统计单台电脑单个游戏付费效率的预埋数据
  571. func (s *LogicalLog) GetStatisticsPcFeeRate(ctx context.Context, pcCode string, gameId int) (num int) {
  572. key := fmt.Sprintf(GamePcFeeRateCacheKey, s.CurrentDate(), gameId, pcCode)
  573. z, err := global.GVA_REDIS.ZRangeWithScores(ctx, key, -1, -1).Result()
  574. if err != nil {
  575. return
  576. }
  577. if len(z) == 0 {
  578. return
  579. }
  580. end := z[0].Score
  581. start := z[0].Score - 30*60*1000
  582. op := redis.ZRangeBy{
  583. Min: strconv.Itoa(int(start)),
  584. Max: strconv.Itoa(int(end)),
  585. }
  586. i, err := global.GVA_REDIS.ZRangeByScore(ctx, key, &op).Result()
  587. if err != nil {
  588. return
  589. }
  590. num = len(i)
  591. return
  592. }
  593. // 统计单台电脑单个游戏付费效率的预埋数据
  594. func (s *LogicalLog) StatisticsPcFeeRateData(ctx context.Context, pcCode string, logUuid string, gameId int) {
  595. key := fmt.Sprintf(GamePcFeeRateCacheKey, s.CurrentDate(), gameId, pcCode)
  596. z := &redis.Z{
  597. Member: logUuid,
  598. Score: float64(time.Now().UnixNano() / 1e6),
  599. }
  600. global.GVA_REDIS.ZAdd(ctx, key, z)
  601. }
  602. // 统计电脑效率的预埋数据
  603. func (s *LogicalLog) StatisticsComputerRateData(ctx context.Context, pcCode string, gameId int) (err error) {
  604. hour := time.Now().Hour()
  605. key := fmt.Sprintf(GameComputerRateCacheKey, s.CurrentDate(), pcCode)
  606. key = key + strconv.Itoa(hour)
  607. err = s.cache.SetCacheStr(ctx, key, gameId)
  608. return
  609. }
  610. // 统计电脑效率的预埋数据
  611. func (s *LogicalLog) GetStatisticsComputerRate(ctx context.Context, pcCode string) (num int) {
  612. key := fmt.Sprintf(GameComputerRateCacheKey, s.CurrentDate(), pcCode)
  613. key = key + "*"
  614. data, _ := s.cache.GetCacheKeys(ctx, key)
  615. h := map[string]int{
  616. "2": 1,
  617. "3": 1,
  618. "4": 1,
  619. "5": 1,
  620. "6": 1,
  621. "7": 1,
  622. "8": 1,
  623. }
  624. var i = 0
  625. for _, v := range data {
  626. key := strings.Split(v, ":")
  627. hr := key[len(key)-1:][0]
  628. if _, ok := h[hr]; ok {
  629. continue
  630. }
  631. i++
  632. }
  633. hour := time.Now().Hour()
  634. //num = hour + 1 - len(data)
  635. if hour >= 8 {
  636. num = hour + 1 - 7 - i
  637. } else {
  638. notCalculated := hour - 2
  639. num = hour + 1 - notCalculated - i
  640. }
  641. return
  642. }
  643. // 请求机房任务数据
  644. func (s *LogicalLog) RequestJfRoom() (result []byte, err error) {
  645. today := time.Now().Format("2006-01-02")
  646. jfurl := "http://xjf.lianyou.fun:8099/v1/task_statistics"
  647. jfparams := map[string]string{
  648. "query": "date:" + today + ",type:machine",
  649. }
  650. result, err = utils.HttpGet(jfurl, jfparams)
  651. return
  652. }
  653. // 请求机房数据接口数据
  654. func (s *LogicalLog) RequestTaskData() (result []byte, err error) {
  655. today := time.Now().Format("2006-01-02")
  656. jfurl := "http://xjf.lianyou.fun:8118/data/taskDateLog"
  657. jfparams := map[string]string{
  658. "date": today,
  659. }
  660. result, err = utils.HttpGet(jfurl, jfparams)
  661. return
  662. }
  663. // 统计在线电脑数据
  664. func (s *LogicalLog) SetOnlineComputerNumCache(ctx context.Context, date string, pcCode string, operator string) (err error) {
  665. key := fmt.Sprintf(OnLineComputerNum, date)
  666. key += pcCode
  667. err = s.cache.SetCacheStr(ctx, key, operator)
  668. return err
  669. }
  670. // 获取在线电脑数据
  671. func (s *LogicalLog) GetOnlineComputerNumCache(ctx context.Context, date string) (mps map[string]string, err error) {
  672. key := fmt.Sprintf(OnLineComputerNum, date)
  673. mps, err = s.GetAllOnlineComputerNumCache(ctx, key)
  674. if len(mps) == 0 {
  675. return
  676. }
  677. return
  678. }
  679. // 获取所有在线电脑
  680. func (s *LogicalLog) GetAllOnlineComputerNumCache(ctx context.Context, key string) (mps map[string]string, err error) {
  681. key += "*"
  682. data, err := s.cache.GetCacheKeys(ctx, key)
  683. if len(data) == 0 {
  684. return
  685. }
  686. mps = make(map[string]string)
  687. for _, k := range data {
  688. pcCode := s.ByCacheKeyGetEndNode(k)
  689. operator, _ := s.cache.GetCacheStr(ctx, k)
  690. mps[pcCode] = operator
  691. }
  692. return
  693. }
  694. // 获取电脑2小时内上报的次数
  695. func (s *LogicalLog) GetPcReportingLog(ctx context.Context, pcCode string) (num int) {
  696. key := fmt.Sprintf(PcReportingLog, s.CurrentDate(), pcCode)
  697. z, err := global.GVA_REDIS.ZRangeWithScores(ctx, key, -1, -1).Result()
  698. if err != nil {
  699. return
  700. }
  701. if len(z) == 0 {
  702. return
  703. }
  704. end := time.Now().UnixNano()/1e6 + 60*1000*2
  705. start := end - (60*60*1000 + 60*1000)
  706. op := redis.ZRangeBy{
  707. Min: strconv.Itoa(int(start)),
  708. Max: strconv.Itoa(int(end)),
  709. }
  710. i, err := global.GVA_REDIS.ZRangeByScore(ctx, key, &op).Result()
  711. if err != nil {
  712. return
  713. }
  714. num = len(i)
  715. return
  716. }
  717. // 添加电脑定时上报记录
  718. func (s *LogicalLog) SetPcReportingLog(ctx context.Context, pcCode string, operator string) {
  719. key := fmt.Sprintf(PcReportingLog, s.CurrentDate(), pcCode)
  720. z := &redis.Z{
  721. Member: operator,
  722. Score: float64(time.Now().UnixNano() / 1e6),
  723. }
  724. global.GVA_REDIS.ZAdd(ctx, key, z)
  725. }
  726. // 删除hash缓存数据
  727. func (s *LogicalLog) DelHashKey(ctx context.Context, date string) {
  728. gameKeys := fmt.Sprintf(GameCacheKey, date)
  729. data, _ := global.GVA_REDIS.HKeys(ctx, gameKeys).Result()
  730. global.GVA_REDIS.HDel(ctx, gameKeys, data...)
  731. logUuidAccountGameId := fmt.Sprintf(logUuidAccountGameId, date)
  732. logUuid, _ := global.GVA_REDIS.HKeys(ctx, logUuidAccountGameId).Result()
  733. global.GVA_REDIS.HDel(ctx, logUuidAccountGameId, logUuid...)
  734. taskStatisticsKey := fmt.Sprintf("%s:taskStatistics", date)
  735. taskStatistics, _ := global.GVA_REDIS.HKeys(ctx, taskStatisticsKey).Result()
  736. global.GVA_REDIS.HDel(ctx, taskStatisticsKey, taskStatistics...)
  737. computerListKey := fmt.Sprintf(ComputerCacheKey, date)
  738. computerListKeys, _ := global.GVA_REDIS.HKeys(ctx, computerListKey).Result()
  739. global.GVA_REDIS.HDel(ctx, computerListKey, computerListKeys...)
  740. }
  741. // 删除ZSet缓存数据
  742. func (s *LogicalLog) DelZSetKey(ctx context.Context, date string) {
  743. gamePcKey := fmt.Sprintf("%s:gamePc:gameFeeRate*", date)
  744. gameKey := fmt.Sprintf("%s:game:gameFeeRate*", date)
  745. pcReportingKey := fmt.Sprintf("%s:pc:Reporting:*", date)
  746. beginTime, _ := time.ParseInLocation("2006-01-02", date, time.Local)
  747. beginTimeI := strconv.Itoa(int(beginTime.Unix() * 1000))
  748. endTime := strconv.Itoa(int(beginTime.Unix()*1000 + 24*60*60*1000))
  749. gamePcKeys, _ := global.GVA_REDIS.Keys(ctx, gamePcKey).Result()
  750. gameKeys, _ := global.GVA_REDIS.Keys(ctx, gameKey).Result()
  751. pcReportingKeys, _ := global.GVA_REDIS.Keys(ctx, pcReportingKey).Result()
  752. for _, k := range gamePcKeys {
  753. global.GVA_REDIS.ZRemRangeByScore(ctx, k, beginTimeI, endTime)
  754. }
  755. for _, k := range gameKeys {
  756. global.GVA_REDIS.ZRemRangeByScore(ctx, k, beginTimeI, endTime)
  757. }
  758. for _, k := range pcReportingKeys {
  759. global.GVA_REDIS.ZRemRangeByScore(ctx, k, beginTimeI, endTime)
  760. }
  761. }
  762. // 删除hashUuid缓存数据
  763. func (s *LogicalLog) DelHashUuidKey(ctx context.Context, date string) {
  764. gameIds, _ := s.GetGameCache(ctx, date)
  765. for id, _ := range gameIds {
  766. gamekeys := fmt.Sprintf("%s:logUuid:%s:*", date, id)
  767. data, _ := global.GVA_REDIS.Keys(ctx, gamekeys).Result()
  768. for _, hkey := range data {
  769. value, _ := global.GVA_REDIS.HKeys(ctx, hkey).Result()
  770. global.GVA_REDIS.HDel(ctx, hkey, value...)
  771. }
  772. }
  773. }
  774. // 记录扫码订单号信息
  775. func (s *LogicalLog) AddCodeLog(request request.AddLogRequest, status int) {
  776. if request.Remarks == "" {
  777. return
  778. }
  779. logSC := new(log.LogScanningCode)
  780. logSC.GameId = request.GameId
  781. logSC.LogUuid = request.LogUuid
  782. logSC.TaskType = request.TaskType
  783. logSC.Status = status
  784. if logSC.Status == 0 {
  785. logSC.Status = -1
  786. }
  787. codeSupplier := strings.Split(request.Remarks, "|")
  788. logSC.OrderNum = codeSupplier[0]
  789. logSC.Supplier = codeSupplier[1]
  790. logSC.CreateDate = time.Now()
  791. err := global.GVA_DB.Omit("create_time").Create(&logSC).Error
  792. if err != nil {
  793. global.GVA_LOG.Error("create LogScanningCode fail", zap.Error(err))
  794. }
  795. }
  796. // 记录ip信息
  797. func (s *LogicalLog) AddIpLog(request request.AddLogRequest) {
  798. if request.SimulatorIp == "" {
  799. return
  800. }
  801. logSC := new(log.IpLog)
  802. logSC.GameId = request.GameId
  803. logSC.LogUuid = request.LogUuid
  804. logSC.Ip = request.SimulatorIp
  805. logSC.Account = request.Account
  806. logSC.PcCode = request.PcCode
  807. logSC.CreateDate = time.Now().Format("2006-01-02")
  808. err := global.GVA_DB.Omit("create_time").Create(&logSC).Error
  809. if err != nil {
  810. global.GVA_LOG.Error("create LogScanningCode fail", zap.Error(err))
  811. }
  812. }
  813. // 进入游戏ip信息
  814. func (s *LogicalLog) UpdateIpLogStatus(logUuid string, createDate string) {
  815. err := global.GVA_DB.Table("ip_log").Where("create_date = ?", createDate).Where("log_uuid = ?", logUuid).Update("status", 2).Error
  816. if err != nil {
  817. global.GVA_LOG.Error("create LogScanningCode fail", zap.Error(err))
  818. }
  819. }