123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- package logic
- import (
- MC "app.yhyue.com/moapp/jybase/common"
- elastic "app.yhyue.com/moapp/jybase/es"
- "context"
- "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 {
- ctx context.Context
- svcCtx *svc.ServiceContext
- logx.Logger
- }
- func NewBuyerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BuyerListLogic {
- return &BuyerListLogic{
- ctx: ctx,
- svcCtx: svcCtx,
- Logger: logx.WithContext(ctx),
- }
- }
- 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))
- resp := &bxbuyer.BuyerListResp{
- Data: &bxbuyer.BuyerData{},
- }
- if in.PageNum < 1 {
- in.PageNum = 1
- }
- if in.PageSize > 100 || in.PageSize < 1 {
- in.PageSize = 10
- }
- if in.PageSize > IC.C.BuyerSearchLimit {
- in.PageSize = IC.C.BuyerSearchLimit
- }
- // 判断数量
- // 采购单位搜索过来的 最多查BuyerSearchLimit条
- if in.PageNum*in.PageSize > IC.C.BuyerSearchLimit && (in.PageNum-1)*in.PageSize >= IC.C.BuyerSearchLimit {
- in.PageNum = IC.C.BuyerSearchLimit / in.PageSize
- }
- if in.BuyerName != "" && in.UserId != "" {
- // 保存搜索记录
- model.SaveHistory(in.BuyerName, in.UserId, "history_buyer_s_%s")
- }
- query, CountQuery := "", ""
- buyerNames := []string{}
- if model.CheckEmpty(in) {
- 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)
- }
- }
- }
- } else {
- query, CountQuery = model.BuyerListQuery(in)
- logx.Info("query:", query)
- buyerNames, resp = model.GetBuyerList(query, CountQuery, false) // 查询数据
- }
- if len(resp.Data.List) > 0 && (in.UserId != "" || in.EntUserId != "") {
- model.SupplyFollowInfo(in, buyerNames, resp)
- }
- if len(resp.Data.List) > 0 {
- if in.PageNum*in.PageSize > IC.C.BuyerSearchLimit && (in.PageNum-1)*in.PageSize < IC.C.BuyerSearchLimit {
- end := IC.C.BuyerSearchLimit - (in.PageNum-1)*in.PageSize
- if len(resp.Data.List) > int(end) {
- resp.Data.List = resp.Data.List[:end]
- }
- }
- }
- if resp.Data.Count > IC.C.BuyerSearchLimit {
- resp.Data.Count = IC.C.BuyerSearchLimit
- }
- return resp, nil
- }
|