freematch.go 3.0 KB

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