user_merchant_api_del.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package user_merchant
  4. import (
  5. "context"
  6. "gd_management/apis"
  7. "gd_management/common.in/storage"
  8. "gd_management/errors"
  9. "gd_management/impl/pubsub"
  10. "fmt"
  11. "github.com/astaxie/beego/orm"
  12. )
  13. // OrderInfo 提供删除订单所需信息
  14. type OrderInfo struct {
  15. MerchantId int64 `json:"merchant_id"`
  16. QueryTypeId int64 `json:"query_type_id"`
  17. }
  18. func getOrderInfos(db orm.Ormer, merchantDataApiId int64) ([]OrderInfo, error) {
  19. orderInfos := []OrderInfo{}
  20. sql := "select merchant_id, query_type_id from t_gd_merchant_data_api where id=?"
  21. _, err := db.Raw(sql, merchantDataApiId).QueryRows(&orderInfos)
  22. if err != nil {
  23. return nil, errors.DataBaseError
  24. }
  25. return orderInfos, nil
  26. }
  27. func getProviderRelationSql(db orm.Ormer, merchantDataApiId int64) (string, error) {
  28. sql := "select id from t_gd_merchant_child_data_api where merchant_data_api_id=?"
  29. ids := []int64{}
  30. _, err := db.Raw(sql, merchantDataApiId).QueryRows(&ids)
  31. if err != nil {
  32. return "", errors.DataBaseError
  33. }
  34. sql = ""
  35. for _, v := range ids {
  36. if sql == "" {
  37. sql = fmt.Sprintf("delete from t_gd_merchant_api_provider_api_relation"+
  38. " where merchant_child_api_id in(%d", v)
  39. continue
  40. }
  41. sql = fmt.Sprintf("%s,%d", sql, v)
  42. }
  43. if sql != "" {
  44. sql = fmt.Sprintf("%s)", sql)
  45. }
  46. return sql, nil
  47. }
  48. func getOrderSql(db orm.Ormer, merchantDataApiId int64) (string, error) {
  49. orderInfos, err := getOrderInfos(db, merchantDataApiId)
  50. if err != nil {
  51. return "", err
  52. }
  53. sql := ""
  54. for _, v := range orderInfos {
  55. if sql == "" {
  56. sql = fmt.Sprintf("delete from t_gd_data_api_order where"+
  57. " (merchant_id=%d and query_type_id=%d)",
  58. v.MerchantId, v.QueryTypeId)
  59. continue
  60. }
  61. sql = fmt.Sprintf("%s or (merchant_id=%d and query_type_id=%d)",
  62. sql, v.MerchantId, v.QueryTypeId)
  63. }
  64. return sql, nil
  65. }
  66. func commonDel(db orm.Ormer, sql string) error {
  67. if sql == "" {
  68. return nil
  69. }
  70. _, err := db.Raw(sql).Exec()
  71. if err != nil {
  72. return errors.DataBaseError
  73. }
  74. return nil
  75. }
  76. func delProviderRelation(db orm.Ormer, sql string) error {
  77. return commonDel(db, sql)
  78. }
  79. func delOrder(db orm.Ormer, sql string) error {
  80. return commonDel(db, sql)
  81. }
  82. func delChildApi(db orm.Ormer, sql string) error {
  83. return commonDel(db, sql)
  84. }
  85. func delDataApi(db orm.Ormer, sql string) error {
  86. return commonDel(db, sql)
  87. }
  88. func ManagementDelMerchantDataApi(ctx context.Context, req *apis.ManagementDelMerchantDataApiReq, reply *apis.ManagementDelMerchantDataApiReply) (err error) {
  89. task := func(db orm.Ormer) error {
  90. if err := pubsub.PublishMerchantApiNotify(req.MerchantDataApiId, 0, 0, 0); err != nil {
  91. return err
  92. }
  93. // 删除订单信息
  94. /*sql, err := getOrderSql(db, req.MerchantDataApiId)
  95. if err != nil {
  96. return err
  97. }
  98. if err = delOrder(db, sql); err != nil {
  99. return err
  100. }*/
  101. // 删除商户数据源信息
  102. sql, err := getProviderRelationSql(db, req.MerchantDataApiId)
  103. if err != nil {
  104. return err
  105. }
  106. if err = delProviderRelation(db, sql); err != nil {
  107. return err
  108. }
  109. // 删除商户基础api信息
  110. sql = fmt.Sprintf("delete from t_gd_merchant_child_data_api where"+
  111. " merchant_data_api_id=%d", req.MerchantDataApiId)
  112. if err = delChildApi(db, sql); err != nil {
  113. return err
  114. }
  115. //删除商户数据api信息
  116. sql = fmt.Sprintf("delete from t_gd_merchant_data_api where"+
  117. " id=%d", req.MerchantDataApiId)
  118. if err = delDataApi(db, sql); err != nil {
  119. return err
  120. }
  121. return nil
  122. }
  123. tasks := []storage.DbaTasker{}
  124. tasks = append(tasks, storage.GenerateDbaTask(task))
  125. storage.ExecTrans(tasks...)
  126. return nil
  127. }