paymatch.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. TopBusinessTypeUsers map[string]map[*UserInfo]bool
  20. SubBusinessTypeUsers map[string]map[*UserInfo]bool
  21. }
  22. func NewPayUser() *PayUser {
  23. return &PayUser{
  24. Users: map[*UserInfo]bool{},
  25. BuyerclassUsers: map[string]map[*UserInfo]bool{},
  26. AreaUsers: map[string]map[*UserInfo]bool{},
  27. CityUsers: map[string]map[*UserInfo]bool{},
  28. DistrictUsers: map[string]map[string]map[*UserInfo]bool{},
  29. SubtypeUsers: map[string]map[*UserInfo]bool{},
  30. TopBusinessTypeUsers: map[string]map[*UserInfo]bool{},
  31. SubBusinessTypeUsers: map[string]map[*UserInfo]bool{},
  32. }
  33. }
  34. func (p *PayUser) Match(poolSize int, datas *[]map[string]interface{}) (*map[*UserInfo]*SortList, *map[*UserInfo]*[]string) {
  35. return EachFoMatch(p, poolSize, datas)
  36. }
  37. func (p *PayUser) ToMatch(info map[string]interface{}) (*map[*UserInfo]*MatchUser, *[]*UserInfo) {
  38. buyerclass, _ := info["buyerclass"].(string)
  39. area, _ := info["area"].(string)
  40. if area == "全国" {
  41. area = ""
  42. }
  43. city, _ := info["city"].(string)
  44. district, _ := info["district"].(string)
  45. subtype, _ := info["subtype"].(string)
  46. var amount float64
  47. if info["bidamount"] != nil {
  48. amount = util.Float64All(info["bidamount"])
  49. } else if info["budget"] != nil {
  50. amount = util.Float64All(info["budget"])
  51. }
  52. tag_topinformation, _ := info["tag_topinformation"].([]interface{})
  53. tag_subinformation, _ := info["tag_subinformation"].([]interface{})
  54. yesUsers := map[*UserInfo]*MatchUser{}
  55. allKeys, notkeys := []string{}, []string{}
  56. //匹配标题
  57. if p.Title_KeyDfa != nil {
  58. if title := GetInfoTitle(info); title != "" {
  59. //订阅词
  60. keys := p.Title_KeyDfa.Key.Analy(title)
  61. allKeys = append(allKeys, keys...)
  62. //排除词
  63. notkeys = append(notkeys, p.Title_KeyDfa.NotKey.Analy(title)...)
  64. p.GetKeyUser(MatchWay_Title, keys, p.Title_KeyDfa.Key_user, yesUsers)
  65. }
  66. }
  67. //匹配项目名称/标的物
  68. if p.PnP_KeyDfa != nil {
  69. if projectname, _ := info["projectname"].(string); projectname != "" {
  70. projectname = strings.ToUpper(projectname)
  71. keys := p.PnP_KeyDfa.Key.Analy(projectname)
  72. allKeys = append(allKeys, keys...)
  73. notkeys = append(notkeys, p.PnP_KeyDfa.NotKey.Analy(projectname)...)
  74. p.GetKeyUser(MatchWay_Projectname, keys, p.PnP_KeyDfa.Key_user, yesUsers)
  75. }
  76. if purchasing, _ := info["purchasing"].(string); purchasing != "" {
  77. purchasing = strings.ToUpper(purchasing)
  78. keys := p.PnP_KeyDfa.Key.Analy(purchasing)
  79. allKeys = append(allKeys, keys...)
  80. notkeys = append(notkeys, p.PnP_KeyDfa.NotKey.Analy(purchasing)...)
  81. p.GetKeyUser(MatchWay_Purchasing, keys, p.PnP_KeyDfa.Key_user, yesUsers)
  82. }
  83. }
  84. //匹配附件
  85. if p.Filetext_KeyDfa != nil {
  86. if filetext, _ := info["filetext"].(string); filetext != "" {
  87. filetext = strings.ToUpper(filetext)
  88. keys := p.Filetext_KeyDfa.Key.Analy(filetext)
  89. allKeys = append(allKeys, keys...)
  90. notkeys = append(notkeys, p.Filetext_KeyDfa.NotKey.Analy(filetext)...)
  91. p.GetKeyUser(MatchWay_Filetext, keys, p.Filetext_KeyDfa.Key_user, yesUsers)
  92. }
  93. }
  94. //匹配正文
  95. if p.Detail_KeyDfa != nil {
  96. if detail, _ := info["detail"].(string); detail != "" {
  97. detail = strings.ToUpper(detail)
  98. keys := p.Detail_KeyDfa.Key.Analy(detail)
  99. allKeys = append(allKeys, keys...)
  100. notkeys = append(notkeys, p.Detail_KeyDfa.NotKey.Analy(detail)...)
  101. p.GetKeyUser(MatchWay_Detail, keys, p.Detail_KeyDfa.Key_user, yesUsers)
  102. }
  103. }
  104. notUsers := p.GetFinalUser(allKeys, notkeys, p.Title_KeyDfa, p.PnP_KeyDfa, p.Filetext_KeyDfa, p.Detail_KeyDfa)
  105. users := map[*UserInfo]*MatchUser{}
  106. var projectUsers []*UserInfo
  107. for k, _ := range p.Users {
  108. if notUsers[k] {
  109. continue
  110. } else if (!p.BuyerclassUsers[""][k] && !p.BuyerclassUsers[buyerclass][k]) ||
  111. (!p.AreaUsers[""][k] && !p.AreaUsers[area][k] && !p.CityUsers[city][k] && (p.DistrictUsers[city] == nil || !p.DistrictUsers[city][district][k])) {
  112. continue
  113. }
  114. //小程序
  115. if k.Extend != nil && k.Extend.Object != nil {
  116. if isMiniProgram, _ := k.Extend.Object["isMiniProgram"].(bool); isMiniProgram && (!p.MatchArray(tag_topinformation, k, p.TopBusinessTypeUsers) || !p.MatchArray(tag_subinformation, k, p.SubBusinessTypeUsers)) {
  117. continue
  118. }
  119. }
  120. //关联项目不需要匹配信息类型
  121. if k.SubSet.ProjectMatch == 1 {
  122. projectUsers = append(projectUsers, k)
  123. }
  124. if !p.SubtypeUsers[""][k] && !p.SubtypeUsers[subtype][k] {
  125. continue
  126. }
  127. if k.SubSet.StartAmount > 0 && k.SubSet.EndAmount > 0 && (amount < k.SubSet.StartAmount || amount > k.SubSet.EndAmount) {
  128. continue
  129. } else if k.SubSet.StartAmount > 0 && amount < k.SubSet.StartAmount {
  130. continue
  131. } else if k.SubSet.EndAmount > 0 && amount > k.SubSet.EndAmount {
  132. continue
  133. }
  134. var matchUser *MatchUser
  135. if len(k.SubSet.Keys) > 0 {
  136. matchUser = yesUsers[k]
  137. if matchUser == nil {
  138. continue
  139. }
  140. } else {
  141. matchUser = &MatchUser{}
  142. }
  143. users[k] = matchUser
  144. }
  145. return &users, &projectUsers
  146. }
  147. // 返回匹配上的用户
  148. func (p *PayUser) GetKeyUser(matchWay string, keys []string, keyUser *map[string]*[]*UserInfo, yesUsers map[*UserInfo]*MatchUser) {
  149. keyMap := map[string]bool{}
  150. for _, v := range keys {
  151. keyMap[v] = true
  152. }
  153. //遍历所有用户
  154. for k, us := range *keyUser {
  155. if !keyMap[k] { //该关键词没有匹配到的用户
  156. continue
  157. }
  158. for _, u := range *us {
  159. //获取该词下面所有的用户
  160. matchUser := yesUsers[u]
  161. if matchUser == nil {
  162. matchUser = &MatchUser{
  163. Keys: []string{},
  164. Items: []string{},
  165. Item: map[string]bool{},
  166. MatchWays: []string{},
  167. MatchWay: map[string]bool{},
  168. }
  169. }
  170. if !matchUser.Key[k] {
  171. matchUser.Keys = append(matchUser.Keys, k)
  172. }
  173. matchUser.Key[k] = true
  174. item := u.SubSet.Key_item[k]
  175. if item != "" {
  176. if !matchUser.Item[item] {
  177. matchUser.Items = append(matchUser.Items, item)
  178. }
  179. matchUser.Item[item] = true
  180. }
  181. if !matchUser.MatchWay[matchWay] {
  182. matchUser.MatchWays = append(matchUser.MatchWays, matchWay)
  183. }
  184. matchUser.MatchWay[matchWay] = true
  185. yesUsers[u] = matchUser
  186. }
  187. }
  188. }
  189. // 获取最终的用户,排除词、信息范围、信息类型之后的
  190. // 返回匹配上的用户和没有匹配到的用户
  191. func (p *PayUser) GetFinalUser(keys, notkeys []string, keyUsers ...*KeyDfa) map[*UserInfo]bool {
  192. notUsers := map[*UserInfo]bool{}
  193. keyMap := map[string]bool{}
  194. for _, v := range keys {
  195. keyMap[v] = true
  196. }
  197. //遍历所有用户
  198. for _, keyUser := range keyUsers {
  199. if keyUser == nil {
  200. continue
  201. }
  202. for k, us := range *keyUser.Key_user {
  203. if !keyMap[k] { //该关键词没有匹配到的用户
  204. continue
  205. }
  206. L:
  207. for _, u := range *us {
  208. //获取该词下面所有的用户
  209. //遍历我的排除词,如果存在的话,排除自己
  210. for _, notkey := range notkeys {
  211. if u.SubSet.Key_notkey[k][notkey] {
  212. notUsers[u] = true
  213. continue L
  214. }
  215. }
  216. }
  217. }
  218. }
  219. return notUsers
  220. }
  221. func (p *PayUser) MatchArray(values []interface{}, u *UserInfo, all map[string]map[*UserInfo]bool) bool {
  222. if all[""][u] {
  223. return true
  224. }
  225. for _, v := range values {
  226. vs, _ := v.(string)
  227. if vs == "" {
  228. continue
  229. }
  230. if all[vs][u] {
  231. return true
  232. }
  233. }
  234. return false
  235. }