sys_autocode_history.go 3.6 KB
Newer Older
1
package system
S
songzhibin97 已提交
2 3

import (
4 5
	"fmt"
	"path/filepath"
S
songzhibin97 已提交
6
	"strings"
7
	"time"
S
songzhibin97 已提交
8

9 10 11 12
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
S
songzhibin97 已提交
13

S
songzhibin97 已提交
14 15 16
	"go.uber.org/zap"
)

17 18 19 20 21
type AutoCodeHistoryService struct {
}

var AutoCodeHistoryServiceApp = new(AutoCodeHistoryService)

S
songzhibin97 已提交
22
// CreateAutoCodeHistory RouterPath : RouterPath@RouterString;RouterPath2@RouterString2
23
func (autoCodeHistoryService *AutoCodeHistoryService) CreateAutoCodeHistory(meta, structName, structCNName, autoCodePath string, injectionMeta string, tableName string, apiIds string) error {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
24
	return global.GVA_DB.Create(&system.SysAutoCodeHistory{
25 26
		RequestMeta:   meta,
		AutoCodePath:  autoCodePath,
S
songzhibin97 已提交
27
		InjectionMeta: injectionMeta,
28 29
		StructName:    structName,
		StructCNName:  structCNName,
S
songzhibin97 已提交
30
		TableName:     tableName,
S
songzhibin97 已提交
31
		ApiIDs:        apiIds,
S
songzhibin97 已提交
32 33 34
	}).Error
}

S
songzhibin97 已提交
35
// RollBack 回滚
36
func (autoCodeHistoryService *AutoCodeHistoryService) RollBack(id uint) error {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
37
	md := system.SysAutoCodeHistory{}
S
songzhibin97 已提交
38 39 40
	if err := global.GVA_DB.First(&md, id).Error; err != nil {
		return err
	}
S
songzhibin97 已提交
41
	// 清除API表
42
	err := ApiServiceApp.DeleteApiByIds(strings.Split(md.ApiIDs, ";"))
S
songzhibin97 已提交
43 44 45 46
	if err != nil {
		global.GVA_LOG.Error("ClearTag DeleteApiByIds:", zap.Error(err))
	}
	// 获取全部表名
47
	err, dbNames := AutoCodeServiceApp.GetTables(global.GVA_CONFIG.Mysql.Dbname)
S
songzhibin97 已提交
48
	if err != nil {
S
songzhibin97 已提交
49
		global.GVA_LOG.Error("ClearTag GetTables:", zap.Error(err))
S
songzhibin97 已提交
50 51 52 53 54
	}
	// 删除表
	for _, name := range dbNames {
		if strings.Contains(strings.ToUpper(strings.Replace(name.TableName, "_", "", -1)), strings.ToUpper(md.TableName)) {
			// 删除表
55
			if err = AutoCodeServiceApp.DropTable(name.TableName); err != nil {
S
songzhibin97 已提交
56 57 58 59 60 61
				global.GVA_LOG.Error("ClearTag DropTable:", zap.Error(err))

			}
		}
	}
	// 删除文件
62

63
	for _, path := range strings.Split(md.AutoCodePath, ";") {
64 65 66 67 68 69 70

		// 增加安全判断补丁:
		_path, err := filepath.Abs(path)
		if err != nil || _path != path {
			continue
		}

71 72 73 74 75 76 77 78
		// 迁移
		nPath := filepath.Join(global.GVA_CONFIG.AutoCode.Root,
			"rm_file", time.Now().Format("20060102"), filepath.Base(filepath.Dir(filepath.Dir(path))), filepath.Base(filepath.Dir(path)), filepath.Base(path))
		err = utils.FileMove(path, nPath)
		if err != nil {
			fmt.Println(">>>>>>>>>>>>>>>>>>>", err)
		}
		//_ = utils.DeLFile(path)
S
songzhibin97 已提交
79 80 81 82 83
	}
	// 清除注入
	for _, v := range strings.Split(md.InjectionMeta, ";") {
		// RouterPath@functionName@RouterString
		meta := strings.Split(v, "@")
84 85
		if len(meta) == 3 {
			_ = utils.AutoClearCode(meta[0], meta[2])
S
songzhibin97 已提交
86 87 88 89 90
		}
	}
	md.Flag = 1
	return global.GVA_DB.Save(&md).Error
}
S
songzhibin97 已提交
91

92
func (autoCodeHistoryService *AutoCodeHistoryService) GetMeta(id uint) (string, error) {
93
	var meta string
Mr.奇淼('s avatar
Mr.奇淼( 已提交
94
	return meta, global.GVA_DB.Model(system.SysAutoCodeHistory{}).Select("request_meta").First(&meta, id).Error
95 96
}

S
songzhibin97 已提交
97
// GetSysHistoryPage  获取系统历史数据
98
func (autoCodeHistoryService *AutoCodeHistoryService) GetSysHistoryPage(info request.PageInfo) (err error, list interface{}, total int64) {
S
songzhibin97 已提交
99 100 101
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB
Mr.奇淼('s avatar
Mr.奇淼( 已提交
102
	var fileLists []system.SysAutoCodeHistory
S
songzhibin97 已提交
103
	err = db.Find(&fileLists).Count(&total).Error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
104
	err = db.Limit(limit).Offset(offset).Order("updated_at desc").Select("id,created_at,updated_at,struct_name,struct_cn_name,flag,table_name").Find(&fileLists).Error
S
songzhibin97 已提交
105 106
	return err, fileLists, total
}
S
songzhibin97 已提交
107 108

// DeletePage 删除历史数据
109
func (autoCodeHistoryService *AutoCodeHistoryService) DeletePage(id uint) error {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
110
	return global.GVA_DB.Delete(system.SysAutoCodeHistory{}, id).Error
S
songzhibin97 已提交
111
}