logical_log.go 37 KB

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