package service import ( "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/date" "app.yhyue.com/moapp/jybase/encrypt" elastic "app.yhyue.com/moapp/jybase/es" "fmt" "jyBXBuyer/rpc/type/bxbuyer" "log" "math/rand" "time" ) func GetRelatesInfo(in *bxbuyer.RelatesInformationReq) *bxbuyer.RelatesInformationResp { relatesInfo := &bxbuyer.RelatesInformationResp{Data: &bxbuyer.RelatesInformation{BiddingList: []*bxbuyer.InfoList{}, BuyerList: []*bxbuyer.InfoList{}}} if err := Checking(in); err != nil { relatesInfo.ErrCode = -1 relatesInfo.ErrMsg = err.Error() return relatesInfo } query := `{"query": {"bool": {"must": [{"exists": {"field": "name"}}%s%s}]}}%s%s}` //获取随机数 // 使用当前时间作为随机数生成器的种子 randGen := rand.New(rand.NewSource(time.Now().UnixNano())) // 获取一个范围在 [0, 100) 的随机整数 start := randGen.Intn(100) //关联采购单位:同省份采购单位名称随机展示30个 if in.Area != "" { in.Area = fmt.Sprintf(`,{"term": {"province": "%s"}`, in.Area) } // if in.City != "" { in.City = fmt.Sprintf(`,{"term": {"city": "%s"}}`, in.City) } countQuery := fmt.Sprintf(query, in.Area, in.City, "", "") if c := elastic.Count("buyer", "buyer", countQuery); c > 0 { //数据量不够30 if c-in.BuyerCount < int64(start) { start = common.If(c-in.BuyerCount > 0, int(c-in.BuyerCount), 0).(int) } fromQuery := fmt.Sprintf(`,"from": %d`, start) sizeQuery := fmt.Sprintf(`,"size": %d`, in.BuyerCount) buyerQuery := fmt.Sprintf(query, in.Area, in.City, fromQuery, sizeQuery) log.Println("buyerQuery:", buyerQuery) buyerList := elastic.Get("buyer", "buyer", buyerQuery) if len(*buyerList) > 0 { log.Println("--------:", len(*buyerList)) var buyers []*bxbuyer.InfoList for _, b := range *buyerList { buyers = append(buyers, &bxbuyer.InfoList{ Name: common.ObjToString(b["name"]), Id: encrypt.EncodeArticleId2ByCheck(common.ObjToString(b["_id"])), }) } relatesInfo.Data.BuyerList = buyers } } //关联动态:采购单位标讯动态 if in.Buyer != "" { biddingQuery := fmt.Sprintf(`{"query":{"bool":{"must":[{"term":{"buyer":"%s"}}],"must_not":[{"terms":{"subtype":["采购意向","拟建"]}}]}},"sort": [{"publishtime": "desc"}],"from":0,"size":%d }`, in.Buyer, in.BuyerCount) biddingList := elastic.Get("bidding", "bidding", biddingQuery) if len(*biddingList) > 0 { var biddingInfos []*bxbuyer.InfoList for _, b := range *biddingList { publishtime := common.Int64All(b["publishtime"]) biddingInfos = append(biddingInfos, &bxbuyer.InfoList{ Name: common.ObjToString(b["title"]), Id: encrypt.EncodeArticleId2ByCheck(common.ObjToString(b["id"])), PublishTime: date.FormatDateByInt64(&publishtime, date.Date_Small_Layout), }) } relatesInfo.Data.BiddingList = biddingInfos } } log.Println("relatesinfo:", relatesInfo) return relatesInfo } func Checking(in *bxbuyer.RelatesInformationReq) error { if in.Buyer == "" && in.Area == "" { return fmt.Errorf("参数异常") } //默认30个采购单位 if in.BuyerCount == 0 { in.BuyerCount = 30 } //默认10条招标信息 if in.BiddingCount == 0 { in.BiddingCount = 10 } return nil }