paymatch.go 5.7 KB

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