freematch.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package matcher
  2. import (
  3. . "bp.jydev.jianyu360.cn/BaseService/pushpkg/p"
  4. )
  5. // 免费用户
  6. type FreeUser struct {
  7. Users map[*UserInfo]bool
  8. Title_KeyDfa *KeyDfa
  9. AreaUsers map[string]map[*UserInfo]bool
  10. SubtypeUsers map[string]map[*UserInfo]bool
  11. }
  12. func NewFreeUser() *FreeUser {
  13. return &FreeUser{
  14. Users: map[*UserInfo]bool{},
  15. AreaUsers: map[string]map[*UserInfo]bool{},
  16. SubtypeUsers: map[string]map[*UserInfo]bool{},
  17. }
  18. }
  19. func (f *FreeUser) Match(poolSize int, datas *[]map[string]interface{}) (*map[*UserInfo]*SortList, *map[*UserInfo]*[]string) {
  20. return EachFoMatch(f, poolSize, datas)
  21. }
  22. func (f *FreeUser) ToMatch(info map[string]interface{}) (*map[*UserInfo]*MatchUser, *[]*UserInfo) {
  23. area, _ := info["area"].(string)
  24. if area == "全国" {
  25. area = ""
  26. }
  27. toptype, _ := info["toptype"].(string)
  28. title := GetInfoTitle(info)
  29. //订阅词
  30. keys := f.Title_KeyDfa.Key.Analy(title)
  31. //排除词
  32. notkeys := f.Title_KeyDfa.NotKey.Analy(title)
  33. title_users := f.GetFinalUser(keys, notkeys, f.Title_KeyDfa.Key_user, info)
  34. users := map[*UserInfo]*MatchUser{}
  35. for k, _ := range f.Users {
  36. if k.SubSet.IsUpgrade && ((!f.AreaUsers[""][k] && !f.AreaUsers[area][k]) || (!f.SubtypeUsers[""][k] && !f.SubtypeUsers[toptype][k])) {
  37. continue
  38. }
  39. var matchUser *MatchUser
  40. if len(k.SubSet.Keys) > 0 {
  41. matchUser = (*title_users)[k]
  42. if matchUser == nil {
  43. continue
  44. }
  45. } else {
  46. matchUser = &MatchUser{}
  47. }
  48. users[k] = matchUser
  49. }
  50. return &users, nil
  51. }
  52. // 获取最终的用户,排除词、信息范围、信息类型之后的
  53. // 返回匹配上的用户和没有匹配到的用户
  54. func (f *FreeUser) GetFinalUser(keys, notkeys []string, keyUser *map[string]*[]*UserInfo, info map[string]interface{}) *map[*UserInfo]*MatchUser {
  55. area, _ := info["area"].(string)
  56. toptype, _ := info["toptype"].(string)
  57. keyMap := map[string]bool{}
  58. for _, v := range keys {
  59. keyMap[v] = true
  60. }
  61. users := map[*UserInfo]*MatchUser{} //匹配到用户
  62. //遍历所有用户
  63. for k, us := range *keyUser {
  64. if !keyMap[k] { //该关键词没有匹配到的用户
  65. continue
  66. }
  67. for _, u := range *us {
  68. //获取该词下面所有的用户
  69. //遍历我的排除词,如果存在的话,排除自己
  70. isContinue := false
  71. for _, notkey := range notkeys {
  72. if u.SubSet.Key_notkey[k][notkey] {
  73. isContinue = true
  74. break
  75. }
  76. }
  77. if isContinue {
  78. continue
  79. }
  80. if !u.SubSet.IsUpgrade {
  81. //遍历我的信息范围,看该信息是不是在我的信息范围中
  82. if len(u.SubSet.Key_area[k]) > 0 && !u.SubSet.Key_area[k][area] {
  83. continue
  84. }
  85. //遍历我的信息类型,看该信息是不是在我的信息类型中
  86. if len(u.SubSet.Key_subtype[k]) > 0 && !u.SubSet.Key_subtype[k][toptype] {
  87. continue
  88. }
  89. }
  90. matchUser := users[u]
  91. if matchUser == nil {
  92. matchUser = &MatchUser{
  93. Keys: []string{},
  94. Key: map[string]bool{},
  95. }
  96. }
  97. if !matchUser.Key[k] {
  98. matchUser.Keys = append(matchUser.Keys, k)
  99. }
  100. matchUser.Key[k] = true
  101. users[u] = matchUser
  102. }
  103. }
  104. return &users
  105. }