Parcourir la source

Merge remote-tracking branch 'origin/feature/v1.1.53' into feature/v1.1.53

duxin il y a 1 an
Parent
commit
b4cc13b87e

+ 4 - 1
jyBXBuyer/go.mod

@@ -12,4 +12,7 @@ require (
 	google.golang.org/protobuf v1.31.1-0.20231027082548-f4a6c1f6e5c1
 )
 
-require app.yhyue.com/moapp/jypkg v1.13.3
+require (
+	app.yhyue.com/moapp/jypkg v1.13.3
+	github.com/gogf/gf/v2 v2.6.2
+)

+ 60 - 31
jyBXBuyer/rpc/internal/logic/buyerlistlogic.go

@@ -1,15 +1,18 @@
 package logic
 
 import (
-	"app.yhyue.com/moapp/jybase/redis"
+	MC "app.yhyue.com/moapp/jybase/common"
+	elastic "app.yhyue.com/moapp/jybase/es"
 	"context"
-	"encoding/json"
 	"fmt"
+	"github.com/gogf/gf/v2/util/gconv"
 	"github.com/zeromicro/go-zero/core/logx"
 	IC "jyBXBuyer/rpc/init"
 	"jyBXBuyer/rpc/internal/svc"
 	"jyBXBuyer/rpc/model"
 	"jyBXBuyer/rpc/type/bxbuyer"
+	"strings"
+	"time"
 )
 
 type BuyerListLogic struct {
@@ -26,6 +29,13 @@ func NewBuyerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BuyerLi
 	}
 }
 
