diff --git a/apis/tools/systables.go b/apis/tools/systables.go index 46077998..1f693c00 100644 --- a/apis/tools/systables.go +++ b/apis/tools/systables.go @@ -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 / 页码" @@ -44,4 +46,113 @@ func GetSysTableList(c *gin.Context) { res.Data = mp c.JSON(http.StatusOK, res.ReturnOK()) -} \ No newline at end of file +} + +// @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()) +} diff --git a/handler/sd/check.go b/handler/sd/check.go index 716417bf..6f0eed76 100644 --- a/handler/sd/check.go +++ b/handler/sd/check.go @@ -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 使用量 diff --git a/models/tools/dbcolumns.go b/models/tools/dbcolumns.go index 9d796495..727a7eab 100644 --- a/models/tools/dbcolumns.go +++ b/models/tools/dbcolumns.go @@ -9,28 +9,28 @@ type DBColumns struct { //select COLUMN_NAME,COLUMN_DEFAULT,IS_NULLABLE,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,CHARACTER_SET_NAME,COLUMN_TYPE,COLUMN_KEY,EXTRA,COLUMN_COMMENT //from information_schema.`COLUMNS` where table_schema='test1db' and TABLE_NAME='sys_config' - 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"` - ColumnType string `gorm:"column:COLUMN_TYPE" json:"columnType"` - ColumnKey string `gorm:"column:COLUMN_KEY" json:"columnKey"` - Extra string `gorm:"column:EXTRA" json:"extra"` - ColumnComment string `gorm:"column:COLUMN_COMMENT" json:"columnComment"` + TableSchema string `gorm:"column:TABLE_SCHEMA" json:"tableSchema"` + TableName string `gorm:"column:TABLE_NAME" json:"tableName"` + 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"` + ColumnComment string `gorm:"column:COLUMN_COMMENT" json:"columnComment"` } func (e *DBColumns) GetPage(pageSize int, pageIndex int) ([]DBColumns, int32, error) { var doc []DBColumns table := orm.Eloquent.Select("*").Table("information_schema.`COLUMNS`") - table=table.Where("table_schema='test1db'") + table = table.Where("table_schema='test1db'") 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) @@ -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 +} diff --git a/models/tools/dbtables.go b/models/tools/dbtables.go index 5726f8a0..b4e77ed0 100644 --- a/models/tools/dbtables.go +++ b/models/tools/dbtables.go @@ -1,24 +1,25 @@ package tools import ( + "errors" orm "go-admin/database" ) type DBTables struct { - TableName string `gorm:"column:TABLE_NAME" json:"tableName"` - Engine string `gorm:"column:ENGINE" json:"engine"` - TableRows string `gorm:"column:TABLE_ROWS" json:"tableRows"` + TableName string `gorm:"column:TABLE_NAME" json:"tableName"` + Engine string `gorm:"column:ENGINE" json:"engine"` + TableRows string `gorm:"column:TABLE_ROWS" json:"tableRows"` TableCollation string `gorm:"column:TABLE_COLLATION" json:"tableCollation"` - CreateTime string `gorm:"column:CREATE_TIME" json:"createTime"` - UpdateTime string `gorm:"column:UPDATE_TIME" json:"updateTime"` - TableComment string `gorm:"column:TABLE_COMMENT" json:"tableComment"` + CreateTime string `gorm:"column:CREATE_TIME" json:"createTime"` + UpdateTime string `gorm:"column:UPDATE_TIME" json:"updateTime"` + TableComment string `gorm:"column:TABLE_COMMENT" json:"tableComment"` } func (e *DBTables) GetPage(pageSize int, pageIndex int) ([]DBTables, int32, error) { var doc []DBTables table := orm.Eloquent.Select("*").Table("information_schema.tables") - table=table.Where("table_schema='test1db'") + table = table.Where("table_schema='test1db'") if e.TableName != "" { table = table.Where("TABLE_NAME = ?", e.TableName) @@ -31,4 +32,20 @@ func (e *DBTables) GetPage(pageSize int, pageIndex int) ([]DBTables, int32, erro } table.Count(&count) return doc, count, nil -} \ No newline at end of file +} + +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 +} diff --git a/models/tools/syscolumns.go b/models/tools/syscolumns.go index db21431c..4421aaa2 100644 --- a/models/tools/syscolumns.go +++ b/models/tools/syscolumns.go @@ -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 +} diff --git a/models/tools/systables.go b/models/tools/systables.go index b43cfa53..79946914 100644 --- a/models/tools/systables.go +++ b/models/tools/systables.go @@ -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"` //表备注 @@ -34,12 +34,13 @@ type SysTables struct { Crud bool `gorm:"column:crud" json:"crud"` Remark string `gorm:"column:remark" json:"remark"` - CreateBy string `gorm:"column:create_by" json:"createBy"` - CreateTime string `gorm:"column:create_time" json:"createTime"` - UpdateBy string `gorm:"column:update_By" json:"updateBy"` - UpdateTime string `gorm:"column:update_time" json:"updateTime"` - DataScope string `gorm:"-" json:"dataScope"` - Params string `gorm:"-" json:"params"` + CreateBy string `gorm:"column:create_by" json:"createBy"` + CreateTime string `gorm:"column:create_time" json:"createTime"` + UpdateBy string `gorm:"column:update_By" json:"updateBy"` + 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) { @@ -50,7 +51,7 @@ func (e *SysTables) GetPage(pageSize int, pageIndex int) ([]SysTables, int32, er if e.TableName != "" { table = table.Where("table_name = ?", e.TableName) } - if e.TableComment !=""{ + if 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) { 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.TableComment !=""{ + 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,10 +124,14 @@ 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 } success = true return -} \ No newline at end of file +} diff --git a/router/router.go b/router/router.go index 82507e47..11be4927 100644 --- a/router/router.go +++ b/router/router.go @@ -8,30 +8,29 @@ import ( "github.com/swaggo/gin-swagger/swaggerFiles" . "go-admin/apis" . "go-admin/apis/tools" - _ "go-admin/docs" - "go-admin/handler" - "go-admin/handler/sd" - _ "go-admin/pkg/jwtauth" - "go-admin/router/middleware" - "log" - ) + _ "go-admin/docs" + "go-admin/handler" + "go-admin/handler/sd" + _ "go-admin/pkg/jwtauth" + "go-admin/router/middleware" + "log" +) - func InitRouter() *gin.Engine { - r := gin.New() - r.Use(middleware.LoggerToFile()) - r.Use(middleware.CustomError) - r.Use(middleware.NoCache) - r.Use(middleware.Options) - r.Use(middleware.Secure) - r.Use(middleware.RequestId()) - r.Use(middleware.DemoEvn()) +func InitRouter() *gin.Engine { + r := gin.New() + r.Use(middleware.LoggerToFile()) + r.Use(middleware.CustomError) + r.Use(middleware.NoCache) + r.Use(middleware.Options) + r.Use(middleware.Secure) + r.Use(middleware.RequestId()) + r.Use(middleware.DemoEvn()) - r.Static("/static", "./static") - r.GET("/info", Ping) - r.GET("/heath", Heath) + r.Static("/static", "./static") + r.GET("/info", Ping) - // 监控信息 - svcd := r.Group("/sd") + // 监控信息 + svcd := r.Group("/sd") { svcd.GET("/health", sd.HealthCheck) svcd.GET("/disk", sd.DiskCheck) @@ -40,32 +39,37 @@ import ( svcd.GET("/os", sd.OSCheck) } - // the jwt middleware - authMiddleware, err := middleware.AuthInit() + // the jwt middleware + authMiddleware, err := middleware.AuthInit() - if err != nil { + if err != nil { _ = fmt.Errorf("JWT Error", err.Error()) } - r.POST("/login", authMiddleware.LoginHandler) + r.POST("/login", authMiddleware.LoginHandler) - // Refresh time can be longer than token timeout - r.GET("/refresh_token", authMiddleware.RefreshHandler) - //r.GET("/dashboard", Dashboard) - r.GET("/routes", Dashboard) + // Refresh time can be longer than token timeout + r.GET("/refresh_token", authMiddleware.RefreshHandler) + //r.GET("/dashboard", Dashboard) + r.GET("/routes", Dashboard) - apiv1 := r.Group("/api/v1") + apiv1 := r.Group("/api/v1") { apiv1.GET("/getCaptcha", GenerateCaptchaHandler) 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) {