analysisManager.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. }
  18. BaseUserId int64
  19. AnalysisRes struct {
  20. Name, Code string //标签名字
  21. Data map[BaseUserId]bool //数据
  22. }
  23. )
  24. // UpdateTag 更新标签对应的用户Bitmap,因sql太长所以拆分成batchSize插入
  25. func (ar *AnalysisRes) UpdateTag(ctx context.Context) {
  26. const batchSize = 5000
  27. var (
  28. updateBatch = [][]string{}
  29. tmpArr = make([]string, 0, batchSize)
  30. total = len(ar.Data)
  31. index = 0
  32. )
  33. for id, ok := range ar.Data {
  34. if !ok {
  35. continue
  36. }
  37. tmpArr = append(tmpArr, fmt.Sprintf("toUInt64(%d)", id))
  38. if len(tmpArr) == batchSize {
  39. updateBatch = append(updateBatch, tmpArr)
  40. tmpArr = make([]string, 0, batchSize)
  41. }
  42. index++
  43. if index == total {
  44. updateBatch = append(updateBatch, tmpArr)
  45. }
  46. }
  47. for i, batch := range updateBatch {
  48. if i == 0 {
  49. execSql := fmt.Sprintf(`ALTER TABLE dwd_d_tag UPDATE bitobj = bitmapBuild([%v]) WHERE code = '%v';`, strings.Join(batch, ","), ar.Code)
  50. if _, err := g.DB().Exec(ctx, execSql); err != nil {
  51. g.Log().Errorf(ctx, "更新标签%s [%d]异常 %v", ar.Code, i, err)
  52. }
  53. } else {
  54. execSql := fmt.Sprintf(`ALTER TABLE dwd_d_tag UPDATE bitobj = bitmapOr(bitobj,bitmapBuild([%v])) WHERE code = '%v';`, strings.Join(batch, ","), ar.Code)
  55. if _, err := g.DB().Exec(ctx, execSql); err != nil {
  56. g.Log().Errorf(ctx, "更新标签%s [%d]异常 %v", ar.Code, i, err)
  57. }
  58. }
  59. }
  60. g.Log().Infof(ctx, "code %s 更新%d个完成", ar.Code, len(ar.Data))
  61. }
  62. func NewManager() *UserAnalysis {
  63. return &UserAnalysis{
  64. UserMapping: map[string]BaseUserId{},
  65. EntUserIdMapping: map[int64]BaseUserId{},
  66. FullBaseUserId: map[BaseUserId]bool{},
  67. BinPhone: map[BaseUserId]bool{},
  68. BindMail: map[BaseUserId]bool{},
  69. Vip: map[BaseUserId]bool{},
  70. Vip15: map[BaseUserId]bool{},
  71. Vip30: map[BaseUserId]bool{},
  72. Member: map[BaseUserId]bool{},
  73. MemberExpire: map[BaseUserId]bool{},
  74. }
  75. }
  76. func (ua *UserAnalysis) LoadMapping() error {
  77. //加载baseUserId对应关系
  78. data, err := g.DB().Query(ctx, "SELECT mgoUserId,positionId,baseUserId,phone FROM dwd_mgo_position")
  79. if err != nil {
  80. return err
  81. }
  82. var (
  83. newMapping = map[string]BaseUserId{}
  84. fullBaseUserId = map[BaseUserId]bool{}
  85. phoneBaseUserIdMapping = map[string]BaseUserId{}
  86. entIdBaseUserIdMapping = map[int64]BaseUserId{}
  87. )
  88. for _, m := range data.List() {
  89. var (
  90. mgoUserId = gconv.String(m["mgoUserId"])
  91. positionId = gconv.String(m["positionId"])
  92. baseUserId = BaseUserId(gconv.Int64(m["baseUserId"]))
  93. phone = gconv.String(m["phone"])
  94. )
  95. newMapping[mgoUserId] = baseUserId
  96. newMapping[positionId] = baseUserId
  97. fullBaseUserId[baseUserId] = true
  98. if phone != "" {
  99. phoneBaseUserIdMapping[phone] = baseUserId
  100. }
  101. }
  102. ua.UserMapping = newMapping
  103. ua.FullBaseUserId = fullBaseUserId
  104. //加载ent_id和BaseUserId对应关系
  105. dataEntRes, err := g.DB("jianyu").Query(ctx, "SELECT id,phone FROM entniche_user")
  106. if err != nil {
  107. return err
  108. }
  109. for _, m := range dataEntRes {
  110. var (
  111. entId = gconv.Int64(m["id"])
  112. phone = gconv.String(m["phone"])
  113. )
  114. if bId, ok := phoneBaseUserIdMapping[phone]; ok {
  115. entIdBaseUserIdMapping[entId] = bId
  116. }
  117. }
  118. ua.EntUserIdMapping = entIdBaseUserIdMapping
  119. return nil
  120. }