buyerlistlogic.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package logic
  2. import (
  3. "app.yhyue.com/moapp/jybase/redis"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. IC "jyBXBuyer/rpc/init"
  9. "jyBXBuyer/rpc/internal/svc"
  10. "jyBXBuyer/rpc/model"
  11. "jyBXBuyer/rpc/type/bxbuyer"
  12. )
  13. type BuyerListLogic struct {
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. logx.Logger
  17. }
  18. func NewBuyerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BuyerListLogic {
  19. return &BuyerListLogic{
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. Logger: logx.WithContext(ctx),
  23. }
  24. }
  25. // 采购单位搜索
  26. func (l *BuyerListLogic) BuyerList(in *bxbuyer.BuyerListReq) (*bxbuyer.BuyerListResp, error) {
  27. logx.Info("----:", model.CheckEmpty(in))
  28. resp := &bxbuyer.BuyerListResp{
  29. Data: &bxbuyer.BuyerData{},
  30. }
  31. if in.PageNum < 1 {
  32. in.PageNum = 1
  33. }
  34. if in.PageSize > 100 || in.PageSize < 1 {
  35. in.PageSize = 10
  36. }
  37. if in.PageSize > IC.C.BuyerSearchLimit {
  38. in.PageSize = IC.C.BuyerSearchLimit
  39. }
  40. // 判断数量
  41. // 采购单位搜索过来的 最多查BuyerSearchLimit条
  42. if in.PageNum*in.PageSize > IC.C.BuyerSearchLimit && (in.PageNum-1)*in.PageSize >= IC.C.BuyerSearchLimit {
  43. in.PageNum = IC.C.BuyerSearchLimit / in.PageSize
  44. }
  45. query, CountQuery := "", ""
  46. buyerNames := []string{}
  47. if model.CheckEmpty(in) {
  48. var isBool = true
  49. list := []*bxbuyer.BuyerList{} //100条数据
  50. bs, err := redis.GetBytes("other", fmt.Sprintf(model.P_redis_key))
  51. if err == nil && bs != nil && len(*bs) > 0 {
  52. isBool = false
  53. if err := json.Unmarshal(*bs, &list); err != nil {
  54. isBool = true
  55. logx.Info("获取redis缓存,序列化异常")
  56. } else {
  57. if len(list) > 0 {
  58. // 根据页码返回数据
  59. start := in.PageSize * (in.PageNum - 1)
  60. end := in.PageSize * in.PageNum
  61. count := len(list)
  62. resp.Data.Count = int64(count)
  63. if end > int64(len(list)) {
  64. end = int64(len(list))
  65. }
  66. resp.Data.List = list[start:end]
  67. for i := 0; i < len(resp.Data.List); i++ {
  68. buyerNames = append(buyerNames, resp.Data.List[i].Buyer)
  69. }
  70. } else {
  71. isBool = true
  72. }
  73. }
  74. }
  75. if isBool {
  76. query = model.BuyerListRedisCacheQuery()
  77. buyerNames, resp = model.BuyerListRedisCache(query, in)
  78. }
  79. } else {
  80. query, CountQuery = model.BuyerListQuery(in)
  81. logx.Info("query:", query)
  82. buyerNames, resp = model.GetBuyerList(query, CountQuery, false) // 查询数据
  83. }
  84. if len(resp.Data.List) > 0 && (in.UserId != "" || in.EntUserId != "") {
  85. model.SupplyFollowInfo(in, buyerNames, resp)
  86. }
  87. if len(resp.Data.List) > 0 {
  88. if in.PageNum*in.PageSize > IC.C.BuyerSearchLimit && (in.PageNum-1)*in.PageSize < IC.C.BuyerSearchLimit {
  89. end := IC.C.BuyerSearchLimit - (in.PageNum-1)*in.PageSize
  90. if len(resp.Data.List) > int(end) {
  91. resp.Data.List = resp.Data.List[:end]
  92. }
  93. }
  94. }
  95. if resp.Data.Count > IC.C.BuyerSearchLimit {
  96. resp.Data.Count = IC.C.BuyerSearchLimit
  97. }
  98. return resp, nil
  99. }