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