buyerlistlogic.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package logic
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. elastic "app.yhyue.com/moapp/jybase/es"
  5. "context"
  6. "fmt"
  7. "github.com/gogf/gf/v2/util/gconv"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. IC "jyBXBuyer/rpc/init"
  10. "jyBXBuyer/rpc/internal/svc"
  11. "jyBXBuyer/rpc/model"
  12. "jyBXBuyer/rpc/type/bxbuyer"
  13. "strings"
  14. "time"
  15. )
  16. type BuyerListLogic struct {
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. logx.Logger
  20. }
  21. func NewBuyerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BuyerListLogic {
  22. return &BuyerListLogic{
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. Logger: logx.WithContext(ctx),
  26. }
  27. }
  28. type TopAgg struct {
  29. Buckets []struct {
  30. Key string `json:"key"`
  31. DocCount int `json:"doc_count"`
  32. } `json:"buckets"`
  33. }
  34. // 采购单位搜索
  35. func (l *BuyerListLogic) BuyerList(in *bxbuyer.BuyerListReq) (*bxbuyer.BuyerListResp, error) {
  36. logx.Info("----:", model.CheckEmpty(in))
  37. resp := &bxbuyer.BuyerListResp{
  38. Data: &bxbuyer.BuyerData{},
  39. }
  40. if in.PageNum < 1 {
  41. in.PageNum = 1
  42. }
  43. if in.PageSize > 100 || in.PageSize < 1 {
  44. in.PageSize = 10
  45. }
  46. if in.PageSize > IC.C.BuyerSearchLimit {
  47. in.PageSize = IC.C.BuyerSearchLimit
  48. }
  49. // 判断数量
  50. // 采购单位搜索过来的 最多查BuyerSearchLimit条
  51. if in.PageNum*in.PageSize > IC.C.BuyerSearchLimit && (in.PageNum-1)*in.PageSize >= IC.C.BuyerSearchLimit {
  52. in.PageNum = IC.C.BuyerSearchLimit / in.PageSize
  53. }
  54. query, CountQuery := "", ""
  55. buyerNames := []string{}
  56. if model.CheckEmpty(in) {
  57. res := model.GetL2CacheData("other", model.BuyerListQueryLock, model.P_redis_key, func() interface{} {
  58. //聚合查询获取最近一个月中标单位数量最多的采购单位
  59. 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))
  60. var ta TopAgg
  61. err := gconv.Struct(gconv.String(agg["buyerTop"]), &ta)
  62. if err != nil {
  63. return nil
  64. }
  65. names := make([]string, 0, 100)
  66. for _, bucket := range ta.Buckets {
  67. if !(len(bucket.Key) > 5 && (strings.HasSuffix(bucket.Key, "公司") || strings.HasSuffix(bucket.Key, "学校") || strings.HasSuffix(bucket.Key, "学院") || strings.HasSuffix(bucket.Key, "医院"))) {
  68. continue
  69. }
  70. names = append(names, bucket.Key)
  71. }
  72. //根据采购单位名称查询列表展示字段
  73. 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)))
  74. if rs == nil || len(*rs) == 0 {
  75. return nil
  76. }
  77. var saveBuyerList []*bxbuyer.BuyerList
  78. for i := 0; i < len(*rs); i++ {
  79. saveBuyerList = append(saveBuyerList, &bxbuyer.BuyerList{
  80. SeoId: MC.ObjToString((*rs)[i]["seo_id"]),
  81. Buyer: MC.ObjToString((*rs)[i]["name"]),
  82. Province: MC.ObjToString((*rs)[i]["province"]),
  83. City: MC.ObjToString((*rs)[i]["city"]),
  84. BuyerClass: MC.ObjToString((*rs)[i]["buyerclass"]),
  85. })
  86. if gconv.Int64(len(saveBuyerList)) > IC.C.BuyerSearchLimit {
  87. break
  88. }
  89. }
  90. return saveBuyerList
  91. }, model.P_redis_time)
  92. if !res.IsEmpty() {
  93. var list []*bxbuyer.BuyerList
  94. if err := res.Struct(&list); err == nil {
  95. start := in.PageSize * (in.PageNum - 1)
  96. end := in.PageSize * in.PageNum
  97. count := len(list)
  98. resp.Data.Count = int64(count)
  99. if end > int64(len(list)) {
  100. end = int64(len(list))
  101. }
  102. resp.Data.List = list[start:end]
  103. for i := 0; i < len(resp.Data.List); i++ {
  104. buyerNames = append(buyerNames, resp.Data.List[i].Buyer)
  105. }
  106. }
  107. }
  108. } else {
  109. query, CountQuery = model.BuyerListQuery(in)
  110. logx.Info("query:", query)
  111. buyerNames, resp = model.GetBuyerList(query, CountQuery, false) // 查询数据
  112. }
  113. if len(resp.Data.List) > 0 && (in.UserId != "" || in.EntUserId != "") {
  114. model.SupplyFollowInfo(in, buyerNames, resp)
  115. }
  116. if len(resp.Data.List) > 0 {
  117. if in.PageNum*in.PageSize > IC.C.BuyerSearchLimit && (in.PageNum-1)*in.PageSize < IC.C.BuyerSearchLimit {
  118. end := IC.C.BuyerSearchLimit - (in.PageNum-1)*in.PageSize
  119. if len(resp.Data.List) > int(end) {
  120. resp.Data.List = resp.Data.List[:end]
  121. }
  122. }
  123. }
  124. if resp.Data.Count > IC.C.BuyerSearchLimit {
  125. resp.Data.Count = IC.C.BuyerSearchLimit
  126. }
  127. return resp, nil
  128. }