paymatch.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package matcher
  2. import (
  3. "strings"
  4. "sync"
  5. util "app.yhyue.com/moapp/jybase/common"
  6. "app.yhyue.com/moapp/jybase/logger"
  7. . "bp.jydev.jianyu360.cn/BaseService/pushpkg/p"
  8. )
  9. //付费用户
  10. type PayUser struct {
  11. Users map[*UserInfo]bool
  12. Title_KeyDfa *KeyDfa
  13. Detail_KeyDfa *KeyDfa
  14. BuyerclassUsers map[string]map[*UserInfo]bool
  15. AreaUsers map[string]map[*UserInfo]bool
  16. CityUsers map[string]map[*UserInfo]bool
  17. DistrictUsers map[string]map[string]map[*UserInfo]bool
  18. SubtypeUsers map[string]map[*UserInfo]bool
  19. }
  20. //
  21. func NewPayUser() *PayUser {
  22. return &PayUser{
  23. Users: map[*UserInfo]bool{},
  24. BuyerclassUsers: map[string]map[*UserInfo]bool{},
  25. AreaUsers: map[string]map[*UserInfo]bool{},
  26. CityUsers: map[string]map[*UserInfo]bool{},
  27. DistrictUsers: map[string]map[string]map[*UserInfo]bool{},
  28. SubtypeUsers: map[string]map[*UserInfo]bool{},
  29. }
  30. }
  31. //遍历数据
  32. func (p *PayUser) Match(poolSize int, datas *[]map[string]interface{}) (*map[*UserInfo]*SortList, *map[*UserInfo]*[]string) {
  33. defer util.Catch()
  34. logger.Info("开始匹配数据。。。")
  35. userMap := map[*UserInfo]*SortList{}
  36. projectUserMap := map[*UserInfo]*[]string{}
  37. lock := &sync.Mutex{}
  38. var index int
  39. matchPool := make(chan bool, poolSize)
  40. matchWaitGroup := &sync.WaitGroup{}
  41. for _, temp := range *datas {
  42. matchPool <- true
  43. matchWaitGroup.Add(1)
  44. go func(info map[string]interface{}) {
  45. defer util.Catch()
  46. defer func() {
  47. matchWaitGroup.Done()
  48. <-matchPool
  49. }()
  50. users, projectUsers := p.ToMatch(info)
  51. lock.Lock()
  52. defer lock.Unlock()
  53. if users != nil && len(*users) > 0 {
  54. for k, v := range *users {
  55. l := userMap[k]
  56. if l == nil {
  57. l = &SortList{}
  58. }
  59. *l = append(*l, &MatchInfo{
  60. Info: info,
  61. Keys: v.Keys,
  62. Items: v.Items,
  63. MatchWays: v.MatchWays,
  64. })
  65. userMap[k] = l
  66. }
  67. }
  68. if projectUsers != nil && len(*projectUsers) > 0 {
  69. for _, v := range *projectUsers {
  70. l := projectUserMap[v]
  71. if l == nil {
  72. l = &[]string{}
  73. }
  74. *l = append(*l, util.ObjToString(info["_id"]))
  75. projectUserMap[v] = l
  76. }
  77. }
  78. }(temp)
  79. index++
  80. if index%500 == 0 {
  81. logger.Info("匹配数据", index)
  82. }
  83. }
  84. matchWaitGroup.Wait()
  85. logger.Info("匹配数据结束。。。", index)
  86. return &userMap, &projectUserMap
  87. }
  88. //
  89. func (p *PayUser) ToMatch(info map[string]interface{}) (*map[*UserInfo]*MatchUser, *[]*UserInfo) {
  90. buyerclass, _ := info["buyerclass"].(string)
  91. area, _ := info["area"].(string)
  92. if area == "全国" {
  93. area = ""
  94. }
  95. city, _ := info["city"].(string)
  96. district, _ := info["district"].(string)
  97. subtype, _ := info["subtype"].(string)
  98. title := GetInfoTitle(info)
  99. //订阅词
  100. keys := p.Title_KeyDfa.Key.Analy(title)
  101. //排除词
  102. notkeys := p.Title_KeyDfa.NotKey.Analy(title)
  103. notUsers := map[*UserInfo]bool{}
  104. title_users := p.GetFinalUser(MatchWay_Title, keys, notkeys, p.Title_KeyDfa.Key_user, &notUsers)
  105. //开启智能匹配的用户,匹配projectscope
  106. detail_users := &map[*UserInfo]*MatchUser{}
  107. if p.Detail_KeyDfa != nil {
  108. //detail, _ := (*info)["projectscope"].(string)
  109. //if detail == "" {
  110. detail, _ := info["detail"].(string)
  111. //}
  112. if detail != "" {
  113. detail = strings.ToUpper(detail)
  114. keys = p.Detail_KeyDfa.Key.Analy(detail)
  115. notkeys = p.Detail_KeyDfa.NotKey.Analy(detail)
  116. detail_users = p.GetFinalUser(MatchWay_Detail, keys, notkeys, p.Detail_KeyDfa.Key_user, &notUsers)
  117. for d_k, d_u := range *detail_users {
  118. if (*title_users)[d_k] != nil {
  119. continue
  120. }
  121. (*title_users)[d_k] = d_u
  122. }
  123. }
  124. }
  125. users := map[*UserInfo]*MatchUser{}
  126. var projectUsers []*UserInfo
  127. for k, _ := range p.Users {
  128. if notUsers[k] {
  129. continue
  130. }
  131. if (!p.BuyerclassUsers[""][k] && !p.BuyerclassUsers[buyerclass][k]) ||
  132. (!p.AreaUsers[""][k] && !p.AreaUsers[area][k] && !p.CityUsers[city][k] && (p.DistrictUsers[city] == nil || !p.DistrictUsers[city][district][k])) {
  133. continue
  134. }
  135. //关联项目不需要匹配信息类型
  136. if k.SubSet.ProjectMatch == 1 {
  137. projectUsers = append(projectUsers, k)
  138. }
  139. if !p.SubtypeUsers[""][k] && !p.SubtypeUsers[subtype][k] {
  140. continue
  141. }
  142. var matchUser *MatchUser
  143. if len(k.SubSet.Keys) > 0 {
  144. matchUser = (*title_users)[k]
  145. if matchUser == nil {
  146. matchUser = (*detail_users)[k]
  147. }
  148. if matchUser == nil {
  149. continue
  150. }
  151. } else {
  152. matchUser = &MatchUser{}
  153. }
  154. users[k] = matchUser
  155. }
  156. return &users, &projectUsers
  157. }
  158. //获取最终的用户,排除词、信息范围、信息类型之后的
  159. //返回匹配上的用户和没有匹配到的用户
  160. func (p *PayUser) GetFinalUser(matchWay string, keys, notkeys []string, key_user *map[string]*[]*UserInfo, notUsers *map[*UserInfo]bool) *map[*UserInfo]*MatchUser {
  161. keyMap := map[string]bool{}
  162. for _, v := range keys {
  163. keyMap[v] = true
  164. }
  165. users := map[*UserInfo]*MatchUser{} //匹配到用户
  166. //遍历所有用户
  167. for k, us := range *key_user {
  168. if !keyMap[k] { //该关键词没有匹配到的用户
  169. continue
  170. }
  171. for _, u := range *us {
  172. //获取该词下面所有的用户
  173. //遍历我的排除词,如果存在的话,排除自己
  174. isContinue := false
  175. for _, notkey := range notkeys {
  176. if u.SubSet.Key_notkey[k][notkey] {
  177. isContinue = true
  178. break
  179. }
  180. }
  181. if isContinue {
  182. (*notUsers)[u] = true
  183. continue
  184. }
  185. matchUser := users[u]
  186. if matchUser == nil {
  187. matchUser = &MatchUser{
  188. Keys: []string{},
  189. Items: []string{},
  190. Item: map[string]bool{},
  191. MatchWays: []string{},
  192. MatchWay: map[string]bool{},
  193. }
  194. }
  195. matchUser.Keys = append(matchUser.Keys, k)
  196. item := u.SubSet.Key_item[k]
  197. if item != "" {
  198. if !matchUser.Item[item] {
  199. matchUser.Items = append(matchUser.Items, item)
  200. }
  201. matchUser.Item[item] = true
  202. }
  203. if !matchUser.MatchWay[matchWay] {
  204. matchUser.MatchWays = append(matchUser.MatchWays, matchWay)
  205. }
  206. matchUser.MatchWay[matchWay] = true
  207. users[u] = matchUser
  208. }
  209. }
  210. return &users
  211. }