sys_auto_code.go 6.3 KB
Newer Older
Mr.奇淼('s avatar
Mr.奇淼( 已提交
1
package v1
2 3

import (
4
	"errors"
5
	"fmt"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
6
	"gin-vue-admin/global"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
7
	"gin-vue-admin/model"
S
songzhibin97 已提交
8
	"gin-vue-admin/model/request"
9
	"gin-vue-admin/model/response"
10
	"gin-vue-admin/service"
11
	"gin-vue-admin/utils"
12
	"net/url"
13
	"os"
14 15 16

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
17 18
)

S
songzhibin97 已提交
19 20 21 22 23 24
// @Tags AutoCode
// @Summary 查询回滚记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysAutoHistory true "查询回滚记录"
S
songzhibin97 已提交
25
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
26
// @Router /autoCode/getSysHistory [post]
S
songzhibin97 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
func GetSysHistory(c *gin.Context) {
	var search request.SysAutoHistory
	_ = c.ShouldBindJSON(&search)
	err, list, total := service.GetSysHistoryPage(search.PageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     search.Page,
			PageSize: search.PageSize,
		}, "获取成功", c)
	}
}

S
songzhibin97 已提交
44 45 46 47 48
// @Tags AutoCode
// @Summary 回滚
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
S
songzhibin97 已提交
49
// @Param data body request.AutoHistoryByID true "回滚自动生成代码"
S
songzhibin97 已提交
50
// @Success 200 {string} string "{"success":true,"data":{},"msg":"回滚成功"}"
51
// @Router /autoCode/rollback [post]
S
songzhibin97 已提交
52
func RollBack(c *gin.Context) {
S
songzhibin97 已提交
53
	var id request.AutoHistoryByID
S
songzhibin97 已提交
54 55 56 57 58 59 60 61
	_ = c.ShouldBindJSON(&id)
	if err := service.RollBack(id.ID); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("回滚成功", c)
}

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
// @Tags AutoCode
// @Summary 回滚
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.AutoHistoryByID true "获取meta信息"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /autoCode/getMeta [post]
func GetMeta(c *gin.Context) {
	var id request.AutoHistoryByID
	_ = c.ShouldBindJSON(&id)
	if v, err := service.GetMeta(id.ID); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	} else {
		response.OkWithDetailed(gin.H{"meta": v}, "获取成功", c)
	}

}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
// @Tags AutoCode
// @Summary 预览创建后的代码
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.AutoCodeStruct true "预览创建代码"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /autoCode/preview [post]
func PreviewTemp(c *gin.Context) {
	var a model.AutoCodeStruct
	_ = c.ShouldBindJSON(&a)
	if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
97
	autoCode, err := service.PreviewTemp(a)
98
	if err != nil {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
99 100
		global.GVA_LOG.Error("预览失败!", zap.Any("err", err))
		response.FailWithMessage("预览失败", c)
101
	} else {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
102
		response.OkWithDetailed(gin.H{"autoCode": autoCode}, "预览成功", c)
103 104 105
	}
}

106
// @Tags AutoCode
107 108 109 110
// @Summary 自动代码模板
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
m0_50812349's avatar
m0_50812349 已提交
111
// @Param data body model.AutoCodeStruct true "创建自动代码"
112 113 114
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /autoCode/createTemp [post]
func CreateTemp(c *gin.Context) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
115
	var a model.AutoCodeStruct
116
	_ = c.ShouldBindJSON(&a)
117 118
	if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
119 120
		return
	}
S
songzhibin97 已提交
121
	var apiIds []uint
122
	if a.AutoCreateApiToSql {
S
songzhibin97 已提交
123
		if ids, err := service.AutoCreateApi(&a); err != nil {
124 125 126 127
			global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err))
			c.Writer.Header().Add("success", "false")
			c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
			return
S
songzhibin97 已提交
128 129
		} else {
			apiIds = ids
130 131
		}
	}
S
songzhibin97 已提交
132
	err := service.CreateTemp(a, apiIds...)
133
	if err != nil {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
134
		if errors.Is(err, model.AutoMoveErr) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
135 136 137
			c.Writer.Header().Add("success", "false")
			c.Writer.Header().Add("msgtype", "success")
			c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
Mr.奇淼('s avatar
Mr.奇淼( 已提交
138 139 140 141 142
		} else {
			c.Writer.Header().Add("success", "false")
			c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
			_ = os.Remove("./ginvueadmin.zip")
		}
143
	} else {
144
		c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "ginvueadmin.zip")) // fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
145 146 147
		c.Writer.Header().Add("Content-Type", "application/json")
		c.Writer.Header().Add("success", "true")
		c.File("./ginvueadmin.zip")
m0_50812349's avatar
m0_50812349 已提交
148
		_ = os.Remove("./ginvueadmin.zip")
149 150
	}
}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
151

152
// @Tags AutoCode
Mr.奇淼('s avatar
Mr.奇淼( 已提交
153 154 155 156
// @Summary 获取当前数据库所有表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
157
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
158 159 160 161 162
// @Router /autoCode/getTables [get]
func GetTables(c *gin.Context) {
	dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
	err, tables := service.GetTables(dbName)
	if err != nil {
163 164
		global.GVA_LOG.Error("查询table失败!", zap.Any("err", err))
		response.FailWithMessage("查询table失败", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
165
	} else {
166
		response.OkWithDetailed(gin.H{"tables": tables}, "获取成功", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
167 168 169
	}
}

170
// @Tags AutoCode
Mr.奇淼('s avatar
Mr.奇淼( 已提交
171 172 173 174
// @Summary 获取当前所有数据库
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
175
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
176 177
// @Router /autoCode/getDatabase [get]
func GetDB(c *gin.Context) {
178 179 180
	if err, dbs := service.GetDB(); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
181
	} else {
182
		response.OkWithDetailed(gin.H{"dbs": dbs}, "获取成功", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
183 184 185
	}
}

186
// @Tags AutoCode
Mr.奇淼('s avatar
Mr.奇淼( 已提交
187 188 189 190
// @Summary 获取当前表所有字段
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
191
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
192 193
// @Router /autoCode/getColumn [get]
func GetColumn(c *gin.Context) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
194 195
	dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
	tableName := c.Query("tableName")
196 197 198
	if err, columns := service.GetColumn(tableName, dbName); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
199
	} else {
200
		response.OkWithDetailed(gin.H{"columns": columns}, "获取成功", c)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
201 202
	}
}