code generation tool
This commit is contained in:
1 parent
1c650ce539
commit
1b03cf2c48
7 files changed
+206
-24
No files matched your search
+114
-3
@@ -5,12 +5,14 @@ import (
|
||||
"go-admin/models"
|
||||
"go-admin/models/tools"
|
||||
"go-admin/pkg"
|
||||
"go-admin/utils"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// @Summary 分页列表数据 / page list data
|
||||
// @Description 生成表分页列表 / gen table page list
|
||||
// @Tags 工具 - Tools / 生成表 - Gen Table
|
||||
// @Summary 分页列表数据
|
||||
// @Description 生成表分页列表
|
||||
// @Tags 工具 - 生成表
|
||||
// @Param tableName query string false "tableName / 数据表名称"
|
||||
// @Param pageSize query int false "pageSize / 页条数"
|
||||
// @Param pageIndex query int false "pageIndex / 页码"
|
||||
@@ -45,3 +47,112 @@ func GetSysTableList(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, res.ReturnOK())
|
||||
}
|
||||
|
||||
// @Summary 获取配置
|
||||
// @Description 获取JSON
|
||||
// @Tags 工具 - 生成表
|
||||
// @Param configKey path int true "configKey"
|
||||
// @Success 200 {object} models.Response "{"code": 200, "data": [...]}"
|
||||
// @Router /api/v1/sys/tables/info/{tableId} [get]
|
||||
// @Security
|
||||
func GetSysTables(c *gin.Context) {
|
||||
var data tools.SysTables
|
||||
data.TableId, _ = utils.StringToInt64(c.Param("tableId"))
|
||||
result, err := data.Get()
|
||||
pkg.AssertErr(err, "抱歉未找到相关信息", -1)
|
||||
|
||||
var res models.Response
|
||||
res.Data = result
|
||||
mp := make(map[string]interface{})
|
||||
mp["rows"] = result.Columns
|
||||
mp["info"] = result
|
||||
res.Data = mp
|
||||
c.JSON(http.StatusOK, res.ReturnOK())
|
||||
}
|
||||
|
||||
// @Summary 添加表结构
|
||||
// @Description 添加表结构
|
||||
// @Tags 工具 - 生成表
|
||||
// @Accept application/json
|
||||
// @Product application/json
|
||||
// @Param tables query string false "tableName / 数据表名称"
|
||||
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
|
||||
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
|
||||
// @Router /api/v1/sys/tables/info [post]
|
||||
// @Security Bearer
|
||||
func InsertSysTable(c *gin.Context) {
|
||||
var data tools.SysTables
|
||||
var dbTable tools.DBTables
|
||||
var dbColumn tools.DBColumns
|
||||
data.TableName = c.Request.FormValue("tables")
|
||||
data.CreateBy = utils.GetUserIdStr(c)
|
||||
|
||||
dbTable.TableName = data.TableName
|
||||
dbtable, err := dbTable.Get()
|
||||
|
||||
dbColumn.TableName = data.TableName
|
||||
dbcolumn, err := dbColumn.GetList()
|
||||
data.CreateTime = utils.GetCurrntTime()
|
||||
data.CreateBy = utils.GetUserIdStr(c)
|
||||
data.TableComment = dbtable.TableComment
|
||||
data.FunctionAuthor = "wenjianzhang"
|
||||
for i := 0; i < len(dbcolumn); i++ {
|
||||
var column tools.SysColumns
|
||||
column.ColumnComment = dbcolumn[i].ColumnComment
|
||||
column.ColumnName = dbcolumn[i].ColumnName
|
||||
column.ColumnType = dbcolumn[i].ColumnType
|
||||
column.Sort = utils.IntToString(i + 1)
|
||||
column.Insert = true
|
||||
column.IsInsert = "1"
|
||||
column.QueryType = "EQ"
|
||||
column.IsPk = "0"
|
||||
if strings.Contains(dbcolumn[i].ColumnKey, "PR") {
|
||||
column.IsPk = "1"
|
||||
column.Pk = true
|
||||
}
|
||||
column.IsRequired = "0"
|
||||
if strings.Contains(dbcolumn[i].IsNullable, "NO") {
|
||||
column.IsRequired = "1"
|
||||
column.Required = true
|
||||
}
|
||||
if strings.Contains(dbcolumn[i].ColumnType, "int") {
|
||||
column.GoType = "int64"
|
||||
column.HtmlType = "input"
|
||||
//} else if strings.Contains(dbcolumn[i].ColumnType, "char") {
|
||||
// column.GoType = "bool"
|
||||
// column.HtmlType = "input"
|
||||
} else {
|
||||
column.GoType = "string"
|
||||
column.HtmlType = "input"
|
||||
}
|
||||
|
||||
data.Columns = append(data.Columns, column)
|
||||
}
|
||||
|
||||
result, err := data.Create()
|
||||
pkg.AssertErr(err, "", -1)
|
||||
|
||||
var res models.Response
|
||||
res.Data = result
|
||||
res.Msg = "添加成功!"
|
||||
c.JSON(http.StatusOK, res.ReturnOK())
|
||||
|
||||
}
|
||||
|
||||
// @Summary 删除表结构
|
||||
// @Description 删除表结构
|
||||
// @Tags 工具 - 生成表
|
||||
// @Param tableId path int true "tableId"
|
||||
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
|
||||
// @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
|
||||
// @Router /api/v1/sys/tables/info/{tableId} [delete]
|
||||
func DeleteSysTables(c *gin.Context) {
|
||||
var data tools.SysTables
|
||||
id, err := utils.StringToInt64(c.Param("tableId"))
|
||||
data.TableId = id
|
||||
_, err = data.Delete()
|
||||
pkg.AssertErr(err, "删除失败", 500)
|
||||
var res models.Response
|
||||
res.Msg = "删除成功"
|
||||
c.JSON(http.StatusOK, res.ReturnOK())
|
||||
}
|
||||
+2
-1
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/shirou/gopsutil/disk"
|
||||
"github.com/shirou/gopsutil/load"
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
"go-admin/utils"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"time"
|
||||
@@ -81,7 +82,7 @@ func OSCheck(c *gin.Context) {
|
||||
fmt.Println(runtime.Version())
|
||||
|
||||
status := http.StatusOK
|
||||
c.String(status, runtime.GOOS+"/"+runtime.GOARCH+"/"+string(runtime.NumCPU()))
|
||||
c.String(status, runtime.GOOS+"/"+runtime.GOARCH+"/"+utils.IntToString(runtime.NumCPU()))
|
||||
}
|
||||
|
||||
// @Summary CPU 使用量
|
||||
|
||||
@@ -11,12 +11,12 @@ type DBColumns struct {
|
||||
|
||||
TableSchema string `gorm:"column:TABLE_SCHEMA" json:"tableSchema"`
|
||||
TableName string `gorm:"column:TABLE_NAME" json:"tableName"`
|
||||
Engine string `gorm:"column:COLUMN_NAME" json:"engine"`
|
||||
TableRows string `gorm:"column:COLUMN_DEFAULT" json:"tableRows"`
|
||||
TableCollation string `gorm:"column:IS_NULLABLE" json:"tableCollation"`
|
||||
CreateTime string `gorm:"column:DATA_TYPE" json:"createTime"`
|
||||
UpdateTime string `gorm:"column:CHARACTER_MAXIMUM_LENGTH" json:"updateTime"`
|
||||
TableComment string `gorm:"column:CHARACTER_SET_NAME" json:"tableComment"`
|
||||
ColumnName string `gorm:"column:COLUMN_NAME" json:"columnName"`
|
||||
ColumnDefault string `gorm:"column:COLUMN_DEFAULT" json:"columnDefault"`
|
||||
IsNullable string `gorm:"column:IS_NULLABLE" json:"isNullable"`
|
||||
DataType string `gorm:"column:DATA_TYPE" json:"dataType"`
|
||||
CharacterMaximumLength string `gorm:"column:CHARACTER_MAXIMUM_LENGTH" json:"characterMaximumLength"`
|
||||
CharacterSetName string `gorm:"column:CHARACTER_SET_NAME" json:"characterSetName"`
|
||||
ColumnType string `gorm:"column:COLUMN_TYPE" json:"columnType"`
|
||||
ColumnKey string `gorm:"column:COLUMN_KEY" json:"columnKey"`
|
||||
Extra string `gorm:"column:EXTRA" json:"extra"`
|
||||
@@ -43,3 +43,21 @@ func (e *DBColumns) GetPage(pageSize int, pageIndex int) ([]DBColumns, int32, er
|
||||
table.Count(&count)
|
||||
return doc, count, nil
|
||||
}
|
||||
|
||||
func (e *DBColumns) GetList() ([]DBColumns, error) {
|
||||
var doc []DBColumns
|
||||
|
||||
table := orm.Eloquent.Select("*").Table("information_schema.columns")
|
||||
table = table.Where("table_schema='test1db'")
|
||||
|
||||
if e.TableName == "" {
|
||||
return nil, errors.New("table name cannot be empty!")
|
||||
}
|
||||
|
||||
table = table.Where("TABLE_NAME = ?", e.TableName)
|
||||
|
||||
if err := table.Find(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"errors"
|
||||
orm "go-admin/database"
|
||||
)
|
||||
|
||||
@@ -32,3 +33,19 @@ func (e *DBTables) GetPage(pageSize int, pageIndex int) ([]DBTables, int32, erro
|
||||
table.Count(&count)
|
||||
return doc, count, nil
|
||||
}
|
||||
|
||||
func (e *DBTables) Get() (DBTables, error) {
|
||||
var doc DBTables
|
||||
|
||||
table := orm.Eloquent.Select("*").Table("information_schema.tables")
|
||||
table = table.Where("table_schema='test1db'")
|
||||
if e.TableName == "" {
|
||||
return doc, errors.New("table name cannot be empty!")
|
||||
}
|
||||
table = table.Where("TABLE_NAME = ?", e.TableName)
|
||||
|
||||
if err := table.First(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
@@ -1,11 +1,14 @@
|
||||
package tools
|
||||
|
||||
import orm "go-admin/database"
|
||||
import (
|
||||
orm "go-admin/database"
|
||||
"go-admin/utils"
|
||||
)
|
||||
|
||||
type SysColumns struct {
|
||||
ColumnId int64 `gorm:"column:configId;primary_key" json:"configId"`
|
||||
TableId int64 `gorm:"column:configId" json:"configId"`
|
||||
ColumnName string `gorm:"column:configId" json:"configId"`
|
||||
ColumnId int64 `gorm:"column:column_id;primary_key" json:"columnId"`
|
||||
TableId int64 `gorm:"column:table_id" json:"tableId"`
|
||||
ColumnName string `gorm:"column:column_name" json:"columnName"`
|
||||
ColumnComment string `gorm:"column:column_comment" json:"columnComment"`
|
||||
ColumnType string `gorm:"column:column_type" json:"columnType"`
|
||||
GoType string `gorm:"column:go_type" json:"goType"`
|
||||
@@ -50,3 +53,15 @@ func (e *SysColumns) GetList() ([]SysColumns, error) {
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *SysColumns) Create() (SysColumns, error) {
|
||||
var doc SysColumns
|
||||
e.CreateTime = utils.GetCurrntTime()
|
||||
result := orm.Eloquent.Table("sys_columns").Create(&e)
|
||||
if result.Error != nil {
|
||||
err := result.Error
|
||||
return doc, err
|
||||
}
|
||||
doc = *e
|
||||
return doc, nil
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
type SysTables struct {
|
||||
//表编码
|
||||
TableId string `gorm:"column:table_id" json:"tableName"`
|
||||
TableId int64 `gorm:"column:table_id;primary_key" json:"tableId"`
|
||||
//表名称
|
||||
TableName string `gorm:"column:table_name" json:"tableName"`
|
||||
//表备注
|
||||
@@ -40,6 +40,7 @@ type SysTables struct {
|
||||
UpdateTime string `gorm:"column:update_time" json:"updateTime"`
|
||||
DataScope string `gorm:"-" json:"dataScope"`
|
||||
Params string `gorm:"-" json:"params"`
|
||||
Columns []SysColumns `gorm:"-" json:"columns"`
|
||||
}
|
||||
|
||||
func (e *SysTables) GetPage(pageSize int, pageIndex int) ([]SysTables, int32, error) {
|
||||
@@ -65,20 +66,28 @@ func (e *SysTables) GetPage(pageSize int, pageIndex int) ([]SysTables, int32, er
|
||||
|
||||
func (e *SysTables) Get() (SysTables, error) {
|
||||
var doc SysTables
|
||||
|
||||
var err error
|
||||
table := orm.Eloquent.Select("*").Table("sys_tables")
|
||||
|
||||
if e.TableName != "" {
|
||||
table = table.Where("table_name = ?", e.TableName)
|
||||
}
|
||||
if e.TableId != 0 {
|
||||
table = table.Where("table_id = ?", e.TableId)
|
||||
}
|
||||
if e.TableComment != "" {
|
||||
table = table.Where("table_comment = ?", e.TableComment)
|
||||
}
|
||||
|
||||
|
||||
if err := table.First(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
var col SysColumns
|
||||
col.TableId = e.TableId
|
||||
if doc.Columns, err = col.GetList(); err != nil {
|
||||
return doc, err
|
||||
}
|
||||
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
@@ -91,6 +100,12 @@ func (e *SysTables) Create() (SysTables, error) {
|
||||
return doc, err
|
||||
}
|
||||
doc = *e
|
||||
for i := 0; i < len(e.Columns); i++ {
|
||||
e.Columns[i].TableId = doc.TableId
|
||||
|
||||
e.Columns[i].Create()
|
||||
}
|
||||
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
@@ -109,7 +124,11 @@ func (e *SysTables) Update() (update SysTables, err error) {
|
||||
}
|
||||
|
||||
func (e *SysTables) Delete() (success bool, err error) {
|
||||
if err = orm.Eloquent.Table("sys_tables").Delete("table_id = ?", e.TableId).Error; err != nil {
|
||||
if err = orm.Eloquent.Table("sys_tables").Delete(SysTables{}, "table_id = ?", e.TableId).Error; err != nil {
|
||||
success = false
|
||||
return
|
||||
}
|
||||
if err = orm.Eloquent.Table("sys_columns").Delete(SysColumns{}, "table_id = ?", e.TableId).Error; err != nil {
|
||||
success = false
|
||||
return
|
||||
}
|
||||
|
||||
+7
-6
@@ -28,7 +28,6 @@ import (
|
||||
|
||||
r.Static("/static", "./static")
|
||||
r.GET("/info", Ping)
|
||||
r.GET("/heath", Heath)
|
||||
|
||||
// 监控信息
|
||||
svcd := r.Group("/sd")
|
||||
@@ -60,12 +59,17 @@ import (
|
||||
apiv1.GET("/db/tables/page", GetDBTableList)
|
||||
apiv1.GET("/db/columns/page", GetDBColumnList)
|
||||
apiv1.GET("/sys/tables/page", GetSysTableList)
|
||||
apiv1.POST("/sys/tables/info", InsertSysTable)
|
||||
apiv1.DELETE("/sys/tables/info/:tableId", DeleteSysTables)
|
||||
apiv1.GET("/sys/tables/info/:tableId", GetSysTables)
|
||||
|
||||
apiv1.GET("/menuTreeselect", GetMenuTreeelect)
|
||||
apiv1.GET("/rolemenu", GetRoleMenu)
|
||||
apiv1.POST("/rolemenu", InsertRoleMenu)
|
||||
apiv1.DELETE("/rolemenu/:id", DeleteRoleMenu)
|
||||
apiv1.GET("/dict/databytype/:dictType", GetDictDataByDictType)
|
||||
|
||||
|
||||
}
|
||||
|
||||
auth := r.Group("/api/v1")
|
||||
@@ -90,6 +94,8 @@ import (
|
||||
auth.PUT("/dict/type", UpdateDictType)
|
||||
auth.DELETE("/dict/type/:dictId", DeleteDictType)
|
||||
|
||||
auth.GET("/dict/typeoptionselect", GetDictTypeOptionSelect)
|
||||
|
||||
auth.GET("/sysUserList", GetSysUserList)
|
||||
auth.GET("/sysUser/:userId", GetSysUser)
|
||||
auth.GET("/sysUser/", GetSysUserInit)
|
||||
@@ -162,11 +168,6 @@ func Ping(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func Heath(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"status": "UP",
|
||||
})
|
||||
}
|
||||
|
||||
func Dashboard(c *gin.Context) {
|
||||
|
||||
|
||||
Reference in new issue
Block a user