recommend.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package recommend
  2. import (
  3. "fmt"
  4. "strings"
  5. . "app.yhyue.com/moapp/jybase/common"
  6. "app.yhyue.com/moapp/jybase/encrypt"
  7. elastic "app.yhyue.com/moapp/jybase/es"
  8. . "app.yhyue.com/moapp/jybase/mysql"
  9. . "bp.jydev.jianyu360.cn/BaseService/pushpkg/p"
  10. )
  11. const (
  12. fields = `"_id","title","area","publishtime","buyerclass","s_subscopeclass","bidamount","budget","subtype","toptype"`
  13. searchTmpl = `您搜索的“%s”有新的机会`
  14. visitTmpl = `您浏览过的“%s”项目有同类项目`
  15. subsetTmpl = `您订阅的“%s”有新的机会`
  16. )
  17. var (
  18. VarRecommend = &Recommend{}
  19. query = `{"query":{"bool":{"must":[{"terms":{"autoid":[%s]}}]}},"_source":[` + fields + `],"sort":[{"publishtime":"desc"}],"from":0,"size":%d}`
  20. newestQuery = `{"query":{"bool":{"must":[{"match_all":{}}]}},"_source":[` + fields + `],"sort":[{"publishtime":"desc"}],"size":%d}`
  21. )
  22. type Recommend struct {
  23. newest *[]map[string]interface{}
  24. }
  25. func (r *Recommend) SetNewest(size int) {
  26. r.newest = elastic.Get(Es_Bidding, Es_Bidding, fmt.Sprintf(newestQuery, size))
  27. }
  28. func (r *Recommend) SubRecommend(tidb *Mysql, domain, userId, content string, mailPush, limit int) (string, string, string, string, int) {
  29. list, area := r.getList(tidb, userId, limit)
  30. if area == "" {
  31. area = "全国"
  32. }
  33. if list == nil || len(*list) == 0 {
  34. list = r.newest
  35. }
  36. if list == nil || len(*list) == 0 {
  37. return "", "", "", "", 0
  38. }
  39. mailContent := ""
  40. firstTitle := ""
  41. infosLength := 0
  42. jcly := ""
  43. for k, v := range *list {
  44. if k == 0 {
  45. matchkeys := ObjToString(v["matchkeys"])
  46. if typ := Int64All(v["type"]); typ == 1 {
  47. jcly = fmt.Sprintf(subsetTmpl, ShortenTxt(20, fmt.Sprintf(subsetTmpl, ""), matchkeys))
  48. } else if typ == 2 {
  49. jcly = fmt.Sprintf(searchTmpl, ShortenTxt(20, fmt.Sprintf(searchTmpl, ""), matchkeys))
  50. } else if typ == 3 {
  51. jcly = fmt.Sprintf(visitTmpl, ShortenTxt(20, fmt.Sprintf(visitTmpl, ""), matchkeys))
  52. }
  53. }
  54. infosLength++
  55. info := NewBiddingInfo(v, []string{})
  56. if infosLength == 1 {
  57. firstTitle = info.ClearTitle
  58. }
  59. if mailPush == 1 { //关于邮件的处理
  60. url := fmt.Sprintf("%s/article/mailprivate/%s.html", domain, encrypt.CommonEncodeArticle("mailprivate", info.Id))
  61. acountClassName := "none_a"
  62. if info.Acount != "" {
  63. acountClassName = "acount"
  64. }
  65. industryClassName := ""
  66. if info.Buyerclass != "" {
  67. industryClassName = "buyerclass"
  68. }
  69. areaClassName := ""
  70. if info.Area != "" {
  71. areaClassName = "area"
  72. }
  73. typeClassName := ""
  74. if info.Infotype != "" {
  75. typeClassName = "type"
  76. }
  77. mailContent += fmt.Sprintf(content, infosLength, url, info.HighlightTitle, areaClassName, info.Area, typeClassName, info.Infotype, industryClassName, info.Buyerclass, acountClassName, info.Acount, info.PublishtimeDiff)
  78. }
  79. }
  80. return firstTitle, area, jcly, mailContent, infosLength
  81. }
  82. //
  83. func (r *Recommend) getList(tidb *Mysql, userId string, limit int) (*[]map[string]interface{}, string) {
  84. datas := tidb.SelectBySql(`select type,autoid,area,matchkeys from push.sub_recommend_list where userid=? order by create_time desc limit ?`, userId, limit)
  85. if datas == nil || len(*datas) == 0 {
  86. return nil, ""
  87. }
  88. areaMap := map[string]bool{}
  89. ids := []string{}
  90. areas := []string{}
  91. for _, v := range *datas {
  92. ids = append(ids, fmt.Sprint(Int64All(v["autoid"])))
  93. area, _ := (*datas)[0]["area"].(string)
  94. for _, vv := range strings.Split(area, " ") {
  95. if !areaMap[vv] {
  96. areas = append(areas, vv)
  97. }
  98. areaMap[vv] = true
  99. }
  100. }
  101. if len(ids) == 0 {
  102. return nil, ""
  103. }
  104. list := elastic.Get(Es_Bidding, Es_Bidding, fmt.Sprintf(query, strings.Join(ids, ","), limit))
  105. area := "全国"
  106. if !areaMap[area] {
  107. area = strings.Join(areas, " ")
  108. }
  109. return list, area
  110. }