analysisManager.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package userAnalysis
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/util/gconv"
  7. "strings"
  8. )
  9. type (
  10. UserAnalysis struct {
  11. UserMapping map[string]BaseUserId //职位id、mongouserId对应的baseUserId
  12. EntUserIdMapping map[int64]BaseUserId //ent_id对应的baseUserId
  13. FullBaseUserId map[BaseUserId]bool //全量BaseUserId
  14. BinPhone, BindMail map[BaseUserId]bool
  15. Vip, Vip15, Vip30 map[BaseUserId]bool
  16. Member, MemberExpire map[BaseUserId]bool
  17. UnSubUser map[string]bool //取关
  18. NewUser map[int]map[BaseUserId]bool //新用户
  19. TestUser map[BaseUserId]bool
  20. }
  21. BaseUserId int64
  22. AnalysisRes struct {
  23. Name, Code string //标签名字
  24. Data map[BaseUserId]bool //数据
  25. SaveOldData bool //累计统计时需要设置为true 是否保留旧数据
  26. }
  27. )
  28. // UpdateTag 更新标签对应的用户Bitmap,因sql太长所以拆分成batchSize插入
  29. func (ar *AnalysisRes) UpdateTag(ctx context.Context) {
  30. const batchSize = 5000
  31. var (
  32. updateBatch = [][]string{}
  33. tmpArr = make([]string, 0, batchSize)
  34. total = len(ar.Data)
  35. index = 0
  36. )
  37. g.Log().Infof(ctx, "UpdateTag code %s 更新%d个开始", ar.Code, len(ar.Data))
  38. for id, ok := range ar.Data {
  39. if !ok {
  40. continue
  41. }
  42. tmpArr = append(tmpArr, fmt.Sprintf("toUInt64(%d)", id))
  43. if len(tmpArr) == batchSize {
  44. updateBatch = append(updateBatch, tmpArr)
  45. tmpArr = make([]string, 0, batchSize)
  46. }
  47. index++
  48. if index == total {
  49. updateBatch = append(updateBatch, tmpArr)
  50. }
  51. }
  52. if len(updateBatch) == 0 {
  53. if !ar.SaveOldData {
  54. execSql := fmt.Sprintf(`ALTER TABLE dwd_d_tag UPDATE bitobj = bitmapBuild([toUInt64(0)]) WHERE code = '%v';`, ar.Code)
  55. if _, err := g.DB().Exec(ctx, execSql); err != nil {
  56. g.Log().Errorf(ctx, "更新标签%s 滞空异常 %v", ar.Code, err)
  57. }
  58. }
  59. } else {
  60. for i, batch := range updateBatch {
  61. if i == 0 && !ar.SaveOldData {
  62. execSql := fmt.Sprintf(`ALTER TABLE dwd_d_tag UPDATE bitobj = bitmapBuild([%v]) WHERE code = '%v';`, strings.Join(batch, ","), ar.Code)
  63. if _, err := g.DB().Exec(ctx, execSql); err != nil {
  64. g.Log().Errorf(ctx, "更新标签%s [%d]异常 %v", ar.Code, i, err)
  65. }
  66. } else {
  67. execSql := fmt.Sprintf(`ALTER TABLE dwd_d_tag UPDATE bitobj = bitmapOr(bitobj,bitmapBuild([%v])) WHERE code = '%v';`, strings.Join(batch, ","), ar.Code)
  68. if _, err := g.DB().Exec(ctx, execSql); err != nil {
  69. g.Log().Errorf(ctx, "更新标签%s [%d]异常 %v", ar.Code, i, err)
  70. }
  71. }
  72. }
  73. }
  74. g.Log().Infof(ctx, "UpdateTag code %s 更新%d个完成", ar.Code, len(ar.Data))
  75. }
  76. func NewManager() *UserAnalysis {
  77. return &UserAnalysis{
  78. UserMapping: map[string]BaseUserId{},
  79. EntUserIdMapping: map[int64]BaseUserId{},
  80. FullBaseUserId: map[BaseUserId]bool{},
  81. BinPhone: map[BaseUserId]bool{},
  82. BindMail: map[BaseUserId]bool{},
  83. Vip: map[BaseUserId]bool{},
  84. Vip15: map[BaseUserId]bool{},
  85. Vip30: map[BaseUserId]bool{},
  86. Member: map[BaseUserId]bool{},
  87. MemberExpire: map[BaseUserId]bool{},
  88. TestUser: map[BaseUserId]bool{},
  89. UnSubUser: map[string]bool{},
  90. NewUser: map[int]map[BaseUserId]bool{},
  91. }
  92. }
  93. func (ua *UserAnalysis) LoadMapping() error {
  94. //加载baseUserId对应关系
  95. data, err := g.DB().Query(ctx, "SELECT mgoUserId,positionId,baseUserId,phone FROM dwd_mgo_position")
  96. if err != nil {
  97. return err
  98. }
  99. var (
  100. newMapping = map[string]BaseUserId{}
  101. fullBaseUserId = map[BaseUserId]bool{}
  102. phoneBaseUserIdMapping = map[string]BaseUserId{}
  103. entIdBaseUserIdMapping = map[int64]BaseUserId{}
  104. )
  105. for _, m := range data.List() {
  106. var (
  107. mgoUserId = gconv.String(m["mgoUserId"])
  108. positionId = gconv.String(m["positionId"])
  109. baseUserId = BaseUserId(gconv.Int64(m["baseUserId"]))
  110. phone = gconv.String(m["phone"])
  111. )
  112. newMapping[mgoUserId] = baseUserId
  113. newMapping[positionId] = baseUserId
  114. fullBaseUserId[baseUserId] = true
  115. if phone != "" {
  116. phoneBaseUserIdMapping[phone] = baseUserId
  117. }
  118. }
  119. ua.UserMapping = newMapping
  120. ua.FullBaseUserId = fullBaseUserId
  121. //加载ent_id和BaseUserId对应关系
  122. dataEntRes, err := g.DB("jianyu").Query(ctx, "SELECT id,phone FROM entniche_user")
  123. if err != nil {
  124. return err
  125. }
  126. for _, m := range dataEntRes {
  127. var (
  128. entId = gconv.Int64(m["id"])
  129. phone = gconv.String(m["phone"])
  130. )
  131. if bId, ok := phoneBaseUserIdMapping[phone]; ok {
  132. entIdBaseUserIdMapping[entId] = bId
  133. }
  134. }
  135. ua.EntUserIdMapping = entIdBaseUserIdMapping
  136. return nil
  137. }