data_api_get_base_api.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package data_api
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "encoding/json"
  7. "github.com/astaxie/beego/orm"
  8. )
  9. type ProviderGroupApiItem struct {
  10. GroupNo int `json:"group_no"`
  11. GroupName string `json:"group_name"`
  12. apis.BaseApiProviderItem
  13. }
  14. func GetDataApiProviderList(childDataApiId int64) (ret []apis.BaseApiProviderGroupItem, err error) {
  15. var list = []ProviderGroupApiItem{}
  16. sql := "select t1.provider_api_id, priority, t1.group_no, t1.group_name," +
  17. " t1.enable,thirdpart_api_name," +
  18. " thirdpart_api_router,platform_name from" +
  19. " (t_gd_data_api_provider_api_relation" +
  20. " as t1 left join t_gd_provider_api as t2 on" +
  21. " t1.provider_api_id = t2.id)" +
  22. " left join t_gd_provider as t3 on t2.provider_id=t3.id" +
  23. " left join t_gd_api_provider_relation as t4 on" +
  24. " t4.provider_api_id=t1.provider_api_id and" +
  25. " t4.group_no=t1.group_no and t4.api_id = " +
  26. " (select api_id from t_gd_child_data_api" +
  27. " where id=t1.child_data_api_id) where" +
  28. " t1.child_data_api_id=? order by group_no, priority"
  29. _, err = orm.NewOrm().Raw(sql, childDataApiId).QueryRows(&list)
  30. if err != nil && err != orm.ErrNoRows {
  31. return nil, errors.DataBaseError
  32. }
  33. m := map[int][]apis.BaseApiProviderItem{}
  34. nameMap := map[int]string{}
  35. groupNoArray := []int{}
  36. for _, v := range list {
  37. if _, ok := m[v.GroupNo]; ok == false {
  38. groupNoArray = append(groupNoArray, v.GroupNo)
  39. }
  40. nameMap[v.GroupNo] = v.GroupName
  41. m[v.GroupNo] = append(m[v.GroupNo], v.BaseApiProviderItem)
  42. }
  43. for _, v := range groupNoArray {
  44. item := apis.BaseApiProviderGroupItem{}
  45. item.GroupNo = v
  46. item.GroupName, _ = nameMap[v]
  47. item.ProviderApiList, _ = m[v]
  48. ret = append(ret, item)
  49. }
  50. return ret, nil
  51. //l.Debug(utils.MarshalJsonString(req, reply))
  52. }
  53. func DataApiGetBaseApi(ctx context.Context, req *apis.ManagementDataApiGetBaseApiReq, reply *apis.ManagementDataApiGetBaseApiReply) error {
  54. if req.DataApiBaseApiId == 0 {
  55. return errors.ArgsError
  56. }
  57. o := orm.NewOrm()
  58. var err error
  59. _, err = o.QueryTable("t_gd_child_data_api").Filter("id", req.DataApiBaseApiId).All(&reply.DataApiBaseApis)
  60. if err != nil {
  61. if err != orm.ErrNoRows {
  62. return errors.DataBaseError
  63. } else {
  64. return nil
  65. }
  66. }
  67. json.Unmarshal([]byte(reply.DataApiBaseApis.ResponseParam), &reply.DataApiBaseApis.ResponseParams)
  68. json.Unmarshal([]byte(reply.DataApiBaseApis.RequestParam), &reply.DataApiBaseApis.RequestParams)
  69. json.Unmarshal([]byte(reply.DataApiBaseApis.Filter), &reply.DataApiBaseApis.Filters)
  70. if len(reply.DataApiBaseApis.RequestParams) == 0 {
  71. reply.DataApiBaseApis.RequestParams = []apis.ManagementBaseApiParam{}
  72. }
  73. if len(reply.DataApiBaseApis.ResponseParams) == 0 {
  74. reply.DataApiBaseApis.ResponseParams = []apis.ManagementBaseApiParam{}
  75. }
  76. if len(reply.DataApiBaseApis.Filters) == 0 {
  77. reply.DataApiBaseApis.Filters = []apis.BaseApiParamFilter{}
  78. }
  79. err = o.Raw("select name from t_gd_api where id=?", reply.DataApiBaseApis.ApiId).QueryRow(&reply.DataApiBaseApis.ApiName)
  80. if err != nil {
  81. if err != orm.ErrNoRows {
  82. return errors.DataBaseError
  83. }
  84. }
  85. reply.DataApiBaseApis.ProviderGroupList, err = GetDataApiProviderList(req.DataApiBaseApiId)
  86. if err != nil {
  87. return err
  88. }
  89. return nil
  90. }