base_api_provider_list.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package base_api
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "github.com/astaxie/beego/orm"
  7. //"strings"
  8. )
  9. type ProviderGroupApiItem struct {
  10. GroupNo int `json:"group_no"`
  11. GroupName string `json:"group_name"`
  12. apis.BaseApiProviderItem
  13. }
  14. func ManagementGetBaseApiProviderList(ctx context.Context, req *apis.ManagementGetBaseApiProviderListReq, reply *apis.ManagementGetBaseApiProviderListReply) (err error) {
  15. if req.ApiId == 0 {
  16. return errors.ArgsError
  17. }
  18. var list = []ProviderGroupApiItem{}
  19. sql := `select provider_api_id, group_no, group_name, priority,thirdpart_api_name,
  20. thirdpart_api_router,platform_name from (t_gd_api_provider_relation
  21. as t1 left join t_gd_provider_api as t2 on t1.provider_api_id = t2.id)
  22. left join t_gd_provider as t3 on t2.provider_id=t3.id where t1.api_id=? order by group_no, priority`
  23. _, err = orm.NewOrm().Raw(sql, req.ApiId).QueryRows(&list)
  24. if err != nil && err != orm.ErrNoRows {
  25. return errors.DataBaseError
  26. }
  27. m := map[int][]apis.BaseApiProviderItem{}
  28. nameMap := map[int]string{}
  29. groupNoArray := []int{}
  30. for _, v := range list {
  31. if _, ok := m[v.GroupNo]; ok == false {
  32. groupNoArray = append(groupNoArray, v.GroupNo)
  33. }
  34. nameMap[v.GroupNo] = v.GroupName
  35. m[v.GroupNo] = append(m[v.GroupNo], v.BaseApiProviderItem)
  36. }
  37. for _, v := range groupNoArray {
  38. item := apis.BaseApiProviderGroupItem{}
  39. item.GroupNo = v
  40. item.GroupName, _ = nameMap[v]
  41. item.ProviderApiList, _ = m[v]
  42. reply.ProviderGroupList = append(reply.ProviderGroupList, item)
  43. }
  44. return nil
  45. //l.Debug(utils.MarshalJsonString(req, reply))
  46. }