123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- package matcher
- import (
- "strings"
- "sync"
- util "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/logger"
- . "bp.jydev.jianyu360.cn/BaseService/pushpkg/p"
- )
- //付费用户
- type PayUser struct {
- Users map[*UserInfo]bool
- Title_KeyDfa *KeyDfa
- Detail_KeyDfa *KeyDfa
- BuyerclassUsers map[string]map[*UserInfo]bool
- AreaUsers map[string]map[*UserInfo]bool
- CityUsers map[string]map[*UserInfo]bool
- DistrictUsers map[string]map[string]map[*UserInfo]bool
- SubtypeUsers map[string]map[*UserInfo]bool
- }
- //
- func NewPayUser() *PayUser {
- return &PayUser{
- Users: map[*UserInfo]bool{},
- BuyerclassUsers: map[string]map[*UserInfo]bool{},
- AreaUsers: map[string]map[*UserInfo]bool{},
- CityUsers: map[string]map[*UserInfo]bool{},
- DistrictUsers: map[string]map[string]map[*UserInfo]bool{},
- SubtypeUsers: map[string]map[*UserInfo]bool{},
- }
- }
- //遍历数据
- func (p *PayUser) Match(poolSize int, datas *[]map[string]interface{}) (*map[*UserInfo]*SortList, *map[*UserInfo]*[]string) {
- defer util.Catch()
- logger.Info("开始匹配数据。。。")
- userMap := map[*UserInfo]*SortList{}
- projectUserMap := map[*UserInfo]*[]string{}
- lock := &sync.Mutex{}
- var index int
- matchPool := make(chan bool, poolSize)
- matchWaitGroup := &sync.WaitGroup{}
- for _, temp := range *datas {
- matchPool <- true
- matchWaitGroup.Add(1)
- go func(info map[string]interface{}) {
- defer util.Catch()
- defer func() {
- matchWaitGroup.Done()
- <-matchPool
- }()
- users, projectUsers := p.ToMatch(info)
- lock.Lock()
- defer lock.Unlock()
- if users != nil && len(*users) > 0 {
- for k, v := range *users {
- l := userMap[k]
- if l == nil {
- l = &SortList{}
- }
- *l = append(*l, &MatchInfo{
- Info: info,
- Keys: v.Keys,
- Items: v.Items,
- MatchWays: v.MatchWays,
- })
- userMap[k] = l
- }
- }
- if projectUsers != nil && len(*projectUsers) > 0 {
- for _, v := range *projectUsers {
- l := projectUserMap[v]
- if l == nil {
- l = &[]string{}
- }
- *l = append(*l, util.ObjToString(info["_id"]))
- projectUserMap[v] = l
- }
- }
- }(temp)
- index++
- if index%500 == 0 {
- logger.Info("匹配数据", index)
- }
- }
- matchWaitGroup.Wait()
- logger.Info("匹配数据结束。。。", index)
- return &userMap, &projectUserMap
- }
- //
- func (p *PayUser) ToMatch(info map[string]interface{}) (*map[*UserInfo]*MatchUser, *[]*UserInfo) {
- buyerclass, _ := info["buyerclass"].(string)
- area, _ := info["area"].(string)
- if area == "全国" {
- area = ""
- }
- city, _ := info["city"].(string)
- district, _ := info["district"].(string)
- subtype, _ := info["subtype"].(string)
- title := GetInfoTitle(info)
- //订阅词
- keys := p.Title_KeyDfa.Key.Analy(title)
- //排除词
- notkeys := p.Title_KeyDfa.NotKey.Analy(title)
- notUsers := map[*UserInfo]bool{}
- title_users := p.GetFinalUser(MatchWay_Title, keys, notkeys, p.Title_KeyDfa.Key_user, ¬Users)
- //开启智能匹配的用户,匹配projectscope
- detail_users := &map[*UserInfo]*MatchUser{}
- if p.Detail_KeyDfa != nil {
- //detail, _ := (*info)["projectscope"].(string)
- //if detail == "" {
- detail, _ := info["detail"].(string)
- //}
- if detail != "" {
- detail = strings.ToUpper(detail)
- keys = p.Detail_KeyDfa.Key.Analy(detail)
- notkeys = p.Detail_KeyDfa.NotKey.Analy(detail)
- detail_users = p.GetFinalUser(MatchWay_Detail, keys, notkeys, p.Detail_KeyDfa.Key_user, ¬Users)
- for d_k, d_u := range *detail_users {
- if (*title_users)[d_k] != nil {
- continue
- }
- (*title_users)[d_k] = d_u
- }
- }
- }
- users := map[*UserInfo]*MatchUser{}
- var projectUsers []*UserInfo
- for k, _ := range p.Users {
- if notUsers[k] {
- continue
- }
- if (!p.BuyerclassUsers[""][k] && !p.BuyerclassUsers[buyerclass][k]) ||
- (!p.AreaUsers[""][k] && !p.AreaUsers[area][k] && !p.CityUsers[city][k] && (p.DistrictUsers[city] == nil || !p.DistrictUsers[city][district][k])) {
- continue
- }
- //关联项目不需要匹配信息类型
- if k.SubSet.ProjectMatch == 1 {
- projectUsers = append(projectUsers, k)
- }
- if !p.SubtypeUsers[""][k] && !p.SubtypeUsers[subtype][k] {
- continue
- }
- var matchUser *MatchUser
- if len(k.SubSet.Keys) > 0 {
- matchUser = (*title_users)[k]
- if matchUser == nil {
- matchUser = (*detail_users)[k]
- }
- if matchUser == nil {
- continue
- }
- } else {
- matchUser = &MatchUser{}
- }
- users[k] = matchUser
- }
- return &users, &projectUsers
- }
- //获取最终的用户,排除词、信息范围、信息类型之后的
- //返回匹配上的用户和没有匹配到的用户
- func (p *PayUser) GetFinalUser(matchWay string, keys, notkeys []string, key_user *map[string]*[]*UserInfo, notUsers *map[*UserInfo]bool) *map[*UserInfo]*MatchUser {
- keyMap := map[string]bool{}
- for _, v := range keys {
- keyMap[v] = true
- }
- users := map[*UserInfo]*MatchUser{} //匹配到用户
- //遍历所有用户
- for k, us := range *key_user {
- if !keyMap[k] { //该关键词没有匹配到的用户
- continue
- }
- for _, u := range *us {
- //获取该词下面所有的用户
- //遍历我的排除词,如果存在的话,排除自己
- isContinue := false
- for _, notkey := range notkeys {
- if u.SubSet.Key_notkey[k][notkey] {
- isContinue = true
- break
- }
- }
- if isContinue {
- (*notUsers)[u] = true
- continue
- }
- matchUser := users[u]
- if matchUser == nil {
- matchUser = &MatchUser{
- Keys: []string{},
- Items: []string{},
- Item: map[string]bool{},
- MatchWays: []string{},
- MatchWay: map[string]bool{},
- }
- }
- matchUser.Keys = append(matchUser.Keys, k)
- item := u.SubSet.Key_item[k]
- if item != "" {
- if !matchUser.Item[item] {
- matchUser.Items = append(matchUser.Items, item)
- }
- matchUser.Item[item] = true
- }
- if !matchUser.MatchWay[matchWay] {
- matchUser.MatchWays = append(matchUser.MatchWays, matchWay)
- }
- matchUser.MatchWay[matchWay] = true
- users[u] = matchUser
- }
- }
- return &users
- }
|