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

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

	"go.uber.org/zap"
)

16 17 18 19 20
type AutoCodeHistoryService struct {
}

var AutoCodeHistoryServiceApp = new(AutoCodeHistoryService)

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

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

			}
		}
	}
	// 删除文件
61

62
	for _, path := range strings.Split(md.AutoCodePath, ";") {
63 64 65 66 67 68 69 70
		// 迁移
		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 已提交
71 72 73 74 75
	}
	// 清除注入
	for _, v := range strings.Split(md.InjectionMeta, ";") {
		// RouterPath@functionName@RouterString
		meta := strings.Split(v, "@")
76 77
		if len(meta) == 3 {
			_ = utils.AutoClearCode(meta[0], meta[2])
S
songzhibin97 已提交
78 79 80 81 82
		}
	}
	md.Flag = 1
	return global.GVA_DB.Save(&md).Error
}
S
songzhibin97 已提交
83

84
func (autoCodeHistoryService *AutoCodeHistoryService) GetMeta(id uint) (string, error) {
85
	var meta string
Mr.奇淼('s avatar
Mr.奇淼( 已提交
86
	return meta, global.GVA_DB.Model(system.SysAutoCodeHistory{}).Select("request_meta").First(&meta, id).Error
87 88
}

S
songzhibin97 已提交
89
// GetSysHistoryPage  获取系统历史数据
90
func (autoCodeHistoryService *AutoCodeHistoryService) GetSysHistoryPage(info request.PageInfo) (err error, list interface{}, total int64) {
S
songzhibin97 已提交
91 92 93
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB
Mr.奇淼('s avatar
Mr.奇淼( 已提交
94
	var fileLists []system.SysAutoCodeHistory
S
songzhibin97 已提交
95
	err = db.Find(&fileLists).Count(&total).Error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
96
	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 已提交
97 98
	return err, fileLists, total
}
S
songzhibin97 已提交
99 100

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