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

import (
	"gin-vue-admin/global"
5
	"gin-vue-admin/model/common/request"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
6
	"gin-vue-admin/model/system"
S
songzhibin97 已提交
7 8 9 10 11 12
	"gin-vue-admin/utils"
	"strings"

	"go.uber.org/zap"
)

13 14 15 16 17
type AutoCodeHistoryService struct {
}

var AutoCodeHistoryServiceApp = new(AutoCodeHistoryService)

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

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

			}
		}
	}
	// 删除文件
58
	for _, path := range strings.Split(md.AutoCodePath, ";") {
S
songzhibin97 已提交
59 60 61 62 63 64
		_ = utils.DeLFile(path)
	}
	// 清除注入
	for _, v := range strings.Split(md.InjectionMeta, ";") {
		// RouterPath@functionName@RouterString
		meta := strings.Split(v, "@")
65 66
		if len(meta) == 3 {
			_ = utils.AutoClearCode(meta[0], meta[2])
S
songzhibin97 已提交
67 68 69 70 71
		}
	}
	md.Flag = 1
	return global.GVA_DB.Save(&md).Error
}
S
songzhibin97 已提交
72

73
func (autoCodeHistoryService *AutoCodeHistoryService) GetMeta(id uint) (string, error) {
74
	var meta string
Mr.奇淼('s avatar
Mr.奇淼( 已提交
75
	return meta, global.GVA_DB.Model(system.SysAutoCodeHistory{}).Select("request_meta").First(&meta, id).Error
76 77
}

S
songzhibin97 已提交
78
// GetSysHistoryPage  获取系统历史数据
79
func (autoCodeHistoryService *AutoCodeHistoryService) GetSysHistoryPage(info request.PageInfo) (err error, list interface{}, total int64) {
S
songzhibin97 已提交
80 81 82
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB
Mr.奇淼('s avatar
Mr.奇淼( 已提交
83
	var fileLists []system.SysAutoCodeHistory
S
songzhibin97 已提交
84
	err = db.Find(&fileLists).Count(&total).Error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
85
	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 已提交
86 87
	return err, fileLists, total
}
S
songzhibin97 已提交
88 89

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