123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- package data_api
- import (
- "context"
- "gd_management/apis"
- "gd_management/errors"
- "fmt"
- "time"
- "gd_management/common.in/storage"
- "github.com/astaxie/beego/orm"
- )
- func insertDataApiProviderRelation(childDataApiId int64, apiId int64) error {
- o := orm.NewOrm()
- apiRelation := []apis.ManagementApiProviderRelation{}
- _, err := o.Raw("select * from t_gd_api_provider_relation where api_id=?", apiId).QueryRows(&apiRelation)
- if err != nil {
- if err == orm.ErrNoRows {
- return nil
- }
- return errors.DataBaseError
- }
- if len(apiRelation) == 0 {
- return nil
- }
- dataApiRelation := make([]apis.TGdDataApiProviderApiRelation, len(apiRelation))
- for i := 0; i < len(apiRelation); i++ {
- dataApiRelation[i].ChildDataApiId = childDataApiId
- dataApiRelation[i].ProviderApiId = apiRelation[i].ProviderApiId
- dataApiRelation[i].GroupNo = apiRelation[i].GroupNo
- dataApiRelation[i].GroupName = apiRelation[i].GroupName
- dataApiRelation[i].Enable = true
- }
- _, err = o.InsertMulti(len(dataApiRelation), &dataApiRelation)
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- func delDataApiProviderRelation(childDataApiId int64) error {
- o := orm.NewOrm()
- _, err := o.Raw("delete from t_gd_data_api_provider_api_relation where child_data_api_id=?", childDataApiId).Exec()
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- func checkMainApiCount(req *apis.ManagementDataApiAddBaseApiReq) error {
- o := orm.NewOrm()
- dataApiId := int64(0)
- err := o.Raw("select data_api_id from t_gd_data_api_query_type where id=?", req.QueryTypeId).QueryRow(&dataApiId)
- if err != nil {
- return errors.DataBaseError
- }
- // 获取除目标查询方式外,该数据api已有的主基础api数量
- oldMainCount := 0
- sql := fmt.Sprintf("select count(t1.api_type) from t_gd_api as t1 left join t_gd_child_data_api as t2 on t2.api_id=t1.id left join t_gd_data_api_query_type as t3 on t3.id = t2.query_type_id where t3.data_api_id=%d and t1.api_type=0 and t3.id <> %d", dataApiId, req.QueryTypeId)
- err = o.Raw(sql).QueryRow(&oldMainCount)
- if err != nil {
- return errors.DataBaseError
- }
- apiIds := ""
- for _, v := range req.BaseApiList {
- if apiIds == "" {
- apiIds = fmt.Sprintf("%d", v)
- } else {
- apiIds = fmt.Sprintf("%s,%d", apiIds, v)
- }
- }
- // 获取将要添加的主基础api数量
- newMainCount := 0
- sql = fmt.Sprintf("select count(api_type) from t_gd_api where api_type=0 and id in(%s)", apiIds)
- err = o.Raw(sql).QueryRow(&newMainCount)
- if err != nil {
- return errors.DataBaseError
- }
- if newMainCount+oldMainCount > 1 {
- return errors.MainApiCountError
- }
- return nil
- }
- func DataApiAddBaseApi(ctx context.Context, req *apis.ManagementDataApiAddBaseApiReq, reply *apis.ManagementDataApiAddBaseApiReply) error {
- if req.QueryTypeId == 0 {
- return errors.ArgsError
- }
- if err := checkMainApiCount(req); err != nil {
- return err
- }
- task := func(o orm.Ormer) error {
- //老的data api中api
- var oldApi []apis.ManagementDataApiBaseApi
- // 构建老数据map
- oldApiMap := make(map[int64]apis.ManagementDataApiBaseApi, 0)
- _, err := o.QueryTable("t_gd_child_data_api").Filter("query_type_id", req.QueryTypeId).All(&oldApi)
- if err != nil && err != orm.ErrNoRows {
- return errors.DataBaseError
- }
- /*if len(req.BaseApiList) == 0 {
- for index, _ := range oldApi {
- _, err := o.Delete(&oldApi[index])
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- }*/
- for index, _ := range oldApi {
- oldApiMap[oldApi[index].ApiId] = oldApi[index]
- }
- // 构建新数据map
- newApiMap := make(map[int64]apis.ManagementDataApiBaseApi, 0)
- for index, _ := range req.BaseApiList {
- var baseApi apis.TGdApi
- err := o.QueryTable("t_gd_api").Filter("id", req.BaseApiList[index]).One(&baseApi)
- if err != nil {
- if err == orm.ErrNoRows {
- return errors.BaseApiNotExist
- }
- return errors.DataBaseError
- }
- var dataApiBaseApi apis.ManagementDataApiBaseApi
- dataApiBaseApi.QueryTypeId = req.QueryTypeId
- dataApiBaseApi.ApiId = req.BaseApiList[index]
- //dataApiBaseApi.Priority = req.BaseApiList[index].Priority
- dataApiBaseApi.Priority = index + 1
- dataApiBaseApi.CountCode = ""
- dataApiBaseApi.CountType = 0
- dataApiBaseApi.ForceUpdate = false
- dataApiBaseApi.IsCrypto = true
- dataApiBaseApi.ReuseTime = 1
- dataApiBaseApi.RequestParam = baseApi.RequestParam
- dataApiBaseApi.ResponseParam = baseApi.ResponseParam
- var timeLayout = "2006-01-02 15:04:05"
- timeNow := time.Now().Format(timeLayout)
- dataApiBaseApi.CreateTime = timeNow
- dataApiBaseApi.UpdateTime = timeNow
- newApiMap[req.BaseApiList[index]] = dataApiBaseApi
- }
- // 新增或更新操作
- for key, newVal := range newApiMap {
- if oldval, ok := oldApiMap[key]; ok {
- //新数据在老数据中
- if newVal.Priority == oldval.Priority {
- // 一致不做任何操作
- continue
- } else {
- oldval.Priority = newVal.Priority
- _, err := o.Update(&oldval)
- if err != nil {
- return errors.DataBaseError
- }
- }
- } else {
- // 新数据不在老数据中,表示新增
- childDataId, err := o.Insert(&newVal)
- if err != nil {
- return errors.DataBaseError
- }
- if err := insertDataApiProviderRelation(childDataId, newVal.ApiId); err != nil {
- return err
- }
- // TODO 商户api添加
- }
- }
- // 删除操作
- for key, oldVal := range oldApiMap {
- if _, ok := newApiMap[key]; ok {
- continue
- } else {
- _, err := o.Delete(&oldVal)
- if err != nil {
- return errors.DataBaseError
- }
- if err := delDataApiProviderRelation(oldVal.Id); err != nil {
- return errors.DataBaseError
- }
- // TODO 商户api删除
- }
- }
- return nil
- }
- tasks := []storage.DbaTasker{}
- tasks = append(tasks, storage.GenerateDbaTask(task))
- storage.ExecTrans(tasks...)
- return nil
- }
|