fuwencai пре 2 година
родитељ
комит
ff5b631663
2 измењених фајлова са 55 додато и 30 уклоњено
  1. 20 10
      jyBXBuyer/rpc/internal/logic/buyerlistlogic.go
  2. 35 20
      jyBXBuyer/rpc/model/buyerListBYEs.go

+ 20 - 10
jyBXBuyer/rpc/internal/logic/buyerlistlogic.go

@@ -4,11 +4,13 @@ import (
 	"app.yhyue.com/moapp/jybase/redis"
 	"context"
 	"encoding/json"
+	"fmt"
 	"github.com/zeromicro/go-zero/core/logx"
 	IC "jyBXBuyer/rpc/init"
 	"jyBXBuyer/rpc/internal/svc"
 	"jyBXBuyer/rpc/model"
 	"jyBXBuyer/rpc/type/bxbuyer"
+	"time"
 )
 
 type BuyerListLogic struct {
@@ -27,14 +29,25 @@ func NewBuyerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BuyerLi
 
 // 采购单位搜索
 func (l *BuyerListLogic) BuyerList(in *bxbuyer.BuyerListReq) (*bxbuyer.BuyerListResp, error) {
+	start2 := time.Now()
 	logx.Info("----:", model.CheckEmpty(in))
 	query, CountQuery := "", ""
 	resp := &bxbuyer.BuyerListResp{}
 	// 采购单位搜索过来的 最多查100条
+	if in.PageSize*in.PageNum > IC.C.BuyerSearchLimit {
+		in.PageNum = IC.C.BuyerSearchLimit / in.PageSize
+	}
+
+	if in.PageNum < 1 {
+		in.PageNum = 1
+	}
+	if in.PageSize < 1 {
+		in.PageNum = 10
+	}
 	if model.CheckEmpty(in) {
 		//获取缓存数据
 		var isBool = true
-		bs, err := redis.GetBytes("other", model.P_redis_key)
+		bs, err := redis.GetBytes("other", fmt.Sprintf(model.P_redis_key, in.PageNum, in.PageSize))
 		if err == nil && bs != nil && len(*bs) > 0 {
 			isBool = false
 			if err := json.Unmarshal(*bs, &resp.Data); err != nil {
@@ -43,28 +56,25 @@ func (l *BuyerListLogic) BuyerList(in *bxbuyer.BuyerListReq) (*bxbuyer.BuyerList
 			}
 		}
 		if isBool {
-			query, CountQuery = model.BuyerListRedisCacheQuery()
+			query, CountQuery = model.BuyerListRedisCacheQuery(in.PageNum, in.PageSize)
 			resp = model.GetBuyerList(query, CountQuery, in, true)
 			b, err := json.Marshal(resp.Data)
 			if err == nil {
-				redis.PutBytes("other", model.P_redis_key, &b, model.P_redis_time)
+				redis.PutBytes("other", fmt.Sprintf(model.P_redis_key, in.PageNum, in.PageSize), &b, model.P_redis_time)
 			} else {
 				logx.Info("缓存数据 序列化异常")
 			}
 		}
 	} else {
-		if in.PageSize*in.PageNum > IC.C.BuyerSearchLimit {
-			in.PageNum = IC.C.BuyerSearchLimit / in.PageSize
-		}
 		query, CountQuery = model.BuyerListQuery(in)
 		logx.Info("query:", query)
 		resp = model.GetBuyerList(query, CountQuery, in, false) // 查询数据
-		//todo 补充招标动态数量 、历史联系人数量、采购项目数量、采购规模数量
 		resp = model.SupplyBuyerListData(resp)
-		if resp.Data.Count > IC.C.BuyerSearchLimit {
-			resp.Data.Count = IC.C.BuyerSearchLimit
-		}
 	}
+	if resp.Data.Count > IC.C.BuyerSearchLimit {
+		resp.Data.Count = IC.C.BuyerSearchLimit
+	}
+	logx.Info("SupplyBuyerListData耗时:", time.Since(start2))
 
 	return resp, nil
 }

+ 35 - 20
jyBXBuyer/rpc/model/buyerListBYEs.go

@@ -100,10 +100,10 @@ func getTimeRage() (st, et time.Time) {
 const (
 	P_INDEX      = "projectset"
 	P_TYPE       = "projectset"
-	P_redis_time = 15 * 24 * 60 * 60 //redis存15天
-	P_redis_key  = "buyerListCache"
-	BuyerIndex   = "buyer_v2" // 采购单位index
-	BuyerType    = "_doc"
+	P_redis_time = 15 * 24 * 60 * 60      //redis存15天
+	P_redis_key  = "buyerListCache_%d_%d" // 按页存缓存
+	BuyerIndex   = "buyer"                // 采购单位index
+	BuyerType    = "buyer"
 	biddingIndex = "bidding"
 	biddingType  = "bidding"
 )
@@ -121,7 +121,9 @@ func GetBuyerList(qstr string, CountQuery string, in *bxbuyer.BuyerListReq, isCa
 	if total == 0 {
 		return
 	}
+	start := time.Now()
 	rs := elastic.Get(BuyerIndex, BuyerType, qstr) // 采购单位列表
+	logx.Info("采购单位列表 es get查询耗时", time.Since(start))
 	if rs == nil || len(*rs) == 0 {
 		return
 	}
@@ -183,6 +185,7 @@ func GetBuyerList(qstr string, CountQuery string, in *bxbuyer.BuyerListReq, isCa
 
 // 补充字段   项目数量 历史联系人数量 采购单位规模
 func SupplyBuyerListData(resp *bxbuyer.BuyerListResp) *bxbuyer.BuyerListResp {
+	start := time.Now()
 	if resp == nil || resp.Data == nil || resp.Data.List == nil || len(resp.Data.List) == 0 {
 		return resp
 	}
@@ -191,10 +194,12 @@ func SupplyBuyerListData(resp *bxbuyer.BuyerListResp) *bxbuyer.BuyerListResp {
 	for i := 0; i < len(resp.Data.List); i++ {
 		buyerList = append(buyerList, resp.Data.List[i].Buyer)
 	}
-	query := SupplyDataQuery(buyerList) // 招标动态数量、项目数量、采购规模
+	t1 := time.Now()
+	query := SupplyDataQuery(buyerList) // 项目数量、采购规模
 	// 聚合查
 	aggs := GetAggs(P_INDEX, P_TYPE, query)
-
+	logx.Info("项目数量采购规模查询耗时", time.Since(t1))
+	logx.Info("查询语句:", query)
 	type BuyerAggStruct struct {
 		Buckets []struct {
 			Key            string `json:"key,omitempty"`
@@ -221,7 +226,8 @@ func SupplyBuyerListData(resp *bxbuyer.BuyerListResp) *bxbuyer.BuyerListResp {
 			if len(bs) == 0 {
 				resp.ErrMsg = "暂无数据"
 			} else {
-				json.Unmarshal(bs, &buyerBuckets)
+				err := json.Unmarshal(bs, &buyerBuckets)
+				logx.Info(err)
 				if len(buyerBuckets.Buckets) > 0 {
 					for _, v := range buyerBuckets.Buckets {
 						buyerMap[v.Key] = supplyDataStruct{
@@ -243,31 +249,33 @@ func SupplyBuyerListData(resp *bxbuyer.BuyerListResp) *bxbuyer.BuyerListResp {
 			resp.Data.List[i].BidAmountCount = supplyData.BidAmountCount
 			resp.Data.List[i].ProjectCount = supplyData.ProjectCount
 		}
-		ch <- 1
+		ch2 <- 1
 		wg.Add(1)
 		go func(list *bxbuyer.BuyerList, buyer string) {
 			defer func() {
-				<-ch
+				<-ch2
 				wg.Done()
 			}()
-			list.BiddingCount = GetNewBiddingCount(buyer)
+			list.ContactCount = GetProjectContactCount(buyer) // 补充联系人字段
 		}(resp.Data.List[i], buyer)
-		ch2 <- 1
+		ch <- 1
 		wg.Add(1)
 		go func(list *bxbuyer.BuyerList, buyer string) {
 			defer func() {
-				<-ch2
+				<-ch
 				wg.Done()
 			}()
-			list.ContactCount = GetProjectContactCount(buyer) // 补充联系人字段
+			list.BiddingCount = GetNewBiddingCount(buyer)
 		}(resp.Data.List[i], buyer)
 
 	}
 	wg.Wait()
+	logx.Info("SupplyBuyerListData 整体耗时:", time.Since(start))
 	return resp
 }
 
 func GetProjectContactCount(buyerName string) int64 {
+	start := time.Now()
 	list := []string{}
 	var musts []string
 	if buyerName != "" {
@@ -277,7 +285,7 @@ func GetProjectContactCount(buyerName string) int64 {
 		return 0
 	}
 	searchSql := fmt.Sprintf(`{"query":{"bool":{"must":[%s]}},"_source":["_id","zbtime","projectname","list"],"sort":[{"zbtime":"desc"}],"size":500}`, strings.Join(musts, ","))
-	projectList := elastic.Get("projectset", "projectset", searchSql)
+	projectList := elastic.Get(P_INDEX, P_TYPE, searchSql)
 	if projectList == nil || len(*projectList) == 0 {
 		return 0
 	}
@@ -325,9 +333,11 @@ func GetProjectContactCount(buyerName string) int64 {
 			}
 		}
 	}
+	logx.Info("GetProjectContactCount 单次耗时:", time.Since(start))
 	return int64(len(list))
 }
 func GetNewBiddingCount(buyer string) int64 {
+	start := time.Now()
 	if buyer == "" {
 		return 0
 	}
@@ -335,7 +345,10 @@ func GetNewBiddingCount(buyer string) int64 {
 	st, et := getTimeRage()
 	mustQuery = append(mustQuery, fmt.Sprintf(`{"range":{"publishtime":{"gte":%d,"lte":%d}}}`, st.Unix(), et.Unix()))
 	mustQuery = append(mustQuery, fmt.Sprintf(`{"term": {"buyer": "%s"}}`, buyer))
-	return elastic.Count(biddingIndex, biddingType, fmt.Sprintf(`{"query":{"bool":{"must":[%s]}}}`, strings.Join(mustQuery, ",")))
+
+	count := elastic.Count(biddingIndex, biddingType, fmt.Sprintf(`{"query":{"bool":{"must":[%s]}}}`, strings.Join(mustQuery, ",")))
+	logx.Info("GetNewBiddingCount 单次耗时:", time.Since(start))
+	return count
 }
 
 // 聚合查询
@@ -421,7 +434,7 @@ func IsReceived(buyerNames []string, entUserId string) (isRws map[string]string)
 
 // 是否为空请求
 func CheckEmpty(in *bxbuyer.BuyerListReq) bool {
-	if in.BuyerName == "" && len(in.BuyerClass) == 0 && len(in.Province) == 0 && len(in.City) == 0 {
+	if in.BuyerName == "" && len(in.BuyerClass) == 0 && len(in.Province) == 0 && len(in.City) == 0 && in.IsContact == 0 {
 		return true
 	}
 	return false
@@ -430,12 +443,13 @@ func CheckEmpty(in *bxbuyer.BuyerListReq) bool {
 //缓存数据查询
 
 // 获取采购单位查询query
-func BuyerListRedisCacheQuery() (qstr string, a string) {
-	query := `{"size": 0,"query":{"bool":{"must":[%s],"must_not": [{"term": {"buyer": ""}}],"should":[],"minimum_should_match": %d}}}`
+func BuyerListRedisCacheQuery(pageNum, pageSize int64) (qstr string, cstr string) {
+	query := `{"from":%d,"size":%d,"query":{"bool":{"must":[%s],"must_not": [{"term": {"buyer_name": ""}}],"should":[],"minimum_should_match": %d}}}`
+	queryCount := `{"query":{"bool":{"must":[%s],"must_not": [{"term": {"buyer": ""}}],"should":[],"minimum_should_match": %d}}}`
 	//21.1.20  为和画像保持一致  数据组要求 budget 改成 bidamount
 	musts := []string{}
 	boolsNum := 0
-	entcustomerClass := `{"terms":{"buyer":[`
+	entcustomerClass := `{"terms":{"buyer_name":[`
 	for k, v := range IC.C.DefaultBuyerNames {
 		if k > 0 {
 			entcustomerClass += `,`
@@ -444,7 +458,8 @@ func BuyerListRedisCacheQuery() (qstr string, a string) {
 	}
 	entcustomerClass += `]}}`
 	musts = append(musts, entcustomerClass)
-	qstr = fmt.Sprintf(query, strings.Join(musts, ","), boolsNum)
+	qstr = fmt.Sprintf(query, (pageNum-1)*pageSize, pageSize, strings.Join(musts, ","), boolsNum)
+	cstr = fmt.Sprintf(queryCount, strings.Join(musts, ","), boolsNum)
 	logx.Info("qstr:", qstr)
 	return
 }