+type TopAgg struct {
+	Buckets []struct {
+		Key      string `json:"key"`
+		DocCount int    `json:"doc_count"`
+	} `json:"buckets"`
+}
+
 // 采购单位搜索
 func (l *BuyerListLogic) BuyerList(in *bxbuyer.BuyerListReq) (*bxbuyer.BuyerListResp, error) {
 	logx.Info("----:", model.CheckEmpty(in))
@@ -50,37 +60,56 @@ func (l *BuyerListLogic) BuyerList(in *bxbuyer.BuyerListReq) (*bxbuyer.BuyerList
 	query, CountQuery := "", ""
 	buyerNames := []string{}
 	if model.CheckEmpty(in) {
-		var isBool = true
-		list := []*bxbuyer.BuyerList{} //100条数据
-		bs, err := redis.GetBytes("other", fmt.Sprintf(model.P_redis_key))
-		if err == nil && bs != nil && len(*bs) > 0 {
-			isBool = false
-			if err := json.Unmarshal(*bs, &list); err != nil {
-				isBool = true
-				logx.Info("获取redis缓存,序列化异常")
-			} else {
-				if len(list) > 0 {
-					// 根据页码返回数据
-					start := in.PageSize * (in.PageNum - 1)
-					end := in.PageSize * in.PageNum
-					count := len(list)
-					resp.Data.Count = int64(count)
-					if end > int64(len(list)) {
-						end = int64(len(list))
-					}
-					resp.Data.List = list[start:end]
-					for i := 0; i < len(resp.Data.List); i++ {
-						buyerNames = append(buyerNames, resp.Data.List[i].Buyer)
-					}
-				} else {
-					isBool = true
+		res := model.GetL2CacheData("other", model.BuyerListQueryLock, model.P_redis_key, func() interface{} {
+			//聚合查询获取最近一个月中标单位数量最多的采购单位
+			agg := model.GetAggs("projectset", "projectset", fmt.Sprintf(`{"query":{"bool":{"must":[{"range":{"jgtime":{"gt":%d}}}]}},"aggs":{"buyerTop":{"terms":{"field":"buyer","size":%d}}}}`, time.Now().AddDate(0, 0, -1).Unix(), IC.C.BuyerSearchLimit*2))
+			var ta TopAgg
+			err := gconv.Struct(gconv.String(agg["buyerTop"]), &ta)
+			if err != nil {
+				return nil
+			}
+			names := make([]string, 0, 100)
+			for _, bucket := range ta.Buckets {
+				if !(len(bucket.Key) > 5 && (strings.HasSuffix(bucket.Key, "公司") || strings.HasSuffix(bucket.Key, "学校") || strings.HasSuffix(bucket.Key, "学院") || strings.HasSuffix(bucket.Key, "医院"))) {
+					continue
+				}
+				names = append(names, bucket.Key)
+			}
+			//根据采购单位名称查询列表展示字段
+			rs := elastic.Get(model.BuyerIndex, model.BuyerIndex, fmt.Sprintf(`{"query":{"bool":{"must":[{"terms":{"buyer_name":["%s"]}}]}},"_source":["seo_id","name","province","city","buyerclass"],"size":%d}`, strings.Join(names, `","`), len(names)))
+			if rs == nil || len(*rs) == 0 {
+				return nil
+			}
+			var saveBuyerList []*bxbuyer.BuyerList
+			for i := 0; i < len(*rs); i++ {
+				saveBuyerList = append(saveBuyerList, &bxbuyer.BuyerList{
+					SeoId:      MC.ObjToString((*rs)[i]["seo_id"]),
+					Buyer:      MC.ObjToString((*rs)[i]["name"]),
+					Province:   MC.ObjToString((*rs)[i]["province"]),
+					City:       MC.ObjToString((*rs)[i]["city"]),
+					BuyerClass: MC.ObjToString((*rs)[i]["buyerclass"]),
+				})
+				if gconv.Int64(len(saveBuyerList)) > IC.C.BuyerSearchLimit {
+					break
+				}
+			}
+			return saveBuyerList
+		}, model.P_redis_time)
+		if !res.IsEmpty() {
+			var list []*bxbuyer.BuyerList
+			if err := res.Struct(&list); err == nil {
+				start := in.PageSize * (in.PageNum - 1)
+				end := in.PageSize * in.PageNum
+				count := len(list)
+				resp.Data.Count = int64(count)
+				if end > int64(len(list)) {
+					end = int64(len(list))
+				}
+				resp.Data.List = list[start:end]
+				for i := 0; i < len(resp.Data.List); i++ {
+					buyerNames = append(buyerNames, resp.Data.List[i].Buyer)
 				}
 			}
-		}
-		if isBool {
-			query = model.BuyerListRedisCacheQuery()
-			buyerNames, resp = model.BuyerListRedisCache(query, in)
-
 		}
 	} else {
 		query, CountQuery = model.BuyerListQuery(in)

+ 7 - 3
jyBXBuyer/rpc/model/buyerListBYEs.go

@@ -111,9 +111,9 @@ func getTimeRange() (st, et time.Time) {
 const (
 	P_INDEX                   = "projectset"
 	P_TYPE                    = "projectset"
-	P_redis_time              = 7 * 24 * 60 * 60 //redis存7天
-	P_redis_key               = "buyerListCache" // 存缓存 100条数据
-	BuyerIndex                = "buyer"          // 采购单位index
+	P_redis_time              = 7 * 24 * 60 * 60    //redis存7天
+	P_redis_key               = "buyerListCacheNew" // 存缓存 100条数据
+	BuyerIndex                = "buyer"             // 采购单位index
 	BuyerType                 = "buyer"
 	biddingIndex              = "bidding"
 	biddingType               = "bidding"
@@ -123,6 +123,10 @@ const (
 	BuyerProjectInfoRedisTime = 24 * 60 * 60          // 采购单位补充项目信息缓存时间
 )
 
+var (
+	BuyerListQueryLock = new(sync.RWMutex)
+)
+
 // GetBuyerList 查询采购单位列表
 func GetBuyerList(qstr string, CountQuery string, isCache bool) (buyerNames []string, resp *bxbuyer.BuyerListResp) {
 	t1 := time.Now()

+ 60 - 0
jyBXBuyer/rpc/model/redisL2Cache.go

@@ -0,0 +1,60 @@
+package model
+
+import (
+	"app.yhyue.com/moapp/jybase/redis"
+	"fmt"
+	"github.com/gogf/gf/v2/container/gvar"
+	"github.com/gogf/gf/v2/errors/gerror"
+	"strings"
+	"sync"
+	"time"
+)
+
+// GetL2CacheData 二级缓存
+// 注意 lock定义为全局锁
+func GetL2CacheData(dbName string, lock *sync.RWMutex, key string, f func() interface{}, cacheSec int) *gvar.Var {
+	var (
+		data    = gvar.New(redis.Get(dbName, key))
+		newData *gvar.Var
+	)
+	if data.IsEmpty() {
+		data = gvar.New(redis.Get(dbName, fmt.Sprintf("%s_L2Cache", key)))
+		go func() {
+			if lock.TryLock() {
+				defer lock.Unlock()
+				if n := f(); n != nil {
+					newData = gvar.New(n)
+					go func() {
+						redis.Put(dbName, key, newData, cacheSec)
+						redis.Put(dbName, fmt.Sprintf("%s_L2Cache", key), newData, -1)
+					}()
+				}
+			}
+		}()
+		if data.IsEmpty() {
+			time.Sleep(100 * time.Millisecond)
+			if newData.IsEmpty() {
+				data = newData
+			}
+		}
+	}
+	return data
+}
+
+// ClearL2Cache 清除缓存
+func ClearL2Cache(dbName, key string) (err error) {
+	var errCacheKey []string
+	for i := 1; i <= 2; i++ {
+		fKey := key
+		if i != 1 {
+			fKey = fmt.Sprintf("%s_L2Cache", key)
+		}
+		if ok := redis.Del(dbName, fKey); !ok {
+			errCacheKey = append(errCacheKey, fKey)
+		}
+	}
+	if len(errCacheKey) == 0 {
+		return nil
+	}
+	return gerror.Newf("清除缓存失败%s", strings.Join(errCacheKey, ","))
+}