123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package user_merchant
- import (
- "context"
- "gd_management/apis"
- "gd_management/common.in/storage"
- "gd_management/errors"
- "gd_management/impl/pubsub"
- "fmt"
- "github.com/astaxie/beego/orm"
- )
- // OrderInfo 提供删除订单所需信息
- type OrderInfo struct {
- MerchantId int64 `json:"merchant_id"`
- QueryTypeId int64 `json:"query_type_id"`
- }
- func getOrderInfos(db orm.Ormer, merchantDataApiId int64) ([]OrderInfo, error) {
- orderInfos := []OrderInfo{}
- sql := "select merchant_id, query_type_id from t_gd_merchant_data_api where id=?"
- _, err := db.Raw(sql, merchantDataApiId).QueryRows(&orderInfos)
- if err != nil {
- return nil, errors.DataBaseError
- }
- return orderInfos, nil
- }
- func getProviderRelationSql(db orm.Ormer, merchantDataApiId int64) (string, error) {
- sql := "select id from t_gd_merchant_child_data_api where merchant_data_api_id=?"
- ids := []int64{}
- _, err := db.Raw(sql, merchantDataApiId).QueryRows(&ids)
- if err != nil {
- return "", errors.DataBaseError
- }
- sql = ""
- for _, v := range ids {
- if sql == "" {
- sql = fmt.Sprintf("delete from t_gd_merchant_api_provider_api_relation"+
- " where merchant_child_api_id in(%d", v)
- continue
- }
- sql = fmt.Sprintf("%s,%d", sql, v)
- }
- if sql != "" {
- sql = fmt.Sprintf("%s)", sql)
- }
- return sql, nil
- }
- func getOrderSql(db orm.Ormer, merchantDataApiId int64) (string, error) {
- orderInfos, err := getOrderInfos(db, merchantDataApiId)
- if err != nil {
- return "", err
- }
- sql := ""
- for _, v := range orderInfos {
- if sql == "" {
- sql = fmt.Sprintf("delete from t_gd_data_api_order where"+
- " (merchant_id=%d and query_type_id=%d)",
- v.MerchantId, v.QueryTypeId)
- continue
- }
- sql = fmt.Sprintf("%s or (merchant_id=%d and query_type_id=%d)",
- sql, v.MerchantId, v.QueryTypeId)
- }
- return sql, nil
- }
- func commonDel(db orm.Ormer, sql string) error {
- if sql == "" {
- return nil
- }
- _, err := db.Raw(sql).Exec()
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- func delProviderRelation(db orm.Ormer, sql string) error {
- return commonDel(db, sql)
- }
- func delOrder(db orm.Ormer, sql string) error {
- return commonDel(db, sql)
- }
- func delChildApi(db orm.Ormer, sql string) error {
- return commonDel(db, sql)
- }
- func delDataApi(db orm.Ormer, sql string) error {
- return commonDel(db, sql)
- }
- func ManagementDelMerchantDataApi(ctx context.Context, req *apis.ManagementDelMerchantDataApiReq, reply *apis.ManagementDelMerchantDataApiReply) (err error) {
- task := func(db orm.Ormer) error {
- if err := pubsub.PublishMerchantApiNotify(req.MerchantDataApiId, 0, 0, 0); err != nil {
- return err
- }
- // 删除订单信息
- /*sql, err := getOrderSql(db, req.MerchantDataApiId)
- if err != nil {
- return err
- }
- if err = delOrder(db, sql); err != nil {
- return err
- }*/
- // 删除商户数据源信息
- sql, err := getProviderRelationSql(db, req.MerchantDataApiId)
- if err != nil {
- return err
- }
- if err = delProviderRelation(db, sql); err != nil {
- return err
- }
- // 删除商户基础api信息
- sql = fmt.Sprintf("delete from t_gd_merchant_child_data_api where"+
- " merchant_data_api_id=%d", req.MerchantDataApiId)
- if err = delChildApi(db, sql); err != nil {
- return err
- }
- //删除商户数据api信息
- sql = fmt.Sprintf("delete from t_gd_merchant_data_api where"+
- " id=%d", req.MerchantDataApiId)
- if err = delDataApi(db, sql); err != nil {
- return err
- }
- return nil
- }
- tasks := []storage.DbaTasker{}
- tasks = append(tasks, storage.GenerateDbaTask(task))
- storage.ExecTrans(tasks...)
- return nil
- }
|