join_channel.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package jobs
  2. import (
  3. "fmt"
  4. "gadmin/config"
  5. "gadmin/internal/gorm/model"
  6. "gadmin/internal/gorm/query"
  7. "gadmin/utility/player"
  8. "github.com/sirupsen/logrus"
  9. "gorm.io/gen"
  10. "sync"
  11. )
  12. var JoinChannel = new(jJoinChannel)
  13. type jJoinChannel struct {
  14. sync.RWMutex
  15. }
  16. func (j *jJoinChannel) Run() {
  17. logrus.Info("jJoinChannel Run.....")
  18. j.handle()
  19. }
  20. func (j *jJoinChannel) SetTable(table string) {
  21. switch table {
  22. case "chapter_logs_0", "chapter_logs_1", "chapter_logs_2", "chapter_logs_3", "chapter_logs_4", "chapter_logs_5", "chapter_logs_6",
  23. "chapter_logs_7", "chapter_logs_8", "chapter_logs_9", "chapter_logs_10", "chapter_logs_11", "chapter_logs_12", "chapter_logs_13",
  24. "chapter_logs_100", "chapter_logs_101", "chapter_logs_102", "chapter_logs_103", "chapter_logs_104", "chapter_logs_105", "chapter_logs_106":
  25. j.chapterLogs(table)
  26. default:
  27. logrus.Infof("jJoinChannel SetTable 无效参数:%+v", table)
  28. }
  29. }
  30. func (j *jJoinChannel) chapterLogs(table string) {
  31. j.RLock()
  32. defer j.RUnlock()
  33. logrus.Warnf("正在更新表:%v", table)
  34. var (
  35. tab = config.DB.Scopes(model.ChapterLogTableSetName(table))
  36. results []*model.ChapterLog
  37. u = query.Use(tab).ChapterLog.Table("")
  38. q = query.Use(tab).ChapterLog.Table("")
  39. )
  40. first, _ := q.Where(q.EventAt.Gte(1680710400)).Order(q.ID).Limit(1).First() // 4.6以后的数据
  41. if first == nil {
  42. first = new(model.ChapterLog)
  43. }
  44. err := u.Select(u.ID, u.UserID).
  45. Where(u.ID.Gte(first.ID)).
  46. Order(u.ID.Desc()).
  47. FindInBatches(&results, 50000, func(tx gen.Dao, batch int) error {
  48. for _, result := range results {
  49. if result.UserID == 0 {
  50. continue
  51. }
  52. channelID := player.GetUserChannel(result.UserID)
  53. //userCreatedAt := player.GetUserStamp(int64(result.PlayerID))
  54. //if userCreatedAt == 0 {
  55. // fmt.Printf("result.PlayerID:%v, userCreatedAt= 0 \n", result.PlayerID)
  56. //}
  57. if channelID != "0" {
  58. if _, err := u.Where(u.ID.Eq(result.ID)).Updates(&model.OnlineDurationLog{
  59. ChannelID: channelID,
  60. //UserCreatedAt: userCreatedAt,
  61. }); err != nil {
  62. fmt.Printf("Updates err: %+v", err)
  63. logrus.WithField("form", "JoinChannel").Warnf("Updates err: %+v", err)
  64. return err
  65. }
  66. }
  67. }
  68. return nil
  69. })
  70. if err != nil {
  71. fmt.Printf("FindInBatches err:%+v", err)
  72. logrus.WithField("form", "JoinChannel").Errorf("FindInBatches err:%+v", err)
  73. }
  74. logrus.Infof("渠道ID导入完成!")
  75. }
  76. func (j *jJoinChannel) handle() {
  77. j.RLock()
  78. defer j.RUnlock()
  79. var (
  80. results []*model.OnlineDurationLog
  81. u = query.Use(config.DB).OnlineDurationLog
  82. //startTime = now.With(time.Now().AddDate(0, -1, 0)).Time
  83. )
  84. //logrus.Infof("开始重新导入渠道ID,date:%v, Unix:%+v", startTime, startTime.Unix())
  85. err := u.Select(u.ID, u.UserID).
  86. Where(u.LoginAt.Gte(int32(1680710400))). // 4.6以后的数据
  87. Order(u.ID.Desc()).
  88. FindInBatches(&results, 50000, func(tx gen.Dao, batch int) error {
  89. for _, result := range results {
  90. if result.UserID == 0 {
  91. continue
  92. }
  93. channelID := player.GetUserChannel(result.UserID)
  94. //userCreatedAt := player.GetUserStamp(int64(result.PlayerID))
  95. //if userCreatedAt == 0 {
  96. // fmt.Printf("result.PlayerID:%v, userCreatedAt= 0 \n", result.PlayerID)
  97. //}
  98. if _, err := u.Where(u.ID.Eq(result.ID)).Updates(&model.OnlineDurationLog{
  99. ChannelID: channelID,
  100. //UserCreatedAt: userCreatedAt,
  101. }); err != nil {
  102. fmt.Printf("Updates err: %+v", err)
  103. logrus.WithField("form", "JoinChannel").Warnf("Updates err: %+v", err)
  104. return err
  105. }
  106. }
  107. return nil
  108. })
  109. if err != nil {
  110. fmt.Printf("FindInBatches err:%+v", err)
  111. logrus.WithField("form", "JoinChannel").Errorf("FindInBatches err:%+v", err)
  112. }
  113. logrus.Infof("渠道ID导入完成!")
  114. }