recommend.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. newestTmpl = `最新采购项目,请及时查看!`
  19. )
  20. var (
  21. VarRecommend = &Recommend{}
  22. query = `{"query":{"bool":{"must":[{"terms":{"autoid":[%s]}}]}},"_source":[` + fields + `],"sort":[{"publishtime":"desc"}],"from":0,"size":%d}`
  23. newestQuery = `{"query":{"bool":{"must":[{"match_all":{}}]}},"_source":[` + fields + `],"sort":[{"publishtime":"desc"}],"size":%d}`
  24. )
  25. type Recommend struct {
  26. newest *[]map[string]interface{}
  27. }
  28. func (r *Recommend) SetNewest(size int) {
  29. r.newest = elastic.Get(Es_Bidding, Es_Bidding, fmt.Sprintf(newestQuery, size))
  30. }
  31. func (r *Recommend) SubRecommend(tidb *Mysql, domain, userId, content string, mailPush, limit int) (string, string, string, string, int, int64) {
  32. list, area, jcly := r.getList(tidb, userId, limit)
  33. if area == "" {
  34. area = "全国"
  35. }
  36. if list == nil || len(*list) == 0 {
  37. list = r.newest
  38. jcly = newestTmpl
  39. }
  40. if list == nil || len(*list) == 0 {
  41. return "", "", "", "", 0, 0
  42. }
  43. var firstTitle, mailContent string
  44. infosLength := 0
  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. }
  53. if mailPush == 1 { //关于邮件的处理
  54. url := fmt.Sprintf("%s/article/mailprivate/%s.html", domain, encrypt.CommonEncodeArticle("mailprivate", info.Id))
  55. acountClassName := "none_a"
  56. if info.Acount != "" {
  57. acountClassName = "acount"
  58. }
  59. industryClassName := ""
  60. if info.Buyerclass != "" {
  61. industryClassName = "buyerclass"
  62. }
  63. areaClassName := ""
  64. if info.Area != "" {
  65. areaClassName = "area"
  66. }
  67. typeClassName := ""
  68. if info.Infotype != "" {
  69. typeClassName = "type"
  70. }
  71. mailContent += fmt.Sprintf(content, infosLength, url, info.HighlightTitle, areaClassName, info.Area, typeClassName, info.Infotype, industryClassName, info.Buyerclass, acountClassName, info.Acount, info.PublishtimeDiff)
  72. }
  73. }
  74. return firstTitle, area, jcly, mailContent, infosLength, firstAutoId
  75. }
  76. //
  77. func (r *Recommend) getList(tidb *Mysql, userId string, limit int) (*[]map[string]interface{}, string, string) {
  78. datas := tidb.SelectBySql(`select type,autoid,area,matchkeys from push.sub_recommend_list where userid=? order by create_time desc limit ?`, userId, limit)
  79. if datas == nil || len(*datas) == 0 {
  80. return nil, "", ""
  81. }
  82. ids := []string{}
  83. for _, v := range *datas {
  84. ids = append(ids, fmt.Sprint(Int64All(v["autoid"])))
  85. }
  86. if len(ids) == 0 {
  87. return nil, "", ""
  88. }
  89. mp := map[int64]map[string]interface{}{}
  90. list := elastic.Get(Es_Bidding, Es_Bidding, fmt.Sprintf(query, strings.Join(ids, ","), limit))
  91. if list == nil {
  92. return nil, "", ""
  93. }
  94. for _, v := range *list {
  95. mp[Int64All(v["autoid"])] = v
  96. }
  97. newList := []map[string]interface{}{}
  98. area := ""
  99. var jcly string
  100. for _, v := range *datas {
  101. autoid := Int64All(v["autoid"])
  102. if mp[autoid] == nil {
  103. continue
  104. }
  105. newList = append(newList, mp[autoid])
  106. if jcly == "" {
  107. area, _ = v["area"].(string)
  108. if matchkeys, typ := ObjToString(v["matchkeys"]), Int64All(v["type"]); typ == 1 {
  109. jcly = fmt.Sprintf(subsetTmpl, ShortenTxt(20, fmt.Sprintf(subsetTmpl, ""), matchkeys))
  110. } else if typ == 2 {
  111. jcly = fmt.Sprintf(searchTmpl, ShortenTxt(20, fmt.Sprintf(searchTmpl, ""), matchkeys))
  112. } else if typ == 3 {
  113. jcly = fmt.Sprintf(visitTmpl, ShortenTxt(20, fmt.Sprintf(visitTmpl, ""), matchkeys))
  114. }
  115. }
  116. }
  117. return &newList, area, jcly
  118. }
  119. //
  120. func (r *Recommend) SaveLog(mgo *MongodbSim, userId string, autoId int64) string {
  121. return mgo.Save("subrecommend_log", map[string]interface{}{
  122. "userid": userId,
  123. "autoid": autoId,
  124. "create_time": time.Now().Unix(),
  125. })
  126. }