data_api_get_all.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package data_api
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "gd_management/common.in/utils"
  10. "github.com/astaxie/beego/orm"
  11. "go.uber.org/zap"
  12. )
  13. const (
  14. DATAAPIPAGESIZE = 10
  15. )
  16. func getDataApiSql(req *apis.ManagementDataApiGetAllReq) string {
  17. if req.Search != "" {
  18. req.Search = strings.TrimSpace(req.Search)
  19. }
  20. sql := "select t1.* from t_gd_data_api as t1"
  21. where := false
  22. if req.MerchantId > 0 {
  23. sql = fmt.Sprintf("select t1.* from t_gd_data_api as t1 left join t_gd_data_api_query_type as t2 on t1.id = t2.data_api_id left join t_gd_merchant_data_api as t3 on t3.query_type_id = t2.id where t3.merchant_id=%d", req.MerchantId)
  24. where = true
  25. }
  26. if req.Search != "" {
  27. search := `%search%`
  28. search = strings.Replace(search, "search", req.Search, -1)
  29. if where {
  30. sql = fmt.Sprintf("%s and t1.data_api_name like '%s'", sql, search)
  31. } else {
  32. sql = fmt.Sprintf("%s where t1.data_api_name like '%s'", sql, search)
  33. }
  34. where = true
  35. }
  36. if req.DataApiType != "" {
  37. dtype, err := strconv.Atoi(req.DataApiType)
  38. if err != nil {
  39. return sql
  40. }
  41. if dtype <= 0 {
  42. return sql
  43. }
  44. if where {
  45. sql = fmt.Sprintf("%s and t1.data_api_type=%d", sql, dtype)
  46. } else {
  47. sql = fmt.Sprintf("%s where t1.data_api_type=%d", sql, dtype)
  48. }
  49. }
  50. return sql
  51. }
  52. func DataApiGetAll(ctx context.Context, req *apis.ManagementDataApiGetAllReq, reply *apis.ManagementDataApiGetAllReply) error {
  53. /*defer func() {
  54. newApis := []apis.DataApi{}
  55. if req.DataApiType == "" {
  56. return
  57. }
  58. dtype, err := strconv.Atoi(req.DataApiType)
  59. if err != nil {
  60. return
  61. }
  62. for _, v := range reply.DataApis {
  63. if v.DataApiType == dtype {
  64. newApis = append(newApis, v)
  65. }
  66. }
  67. reply.DataApis = newApis
  68. }()*/
  69. o := orm.NewOrm()
  70. sql := getDataApiSql(req)
  71. if req.IsAll == true {
  72. sql = fmt.Sprintf("%s order by update_time desc", sql)
  73. _, err := o.Raw(sql).QueryRows(&reply.DataApis)
  74. if err != nil && err != orm.ErrNoRows {
  75. l.Error("mysql",
  76. zap.String("sql", "select t_gd_data_api"),
  77. zap.String("fields", utils.MarshalJsonString(req)),
  78. zap.String("error", err.Error()))
  79. return errors.DataBaseError
  80. }
  81. return nil
  82. }
  83. /*sql := ""
  84. if req.DataApiType != "" {
  85. sql = fmt.Sprintf(`select count(id) from t_gd_data_api where data_api_name like "%?%" and data_api_type = %s`, req.DataApiType)
  86. } else {
  87. sql = `select count(id) from t_gd_data_api where data_api_name like "%?%"`
  88. }
  89. sql = strings.Replace(sql, "?", req.Search, -1)
  90. err := o.Raw(sql).QueryRow(&reply.Total)
  91. if err != nil && err != orm.ErrNoRows {
  92. l.Error("mysql",
  93. zap.String("sql", sql),
  94. zap.String("fields", utils.MarshalJsonString(req)),
  95. zap.String("error", err.Error()))
  96. return errors.DataBaseError
  97. }*/
  98. if req.PageSize == 0 {
  99. req.PageSize = DATAAPIPAGESIZE
  100. }
  101. if req.PageNumber == 0 {
  102. req.PageNumber = 1
  103. }
  104. var querySeter orm.QuerySeter
  105. querySeter = o.QueryTable("t_gd_data_api").Filter("data_api_name__contains", req.Search)
  106. if req.DataApiType != "" {
  107. dtype, err := strconv.Atoi(req.DataApiType)
  108. if err != nil {
  109. return err
  110. }
  111. if dtype > 0 {
  112. querySeter = querySeter.Filter("data_api_type", dtype)
  113. }
  114. }
  115. reply.Total, _ = querySeter.Count()
  116. _, err := querySeter.OrderBy("-update_time").Limit(req.PageSize, (req.PageNumber-1)*req.PageSize).All(&reply.DataApis)
  117. if err != nil && err != orm.ErrNoRows {
  118. l.Error("mysql",
  119. zap.String("sql", "select t_gd_data_api"),
  120. zap.String("fields", utils.MarshalJsonString(req)),
  121. zap.String("error", err.Error()))
  122. return errors.DataBaseError
  123. }
  124. reply.PageNumber = req.PageNumber
  125. reply.PageSize = req.PageSize
  126. return nil
  127. }