123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package data_api
- import (
- "context"
- "gd_management/apis"
- "gd_management/errors"
- "encoding/json"
- "github.com/astaxie/beego/orm"
- )
- type ProviderGroupApiItem struct {
- GroupNo int `json:"group_no"`
- GroupName string `json:"group_name"`
- apis.BaseApiProviderItem
- }
- func GetDataApiProviderList(childDataApiId int64) (ret []apis.BaseApiProviderGroupItem, err error) {
- var list = []ProviderGroupApiItem{}
- sql := "select t1.provider_api_id, priority, t1.group_no, t1.group_name," +
- " t1.enable,thirdpart_api_name," +
- " thirdpart_api_router,platform_name from" +
- " (t_gd_data_api_provider_api_relation" +
- " as t1 left join t_gd_provider_api as t2 on" +
- " t1.provider_api_id = t2.id)" +
- " left join t_gd_provider as t3 on t2.provider_id=t3.id" +
- " left join t_gd_api_provider_relation as t4 on" +
- " t4.provider_api_id=t1.provider_api_id and" +
- " t4.group_no=t1.group_no and t4.api_id = " +
- " (select api_id from t_gd_child_data_api" +
- " where id=t1.child_data_api_id) where" +
- " t1.child_data_api_id=? order by group_no, priority"
- _, err = orm.NewOrm().Raw(sql, childDataApiId).QueryRows(&list)
- if err != nil && err != orm.ErrNoRows {
- return nil, errors.DataBaseError
- }
- m := map[int][]apis.BaseApiProviderItem{}
- nameMap := map[int]string{}
- groupNoArray := []int{}
- for _, v := range list {
- if _, ok := m[v.GroupNo]; ok == false {
- groupNoArray = append(groupNoArray, v.GroupNo)
- }
- nameMap[v.GroupNo] = v.GroupName
- m[v.GroupNo] = append(m[v.GroupNo], v.BaseApiProviderItem)
- }
- for _, v := range groupNoArray {
- item := apis.BaseApiProviderGroupItem{}
- item.GroupNo = v
- item.GroupName, _ = nameMap[v]
- item.ProviderApiList, _ = m[v]
- ret = append(ret, item)
- }
- return ret, nil
- //l.Debug(utils.MarshalJsonString(req, reply))
- }
- func DataApiGetBaseApi(ctx context.Context, req *apis.ManagementDataApiGetBaseApiReq, reply *apis.ManagementDataApiGetBaseApiReply) error {
- if req.DataApiBaseApiId == 0 {
- return errors.ArgsError
- }
- o := orm.NewOrm()
- var err error
- _, err = o.QueryTable("t_gd_child_data_api").Filter("id", req.DataApiBaseApiId).All(&reply.DataApiBaseApis)
- if err != nil {
- if err != orm.ErrNoRows {
- return errors.DataBaseError
- } else {
- return nil
- }
- }
- json.Unmarshal([]byte(reply.DataApiBaseApis.ResponseParam), &reply.DataApiBaseApis.ResponseParams)
- json.Unmarshal([]byte(reply.DataApiBaseApis.RequestParam), &reply.DataApiBaseApis.RequestParams)
- json.Unmarshal([]byte(reply.DataApiBaseApis.Filter), &reply.DataApiBaseApis.Filters)
- if len(reply.DataApiBaseApis.RequestParams) == 0 {
- reply.DataApiBaseApis.RequestParams = []apis.ManagementBaseApiParam{}
- }
- if len(reply.DataApiBaseApis.ResponseParams) == 0 {
- reply.DataApiBaseApis.ResponseParams = []apis.ManagementBaseApiParam{}
- }
- if len(reply.DataApiBaseApis.Filters) == 0 {
- reply.DataApiBaseApis.Filters = []apis.BaseApiParamFilter{}
- }
- err = o.Raw("select name from t_gd_api where id=?", reply.DataApiBaseApis.ApiId).QueryRow(&reply.DataApiBaseApis.ApiName)
- if err != nil {
- if err != orm.ErrNoRows {
- return errors.DataBaseError
- }
- }
- reply.DataApiBaseApis.ProviderGroupList, err = GetDataApiProviderList(req.DataApiBaseApiId)
- if err != nil {
- return err
- }
- return nil
- }
|