From 9947ab3fa8f89ab8e4f2a8dfb034ddd484f88a44 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sun, 22 Mar 2020 00:06:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE=E8=A1=A8?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apis/tools/systables.go | 49 +++++++++++++++++++++++++++++++++++--- models/tools/syscolumns.go | 17 +++++++++++++ models/tools/systables.go | 12 +++++++++- router/router.go | 2 +- 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/apis/tools/systables.go b/apis/tools/systables.go index 1f693c0..8558721 100644 --- a/apis/tools/systables.go +++ b/apis/tools/systables.go @@ -91,6 +91,15 @@ func InsertSysTable(c *gin.Context) { dbtable, err := dbTable.Get() dbColumn.TableName = data.TableName + tablenamelist := strings.Split(dbColumn.TableName, "_") + for i := 0; i < len(tablenamelist); i++ { + strStart := string([]byte(tablenamelist[i])[:1]) + strend := string([]byte(tablenamelist[i])[1:]) + data.ClassName += strings.ToUpper(strStart) + strend + data.PackageName += strings.ToLower(strStart) + strings.ToLower(strend) + } + data.TplCategory = "crud" + dbcolumn, err := dbColumn.GetList() data.CreateTime = utils.GetCurrntTime() data.CreateBy = utils.GetUserIdStr(c) @@ -110,17 +119,27 @@ func InsertSysTable(c *gin.Context) { column.IsPk = "1" column.Pk = true } + + namelist := strings.Split(dbcolumn[i].ColumnName, "_") + for i := 0; i < len(namelist); i++ { + strStart := string([]byte(namelist[i])[:1]) + strend := string([]byte(namelist[i])[1:]) + column.GoField += strings.ToUpper(strStart) + strend + if i == 0 { + column.JsonField = strings.ToLower(strStart) + strend + } else { + column.JsonField += strings.ToUpper(strStart) + strend + } + } 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" @@ -139,6 +158,30 @@ func InsertSysTable(c *gin.Context) { } +// @Summary 修改表结构 +// @Description 修改表结构 +// @Tags 工具 - 生成表 +// @Accept application/json +// @Product application/json +// @Param data body models.Dept true "body" +// @Success 200 {string} string "{"code": 200, "message": "添加成功"}" +// @Success 200 {string} string "{"code": -1, "message": "添加失败"}" +// @Router /api/v1/sys/tables/info [put] +// @Security Bearer +func UpdateSysTable(c *gin.Context) { + var data tools.SysTables + err := c.Bind(&data) + pkg.AssertErr(err, "数据解析失败", -1) + data.UpdateBy = utils.GetUserIdStr(c) + result, err := data.Update() + pkg.AssertErr(err, "", -1) + + var res models.Response + res.Data = result + res.Msg = "修改成功" + c.JSON(http.StatusOK, res.ReturnOK()) +} + // @Summary 删除表结构 // @Description 删除表结构 // @Tags 工具 - 生成表 diff --git a/models/tools/syscolumns.go b/models/tools/syscolumns.go index 4421aaa..222c58b 100644 --- a/models/tools/syscolumns.go +++ b/models/tools/syscolumns.go @@ -13,6 +13,7 @@ type SysColumns struct { ColumnType string `gorm:"column:column_type" json:"columnType"` GoType string `gorm:"column:go_type" json:"goType"` GoField string `gorm:"column:go_field" json:"goField"` + JsonField string `gorm:"column:json_field" json:"jsonField"` IsPk string `gorm:"column:is_pk" json:"isPk"` IsIncrement string `gorm:"column:is_increment" json:"isIncrement"` IsRequired string `gorm:"column:is_required" json:"isRequired"` @@ -65,3 +66,19 @@ func (e *SysColumns) Create() (SysColumns, error) { doc = *e return doc, nil } + +func (e *SysColumns) Update() (update SysColumns, err error) { + e.UpdateTime = utils.GetCurrntTime() + if err = orm.Eloquent.Table("sys_columns").First(&update, e.ColumnId).Error; err != nil { + return + } + + //参数1:是要修改的数据 + //参数2:是修改的数据 + if err = orm.Eloquent.Table("sys_columns").Model(&update).Updates(&e).Error; err != nil { + return + } + + return +} + diff --git a/models/tools/systables.go b/models/tools/systables.go index 7994691..0daaf42 100644 --- a/models/tools/systables.go +++ b/models/tools/systables.go @@ -39,10 +39,16 @@ type SysTables struct { 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"` + Params Params `gorm:"-" json:"params"` Columns []SysColumns `gorm:"-" json:"columns"` } +type Params struct { + TreeCode string `gorm:"-" json:"treeCode"` + TreeParentCode string `gorm:"-" json:"treeParentCode"` + TreeName string `gorm:"-" json:"treeName"` +} + func (e *SysTables) GetPage(pageSize int, pageIndex int) ([]SysTables, int32, error) { var doc []SysTables @@ -120,6 +126,10 @@ func (e *SysTables) Update() (update SysTables, err error) { if err = orm.Eloquent.Table("sys_tables").Model(&update).Updates(&e).Error; err != nil { return } + + for i:=0;i< len(e.Columns);i++ { + _, _ = e.Columns[i].Update() + } return } diff --git a/router/router.go b/router/router.go index 11be492..d1bcfc2 100644 --- a/router/router.go +++ b/router/router.go @@ -60,9 +60,9 @@ func InitRouter() *gin.Engine { apiv1.GET("/db/columns/page", GetDBColumnList) apiv1.GET("/sys/tables/page", GetSysTableList) apiv1.POST("/sys/tables/info", InsertSysTable) + apiv1.PUT("/sys/tables/info", UpdateSysTable) 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)