base_api_list.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package base_api
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "fmt"
  7. "strings"
  8. "gd_management/common.in/utils"
  9. "github.com/astaxie/beego/orm"
  10. "go.uber.org/zap"
  11. )
  12. var PAGESIZE = 10
  13. func getBaseApiType(typeInt int) string {
  14. if typeInt == 0 {
  15. return "数据"
  16. }
  17. if typeInt == 1 {
  18. return "h5"
  19. }
  20. return "辅助"
  21. }
  22. func getMerchantApiIds(o orm.Ormer, req *apis.ManagementGetBaseApiListReq) ([]int64, error) {
  23. ids := []int64{}
  24. sql := fmt.Sprintf("select api_id from t_gd_merchant_child_data_api as t1 left join t_gd_merchant_data_api as t2 on t1.merchant_data_api_id=t2.id where t2.merchant_id=%d", req.MerchantId)
  25. _, err := o.Raw(sql).QueryRows(&ids)
  26. if err != nil && err != orm.ErrNoRows {
  27. return nil, errors.DataBaseError
  28. }
  29. return ids, nil
  30. }
  31. func ManagementGetBaseApiList(ctx context.Context, req *apis.ManagementGetBaseApiListReq, reply *apis.ManagementGetBaseApiListReply) (err error) {
  32. o := orm.NewOrm()
  33. list := []apis.TGdApi{}
  34. apiIds := []interface{}{}
  35. if req.MerchantId != 0 && req.IsAll {
  36. ids, err := getMerchantApiIds(o, req)
  37. if err != nil {
  38. return errors.DataBaseError
  39. }
  40. if len(ids) == 0 {
  41. return nil
  42. }
  43. apiIds = make([]interface{}, len(ids))
  44. for i, _ := range ids {
  45. apiIds[i] = ids[i]
  46. }
  47. }
  48. if req.IsAll == true {
  49. if len(apiIds) == 0 {
  50. _, err = o.QueryTable("t_gd_api").Filter("name__contains", req.Search).OrderBy("-update_time").All(&list)
  51. } else {
  52. _, err = o.QueryTable("t_gd_api").Filter("name__contains", req.Search).Filter("id__in", apiIds...).OrderBy("-update_time").All(&list)
  53. }
  54. if err != nil && err != orm.ErrNoRows {
  55. l.Error("func",
  56. zap.String("call", "ManagementGetBaseApiList"),
  57. zap.String("args", utils.MarshalJsonString(req)),
  58. zap.String("error", err.Error()))
  59. return errors.DataBaseError
  60. }
  61. for _, v := range list {
  62. iterm := apis.ManagementBaseApiListItem{}
  63. iterm.ApiId = v.Id
  64. iterm.Enable = v.Enable
  65. iterm.IsShow = v.IsShow
  66. iterm.Name = v.Name
  67. iterm.Type = getBaseApiType(v.ApiType)
  68. iterm.Router = v.Router
  69. iterm.OperationTime = v.UpdateTime
  70. iterm.Method = v.Method
  71. iterm.RequestParam = v.RequestParam
  72. iterm.ResponseParam = v.ResponseParam
  73. iterm.ThresholdTimeout = v.ThresholdTimeout
  74. iterm.ThresholdFailRate = v.ThresholdFailRate
  75. iterm.ThresholdNorecordRate = v.ThresholdNorecordRate
  76. iterm.WarningEnable = v.WarningEnable
  77. iterm.RateLimit = v.RateLimit
  78. reply.BaseApiItemList = append(reply.BaseApiItemList, iterm)
  79. }
  80. return nil
  81. }
  82. if req.PageNumber <= 0 {
  83. req.PageNumber = 1
  84. }
  85. if req.Search != "" {
  86. req.Search = strings.TrimSpace(req.Search)
  87. }
  88. sql := `select count(id) from t_gd_api where name like binary "%+%" or router like binary "%+%" and api_type=?`
  89. sql = strings.Replace(sql, "+", req.Search, -1)
  90. err = o.Raw(sql, req.ApiType).QueryRow(&reply.Total)
  91. if err != nil && err != orm.ErrNoRows {
  92. l.Error("mysql",
  93. zap.String("sql", sql),
  94. zap.String("args", utils.MarshalJsonString(req)),
  95. zap.String("error", err.Error()))
  96. return errors.DataBaseError
  97. }
  98. if err == orm.ErrNoRows {
  99. return nil
  100. }
  101. querySet := o.QueryTable("t_gd_api").Filter("api_type", req.ApiType)
  102. cond := orm.NewCondition().AndCond(orm.NewCondition().And("name__contains", req.Search).Or("router__contains", req.Search))
  103. querySet = querySet.SetCond(cond)
  104. _, err = querySet.OrderBy("-update_time").Limit(PAGESIZE, (req.PageNumber-1)*PAGESIZE).All(&list)
  105. //_, err = o.QueryTable("t_gd_api").Filter("name__contains", req.Search).Filter("api_type", req.ApiType).OrderBy("-update_time").Limit(PAGESIZE, (req.PageNumber-1)*PAGESIZE).All(&list)
  106. if err != nil && err != orm.ErrNoRows {
  107. l.Error("func",
  108. zap.String("call", "ManagementGetBaseApiList"),
  109. zap.String("args", utils.MarshalJsonString(req)),
  110. zap.String("error", err.Error()))
  111. return errors.DataBaseError
  112. }
  113. if err != nil && err == orm.ErrNoRows {
  114. return nil
  115. }
  116. for _, v := range list {
  117. iterm := apis.ManagementBaseApiListItem{}
  118. iterm.ApiId = v.Id
  119. iterm.Enable = v.Enable
  120. iterm.IsShow = v.IsShow
  121. iterm.Name = v.Name
  122. iterm.Type = getBaseApiType(v.ApiType)
  123. iterm.Router = v.Router
  124. iterm.OperationTime = v.UpdateTime
  125. iterm.ThresholdTimeout = v.ThresholdTimeout
  126. iterm.ThresholdFailRate = v.ThresholdFailRate
  127. iterm.ThresholdNorecordRate = v.ThresholdNorecordRate
  128. iterm.WarningEnable = v.WarningEnable
  129. iterm.RateLimit = v.RateLimit
  130. reply.BaseApiItemList = append(reply.BaseApiItemList, iterm)
  131. }
  132. reply.PageSize = 10
  133. reply.PageNumber = req.PageNumber
  134. l.Debug(utils.MarshalJsonString(req, reply))
  135. return
  136. }