paymatch.go 7.6 KB

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