paymatch.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package matcher
  2. import (
  3. "strings"
  4. util "app.yhyue.com/moapp/jybase/common"
  5. . "bp.jydev.jianyu360.cn/BaseService/pushpkg/p"
  6. )
  7. //付费用户
  8. type PayUser struct {
  9. Users map[*UserInfo]bool
  10. Title_KeyDfa *KeyDfa
  11. Detail_KeyDfa *KeyDfa
  12. Filetext_KeyDfa *KeyDfa
  13. PnP_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. return EachFoMatch(p, poolSize, datas)
  34. }
  35. //
  36. func (p *PayUser) ToMatch(info map[string]interface{}) (*map[*UserInfo]*MatchUser, *[]*UserInfo) {
  37. buyerclass, _ := info["buyerclass"].(string)
  38. area, _ := info["area"].(string)
  39. if area == "全国" {
  40. area = ""
  41. }
  42. city, _ := info["city"].(string)
  43. district, _ := info["district"].(string)
  44. subtype, _ := info["subtype"].(string)
  45. var amount float64
  46. if info["bidamount"] != nil {
  47. amount = util.Float64All(info["bidamount"])
  48. } else if info["budget"] != nil {
  49. amount = util.Float64All(info["budget"])
  50. }
  51. notUsers := map[*UserInfo]bool{}
  52. yesUsers := &map[*UserInfo]*MatchUser{}
  53. //匹配标题
  54. if p.Title_KeyDfa != nil {
  55. if title := GetInfoTitle(info); title != "" {
  56. //订阅词
  57. keys := p.Title_KeyDfa.Key.Analy(title)
  58. //排除词
  59. notkeys := p.Title_KeyDfa.NotKey.Analy(title)
  60. yesUsers = p.GetFinalUser(MatchWay_Title, keys, notkeys, p.Title_KeyDfa.Key_user, &notUsers)
  61. }
  62. }
  63. //匹配项目名称/标的物
  64. if p.PnP_KeyDfa != nil {
  65. if projectname, _ := info["projectname"].(string); projectname != "" {
  66. projectname = strings.ToUpper(projectname)
  67. keys := p.PnP_KeyDfa.Key.Analy(projectname)
  68. notkeys := p.PnP_KeyDfa.NotKey.Analy(projectname)
  69. projectname_users := p.GetFinalUser(MatchWay_Projectname, keys, notkeys, p.PnP_KeyDfa.Key_user, &notUsers)
  70. for d_k, d_u := range *projectname_users {
  71. if (*yesUsers)[d_k] != nil {
  72. continue
  73. }
  74. (*yesUsers)[d_k] = d_u
  75. }
  76. }
  77. if purchasing, _ := info["purchasing"].(string); purchasing != "" {
  78. purchasing = strings.ToUpper(purchasing)
  79. keys := p.PnP_KeyDfa.Key.Analy(purchasing)
  80. notkeys := p.PnP_KeyDfa.NotKey.Analy(purchasing)
  81. purchasing_users := p.GetFinalUser(MatchWay_Purchasing, keys, notkeys, p.PnP_KeyDfa.Key_user, &notUsers)
  82. for d_k, d_u := range *purchasing_users {
  83. if (*yesUsers)[d_k] != nil {
  84. continue
  85. }
  86. (*yesUsers)[d_k] = d_u
  87. }
  88. }
  89. }
  90. //匹配附件
  91. if p.Filetext_KeyDfa != nil {
  92. if filetext, _ := info["filetext"].(string); filetext != "" {
  93. filetext = strings.ToUpper(filetext)
  94. keys := p.Filetext_KeyDfa.Key.Analy(filetext)
  95. notkeys := p.Filetext_KeyDfa.NotKey.Analy(filetext)
  96. filetext_users := p.GetFinalUser(MatchWay_Filetext, keys, notkeys, p.Filetext_KeyDfa.Key_user, &notUsers)
  97. for d_k, d_u := range *filetext_users {
  98. if (*yesUsers)[d_k] != nil {
  99. continue
  100. }
  101. (*yesUsers)[d_k] = d_u
  102. }
  103. }
  104. }
  105. //匹配正文
  106. if p.Detail_KeyDfa != nil {
  107. if detail, _ := info["detail"].(string); detail != "" {
  108. detail = strings.ToUpper(detail)
  109. keys := p.Detail_KeyDfa.Key.Analy(detail)
  110. notkeys := p.Detail_KeyDfa.NotKey.Analy(detail)
  111. detail_users := p.GetFinalUser(MatchWay_Detail, keys, notkeys, p.Detail_KeyDfa.Key_user, &notUsers)
  112. for d_k, d_u := range *detail_users {
  113. if (*yesUsers)[d_k] != nil {
  114. continue
  115. }
  116. (*yesUsers)[d_k] = d_u
  117. }
  118. }
  119. }
  120. users := map[*UserInfo]*MatchUser{}
  121. var projectUsers []*UserInfo
  122. for k, _ := range p.Users {
  123. if notUsers[k] {
  124. continue
  125. } else if (!p.BuyerclassUsers[""][k] && !p.BuyerclassUsers[buyerclass][k]) ||
  126. (!p.AreaUsers[""][k] && !p.AreaUsers[area][k] && !p.CityUsers[city][k] && (p.DistrictUsers[city] == nil || !p.DistrictUsers[city][district][k])) {
  127. continue
  128. }
  129. //关联项目不需要匹配信息类型
  130. if k.SubSet.ProjectMatch == 1 {
  131. projectUsers = append(projectUsers, k)
  132. }
  133. if !p.SubtypeUsers[""][k] && !p.SubtypeUsers[subtype][k] {
  134. continue
  135. }
  136. if k.SubSet.StartAmount > 0 && k.SubSet.EndAmount > 0 && (amount < k.SubSet.StartAmount || amount > k.SubSet.EndAmount) {
  137. continue
  138. } else if k.SubSet.StartAmount > 0 && amount < k.SubSet.StartAmount {
  139. continue
  140. } else if k.SubSet.EndAmount > 0 && amount > k.SubSet.EndAmount {
  141. continue
  142. }
  143. var matchUser *MatchUser
  144. if len(k.SubSet.Keys) > 0 {
  145. matchUser = (*yesUsers)[k]
  146. if matchUser == nil {
  147. continue
  148. }
  149. } else {
  150. matchUser = &MatchUser{}
  151. }
  152. users[k] = matchUser
  153. }
  154. return &users, &projectUsers
  155. }
  156. //获取最终的用户,排除词、信息范围、信息类型之后的
  157. //返回匹配上的用户和没有匹配到的用户
  158. func (p *PayUser) GetFinalUser(matchWay string, keys, notkeys []string, key_user *map[string]*[]*UserInfo, notUsers *map[*UserInfo]bool) *map[*UserInfo]*MatchUser {
  159. keyMap := map[string]bool{}
  160. for _, v := range keys {
  161. keyMap[v] = true
  162. }
  163. users := map[*UserInfo]*MatchUser{} //匹配到用户
  164. //遍历所有用户
  165. for k, us := range *key_user {
  166. if !keyMap[k] { //该关键词没有匹配到的用户
  167. continue
  168. }
  169. for _, u := range *us {
  170. //获取该词下面所有的用户
  171. //遍历我的排除词,如果存在的话,排除自己
  172. isContinue := false
  173. for _, notkey := range notkeys {
  174. if u.SubSet.Key_notkey[k][notkey] {
  175. isContinue = true
  176. break
  177. }
  178. }
  179. if isContinue {
  180. (*notUsers)[u] = true
  181. continue
  182. }
  183. matchUser := users[u]
  184. if matchUser == nil {
  185. matchUser = &MatchUser{
  186. Keys: []string{},
  187. Items: []string{},
  188. Item: map[string]bool{},
  189. MatchWays: []string{},
  190. MatchWay: map[string]bool{},
  191. }
  192. }
  193. matchUser.Keys = append(matchUser.Keys, k)
  194. item := u.SubSet.Key_item[k]
  195. if item != "" {
  196. if !matchUser.Item[item] {
  197. matchUser.Items = append(matchUser.Items, item)
  198. }
  199. matchUser.Item[item] = true
  200. }
  201. if !matchUser.MatchWay[matchWay] {
  202. matchUser.MatchWays = append(matchUser.MatchWays, matchWay)
  203. }
  204. matchUser.MatchWay[matchWay] = true
  205. users[u] = matchUser
  206. }
  207. }
  208. return &users
  209. }