|
@@ -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)
|