forked from flipped-aurora/gin-vue-admin
/
sys_autocode_history.go
143 lines (127 loc) · 4.97 KB
/
sys_autocode_history.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package system
import (
"errors"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
"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"
"go.uber.org/zap"
)
var RepeatErr = errors.New("重复创建")
type AutoCodeHistoryService struct{}
var AutoCodeHistoryServiceApp = new(AutoCodeHistoryService)
// CreateAutoCodeHistory 创建代码生成器历史记录
// RouterPath : RouterPath@RouterString;RouterPath2@RouterString2
// Author [SliverHorn](https://github.com/SliverHorn)
// Author [songzhibin97](https://github.com/songzhibin97)
func (autoCodeHistoryService *AutoCodeHistoryService) CreateAutoCodeHistory(meta, structName, structCNName, autoCodePath string, injectionMeta string, tableName string, apiIds string) error {
return global.GVA_DB.Create(&system.SysAutoCodeHistory{
RequestMeta: meta,
AutoCodePath: autoCodePath,
InjectionMeta: injectionMeta,
StructName: structName,
StructCNName: structCNName,
TableName: tableName,
ApiIDs: apiIds,
}).Error
}
// First 根据id获取代码生成器历史的数据
// Author [SliverHorn](https://github.com/SliverHorn)
// Author [songzhibin97](https://github.com/songzhibin97)
func (autoCodeHistoryService *AutoCodeHistoryService) First(info *request.GetById) (string, error) {
var meta string
return meta, global.GVA_DB.Model(system.SysAutoCodeHistory{}).Select("request_meta").Where("id = ?", info.Uint()).First(&meta).Error
}
// Repeat 检测重复
// Author [SliverHorn](https://github.com/SliverHorn)
// Author [songzhibin97](https://github.com/songzhibin97)
func (autoCodeHistoryService *AutoCodeHistoryService) Repeat(structName string) bool {
var count int64
global.GVA_DB.Model(&system.SysAutoCodeHistory{}).Where("struct_name = ? and flag = 0", structName).Count(&count)
return count > 0
}
// RollBack 回滚
// Author [SliverHorn](https://github.com/SliverHorn)
// Author [songzhibin97](https://github.com/songzhibin97)
func (autoCodeHistoryService *AutoCodeHistoryService) RollBack(info *request.GetById) error {
md := system.SysAutoCodeHistory{}
if err := global.GVA_DB.Where("id = ?", info.Uint()).First(&md).Error; err != nil {
return err
}
// 清除API表
err := ApiServiceApp.DeleteApiByIds(strings.Split(md.ApiIDs, ";"))
if err != nil {
global.GVA_LOG.Error("ClearTag DeleteApiByIds:", zap.Error(err))
}
// 获取全部表名
dbNames, err := AutoCodeServiceApp.Database().GetTables(global.GVA_CONFIG.Mysql.Dbname)
if err != nil {
global.GVA_LOG.Error("ClearTag GetTables:", zap.Error(err))
}
// 删除表
for _, name := range dbNames {
if strings.Contains(strings.ToUpper(strings.Replace(name.TableName, "_", "", -1)), strings.ToUpper(md.TableName)) {
// 删除表
if err = AutoCodeServiceApp.DropTable(name.TableName); err != nil {
global.GVA_LOG.Error("ClearTag DropTable:", zap.Error(err))
}
}
}
// 删除文件
for _, path := range strings.Split(md.AutoCodePath, ";") {
// 增加安全判断补丁:
_path, err := filepath.Abs(path)
if err != nil || _path != path {
continue
}
// 迁移
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))
// 判断目标文件是否存在
for utils.FileExist(nPath) {
fmt.Println("文件已存在:", nPath)
nPath += fmt.Sprintf("_%d", time.Now().Nanosecond())
}
err = utils.FileMove(path, nPath)
if err != nil {
fmt.Println(">>>>>>>>>>>>>>>>>>>", err)
}
//_ = utils.DeLFile(path)
}
// 清除注入
for _, v := range strings.Split(md.InjectionMeta, ";") {
// RouterPath@functionName@RouterString
meta := strings.Split(v, "@")
if len(meta) == 3 {
_ = utils.AutoClearCode(meta[0], meta[2])
}
}
md.Flag = 1
return global.GVA_DB.Save(&md).Error
}
// Delete 删除历史数据
// Author [SliverHorn](https://github.com/SliverHorn)
// Author [songzhibin97](https://github.com/songzhibin97)
func (autoCodeHistoryService *AutoCodeHistoryService) Delete(info *request.GetById) error {
return global.GVA_DB.Where("id = ?", info.Uint()).Delete(&system.SysAutoCodeHistory{}).Error
}
// GetList 获取系统历史数据
// Author [SliverHorn](https://github.com/SliverHorn)
// Author [songzhibin97](https://github.com/songzhibin97)
func (autoCodeHistoryService *AutoCodeHistoryService) GetList(info request.PageInfo) (list []response.AutoCodeHistory, total int64, err error) {
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
db := global.GVA_DB.Model(&system.SysAutoCodeHistory{})
var entities []response.AutoCodeHistory
err = db.Count(&total).Error
if err != nil {
return nil, total, err
}
err = db.Limit(limit).Offset(offset).Order("updated_at desc").Find(&entities).Error
return entities, total, err
}