try_api_task_list.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package base_api
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "strings"
  7. "github.com/astaxie/beego/orm"
  8. )
  9. func ManagementGetTryApiTaskList(ctx context.Context, req *apis.ManagementGetTryApiTaskListReq, reply *apis.ManagementGetTryApiTaskListReply) (err error) {
  10. total := 0
  11. sql := "select count(1) from t_gd_try_api_task where type = ?"
  12. o := orm.NewOrm()
  13. if req.MerchantId > 0 {
  14. sql += " AND merchant_id = ?"
  15. err = o.Raw(sql, req.Type, req.MerchantId).QueryRow(&total)
  16. } else {
  17. err = o.Raw(sql, req.Type).QueryRow(&total)
  18. }
  19. if err != nil {
  20. return errors.DataBaseError
  21. }
  22. if req.PageSize == 0 {
  23. req.PageSize = PAGESIZE
  24. }
  25. reply.Total = total
  26. var list = []apis.TryApi{}
  27. defer func() {
  28. for _, v := range list {
  29. item := apis.TryApiItem{}
  30. item.ApiName = v.ApiName
  31. item.CreatedAt = v.CreatedAt
  32. item.FileName = v.FileName
  33. item.DownloadFile = v.DownloadFile
  34. item.ApiRouter = v.ApiRouter
  35. item.TaskName = v.TaskName
  36. item.Merchant = v.Merchant
  37. item.Id = v.Id
  38. item.StatusCode = v.StatusCode
  39. reply.Tasks = append(reply.Tasks, item)
  40. }
  41. }()
  42. if req.IsAll {
  43. if req.MerchantId > 0 {
  44. sql := `select * from t_gd_try_api_task where api_name like "%??%" and type = ? AND merchant_id = ? order by updated_at desc`
  45. sql = strings.ReplaceAll(sql, "??", req.ApiName)
  46. _, err = o.Raw(sql, req.Type, req.MerchantId).QueryRows(&list)
  47. } else {
  48. sql := `select * from t_gd_try_api_task where api_name like "%??%" and type = ? order by updated_at desc`
  49. sql = strings.ReplaceAll(sql, "??", req.ApiName)
  50. _, err = o.Raw(sql, req.Type).QueryRows(&list)
  51. }
  52. if err != nil && err != orm.ErrNoRows {
  53. return errors.DataBaseError
  54. }
  55. return nil
  56. }
  57. if req.PageNumber > 0 {
  58. req.PageNumber--
  59. }
  60. reply.PageNumber = req.PageNumber + 1
  61. reply.PageSize = req.PageSize
  62. if req.MerchantId > 0 {
  63. sql = `select * from t_gd_try_api_task where api_name like "%??%" and type = ? AND merchant_id = ? order by updated_at desc limit ? offset ?`
  64. sql = strings.ReplaceAll(sql, "??", req.ApiName)
  65. _, err = o.Raw(sql, req.Type, req.MerchantId, req.PageSize, req.PageNumber*req.PageSize).QueryRows(&list)
  66. } else {
  67. sql = `select * from t_gd_try_api_task where api_name like "%??%" and type = ? order by updated_at desc limit ? offset ?`
  68. sql = strings.ReplaceAll(sql, "??", req.ApiName)
  69. _, err = o.Raw(sql, req.Type, req.PageSize, req.PageNumber*req.PageSize).QueryRows(&list)
  70. }
  71. if err != nil && err != orm.ErrNoRows {
  72. return errors.DataBaseError
  73. }
  74. return nil
  75. }