freematch.go 2.9 KB

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