package recommend import ( "fmt" "strings" "time" . "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/encrypt" elastic "app.yhyue.com/moapp/jybase/es" . "app.yhyue.com/moapp/jybase/mongodb" . "app.yhyue.com/moapp/jybase/mysql" . "bp.jydev.jianyu360.cn/BaseService/pushpkg/p" ) const ( fields = `"_id","title","area","publishtime","buyerclass","s_subscopeclass","bidamount","budget","subtype","toptype","autoid"` searchTmpl = `您搜索的“%s”有新的机会` visitTmpl = `您浏览过的“%s”项目有同类项目` subsetTmpl = `您订阅的“%s”有新的机会` ) var ( VarRecommend = &Recommend{} query = `{"query":{"bool":{"must":[{"terms":{"autoid":[%s]}}]}},"_source":[` + fields + `],"sort":[{"publishtime":"desc"}],"from":0,"size":%d}` newestQuery = `{"query":{"bool":{"must":[{"match_all":{}}]}},"_source":[` + fields + `],"sort":[{"publishtime":"desc"}],"size":%d}` ) type Recommend struct { newest *[]map[string]interface{} } func (r *Recommend) SetNewest(size int) { r.newest = elastic.Get(Es_Bidding, Es_Bidding, fmt.Sprintf(newestQuery, size)) } func (r *Recommend) SubRecommend(tidb *Mysql, domain, userId, content string, mailPush, limit int) (string, string, string, string, int, int64) { list, area := r.getList(tidb, userId, limit) if area == "" { area = "全国" } if list == nil || len(*list) == 0 { list = r.newest } if list == nil || len(*list) == 0 { return "", "", "", "", 0, 0 } mailContent := "" firstTitle := "" infosLength := 0 jcly := "" var firstAutoId int64 for _, v := range *list { infosLength++ info := NewBiddingInfo(v, []string{}) if infosLength == 1 { firstTitle = info.ClearTitle firstAutoId = info.AutoId matchkeys := ObjToString(v["matchkeys"]) if typ := Int64All(v["type"]); typ == 1 { jcly = fmt.Sprintf(subsetTmpl, ShortenTxt(20, fmt.Sprintf(subsetTmpl, ""), matchkeys)) } else if typ == 2 { jcly = fmt.Sprintf(searchTmpl, ShortenTxt(20, fmt.Sprintf(searchTmpl, ""), matchkeys)) } else if typ == 3 { jcly = fmt.Sprintf(visitTmpl, ShortenTxt(20, fmt.Sprintf(visitTmpl, ""), matchkeys)) } } if mailPush == 1 { //关于邮件的处理 url := fmt.Sprintf("%s/article/mailprivate/%s.html", domain, encrypt.CommonEncodeArticle("mailprivate", info.Id)) acountClassName := "none_a" if info.Acount != "" { acountClassName = "acount" } industryClassName := "" if info.Buyerclass != "" { industryClassName = "buyerclass" } areaClassName := "" if info.Area != "" { areaClassName = "area" } typeClassName := "" if info.Infotype != "" { typeClassName = "type" } mailContent += fmt.Sprintf(content, infosLength, url, info.HighlightTitle, areaClassName, info.Area, typeClassName, info.Infotype, industryClassName, info.Buyerclass, acountClassName, info.Acount, info.PublishtimeDiff) } } return firstTitle, area, jcly, mailContent, infosLength, firstAutoId } // func (r *Recommend) getList(tidb *Mysql, userId string, limit int) (*[]map[string]interface{}, string) { datas := tidb.SelectBySql(`select type,autoid,area,matchkeys from push.sub_recommend_list where userid=? order by create_time desc limit ?`, userId, limit) if datas == nil || len(*datas) == 0 { return nil, "" } areaMap := map[string]bool{} ids := []string{} areas := []string{} for _, v := range *datas { ids = append(ids, fmt.Sprint(Int64All(v["autoid"]))) area, _ := (*datas)[0]["area"].(string) for _, vv := range strings.Split(area, " ") { if !areaMap[vv] { areas = append(areas, vv) } areaMap[vv] = true } } if len(ids) == 0 { return nil, "" } list := elastic.Get(Es_Bidding, Es_Bidding, fmt.Sprintf(query, strings.Join(ids, ","), limit)) area := "全国" if !areaMap[area] { area = strings.Join(areas, " ") } return list, area } // func (r *Recommend) SaveLog(mgo *MongodbSim, userId string, autoId int64) string { return mgo.Save("subrecommend_log", map[string]interface{}{ "userid": userId, "autoid": autoId, "create_time": time.Now().Unix(), }) }