logical_log.go 26 KB

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