data_api_add_base_api.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package data_api
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "fmt"
  7. "time"
  8. "gd_management/common.in/storage"
  9. "github.com/astaxie/beego/orm"
  10. )
  11. func insertDataApiProviderRelation(childDataApiId int64, apiId int64) error {
  12. o := orm.NewOrm()
  13. apiRelation := []apis.ManagementApiProviderRelation{}
  14. _, err := o.Raw("select * from t_gd_api_provider_relation where api_id=?", apiId).QueryRows(&apiRelation)
  15. if err != nil {
  16. if err == orm.ErrNoRows {
  17. return nil
  18. }
  19. return errors.DataBaseError
  20. }
  21. if len(apiRelation) == 0 {
  22. return nil
  23. }
  24. dataApiRelation := make([]apis.TGdDataApiProviderApiRelation, len(apiRelation))
  25. for i := 0; i < len(apiRelation); i++ {
  26. dataApiRelation[i].ChildDataApiId = childDataApiId
  27. dataApiRelation[i].ProviderApiId = apiRelation[i].ProviderApiId
  28. dataApiRelation[i].GroupNo = apiRelation[i].GroupNo
  29. dataApiRelation[i].GroupName = apiRelation[i].GroupName
  30. dataApiRelation[i].Enable = true
  31. }
  32. _, err = o.InsertMulti(len(dataApiRelation), &dataApiRelation)
  33. if err != nil {
  34. return errors.DataBaseError
  35. }
  36. return nil
  37. }
  38. func delDataApiProviderRelation(childDataApiId int64) error {
  39. o := orm.NewOrm()
  40. _, err := o.Raw("delete from t_gd_data_api_provider_api_relation where child_data_api_id=?", childDataApiId).Exec()
  41. if err != nil {
  42. return errors.DataBaseError
  43. }
  44. return nil
  45. }
  46. func checkMainApiCount(req *apis.ManagementDataApiAddBaseApiReq) error {
  47. o := orm.NewOrm()
  48. dataApiId := int64(0)
  49. err := o.Raw("select data_api_id from t_gd_data_api_query_type where id=?", req.QueryTypeId).QueryRow(&dataApiId)
  50. if err != nil {
  51. return errors.DataBaseError
  52. }
  53. // 获取除目标查询方式外,该数据api已有的主基础api数量
  54. oldMainCount := 0
  55. 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)
  56. err = o.Raw(sql).QueryRow(&oldMainCount)
  57. if err != nil {
  58. return errors.DataBaseError
  59. }
  60. apiIds := ""
  61. for _, v := range req.BaseApiList {
  62. if apiIds == "" {
  63. apiIds = fmt.Sprintf("%d", v)
  64. } else {
  65. apiIds = fmt.Sprintf("%s,%d", apiIds, v)
  66. }
  67. }
  68. // 获取将要添加的主基础api数量
  69. newMainCount := 0
  70. sql = fmt.Sprintf("select count(api_type) from t_gd_api where api_type=0 and id in(%s)", apiIds)
  71. err = o.Raw(sql).QueryRow(&newMainCount)
  72. if err != nil {
  73. return errors.DataBaseError
  74. }
  75. if newMainCount+oldMainCount > 1 {
  76. return errors.MainApiCountError
  77. }
  78. return nil
  79. }
  80. func DataApiAddBaseApi(ctx context.Context, req *apis.ManagementDataApiAddBaseApiReq, reply *apis.ManagementDataApiAddBaseApiReply) error {
  81. if req.QueryTypeId == 0 {
  82. return errors.ArgsError
  83. }
  84. if err := checkMainApiCount(req); err != nil {
  85. return err
  86. }
  87. task := func(o orm.Ormer) error {
  88. //老的data api中api
  89. var oldApi []apis.ManagementDataApiBaseApi
  90. // 构建老数据map
  91. oldApiMap := make(map[int64]apis.ManagementDataApiBaseApi, 0)
  92. _, err := o.QueryTable("t_gd_child_data_api").Filter("query_type_id", req.QueryTypeId).All(&oldApi)
  93. if err != nil && err != orm.ErrNoRows {
  94. return errors.DataBaseError
  95. }
  96. /*if len(req.BaseApiList) == 0 {
  97. for index, _ := range oldApi {
  98. _, err := o.Delete(&oldApi[index])
  99. if err != nil {
  100. return errors.DataBaseError
  101. }
  102. return nil
  103. }
  104. }*/
  105. for index, _ := range oldApi {
  106. oldApiMap[oldApi[index].ApiId] = oldApi[index]
  107. }
  108. // 构建新数据map
  109. newApiMap := make(map[int64]apis.ManagementDataApiBaseApi, 0)
  110. for index, _ := range req.BaseApiList {
  111. var baseApi apis.TGdApi
  112. err := o.QueryTable("t_gd_api").Filter("id", req.BaseApiList[index]).One(&baseApi)
  113. if err != nil {
  114. if err == orm.ErrNoRows {
  115. return errors.BaseApiNotExist
  116. }
  117. return errors.DataBaseError
  118. }
  119. var dataApiBaseApi apis.ManagementDataApiBaseApi
  120. dataApiBaseApi.QueryTypeId = req.QueryTypeId
  121. dataApiBaseApi.ApiId = req.BaseApiList[index]
  122. //dataApiBaseApi.Priority = req.BaseApiList[index].Priority
  123. dataApiBaseApi.Priority = index + 1
  124. dataApiBaseApi.CountCode = ""
  125. dataApiBaseApi.CountType = 0
  126. dataApiBaseApi.ForceUpdate = false
  127. dataApiBaseApi.IsCrypto = true
  128. dataApiBaseApi.ReuseTime = 1
  129. dataApiBaseApi.RequestParam = baseApi.RequestParam
  130. dataApiBaseApi.ResponseParam = baseApi.ResponseParam
  131. var timeLayout = "2006-01-02 15:04:05"
  132. timeNow := time.Now().Format(timeLayout)
  133. dataApiBaseApi.CreateTime = timeNow
  134. dataApiBaseApi.UpdateTime = timeNow
  135. newApiMap[req.BaseApiList[index]] = dataApiBaseApi
  136. }
  137. // 新增或更新操作
  138. for key, newVal := range newApiMap {
  139. if oldval, ok := oldApiMap[key]; ok {
  140. //新数据在老数据中
  141. if newVal.Priority == oldval.Priority {
  142. // 一致不做任何操作
  143. continue
  144. } else {
  145. oldval.Priority = newVal.Priority
  146. _, err := o.Update(&oldval)
  147. if err != nil {
  148. return errors.DataBaseError
  149. }
  150. }
  151. } else {
  152. // 新数据不在老数据中,表示新增
  153. childDataId, err := o.Insert(&newVal)
  154. if err != nil {
  155. return errors.DataBaseError
  156. }
  157. if err := insertDataApiProviderRelation(childDataId, newVal.ApiId); err != nil {
  158. return err
  159. }
  160. // TODO 商户api添加
  161. }
  162. }
  163. // 删除操作
  164. for key, oldVal := range oldApiMap {
  165. if _, ok := newApiMap[key]; ok {
  166. continue
  167. } else {
  168. _, err := o.Delete(&oldVal)
  169. if err != nil {
  170. return errors.DataBaseError
  171. }
  172. if err := delDataApiProviderRelation(oldVal.Id); err != nil {
  173. return errors.DataBaseError
  174. }
  175. // TODO 商户api删除
  176. }
  177. }
  178. return nil
  179. }
  180. tasks := []storage.DbaTasker{}
  181. tasks = append(tasks, storage.GenerateDbaTask(task))
  182. storage.ExecTrans(tasks...)
  183. return nil
  184. }