recommend.go 4.4 KB

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