logical_log.go 31 KB

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