analysisManager.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. BinPhone, BindMail map[BaseUserId]bool
  13. Vip, Vip15, Vip30 map[BaseUserId]bool
  14. Member, MemberExpire map[BaseUserId]bool
  15. }
  16. BaseUserId int64
  17. AnalysisRes struct {
  18. Name, Code string //标签名字
  19. Data map[BaseUserId]bool //数据
  20. }
  21. )
  22. func (ar *AnalysisRes) UpdateTag(ctx context.Context) {
  23. list := make([]string, 0, len(ar.Data))
  24. for id, ok := range ar.Data {
  25. if !ok {
  26. continue
  27. }
  28. list = append(list, fmt.Sprintf("toUInt64(%d)", id))
  29. }
  30. execSql := fmt.Sprintf(`ALTER TABLE dwd_d_tag UPDATE bitobj = bitmapBuild([%v]) WHERE code = '%v';`, strings.Join(list, ","), ar.Code)
  31. if _, err := g.DB().Exec(ctx, execSql); err != nil {
  32. g.Log().Errorf(ctx, "更新标签%s异常 %v", ar.Code, err)
  33. }
  34. }
  35. func NewManager() *UserAnalysis {
  36. return &UserAnalysis{
  37. UserMapping: map[string]BaseUserId{},
  38. BinPhone: map[BaseUserId]bool{},
  39. BindMail: map[BaseUserId]bool{},
  40. Vip: map[BaseUserId]bool{},
  41. Vip15: map[BaseUserId]bool{},
  42. Vip30: map[BaseUserId]bool{},
  43. Member: map[BaseUserId]bool{},
  44. MemberExpire: map[BaseUserId]bool{},
  45. }
  46. }
  47. func (ua *UserAnalysis) LoadMapping() error {
  48. data, err := g.DB().Query(ctx, "SELECT mgoUserId,positionId,baseUserId FROM dwd_mgo_position")
  49. if err != nil {
  50. return err
  51. }
  52. newMapping := map[string]BaseUserId{}
  53. for _, m := range data.List() {
  54. var (
  55. mgoUserId = gconv.String(m["mgoUserId"])
  56. positionId = gconv.String(m["positionId"])
  57. baseUserId = BaseUserId(gconv.Int64(m["baseUserId"]))
  58. )
  59. newMapping[mgoUserId] = baseUserId
  60. newMapping[positionId] = baseUserId
  61. }
  62. ua.UserMapping = newMapping
  63. return nil
  64. }