code generation tool

This commit is contained in:
zhangwenjian committed 2020-03-19 22:04:38 +08:00
1 parent 1c650ce539
commit 1b03cf2c48
7 files changed
+213 -31

No files matched your search

+114 -3
View File
@@ -5,12 +5,14 @@ import (
"go-admin/models" "go-admin/models"
"go-admin/models/tools" "go-admin/models/tools"
"go-admin/pkg" "go-admin/pkg"
"go-admin/utils"
"net/http" "net/http"
"strings"
) )
// @Summary 分页列表数据 / page list data // @Summary 分页列表数据
// @Description 生成表分页列表 / gen table page list // @Description 生成表分页列表
// @Tags 工具 - Tools / 生成表 - Gen Table // @Tags 工具 - 生成表
// @Param tableName query string false "tableName / 数据表名称" // @Param tableName query string false "tableName / 数据表名称"
// @Param pageSize query int false "pageSize / 页条数" // @Param pageSize query int false "pageSize / 页条数"
// @Param pageIndex query int false "pageIndex / 页码" // @Param pageIndex query int false "pageIndex / 页码"
@@ -45,3 +47,112 @@ func GetSysTableList(c *gin.Context) {
c.JSON(http.StatusOK, res.ReturnOK()) 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
View File
@@ -7,6 +7,7 @@ import (
"github.com/shirou/gopsutil/disk" "github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/load" "github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem" "github.com/shirou/gopsutil/mem"
"go-admin/utils"
"net/http" "net/http"
"runtime" "runtime"
"time" "time"
@@ -81,7 +82,7 @@ func OSCheck(c *gin.Context) {
fmt.Println(runtime.Version()) fmt.Println(runtime.Version())
status := http.StatusOK 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 使用量 // @Summary CPU 使用量
+26 -8
View File
@@ -11,12 +11,12 @@ type DBColumns struct {
TableSchema string `gorm:"column:TABLE_SCHEMA" json:"tableSchema"` TableSchema string `gorm:"column:TABLE_SCHEMA" json:"tableSchema"`
TableName string `gorm:"column:TABLE_NAME" json:"tableName"` TableName string `gorm:"column:TABLE_NAME" json:"tableName"`
Engine string `gorm:"column:COLUMN_NAME" json:"engine"` ColumnName string `gorm:"column:COLUMN_NAME" json:"columnName"`
TableRows string `gorm:"column:COLUMN_DEFAULT" json:"tableRows"` ColumnDefault string `gorm:"column:COLUMN_DEFAULT" json:"columnDefault"`
TableCollation string `gorm:"column:IS_NULLABLE" json:"tableCollation"` IsNullable string `gorm:"column:IS_NULLABLE" json:"isNullable"`
CreateTime string `gorm:"column:DATA_TYPE" json:"createTime"` DataType string `gorm:"column:DATA_TYPE" json:"dataType"`
UpdateTime string `gorm:"column:CHARACTER_MAXIMUM_LENGTH" json:"updateTime"` CharacterMaximumLength string `gorm:"column:CHARACTER_MAXIMUM_LENGTH" json:"characterMaximumLength"`
TableComment string `gorm:"column:CHARACTER_SET_NAME" json:"tableComment"` CharacterSetName string `gorm:"column:CHARACTER_SET_NAME" json:"characterSetName"`
ColumnType string `gorm:"column:COLUMN_TYPE" json:"columnType"` ColumnType string `gorm:"column:COLUMN_TYPE" json:"columnType"`
ColumnKey string `gorm:"column:COLUMN_KEY" json:"columnKey"` ColumnKey string `gorm:"column:COLUMN_KEY" json:"columnKey"`
Extra string `gorm:"column:EXTRA" json:"extra"` Extra string `gorm:"column:EXTRA" json:"extra"`
@@ -27,10 +27,10 @@ func (e *DBColumns) GetPage(pageSize int, pageIndex int) ([]DBColumns, int32, er
var doc []DBColumns var doc []DBColumns
table := orm.Eloquent.Select("*").Table("information_schema.`COLUMNS`") table := orm.Eloquent.Select("*").Table("information_schema.`COLUMNS`")
table=table.Where("table_schema='test1db'") table = table.Where("table_schema='test1db'")
if e.TableName != "" { if e.TableName != "" {
return nil,0,errors.New("table name cannot be empty!") return nil, 0, errors.New("table name cannot be empty!")
} }
table = table.Where("TABLE_NAME = ?", e.TableName) table = table.Where("TABLE_NAME = ?", e.TableName)
@@ -43,3 +43,21 @@ func (e *DBColumns) GetPage(pageSize int, pageIndex int) ([]DBColumns, int32, er
table.Count(&count) table.Count(&count)
return doc, count, nil 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
}
+18 -1
View File
@@ -1,6 +1,7 @@
package tools package tools
import ( import (
"errors"
orm "go-admin/database" orm "go-admin/database"
) )
@@ -18,7 +19,7 @@ func (e *DBTables) GetPage(pageSize int, pageIndex int) ([]DBTables, int32, erro
var doc []DBTables var doc []DBTables
table := orm.Eloquent.Select("*").Table("information_schema.tables") table := orm.Eloquent.Select("*").Table("information_schema.tables")
table=table.Where("table_schema='test1db'") table = table.Where("table_schema='test1db'")
if e.TableName != "" { if e.TableName != "" {
table = table.Where("TABLE_NAME = ?", e.TableName) table = table.Where("TABLE_NAME = ?", e.TableName)
@@ -32,3 +33,19 @@ func (e *DBTables) GetPage(pageSize int, pageIndex int) ([]DBTables, int32, erro
table.Count(&count) table.Count(&count)
return doc, count, nil 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
}
+19 -4
View File
@@ -1,11 +1,14 @@
package tools package tools
import orm "go-admin/database" import (
orm "go-admin/database"
"go-admin/utils"
)
type SysColumns struct { type SysColumns struct {
ColumnId int64 `gorm:"column:configId;primary_key" json:"configId"` ColumnId int64 `gorm:"column:column_id;primary_key" json:"columnId"`
TableId int64 `gorm:"column:configId" json:"configId"` TableId int64 `gorm:"column:table_id" json:"tableId"`
ColumnName string `gorm:"column:configId" json:"configId"` ColumnName string `gorm:"column:column_name" json:"columnName"`
ColumnComment string `gorm:"column:column_comment" json:"columnComment"` ColumnComment string `gorm:"column:column_comment" json:"columnComment"`
ColumnType string `gorm:"column:column_type" json:"columnType"` ColumnType string `gorm:"column:column_type" json:"columnType"`
GoType string `gorm:"column:go_type" json:"goType"` GoType string `gorm:"column:go_type" json:"goType"`
@@ -50,3 +53,15 @@ func (e *SysColumns) GetList() ([]SysColumns, error) {
} }
return doc, nil 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
}
+25 -6
View File
@@ -7,7 +7,7 @@ import (
type SysTables struct { 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"` TableName string `gorm:"column:table_name" json:"tableName"`
//表备注 //表备注
@@ -40,6 +40,7 @@ type SysTables struct {
UpdateTime string `gorm:"column:update_time" json:"updateTime"` UpdateTime string `gorm:"column:update_time" json:"updateTime"`
DataScope string `gorm:"-" json:"dataScope"` DataScope string `gorm:"-" json:"dataScope"`
Params string `gorm:"-" json:"params"` Params string `gorm:"-" json:"params"`
Columns []SysColumns `gorm:"-" json:"columns"`
} }
func (e *SysTables) GetPage(pageSize int, pageIndex int) ([]SysTables, int32, error) { func (e *SysTables) GetPage(pageSize int, pageIndex int) ([]SysTables, int32, error) {
@@ -50,7 +51,7 @@ func (e *SysTables) GetPage(pageSize int, pageIndex int) ([]SysTables, int32, er
if e.TableName != "" { if e.TableName != "" {
table = table.Where("table_name = ?", e.TableName) table = table.Where("table_name = ?", e.TableName)
} }
if e.TableComment !=""{ if e.TableComment != "" {
table = table.Where("table_comment = ?", e.TableComment) table = table.Where("table_comment = ?", e.TableComment)
} }
@@ -65,20 +66,28 @@ func (e *SysTables) GetPage(pageSize int, pageIndex int) ([]SysTables, int32, er
func (e *SysTables) Get() (SysTables, error) { func (e *SysTables) Get() (SysTables, error) {
var doc SysTables var doc SysTables
var err error
table := orm.Eloquent.Select("*").Table("sys_tables") table := orm.Eloquent.Select("*").Table("sys_tables")
if e.TableName != "" { if e.TableName != "" {
table = table.Where("table_name = ?", e.TableName) table = table.Where("table_name = ?", e.TableName)
} }
if e.TableComment !=""{ if e.TableId != 0 {
table = table.Where("table_id = ?", e.TableId)
}
if e.TableComment != "" {
table = table.Where("table_comment = ?", e.TableComment) table = table.Where("table_comment = ?", e.TableComment)
} }
if err := table.First(&doc).Error; err != nil { if err := table.First(&doc).Error; err != nil {
return doc, err return doc, err
} }
var col SysColumns
col.TableId = e.TableId
if doc.Columns, err = col.GetList(); err != nil {
return doc, err
}
return doc, nil return doc, nil
} }
@@ -91,6 +100,12 @@ func (e *SysTables) Create() (SysTables, error) {
return doc, err return doc, err
} }
doc = *e doc = *e
for i := 0; i < len(e.Columns); i++ {
e.Columns[i].TableId = doc.TableId
e.Columns[i].Create()
}
return doc, nil return doc, nil
} }
@@ -109,7 +124,11 @@ func (e *SysTables) Update() (update SysTables, err error) {
} }
func (e *SysTables) Delete() (success bool, 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 success = false
return return
} }
+9 -8
View File
@@ -14,9 +14,9 @@ import (
_ "go-admin/pkg/jwtauth" _ "go-admin/pkg/jwtauth"
"go-admin/router/middleware" "go-admin/router/middleware"
"log" "log"
) )
func InitRouter() *gin.Engine { func InitRouter() *gin.Engine {
r := gin.New() r := gin.New()
r.Use(middleware.LoggerToFile()) r.Use(middleware.LoggerToFile())
r.Use(middleware.CustomError) r.Use(middleware.CustomError)
@@ -28,7 +28,6 @@ import (
r.Static("/static", "./static") r.Static("/static", "./static")
r.GET("/info", Ping) r.GET("/info", Ping)
r.GET("/heath", Heath)
// 监控信息 // 监控信息
svcd := r.Group("/sd") svcd := r.Group("/sd")
@@ -60,12 +59,17 @@ import (
apiv1.GET("/db/tables/page", GetDBTableList) apiv1.GET("/db/tables/page", GetDBTableList)
apiv1.GET("/db/columns/page", GetDBColumnList) apiv1.GET("/db/columns/page", GetDBColumnList)
apiv1.GET("/sys/tables/page", GetSysTableList) 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("/menuTreeselect", GetMenuTreeelect)
apiv1.GET("/rolemenu", GetRoleMenu) apiv1.GET("/rolemenu", GetRoleMenu)
apiv1.POST("/rolemenu", InsertRoleMenu) apiv1.POST("/rolemenu", InsertRoleMenu)
apiv1.DELETE("/rolemenu/:id", DeleteRoleMenu) apiv1.DELETE("/rolemenu/:id", DeleteRoleMenu)
apiv1.GET("/dict/databytype/:dictType", GetDictDataByDictType) apiv1.GET("/dict/databytype/:dictType", GetDictDataByDictType)
} }
auth := r.Group("/api/v1") auth := r.Group("/api/v1")
@@ -90,6 +94,8 @@ import (
auth.PUT("/dict/type", UpdateDictType) auth.PUT("/dict/type", UpdateDictType)
auth.DELETE("/dict/type/:dictId", DeleteDictType) auth.DELETE("/dict/type/:dictId", DeleteDictType)
auth.GET("/dict/typeoptionselect", GetDictTypeOptionSelect)
auth.GET("/sysUserList", GetSysUserList) auth.GET("/sysUserList", GetSysUserList)
auth.GET("/sysUser/:userId", GetSysUser) auth.GET("/sysUser/:userId", GetSysUser)
auth.GET("/sysUser/", GetSysUserInit) 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) { func Dashboard(c *gin.Context) {