buyerlistlogic.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. query, CountQuery := "", ""
  31. resp := &bxbuyer.BuyerListResp{}
  32. // 采购单位搜索过来的 最多查100条
  33. if in.PageSize*in.PageNum > IC.C.BuyerSearchLimit {
  34. in.PageNum = IC.C.BuyerSearchLimit / in.PageSize
  35. }
  36. if in.PageNum < 1 {
  37. in.PageNum = 1
  38. }
  39. if in.PageSize < 1 {
  40. in.PageNum = 10
  41. }
  42. if model.CheckEmpty(in) {
  43. //获取缓存数据
  44. var isBool = true
  45. bs, err := redis.GetBytes("other", fmt.Sprintf(model.P_redis_key, in.PageNum, in.PageSize))
  46. if err == nil && bs != nil && len(*bs) > 0 {
  47. isBool = false
  48. if err := json.Unmarshal(*bs, &resp.Data); err != nil {
  49. isBool = true
  50. logx.Info("获取redis缓存,序列化异常")
  51. }
  52. }
  53. if isBool {
  54. query, CountQuery = model.BuyerListRedisCacheQuery(in.PageNum, in.PageSize)
  55. resp = model.GetBuyerList(query, CountQuery, in, true)
  56. if len(resp.Data.List) > 0 {
  57. model.SupplyBuyerListData(resp)
  58. }
  59. b, err := json.Marshal(resp.Data)
  60. if err == nil {
  61. redis.PutBytes("other", fmt.Sprintf(model.P_redis_key, in.PageNum, in.PageSize), &b, model.P_redis_time)
  62. } else {
  63. logx.Info("缓存数据 序列化异常")
  64. }
  65. }
  66. } else {
  67. query, CountQuery = model.BuyerListQuery(in)
  68. logx.Info("query:", query)
  69. resp = model.GetBuyerList(query, CountQuery, in, false) // 查询数据
  70. if len(resp.Data.List) > 0 {
  71. model.SupplyBuyerListData(resp)
  72. }
  73. }
  74. if resp.Data.Count > IC.C.BuyerSearchLimit {
  75. resp.Data.Count = IC.C.BuyerSearchLimit
  76. }
  77. logx.Info("SupplyBuyerListData耗时:", time.Since(start2))
  78. return resp, nil
  79. }