提交 e1e3a31e 编写于 作者: S songzhibin97

feat:回滚删除对应api

上级 dac4e733
...@@ -72,15 +72,18 @@ func CreateTemp(c *gin.Context) { ...@@ -72,15 +72,18 @@ func CreateTemp(c *gin.Context) {
response.FailWithMessage(err.Error(), c) response.FailWithMessage(err.Error(), c)
return return
} }
var apiIds []uint
if a.AutoCreateApiToSql { if a.AutoCreateApiToSql {
if err := service.AutoCreateApi(&a); err != nil { if ids, err := service.AutoCreateApi(&a); err != nil {
global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err)) global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err))
c.Writer.Header().Add("success", "false") c.Writer.Header().Add("success", "false")
c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!")) c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
return return
} else {
apiIds = ids
} }
} }
err := service.CreateTemp(a) err := service.CreateTemp(a, apiIds...)
if err != nil { if err != nil {
if errors.Is(err, model.AutoMoveErr) { if errors.Is(err, model.AutoMoveErr) {
c.Writer.Header().Add("success", "false") c.Writer.Header().Add("success", "false")
......
...@@ -9,5 +9,6 @@ type SysAutoCodeHistory struct { ...@@ -9,5 +9,6 @@ type SysAutoCodeHistory struct {
TableName string TableName string
AutoCodeMeta string `gorm:"type:text"` // 其他meta信息 path;path AutoCodeMeta string `gorm:"type:text"` // 其他meta信息 path;path
InjectionMeta string `gorm:"type:text"` // 注入的内容 RouterPath@functionName@RouterString; InjectionMeta string `gorm:"type:text"` // 注入的内容 RouterPath@functionName@RouterString;
ApiIDs string // api表注册内容
Flag int // 表示对应状态 0 代表创建, 1 代表回滚 ... Flag int // 表示对应状态 0 代表创建, 1 代表回滚 ...
} }
...@@ -141,3 +141,7 @@ func DeleteApisByIds(ids request.IdsReq) (err error) { ...@@ -141,3 +141,7 @@ func DeleteApisByIds(ids request.IdsReq) (err error) {
err = global.GVA_DB.Delete(&[]model.SysApi{}, "id in ?", ids.Ids).Error err = global.GVA_DB.Delete(&[]model.SysApi{}, "id in ?", ids.Ids).Error
return err return err
} }
func DeleteApiByIds(ids []string) (err error) {
return global.GVA_DB.Delete(model.SysApi{}, ids).Error
}
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"text/template" "text/template"
...@@ -99,7 +100,7 @@ func PreviewTemp(autoCode model.AutoCodeStruct) (map[string]string, error) { ...@@ -99,7 +100,7 @@ func PreviewTemp(autoCode model.AutoCodeStruct) (map[string]string, error) {
//@param: model.AutoCodeStruct //@param: model.AutoCodeStruct
//@return: err error //@return: err error
func CreateTemp(autoCode model.AutoCodeStruct) (err error) { func CreateTemp(autoCode model.AutoCodeStruct, ids ...uint) (err error) {
dataList, fileList, needMkdir, err := getNeedList(&autoCode) dataList, fileList, needMkdir, err := getNeedList(&autoCode)
if err != nil { if err != nil {
return err return err
...@@ -160,27 +161,33 @@ func CreateTemp(autoCode model.AutoCodeStruct) (err error) { ...@@ -160,27 +161,33 @@ func CreateTemp(autoCode model.AutoCodeStruct) (err error) {
bf.WriteString(";") bf.WriteString(";")
} }
} }
idBf := strings.Builder{}
for _, id := range ids {
idBf.WriteString(strconv.Itoa(int(id)))
idBf.WriteString(";")
}
if autoCode.TableName != "" { if autoCode.TableName != "" {
err = CreateAutoCodeHistory(bf.String(), err = CreateAutoCodeHistory(bf.String(),
injectionCodeMeta.String(), injectionCodeMeta.String(),
autoCode.TableName, autoCode.TableName,
idBf.String(),
) )
} else { } else {
err = CreateAutoCodeHistory(bf.String(), err = CreateAutoCodeHistory(bf.String(),
injectionCodeMeta.String(), injectionCodeMeta.String(),
autoCode.StructName, autoCode.StructName,
idBf.String(),
) )
} }
if err != nil { if err != nil {
return err return err
} }
if global.GVA_CONFIG.AutoCode.TransferRestart { //if global.GVA_CONFIG.AutoCode.TransferRestart {
go func() { // go func() {
_ = utils.Reload() // _ = utils.Reload()
}() // }()
} //}
return errors.New("创建代码成功并移动文件成功") return errors.New("创建代码成功并移动文件成功")
} else { // 打包 } else { // 打包
if err := utils.ZipFiles("./ginvueadmin.zip", fileList, ".", "."); err != nil { if err := utils.ZipFiles("./ginvueadmin.zip", fileList, ".", "."); err != nil {
...@@ -301,7 +308,7 @@ func addAutoMoveFile(data *tplData) { ...@@ -301,7 +308,7 @@ func addAutoMoveFile(data *tplData) {
//@param: a *model.AutoCodeStruct //@param: a *model.AutoCodeStruct
//@return: err error //@return: err error
func AutoCreateApi(a *model.AutoCodeStruct) (err error) { func AutoCreateApi(a *model.AutoCodeStruct) (ids []uint, err error) {
var apiList = []model.SysApi{ var apiList = []model.SysApi{
{ {
Path: "/" + a.Abbreviation + "/" + "create" + a.StructName, Path: "/" + a.Abbreviation + "/" + "create" + a.StructName,
...@@ -341,17 +348,20 @@ func AutoCreateApi(a *model.AutoCodeStruct) (err error) { ...@@ -341,17 +348,20 @@ func AutoCreateApi(a *model.AutoCodeStruct) (err error) {
}, },
} }
err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
for _, v := range apiList { for _, v := range apiList {
var api model.SysApi var api model.SysApi
if errors.Is(tx.Where("path = ? AND method = ?", v.Path, v.Method).First(&api).Error, gorm.ErrRecordNotFound) { if errors.Is(tx.Where("path = ? AND method = ?", v.Path, v.Method).First(&api).Error, gorm.ErrRecordNotFound) {
if err := tx.Create(&v).Error; err != nil { // 遇到错误时回滚事务 if err = tx.Create(&v).Error; err != nil { // 遇到错误时回滚事务
return err return err
} else {
ids = append(ids, v.ID)
} }
} }
} }
return nil return nil
}) })
return err return ids, err
} }
func getNeedList(autoCode *model.AutoCodeStruct) (dataList []tplData, fileList []string, needMkdir []string, err error) { func getNeedList(autoCode *model.AutoCodeStruct) (dataList []tplData, fileList []string, needMkdir []string, err error) {
......
...@@ -11,11 +11,12 @@ import ( ...@@ -11,11 +11,12 @@ import (
) )
// CreateAutoCodeHistory RouterPath : RouterPath@RouterString;RouterPath2@RouterString2 // CreateAutoCodeHistory RouterPath : RouterPath@RouterString;RouterPath2@RouterString2
func CreateAutoCodeHistory(autoCodeMeta string, injectionMeta string, tableName string) error { func CreateAutoCodeHistory(autoCodeMeta string, injectionMeta string, tableName string, apiIds string) error {
return global.GVA_DB.Create(&model.SysAutoCodeHistory{ return global.GVA_DB.Create(&model.SysAutoCodeHistory{
AutoCodeMeta: autoCodeMeta, AutoCodeMeta: autoCodeMeta,
InjectionMeta: injectionMeta, InjectionMeta: injectionMeta,
TableName: tableName, TableName: tableName,
ApiIDs: apiIds,
}).Error }).Error
} }
...@@ -24,10 +25,15 @@ func RollBack(id uint) error { ...@@ -24,10 +25,15 @@ func RollBack(id uint) error {
if err := global.GVA_DB.First(&md, id).Error; err != nil { if err := global.GVA_DB.First(&md, id).Error; err != nil {
return err return err
} }
// 切分数据 // 清除API表
err := DeleteApiByIds(strings.Split(md.ApiIDs, ";"))
if err != nil {
global.GVA_LOG.Error("ClearTag DeleteApiByIds:", zap.Error(err))
}
// 获取全部表名
err, dbNames := GetTables(global.GVA_CONFIG.Mysql.Dbname) err, dbNames := GetTables(global.GVA_CONFIG.Mysql.Dbname)
if err != nil { if err != nil {
return err global.GVA_LOG.Error("ClearTag GetTables:", zap.Error(err))
} }
// 删除表 // 删除表
for _, name := range dbNames { for _, name := range dbNames {
...@@ -40,7 +46,6 @@ func RollBack(id uint) error { ...@@ -40,7 +46,6 @@ func RollBack(id uint) error {
} }
} }
// 删除文件 // 删除文件
for _, path := range strings.Split(md.AutoCodeMeta, ";") { for _, path := range strings.Split(md.AutoCodeMeta, ";") {
_ = utils.DeLFile(path) _ = utils.DeLFile(path)
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册