base_api_del.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package base_api
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "gd_management/impl/pubsub"
  7. "fmt"
  8. "gd_management/common.in/storage"
  9. "gd_management/common.in/utils"
  10. "github.com/astaxie/beego/orm"
  11. "go.uber.org/zap"
  12. )
  13. func ManagementDelBaseApi(ctx context.Context, req *apis.ManagementDelBaseApiReq, reply *apis.ManagementDelBaseApiReply) (err error) {
  14. if req.ApiId == 0 {
  15. return errors.ArgsError
  16. }
  17. task := func(o orm.Ormer) error {
  18. exist := o.QueryTable("t_gd_child_data_api").Filter("api_id", req.ApiId).Exist()
  19. if exist {
  20. return errors.ApiInDataApi
  21. }
  22. tab := apis.TGdApi{}
  23. o.QueryTable("t_gd_api").Filter("id", req.ApiId).One(&tab)
  24. if err != nil {
  25. if err == orm.ErrNoRows {
  26. return errors.DataBaseNoRecord
  27. }
  28. return errors.DataBaseError
  29. }
  30. if err := pubsub.PublishApiNotify(0, tab.Router, tab.Method); err != nil {
  31. return err
  32. }
  33. if err := pubsub.PublishNameNotify("api_id", req.ApiId, 0); err != nil {
  34. return err
  35. }
  36. sql := "delete from t_gd_api where id=?"
  37. _, err = o.Raw(sql, req.ApiId).Exec()
  38. if err != nil {
  39. l.Error("mysql",
  40. zap.String("sql", sql),
  41. zap.String("args", fmt.Sprintf("%d\n", req.ApiId)),
  42. zap.String("error", err.Error()))
  43. return errors.DataBaseError
  44. }
  45. _, err = o.Raw("delete from t_gd_api_provider_relation where api_id=?", req.ApiId).Exec()
  46. if err != nil {
  47. return errors.DataBaseError
  48. }
  49. key := fmt.Sprintf("t_gd_api-%s-%s", tab.Method, tab.Router)
  50. if err == nil {
  51. utils.RedisDelKey(key)
  52. }
  53. return nil
  54. }
  55. tasks := []storage.DbaTasker{}
  56. tasks = append(tasks, storage.GenerateDbaTask(task))
  57. storage.ExecTrans(tasks...)
  58. l.Debug(utils.MarshalJsonString(req, reply))
  59. return
  60. }