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","autoid","title","area","publishtime","buyerclass","s_subscopeclass","bidamount","budget","subtype","toptype"` searchTmpl = `您搜索的“%s”有新的机会` visitTmpl = `您浏览过的“%s”项目有同类项目` subsetTmpl = `您订阅的“%s”有新的机会` newestTmpl = `最新采购项目,请及时查看!` ) 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, string, bool) { list, area, jcly := r.getList(tidb, userId, limit) if area == "" { area = "全国" } var isNewestBid bool if list == nil || len(*list) == 0 { list = r.newest jcly = newestTmpl isNewestBid = true } if list == nil || len(*list) == 0 { return "", "", "", "", 0, 0, "", false } var firstTitle, mailContent, firstId string infosLength := 0 var firstAutoId int64 for _, v := range *list { infosLength++ info := NewBiddingInfo(v, []string{}) if infosLength == 1 { firstTitle = info.ClearTitle firstAutoId = info.AutoId firstId = info.Id } 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, firstId, isNewestBid } // func (r *Recommend) getList(tidb *Mysql, userId string, limit int) (*[]map[string]interface{}, string, 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, "", "" } ids := []string{} for _, v := range *datas { ids = append(ids, fmt.Sprint(Int64All(v["autoid"]))) } if len(ids) == 0 { return nil, "", "" } mp := map[int64]map[string]interface{}{} list := elastic.Get(Es_Bidding, Es_Bidding, fmt.Sprintf(query, strings.Join(ids, ","), limit)) if list == nil { return nil, "", "" } for _, v := range *list { mp[Int64All(v["autoid"])] = v } newList := []map[string]interface{}{} area := "" var jcly string for _, v := range *datas { autoid := Int64All(v["autoid"]) if mp[autoid] == nil { continue } newList = append(newList, mp[autoid]) if jcly == "" { area, _ = v["area"].(string) if matchkeys, typ := ObjToString(v["matchkeys"]), 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)) } } } return &newList, area, jcly } // func (r *Recommend) SaveLog(mgo *MongodbSim, userId string, autoId int64, t string, hasPhone, isNewestBid bool) string { m := map[string]interface{}{ "userid": userId, "autoid": autoId, "type": t, "hasphone": 0, "isnewestbid": 0, "create_time": time.Now().Unix(), } if hasPhone { m["hasphone"] = 1 } if isNewestBid { m["isnewestbid"] = 1 } return mgo.Save("subrecommend_log", m) }