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

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

10 11 12 13
	"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 已提交
14

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

S
songzhibin97 已提交
18 19
var RepeatErr = errors.New("重复创建")

20 21 22 23 24
type AutoCodeHistoryService struct {
}

var AutoCodeHistoryServiceApp = new(AutoCodeHistoryService)

S
songzhibin97 已提交
25 26 27
func (autoCodeHistoryService *AutoCodeHistoryService) Repeat(structName string) bool {

	var count int64
S
songzhibin97 已提交
28
	global.GVA_DB.Model(&system.SysAutoCodeHistory{}).Where("struct_name = ? and flag = 0", structName).Count(&count)
S
songzhibin97 已提交
29 30 31
	return count > 0
}

S
songzhibin97 已提交
32
// CreateAutoCodeHistory RouterPath : RouterPath@RouterString;RouterPath2@RouterString2
33
func (autoCodeHistoryService *AutoCodeHistoryService) CreateAutoCodeHistory(meta, structName, structCNName, autoCodePath string, injectionMeta string, tableName string, apiIds string) error {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
34
	return global.GVA_DB.Create(&system.SysAutoCodeHistory{
35 36
		RequestMeta:   meta,
		AutoCodePath:  autoCodePath,
S
songzhibin97 已提交
37
		InjectionMeta: injectionMeta,
38 39
		StructName:    structName,
		StructCNName:  structCNName,
S
songzhibin97 已提交
40
		TableName:     tableName,
S
songzhibin97 已提交
41
		ApiIDs:        apiIds,
S
songzhibin97 已提交
42 43 44
	}).Error
}

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

			}
		}
	}
	// 删除文件
72

73
	for _, path := range strings.Split(md.AutoCodePath, ";") {
74 75 76 77 78 79 80

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

81 82 83 84 85 86 87 88
		// 迁移
		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 已提交
89 90 91 92 93
	}
	// 清除注入
	for _, v := range strings.Split(md.InjectionMeta, ";") {
		// RouterPath@functionName@RouterString
		meta := strings.Split(v, "@")
94 95
		if len(meta) == 3 {
			_ = utils.AutoClearCode(meta[0], meta[2])
S
songzhibin97 已提交
96 97 98 99 100
		}
	}
	md.Flag = 1
	return global.GVA_DB.Save(&md).Error
}
S
songzhibin97 已提交
101

102
func (autoCodeHistoryService *AutoCodeHistoryService) GetMeta(id uint) (string, error) {
103
	var meta string
Mr.奇淼('s avatar
Mr.奇淼( 已提交
104
	return meta, global.GVA_DB.Model(system.SysAutoCodeHistory{}).Select("request_meta").First(&meta, id).Error
105 106
}

S
songzhibin97 已提交
107
// GetSysHistoryPage  获取系统历史数据
108
func (autoCodeHistoryService *AutoCodeHistoryService) GetSysHistoryPage(info request.PageInfo) (err error, list interface{}, total int64) {
S
songzhibin97 已提交
109 110 111
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB
Mr.奇淼('s avatar
Mr.奇淼( 已提交
112
	var fileLists []system.SysAutoCodeHistory
S
songzhibin97 已提交
113
	err = db.Find(&fileLists).Count(&total).Error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
114
	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 已提交
115 116
	return err, fileLists, total
}
S
songzhibin97 已提交
117 118

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