recommend.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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","autoid","title","area","publishtime","buyerclass","s_subscopeclass","bidamount","budget","subtype","toptype"`
  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, jcly := 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. var firstTitle, mailContent string
  42. infosLength := 0
  43. var firstAutoId int64
  44. for _, v := range *list {
  45. infosLength++
  46. info := NewBiddingInfo(v, []string{})
  47. if infosLength == 1 {
  48. firstTitle = info.ClearTitle
  49. firstAutoId = info.AutoId
  50. }
  51. if mailPush == 1 { //关于邮件的处理
  52. url := fmt.Sprintf("%s/article/mailprivate/%s.html", domain, encrypt.CommonEncodeArticle("mailprivate", info.Id))
  53. acountClassName := "none_a"
  54. if info.Acount != "" {
  55. acountClassName = "acount"
  56. }
  57. industryClassName := ""
  58. if info.Buyerclass != "" {
  59. industryClassName = "buyerclass"
  60. }
  61. areaClassName := ""
  62. if info.Area != "" {
  63. areaClassName = "area"
  64. }
  65. typeClassName := ""
  66. if info.Infotype != "" {
  67. typeClassName = "type"
  68. }
  69. mailContent += fmt.Sprintf(content, infosLength, url, info.HighlightTitle, areaClassName, info.Area, typeClassName, info.Infotype, industryClassName, info.Buyerclass, acountClassName, info.Acount, info.PublishtimeDiff)
  70. }
  71. }
  72. return firstTitle, area, jcly, mailContent, infosLength, firstAutoId
  73. }
  74. //
  75. func (r *Recommend) getList(tidb *Mysql, userId string, limit int) (*[]map[string]interface{}, string, string) {
  76. datas := tidb.SelectBySql(`select type,autoid,area,matchkeys from push.sub_recommend_list where userid=? order by create_time desc limit ?`, userId, limit)
  77. if datas == nil || len(*datas) == 0 {
  78. return nil, "", ""
  79. }
  80. ids := []string{}
  81. for _, v := range *datas {
  82. ids = append(ids, fmt.Sprint(Int64All(v["autoid"])))
  83. }
  84. if len(ids) == 0 {
  85. return nil, "", ""
  86. }
  87. mp := map[int64]map[string]interface{}{}
  88. list := elastic.Get(Es_Bidding, Es_Bidding, fmt.Sprintf(query, strings.Join(ids, ","), limit))
  89. if list == nil {
  90. return nil, "", ""
  91. }
  92. for _, v := range *list {
  93. mp[Int64All(v["autoid"])] = v
  94. }
  95. newList := []map[string]interface{}{}
  96. area := ""
  97. var jcly string
  98. for _, v := range *datas {
  99. autoid := Int64All(v["autoid"])
  100. if mp[autoid] == nil {
  101. continue
  102. }
  103. newList = append(newList, mp[autoid])
  104. if jcly == "" {
  105. area, _ = v["area"].(string)
  106. if matchkeys, typ := ObjToString(v["matchkeys"]), Int64All(v["type"]); typ == 1 {
  107. jcly = fmt.Sprintf(subsetTmpl, ShortenTxt(20, fmt.Sprintf(subsetTmpl, ""), matchkeys))
  108. } else if typ == 2 {
  109. jcly = fmt.Sprintf(searchTmpl, ShortenTxt(20, fmt.Sprintf(searchTmpl, ""), matchkeys))
  110. } else if typ == 3 {
  111. jcly = fmt.Sprintf(visitTmpl, ShortenTxt(20, fmt.Sprintf(visitTmpl, ""), matchkeys))
  112. }
  113. }
  114. }
  115. return &newList, area, jcly
  116. }
  117. //
  118. func (r *Recommend) SaveLog(mgo *MongodbSim, userId string, autoId int64) string {
  119. return mgo.Save("subrecommend_log", map[string]interface{}{
  120. "userid": userId,
  121. "autoid": autoId,
  122. "create_time": time.Now().Unix(),
  123. })
  124. }