package model import ( "fmt" "jyBXBase/rpc/bxbase" IC "jyBXBase/rpc/init" "strings" "time" MC "app.yhyue.com/moapp/jybase/common" ME "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" "github.com/zeromicro/go-zero/core/logx" "go.mongodb.org/mongo-driver/bson/primitive" ) const ( search_index = "bidding" search_type = "bidding" mongodb_fields = `{"_id":1,"area":1,"publishtime":1,"s_subscopeclass":1,"subtype":1,"title":1,"toptype":1,"type":1, "buyerclass":1,"budget":1,"bidamount":1,"s_winner":1,"bidopentime":1,"buyer":1,"projectname":1,"spidercode":1,"site":1}` query = `{"query":{"terms":{"_id":["%s"]}},"_source":["_id","area", "publishtime", "s_subscopeclass", "subtype", "title", "toptype", "type", "buyerclass","bidamount","budget","projectname","buyer","bidopentime","s_winner","filetext","spidercode","site"],"from":0,"size":%d}` multi_match = `{"multi_match": {"query": %s,"type": "phrase", "fields": ["title"]}}` query_bool_must = `{"terms":{"%s":[%s]}}` query_bool_must_and = `{"bool":{"must":[%s],"must_not":[%s]}}` search_field = `"_id","area", "publishtime", "s_subscopeclass", "subtype", "title", "toptype", "type", "buyerclass","bidamount","budget","projectname","buyer","bidopentime","s_winner","filetext","isValidFile","spidercode","site"` query_city_hkeys = `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match": 1}},"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {"title": {"fragment_size": 0,"number_of_fragments": 1}}},"_source":[` + search_field + `],"sort":[{"publishtime":"desc"},{"budget":"desc"}],"from":0,"size":50}` Pushbidding = "global_common_data.dws_f_bid_baseinfo" Province = "province" ) type NewestInfo struct { TableName string UserId string MysqlDb *mysql.Mysql NewUserId int64 } var mysqlTables = map[string]string{ "f": "pushsubscribe", "v": "pushsubscribe", "m": "pushmember", "e": "pushentniche", } func GetNewestInfo(userId, userType string, newUserId int64) *NewestInfo { nt := &NewestInfo{ UserId: userId, TableName: mysqlTables[userType], MysqlDb: IC.BaseServiceMysql, NewUserId: newUserId, } return nt } func (n *NewestInfo) GetPushHistory() (res []*bxbase.NewestList) { findSQL := "select a.infoid,a.matchkeys,b.isvalidfile as attachment_count,b.budget,b.bidamount from %s a INNER JOIN %s b ON a.userid=%d and a.date>=%d and a.infoid=b.infoid order by a.id desc limit 50" findSQL = fmt.Sprintf(findSQL, n.TableName, Pushbidding, n.NewUserId, time.Now().AddDate(0, 0, -7).Unix()) logx.Info(n.TableName, "-------", n.NewUserId, ",findSQL:", findSQL) list := n.MysqlDb.SelectBySql(findSQL) if len(*list) > 0 && list != nil { m := map[string]bool{} es_ids := []string{} infos := map[string]*bxbase.NewestList{} for _, v := range *list { infoId := MC.ObjToString(v["infoid"]) if m[infoId] { continue } es_ids = append(es_ids, infoId) m[MC.ObjToString(v["infoid"])] = true // infos[infoId] = &bxbase.NewestList{ Id: ME.EncodeArticleId2ByCheck(MC.ObjToString(v["infoid"])), Matchkeys: MC.ObjToString(v["matchkeys"]), Budget: MC.Int64All(v["budget"]), Bidamount: MC.Int64All(v["bidamount"]), FileExists: MC.Int64All(v["attachment_count"]) > 0, } } if len(es_ids) > 0 { list := elastic.Get(search_index, search_type, fmt.Sprintf(query, strings.Join(es_ids, `","`), len(es_ids))) if list != nil { for _, v := range *list { _id := MC.ObjToString(v["_id"]) bn := infos[_id] bn.Title = MC.ObjToString(v["title"]) bn.PublishTime = MC.Int64All(v["publishtime"]) bn.Subtype = MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string) bn.Area = MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string) bn.Buyerclass = MC.ObjToString(v["buyerclass"]) bn.City = MC.ObjToString(v["city"]) bn.Industry = MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string) bn.SpiderCode = MC.ObjToString(v["spidercode"]) bn.Site = MC.ObjToString(v["site"]) } } } //mongodb bidding mgo_ids := []primitive.ObjectID{} for _, v := range es_ids { if infos[v].Title == "" { mgo_ids = append(mgo_ids, mongodb.StringTOBsonId(v)) } } if len(mgo_ids) > 0 { list, ok := IC.MgoBidding.Find("bidding", map[string]interface{}{"_id": map[string]interface{}{"$in": mgo_ids}}, nil, mongodb_fields, false, -1, -1) if ok && *list != nil { for _, v := range *list { _id := mongodb.BsonIdToSId(v["_id"]) bn := infos[_id] bn.Title = MC.ObjToString(v["title"]) bn.PublishTime = MC.Int64All(v["publishtime"]) bn.Subtype = MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string) bn.Area = MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string) bn.Buyerclass = MC.ObjToString(v["buyerclass"]) bn.City = MC.ObjToString(v["city"]) bn.Industry = MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string) bn.SpiderCode = MC.ObjToString(v["spidercode"]) bn.Site = MC.ObjToString(v["site"]) } } } //mongodb bidding_back mgo_back_ids := []primitive.ObjectID{} for _, v := range mgo_ids { if infos[mongodb.BsonIdToSId(v)].Title == "" { mgo_back_ids = append(mgo_back_ids, v) } } if len(mgo_back_ids) > 0 { list, ok := IC.MgoBidding.Find("bidding_back", map[string]interface{}{"_id": map[string]interface{}{"$in": mgo_back_ids}}, nil, mongodb_fields, false, -1, -1) if ok && *list != nil { for _, v := range *list { _id := mongodb.BsonIdToSId(v["_id"]) bn := infos[_id] bn.Title = MC.ObjToString(v["title"]) bn.PublishTime = MC.Int64All(v["publishtime"]) bn.Subtype = MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string) bn.Area = MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string) bn.Buyerclass = MC.ObjToString(v["buyerclass"]) bn.City = MC.ObjToString(v["city"]) bn.Industry = MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string) bn.SpiderCode = MC.ObjToString(v["spidercode"]) bn.Site = MC.ObjToString(v["site"]) } } } // for _, v := range infos { res = append(res, v) } } return } // 根据定位或者搜索历史 查es func NewestQuery(city, keys, subtype string) (str string) { var musts, bools []string if keys != "" { for _, v := range strings.Split(keys, ",") { keys := strings.Split(v, " ") //历史搜索 空格划分 must_tmp := []string{} for _, key := range keys { must_tmp = append(must_tmp, fmt.Sprintf(multi_match, "\""+key+"\"")) } bools = append(bools, fmt.Sprintf(query_bool_must_and, strings.Join(must_tmp, ","), "")) } } if city != "" { musts = append(musts, fmt.Sprintf(query_bool_must, "city", `"`+city+`"`)) } //未登录首页推送数据限制 if subtype != "" { musts = append(musts, fmt.Sprintf(query_bool_must, "subtype", subtype)) } str = fmt.Sprintf(query_city_hkeys, strings.Join(musts, ","), strings.Join(bools, ",")) logx.Info("str:", str) return } // es查询 func NewestES(doSearchStr string) (res []*bxbase.NewestList) { list := elastic.Get(search_index, search_type, doSearchStr) if list != nil && len(*list) > 0 { for _, v := range *list { _id := mongodb.BsonIdToSId(v["_id"]) isValidFile, _ := v["isValidFile"].(bool) res = append(res, &bxbase.NewestList{ Id: ME.EncodeArticleId2ByCheck(_id), Title: MC.ObjToString(v["title"]), Subtype: MC.If(v["subtype"] != nil, MC.ObjToString(v["subtype"]), MC.ObjToString(v["toptype"])).(string), Area: MC.If(MC.ObjToString(v["area"]) == "A", "全国", MC.ObjToString(v["area"])).(string), Buyerclass: MC.ObjToString(v["buyerclass"]), City: MC.ObjToString(v["city"]), Industry: MC.If(MC.ObjToString(v["s_subscopeclass"]) != "", strings.Split(strings.Split(MC.ObjToString(v["s_subscopeclass"]), ",")[0], "_")[0], "").(string), Budget: MC.Int64All(v["budget"]), Bidamount: MC.Int64All(v["bidamount"]), FileExists: isValidFile, //附件 PublishTime: MC.Int64All(v["publishtime"]), Site: MC.ObjToString(v["site"]), SpiderCode: MC.ObjToString(v["spidercode"]), }) } } return }