logical_log.go 23 KB

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