paymatch.go 7.1 KB

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