buyerlistlogic.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "time"
  13. )
  14. type BuyerListLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewBuyerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BuyerListLogic {
  20. return &BuyerListLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. // 采购单位搜索
  27. func (l *BuyerListLogic) BuyerList(in *bxbuyer.BuyerListReq) (*bxbuyer.BuyerListResp, error) {
  28. start2 := time.Now()
  29. logx.Info("----:", model.CheckEmpty(in))
  30. resp := &bxbuyer.BuyerListResp{}
  31. // 采购单位搜索过来的 最多查100条
  32. if in.PageSize*in.PageNum > IC.C.BuyerSearchLimit {
  33. in.PageNum = IC.C.BuyerSearchLimit / in.PageSize
  34. }
  35. if in.PageNum < 1 {
  36. in.PageNum = 1
  37. }
  38. if in.PageSize < 1 || in.PageSize > 100 {
  39. in.PageSize = 10
  40. }
  41. query, CountQuery := "", ""
  42. buyerNames := []string{}
  43. if model.CheckEmpty(in) {
  44. //获取缓存数据
  45. var isBool = true
  46. bs, err := redis.GetBytes("other", fmt.Sprintf(model.P_redis_key, in.PageNum, in.PageSize))
  47. if err == nil && bs != nil && len(*bs) > 0 {
  48. isBool = false
  49. if err := json.Unmarshal(*bs, &resp.Data); err != nil {
  50. isBool = true
  51. logx.Info("获取redis缓存,序列化异常")
  52. }
  53. }
  54. if isBool {
  55. query, CountQuery = model.BuyerListRedisCacheQuery(in.PageNum, in.PageSize)
  56. buyerNames, resp = model.GetBuyerList(query, CountQuery, true)
  57. if len(resp.Data.List) > 0 {
  58. b, err := json.Marshal(resp.Data)
  59. if err == nil {
  60. redis.PutBytes("other", fmt.Sprintf(model.P_redis_key, in.PageNum, in.PageSize), &b, model.P_redis_time)
  61. } else {
  62. logx.Info("缓存数据 序列化异常")
  63. }
  64. }
  65. }
  66. } else {
  67. query, CountQuery = model.BuyerListQuery(in)
  68. logx.Info("query:", query)
  69. buyerNames, resp = model.GetBuyerList(query, CountQuery, false) // 查询数据
  70. }
  71. if len(resp.Data.List) > 0 {
  72. model.SupplyFollowInfo(in, buyerNames, resp)
  73. model.SupplyBuyerListData(buyerNames, resp)
  74. }
  75. if resp.Data.Count > IC.C.BuyerSearchLimit {
  76. resp.Data.Count = IC.C.BuyerSearchLimit
  77. }
  78. logx.Info("SupplyBuyerListData耗时:", time.Since(start2))
  79. return resp, nil
  80. }