recommend.go 4.0 KB

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