buyerlistlogic.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. if in.BuyerName != "" && in.UserId != "" {
  55. // 保存搜索记录
  56. model.SaveHistory(in.BuyerName, in.UserId, "history_buyer_s_%s")
  57. }
  58. query, CountQuery := "", ""
  59. buyerNames := []string{}
  60. if model.CheckEmpty(in) {
  61. res := model.GetL2CacheData("other", model.BuyerListQueryLock, model.P_redis_key, func() interface{} {
  62. //聚合查询获取最近一个月中标单位数量最多的采购单位
  63. 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))
  64. var ta TopAgg
  65. err := gconv.Struct(gconv.String(agg["buyerTop"]), &ta)
  66. if err != nil {
  67. return nil
  68. }
  69. names := make([]string, 0, 100)
  70. for _, bucket := range ta.Buckets {
  71. if !(len(bucket.Key) > 5 && (strings.HasSuffix(bucket.Key, "公司") || strings.HasSuffix(bucket.Key, "学校") || strings.HasSuffix(bucket.Key, "学院") || strings.HasSuffix(bucket.Key, "医院"))) {
  72. continue
  73. }
  74. names = append(names, bucket.Key)
  75. }
  76. //根据采购单位名称查询列表展示字段
  77. 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)))
  78. if rs == nil || len(*rs) == 0 {
  79. return nil
  80. }
  81. var saveBuyerList []*bxbuyer.BuyerList
  82. for i := 0; i < len(*rs); i++ {
  83. saveBuyerList = append(saveBuyerList, &bxbuyer.BuyerList{
  84. SeoId: MC.ObjToString((*rs)[i]["seo_id"]),
  85. Buyer: MC.ObjToString((*rs)[i]["name"]),
  86. Province: MC.ObjToString((*rs)[i]["province"]),
  87. City: MC.ObjToString((*rs)[i]["city"]),
  88. BuyerClass: MC.ObjToString((*rs)[i]["buyerclass"]),
  89. })
  90. if gconv.Int64(len(saveBuyerList)) > IC.C.BuyerSearchLimit {
  91. break
  92. }
  93. }
  94. return saveBuyerList
  95. }, model.P_redis_time)
  96. if !res.IsEmpty() {
  97. var list []*bxbuyer.BuyerList
  98. if err := res.Struct(&list); err == nil {
  99. start := in.PageSize * (in.PageNum - 1)
  100. end := in.PageSize * in.PageNum
  101. count := len(list)
  102. resp.Data.Count = int64(count)
  103. if end > int64(len(list)) {
  104. end = int64(len(list))
  105. }
  106. resp.Data.List = list[start:end]
  107. for i := 0; i < len(resp.Data.List); i++ {
  108. buyerNames = append(buyerNames, resp.Data.List[i].Buyer)
  109. }
  110. }
  111. }
  112. } else {
  113. query, CountQuery = model.BuyerListQuery(in)
  114. logx.Info("query:", query)
  115. buyerNames, resp = model.GetBuyerList(query, CountQuery, false) // 查询数据
  116. }
  117. if len(resp.Data.List) > 0 && (in.UserId != "" || in.EntUserId != "") {
  118. model.SupplyFollowInfo(in, buyerNames, resp)
  119. }
  120. if len(resp.Data.List) > 0 {
  121. if in.PageNum*in.PageSize > IC.C.BuyerSearchLimit && (in.PageNum-1)*in.PageSize < IC.C.BuyerSearchLimit {
  122. end := IC.C.BuyerSearchLimit - (in.PageNum-1)*in.PageSize
  123. if len(resp.Data.List) > int(end) {
  124. resp.Data.List = resp.Data.List[:end]
  125. }
  126. }
  127. }
  128. if resp.Data.Count > IC.C.BuyerSearchLimit {
  129. resp.Data.Count = IC.C.BuyerSearchLimit
  130. }
  131. return resp, nil
  132. }