analysisManager.go 5.2 KB

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