provider_get_api_list.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package provider
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "encoding/json"
  7. "gd_management/common.in/utils"
  8. "github.com/astaxie/beego/orm"
  9. "go.uber.org/zap"
  10. )
  11. const (
  12. PORVIDERAPIPAGESIZE = 10
  13. )
  14. func ManagementGetProviderApiList(ctx context.Context, req *apis.ManagementGetProviderApiListReq, reply *apis.ManagementGetProviderApiListReply) error {
  15. o := orm.NewOrm()
  16. var err error
  17. if req.ProviderId == 0 {
  18. err = o.Raw("select count(id) from t_gd_provider_api order by id").QueryRow(&reply.Total)
  19. } else {
  20. err = o.Raw("select count(id) from t_gd_provider_api where provider_id=? order by id", req.ProviderId).QueryRow(&reply.Total)
  21. }
  22. if err != nil && err != orm.ErrNoRows {
  23. l.Error("mysql",
  24. zap.String("sql", "select count(id) from t_gd_provider_api"),
  25. zap.String("fields", utils.MarshalJsonString(req)),
  26. zap.String("error", err.Error()))
  27. return errors.DataBaseError
  28. }
  29. defer func() {
  30. for i, v := range reply.ProviderApis {
  31. reply.ProviderApis[i].Filters = []apis.ProviderParamFilter{}
  32. if v.Filter == "" {
  33. continue
  34. }
  35. json.Unmarshal([]byte(v.Filter), &reply.ProviderApis[i].Filters)
  36. }
  37. }()
  38. if req.IsAll {
  39. if req.ProviderId == 0 {
  40. _, err = o.Raw("select * from t_gd_provider_api order by id").QueryRows(&reply.ProviderApis)
  41. } else {
  42. _, err = o.Raw("select * from t_gd_provider_api where provider_id=? order by id", req.ProviderId).QueryRows(&reply.ProviderApis)
  43. }
  44. if err != nil && err != orm.ErrNoRows {
  45. l.Error("mysql",
  46. zap.String("sql", "select t_gd_provider_api"),
  47. zap.String("fields", utils.MarshalJsonString(req)),
  48. zap.String("error", err.Error()))
  49. return errors.DataBaseError
  50. }
  51. return nil
  52. }
  53. if req.PageSize == 0 {
  54. req.PageSize = PORVIDERAPIPAGESIZE
  55. }
  56. if req.PageNumber == 0 {
  57. req.PageNumber = 1
  58. }
  59. if req.ProviderId == 0 {
  60. _, err = o.Raw("select * from t_gd_provider_api order by id limit ? offset ?", req.PageSize, (req.PageNumber-1)*req.PageSize).QueryRows(&reply.ProviderApis)
  61. } else {
  62. _, err = o.Raw("select * from t_gd_provider_api where provider_id=? order by id limit ? offset ?", req.ProviderId, req.PageSize, (req.PageNumber-1)*req.PageSize).QueryRows(&reply.ProviderApis)
  63. }
  64. if err != nil && err != orm.ErrNoRows {
  65. l.Error("mysql",
  66. zap.String("sql", "select t_gd_provider_api"),
  67. zap.String("fields", utils.MarshalJsonString(req)),
  68. zap.String("error", err.Error()))
  69. return errors.DataBaseError
  70. }
  71. reply.PageNumber = req.PageNumber
  72. reply.PageSize = req.PageSize
  73. return nil
  74. }