mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-30 17:11:28 +08:00
代码生成
This commit is contained in:
41
apps/develop/api/gen.go
Normal file
41
apps/develop/api/gen.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"pandax/apps/develop/gen"
|
||||
"pandax/apps/develop/services"
|
||||
"pandax/base/ctx"
|
||||
"pandax/base/ginx"
|
||||
)
|
||||
|
||||
type GenApi struct {
|
||||
GenTableApp services.SysGenTableModel
|
||||
}
|
||||
|
||||
// @Summary 代码视图
|
||||
// @Description 获取JSON
|
||||
// @Tags 工具 / 生成工具
|
||||
// @Param tableId path int true "tableId"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
|
||||
// @Router /develop/code/gen/preview/{tableId} [get]
|
||||
// @Security X-TOKEN
|
||||
func (e *GenApi) Preview(rc *ctx.ReqCtx) {
|
||||
tableId := ginx.PathParamInt(rc.GinCtx, rc.GinCtx.Param("tableId"))
|
||||
rc.ResData = gen.Preview(int64(tableId))
|
||||
}
|
||||
|
||||
// @Summary 代码生成
|
||||
// @Description 获取JSON
|
||||
// @Tags 工具 / 生成工具
|
||||
// @Param tableId path int true "tableId"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
|
||||
// @Router /develop/code/gen/code/{tableId} [get]
|
||||
// @Security X-TOKEN
|
||||
func (e *GenApi) GenCode(rc *ctx.ReqCtx) {
|
||||
tableId := ginx.PathParamInt(rc.GinCtx, rc.GinCtx.Param("tableId"))
|
||||
gen.GenCode(int64(tableId))
|
||||
}
|
||||
|
||||
//自动创建菜单,api
|
||||
func (e *GenApi) AutoApi(rc *ctx.ReqCtx) {
|
||||
|
||||
}
|
||||
149
apps/develop/api/table.go
Normal file
149
apps/develop/api/table.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"pandax/apps/develop/entity"
|
||||
"pandax/apps/develop/gen"
|
||||
"pandax/apps/develop/services"
|
||||
"pandax/base/ctx"
|
||||
"pandax/base/ginx"
|
||||
"pandax/base/utils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type GenTableApi struct {
|
||||
GenTableApp services.SysGenTableModel
|
||||
}
|
||||
|
||||
// @Summary 分页列表数据 / page list data
|
||||
// @Description 数据库表列分页列表 / database table column page list
|
||||
// @Tags 工具 / 生成工具
|
||||
// @Param tableName query string false "tableName / 数据表名称"
|
||||
// @Param pageSize query int false "pageSize / 页条数"
|
||||
// @Param pageNum query int false "pageNum / 页码"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
|
||||
// @Router /develop/code/table/db/list [get]
|
||||
func (g *GenTableApi) GetDBTableList(rc *ctx.ReqCtx) {
|
||||
pageNum := ginx.QueryInt(rc.GinCtx, "pageNum", 1)
|
||||
pageSize := ginx.QueryInt(rc.GinCtx, "pageSize", 10)
|
||||
tableName := rc.GinCtx.Query("tableName")
|
||||
list, total := g.GenTableApp.FindDbTablesListPage(pageNum, pageSize, entity.DBTables{TableName: tableName})
|
||||
rc.ResData = map[string]interface{}{
|
||||
"data": list,
|
||||
"total": total,
|
||||
"pageNum": pageNum,
|
||||
"pageSize": pageSize,
|
||||
}
|
||||
}
|
||||
|
||||
// @Summary 分页列表数据
|
||||
// @Description 生成表分页列表
|
||||
// @Tags 工具 / 生成工具
|
||||
// @Param tableName query string false "tableName / 数据表名称"
|
||||
// @Param tableComment query string false "tableComment / 数据表描述"
|
||||
// @Param pageSize query int false "pageSize / 页条数"
|
||||
// @Param pageIndex query int false "pageIndex / 页码"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
|
||||
// @Router /develop/code/table/list [get]
|
||||
func (g *GenTableApi) GetTablePage(rc *ctx.ReqCtx) {
|
||||
pageNum := ginx.QueryInt(rc.GinCtx, "pageNum", 1)
|
||||
pageSize := ginx.QueryInt(rc.GinCtx, "pageSize", 10)
|
||||
tableName := rc.GinCtx.Query("tableName")
|
||||
tableComment := rc.GinCtx.Query("tableComment")
|
||||
list, total := g.GenTableApp.FindListPage(pageNum, pageSize, entity.DevGenTable{TableName: tableName, TableComment: tableComment})
|
||||
rc.ResData = map[string]interface{}{
|
||||
"data": list,
|
||||
"total": total,
|
||||
"pageNum": pageNum,
|
||||
"pageSize": pageSize,
|
||||
}
|
||||
}
|
||||
|
||||
// @Summary 获取表信息
|
||||
// @Description 获取JSON
|
||||
// @Tags 工具 / 生成工具
|
||||
// @Param tableId path int true "tableId"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
|
||||
// @Router /develop/code/table/info/{tableId} [get]
|
||||
// @Security Bearer
|
||||
func (g *GenTableApi) GetTableInfo(rc *ctx.ReqCtx) {
|
||||
tableId := ginx.PathParamInt(rc.GinCtx, rc.GinCtx.Param("tableId"))
|
||||
result := g.GenTableApp.FindOne(entity.DevGenTable{TableId: int64(tableId)}, true)
|
||||
mp := make(map[string]interface{})
|
||||
mp["list"] = result.Columns
|
||||
mp["info"] = result
|
||||
rc.ResData = mp
|
||||
}
|
||||
|
||||
// @Summary 获取表信息
|
||||
// @Description 获取JSON
|
||||
// @Tags 工具 / 生成工具
|
||||
// @Param tableName query string false "tableName / 数据表名称"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
|
||||
// @Router /develop/code/table/info/tableName [get]
|
||||
// @Security X-TOKEN
|
||||
func (g *GenTableApi) GetTableInfoByName(rc *ctx.ReqCtx) {
|
||||
tableName := rc.GinCtx.Query("tableName")
|
||||
result := g.GenTableApp.FindOne(entity.DevGenTable{TableName: tableName}, true)
|
||||
mp := make(map[string]interface{})
|
||||
mp["list"] = result.Columns
|
||||
mp["info"] = result
|
||||
rc.ResData = mp
|
||||
}
|
||||
|
||||
// @Summary 获取树表信息
|
||||
// @Description 获取JSON
|
||||
// @Tags 工具 / 生成工具
|
||||
// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
|
||||
// @Router /develop/code/table/tableTree [get]
|
||||
// @Security X-TOKEN
|
||||
func (g *GenTableApi) GetTableTree(rc *ctx.ReqCtx) {
|
||||
rc.ResData = g.GenTableApp.FindTree(entity.DevGenTable{})
|
||||
}
|
||||
|
||||
// @Summary 添加表结构
|
||||
// @Description 添加表结构
|
||||
// @Tags 工具 / 生成工具
|
||||
// @Accept application/json
|
||||
// @Product application/json
|
||||
// @Param tableName query string false "tableName / 数据表名称"
|
||||
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
|
||||
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
|
||||
// @Router /develop/code/table [post]
|
||||
// @Security Bearer
|
||||
func (g *GenTableApi) Insert(rc *ctx.ReqCtx) {
|
||||
tablesList := strings.Split(rc.GinCtx.Query("tableName"), ",")
|
||||
for i := 0; i < len(tablesList); i++ {
|
||||
genTable := gen.ToolsGenTableColumn.GenTableInit(tablesList[i])
|
||||
g.GenTableApp.Insert(genTable)
|
||||
}
|
||||
}
|
||||
|
||||
// @Summary 修改表结构
|
||||
// @Description 修改表结构
|
||||
// @Tags 工具 / 生成工具
|
||||
// @Accept application/json
|
||||
// @Product application/json
|
||||
// @Param data body tools.SysTables true "body"
|
||||
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
|
||||
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
|
||||
// @Router /develop/code/table [put]
|
||||
// @Security Bearer
|
||||
func (g *GenTableApi) Update(rc *ctx.ReqCtx) {
|
||||
var data entity.DevGenTable
|
||||
ginx.BindJsonAndValid(rc.GinCtx, &data)
|
||||
g.GenTableApp.Update(data)
|
||||
}
|
||||
|
||||
// Delete
|
||||
// @Summary 删除表结构
|
||||
// @Description 删除表结构
|
||||
// @Tags 工具 / 生成工具
|
||||
// @Param tableId path int true "tableId"
|
||||
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
|
||||
// @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
|
||||
// @Router /develop/code/table/{tableId} [delete]
|
||||
func (g *GenTableApi) Delete(rc *ctx.ReqCtx) {
|
||||
tableIds := rc.GinCtx.Param("tableId")
|
||||
group := utils.IdsStrToIdsIntGroup(tableIds)
|
||||
g.GenTableApp.Delete(group)
|
||||
}
|
||||
@@ -3,26 +3,34 @@ package entity
|
||||
import "pandax/base/model"
|
||||
|
||||
type DevGenTable struct {
|
||||
TableId int64 `gorm:"table_id,primary" json:"tableId"` // 编号
|
||||
TableName string `gorm:"table_name" json:"tableName"` // 表名称
|
||||
TableComment string `gorm:"table_comment" json:"tableComment"` // 表描述
|
||||
ClassName string `gorm:"class_name" json:"className"` // 实体类名称
|
||||
TplCategory string `gorm:"tpl_category" json:"tplCategory"` // 使用的模板(crud单表操作 tree树表操作)
|
||||
PackageName string `gorm:"package_name" json:"packageName"` // 生成包路径
|
||||
ModuleName string `gorm:"module_name" json:"moduleName"` // 生成模块名
|
||||
BusinessName string `gorm:"business_name" json:"businessName"` // 生成业务名
|
||||
FunctionName string `gorm:"function_name" json:"functionName"` // 生成功能名
|
||||
FunctionAuthor string `gorm:"function_author" json:"functionAuthor"` // 生成功能作者
|
||||
Options string `gorm:"options" json:"options"` // 其它生成选项
|
||||
Remark string `gorm:"remark" json:"remark"` // 备注
|
||||
Columns []DevGenTableColumn `gorm:"-" json:"columns"` // 字段信息
|
||||
PkColumn DevGenTableColumn `gorm:"-" json:"pkColumn"` // 主键列信息
|
||||
TableId int64 `gorm:"primaryKey;autoIncrement" json:"tableId"` // 编号
|
||||
TableName string `gorm:"table_name" json:"tableName"` // 表名称
|
||||
TableComment string `gorm:"table_comment" json:"tableComment"` // 表描述
|
||||
ClassName string `gorm:"class_name" json:"className"` // 实体类名称
|
||||
TplCategory string `gorm:"tpl_category" json:"tplCategory"` // 使用的模板(crud单表操作 tree树表操作)
|
||||
PackageName string `gorm:"package_name" json:"packageName"` // 生成包路径
|
||||
ModuleName string `gorm:"module_name" json:"moduleName"` // 生成模块名
|
||||
BusinessName string `gorm:"business_name" json:"businessName"` // 生成业务名
|
||||
FunctionName string `gorm:"function_name" json:"functionName"` // 生成功能名
|
||||
FunctionAuthor string `gorm:"function_author" json:"functionAuthor"` // 生成功能作者
|
||||
TreeCode string `gorm:"tree_code" json:"treeCode"`
|
||||
TreeParentCode string `gorm:"tree_parent_code" json:"treeParentCode"`
|
||||
TreeName string `gorm:"tree_name" json:"treeName"`
|
||||
Options string `gorm:"options" json:"options"` // 其它生成选项
|
||||
Remark string `gorm:"remark" json:"remark"` // 备注
|
||||
PkColumn string `gorm:"pk_column;" json:"pkColumn"`
|
||||
PkGoField string `gorm:"pk_go_field" json:"pkGoField"`
|
||||
PkJsonField string `gorm:"pk_json_field" json:"pkJsonField"`
|
||||
Columns []DevGenTableColumn `gorm:"-" json:"columns"` // 字段信息
|
||||
model.BaseModel
|
||||
}
|
||||
|
||||
type ToolsGenTableExtend struct {
|
||||
DevGenTable
|
||||
TreeCode string `gorm:"-" json:"treeCode"` // 树编码字段
|
||||
TreeParentCode string `gorm:"-" json:"treeParentCode"` // 树父编码字段
|
||||
TreeName string `gorm:"-" json:"treeName"` // 树名称字段
|
||||
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"`
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
package entity
|
||||
|
||||
type DevGenTableColumn struct {
|
||||
ColumnId int64 `gorm:"column_id,primary" json:"columnId"` // 编号
|
||||
TableId int64 `gorm:"table_id" json:"tableId"` // 归属表编号
|
||||
ColumnId int64 `gorm:"primaryKey;autoIncrement" json:"columnId"` // 编号
|
||||
TableId int64 `gorm:"table_id" json:"tableId"` // 归属表编号
|
||||
TableName string `gorm:"table_name" json:"tableName"`
|
||||
ColumnName string `gorm:"column_name" json:"columnName"` // 列名称
|
||||
ColumnComment string `gorm:"column_comment" json:"columnComment"` // 列描述
|
||||
ColumnType string `gorm:"column_type" json:"columnType"` // 列类型
|
||||
GoType string `gorm:"go_type" json:"goType"` // Go类型
|
||||
GoField string `gorm:"go_field" json:"goField"` // Go字段名
|
||||
ColumnName string `gorm:"column_name" json:"columnName"` // 列名称
|
||||
ColumnComment string `gorm:"column_comment" json:"columnComment"` // 列描述
|
||||
ColumnType string `gorm:"column_type" json:"columnType"` // 列类型
|
||||
ColumnKey string `gorm:"column_key" json:"columnKey"`
|
||||
GoType string `gorm:"go_type" json:"goType"` // Go类型
|
||||
GoField string `gorm:"go_field" json:"goField"` // Go字段名
|
||||
JsonField string `gorm:"json_field;" json:"jsonField"`
|
||||
HtmlField string `gorm:"html_field" json:"htmlField"` // html字段名
|
||||
IsPk string `gorm:"is_pk" json:"isPk"` // 是否主键(1是)
|
||||
IsIncrement string `gorm:"is_increment" json:"isIncrement"` // 是否自增(1是)
|
||||
@@ -27,3 +29,18 @@ type DevGenTableColumn struct {
|
||||
LinkLabelId string `gorm:"link_label_id" json:"linkLabelId"` // 关联表键名
|
||||
LinkLabelName string `gorm:"link_label_name" json:"linkLabelName"` // 关联表字段值
|
||||
}
|
||||
|
||||
type DBColumns struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
421
apps/develop/gen/gen.go
Normal file
421
apps/develop/gen/gen.go
Normal file
@@ -0,0 +1,421 @@
|
||||
package gen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/kakuilan/kgo"
|
||||
"os"
|
||||
"pandax/apps/develop/entity"
|
||||
"pandax/apps/develop/services"
|
||||
"pandax/base/biz"
|
||||
"pandax/base/config"
|
||||
"pandax/base/utils"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var (
|
||||
ToolsGenTableColumn = &toolsGenTableColumn{
|
||||
ColumnTypeStr: []string{"char", "varchar", "narchar", "varchar2", "tinytext", "text", "mediumtext", "longtext"},
|
||||
ColumnTypeTime: []string{"datetime", "time", "date", "timestamp"},
|
||||
ColumnTypeNumber: []string{"tinyint", "smallint", "mediumint", "int", "number", "integer", "bigint", "float", "float", "double", "decimal"},
|
||||
ColumnNameNotEdit: []string{"id", "created_by", "created_at", "updated_by", "updated_at", "deleted_at"},
|
||||
ColumnNameNotList: []string{"id", "created_by", "updated_by", "created_at", "updated_at", "deleted_at"},
|
||||
ColumnNameNotQuery: []string{"id", "created_by", "updated_by", "created_at", "updated_at", "deleted_at", "remark"},
|
||||
}
|
||||
)
|
||||
|
||||
type toolsGenTableColumn struct {
|
||||
ColumnTypeStr []string //数据库字符串类型
|
||||
ColumnTypeTime []string //数据库时间类型
|
||||
ColumnTypeNumber []string //数据库数字类型
|
||||
ColumnNameNotEdit []string //页面不需要编辑字段
|
||||
ColumnNameNotList []string //页面不需要显示的列表字段
|
||||
ColumnNameNotQuery []string //页面不需要查询字段
|
||||
}
|
||||
|
||||
// GetDbType 获取数据库类型字段
|
||||
func (s *toolsGenTableColumn) GetDbType(columnType string) string {
|
||||
if strings.Index(columnType, "(") > 0 {
|
||||
return columnType[0:strings.Index(columnType, "(")]
|
||||
} else {
|
||||
return columnType
|
||||
}
|
||||
}
|
||||
|
||||
// IsExistInArray 判断 value 是否存在在切片array中
|
||||
func (s *toolsGenTableColumn) IsExistInArray(value string, array []string) bool {
|
||||
for _, v := range array {
|
||||
if v == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// GetColumnLength 获取字段长度
|
||||
func (s *toolsGenTableColumn) GetColumnLength(columnType string) int {
|
||||
start := strings.Index(columnType, "(")
|
||||
end := strings.Index(columnType, ")")
|
||||
result := ""
|
||||
if start >= 0 && end >= 0 {
|
||||
result = columnType[start+1 : end-1]
|
||||
}
|
||||
i, _ := strconv.Atoi(result)
|
||||
return i
|
||||
}
|
||||
|
||||
// IsStringObject 判断是否是数据库字符串类型
|
||||
func (s *toolsGenTableColumn) IsStringObject(dataType string) bool {
|
||||
return s.IsExistInArray(dataType, s.ColumnTypeStr)
|
||||
}
|
||||
|
||||
// IsTimeObject 判断是否是数据库时间类型
|
||||
func (s *toolsGenTableColumn) IsTimeObject(dataType string) bool {
|
||||
return s.IsExistInArray(dataType, s.ColumnTypeTime)
|
||||
}
|
||||
|
||||
// IsNumberObject 是否数字类型
|
||||
func (s *toolsGenTableColumn) IsNumberObject(dataType string) bool {
|
||||
return s.IsExistInArray(dataType, s.ColumnTypeNumber)
|
||||
}
|
||||
|
||||
// IsNotEdit 是否不可编辑
|
||||
func (s *toolsGenTableColumn) IsNotEdit(name string) bool {
|
||||
return s.IsExistInArray(name, s.ColumnNameNotEdit)
|
||||
}
|
||||
|
||||
// IsNotList 不在列表显示
|
||||
func (s *toolsGenTableColumn) IsNotList(name string) bool {
|
||||
return s.IsExistInArray(name, s.ColumnNameNotList)
|
||||
}
|
||||
|
||||
// IsNotQuery 不可用于查询
|
||||
func (s *toolsGenTableColumn) IsNotQuery(name string) bool {
|
||||
return s.IsExistInArray(name, s.ColumnNameNotQuery)
|
||||
}
|
||||
|
||||
// CheckNameColumn 检查字段名后4位是否是name
|
||||
func (s *toolsGenTableColumn) CheckNameColumn(columnName string) bool {
|
||||
if len(columnName) >= 4 {
|
||||
end := len(columnName)
|
||||
start := end - 4
|
||||
if start <= 0 {
|
||||
start = 0
|
||||
}
|
||||
tmp := columnName[start:end]
|
||||
if tmp == "name" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// CheckStatusColumn 检查字段名后6位是否是status
|
||||
func (s *toolsGenTableColumn) CheckStatusColumn(columnName string) bool {
|
||||
if len(columnName) >= 6 {
|
||||
end := len(columnName)
|
||||
start := end - 6
|
||||
|
||||
if start <= 0 {
|
||||
start = 0
|
||||
}
|
||||
tmp := columnName[start:end]
|
||||
|
||||
if tmp == "status" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// CheckTypeColumn 检查字段名后4位是否是type
|
||||
func (s *toolsGenTableColumn) CheckTypeColumn(columnName string) bool {
|
||||
if len(columnName) >= 4 {
|
||||
end := len(columnName)
|
||||
start := end - 4
|
||||
|
||||
if start <= 0 {
|
||||
start = 0
|
||||
}
|
||||
|
||||
if columnName[start:end] == "type" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// CheckSexColumn 检查字段名后3位是否是sex
|
||||
func (s *toolsGenTableColumn) CheckSexColumn(columnName string) bool {
|
||||
if len(columnName) >= 3 {
|
||||
end := len(columnName)
|
||||
start := end - 3
|
||||
if start <= 0 {
|
||||
start = 0
|
||||
}
|
||||
if columnName[start:end] == "sex" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *toolsGenTableColumn) GenTableInit(tableName string) entity.DevGenTable {
|
||||
var data entity.DevGenTable
|
||||
data.TableName = tableName
|
||||
tableNameList := strings.Split(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
|
||||
// 小驼峰表名 js函数名和权限标识使用
|
||||
if i == 0 {
|
||||
data.BusinessName += strings.ToLower(strStart) + strEnd
|
||||
} else {
|
||||
data.BusinessName += strings.ToUpper(strStart) + strEnd
|
||||
}
|
||||
}
|
||||
data.PackageName = "admin"
|
||||
data.TplCategory = "crud"
|
||||
// 中横线表名称,接口路径、前端文件夹名称和js名称使用
|
||||
data.ModuleName = strings.Replace(tableName, "_", "-", -1)
|
||||
|
||||
dbTable := services.DevGenTableModelDao.FindDbTableOne(tableName)
|
||||
dbColumn := services.DevTableColumnModelDao.FindDbTableColumnList(tableName)
|
||||
|
||||
data.TableComment = dbTable.TableComment
|
||||
if dbTable.TableComment == "" {
|
||||
data.TableComment = data.ClassName
|
||||
}
|
||||
data.FunctionName = data.TableComment
|
||||
data.FunctionAuthor = "panda"
|
||||
|
||||
dcs := *dbColumn
|
||||
for i := 0; i < len(dcs); i++ {
|
||||
var column entity.DevGenTableColumn
|
||||
column.ColumnComment = dcs[i].ColumnComment
|
||||
column.ColumnName = dcs[i].ColumnName
|
||||
column.ColumnType = dcs[i].ColumnType
|
||||
column.Sort = i + 1
|
||||
column.IsPk = "0"
|
||||
|
||||
nameList := strings.Split(dcs[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
|
||||
}
|
||||
}
|
||||
if strings.Contains(dcs[i].ColumnKey, "PR") {
|
||||
column.IsPk = "1"
|
||||
data.PkColumn = dcs[i].ColumnName
|
||||
data.PkGoField = column.GoField
|
||||
data.PkJsonField = column.JsonField
|
||||
}
|
||||
|
||||
dataType := s.GetDbType(column.ColumnType)
|
||||
if s.IsStringObject(dataType) {
|
||||
//字段为字符串类型
|
||||
column.GoType = "string"
|
||||
columnLength := s.GetColumnLength(column.ColumnType)
|
||||
if columnLength >= 500 {
|
||||
column.HtmlType = "textarea"
|
||||
} else {
|
||||
column.HtmlType = "input"
|
||||
}
|
||||
} else if s.IsTimeObject(dataType) {
|
||||
//字段为时间类型
|
||||
column.GoType = "Time"
|
||||
column.HtmlType = "datetime"
|
||||
} else if s.IsNumberObject(dataType) {
|
||||
//字段为数字类型
|
||||
column.HtmlType = "input"
|
||||
t, _ := utils.ReplaceString(`\(.+\)`, "", column.ColumnType)
|
||||
t = strings.Split(strings.TrimSpace(t), " ")[0]
|
||||
t = strings.ToLower(t)
|
||||
// 如果是浮点型
|
||||
switch t {
|
||||
case "float", "double", "decimal":
|
||||
column.GoType = "float64"
|
||||
case "bit", "int", "tinyint", "small_int", "smallint", "medium_int", "mediumint":
|
||||
if utils.Contains(column.ColumnType, "unsigned") != -1 {
|
||||
column.GoType = "uint"
|
||||
} else {
|
||||
column.GoType = "int"
|
||||
}
|
||||
case "big_int", "bigint":
|
||||
if utils.Contains(column.ColumnType, "unsigned") != -1 {
|
||||
column.GoType = "uint64"
|
||||
} else {
|
||||
column.GoType = "int64"
|
||||
}
|
||||
}
|
||||
}
|
||||
//新增字段
|
||||
if s.IsNotEdit(column.ColumnName) {
|
||||
column.IsRequired = "0"
|
||||
column.IsInsert = "0"
|
||||
} else {
|
||||
column.IsInsert = "1"
|
||||
if strings.Index(column.ColumnName, "name") >= 0 || strings.Index(column.ColumnName, "status") >= 0 {
|
||||
column.IsRequired = "1"
|
||||
}
|
||||
}
|
||||
|
||||
// 编辑字段
|
||||
if s.IsNotEdit(column.ColumnName) {
|
||||
column.IsEdit = "0"
|
||||
} else {
|
||||
if column.IsPk == "1" {
|
||||
column.IsEdit = "0"
|
||||
} else {
|
||||
column.IsEdit = "1"
|
||||
}
|
||||
}
|
||||
// 列表字段
|
||||
if s.IsNotList(column.ColumnName) {
|
||||
column.IsList = "0"
|
||||
} else {
|
||||
column.IsList = "1"
|
||||
}
|
||||
// 查询字段
|
||||
if s.IsNotQuery(column.ColumnName) {
|
||||
column.IsQuery = "0"
|
||||
} else {
|
||||
column.IsQuery = "1"
|
||||
}
|
||||
|
||||
// 查询字段类型
|
||||
if s.CheckNameColumn(column.ColumnName) {
|
||||
column.QueryType = "LIKE"
|
||||
} else {
|
||||
column.QueryType = "EQ"
|
||||
}
|
||||
// 状态字段设置单选框
|
||||
if s.CheckStatusColumn(column.ColumnName) {
|
||||
column.HtmlType = "radio"
|
||||
} else if s.CheckTypeColumn(column.ColumnName) || s.CheckSexColumn(column.ColumnName) {
|
||||
// 类型&性别字段设置下拉框
|
||||
column.HtmlType = "select"
|
||||
}
|
||||
data.Columns = append(data.Columns, column)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// 视图预览
|
||||
func Preview(tableId int64) map[string]interface{} {
|
||||
t1, err := template.ParseFiles("resource/template/go/entity.template")
|
||||
biz.ErrIsNil(err, fmt.Sprintf("entity模版读取失败!错误详情:%s", err.Error()))
|
||||
|
||||
t2, err := template.ParseFiles("resource/template/go/api.template")
|
||||
biz.ErrIsNil(err, fmt.Sprintf("api模版读取失败!错误详情:%s", err.Error()))
|
||||
|
||||
t3, err := template.ParseFiles("resource/template/go/service.template")
|
||||
biz.ErrIsNil(err, fmt.Sprintf("service模版读取失败!错误详情:%s", err.Error()))
|
||||
|
||||
t4, err := template.ParseFiles("resource/template/go/router.template")
|
||||
biz.ErrIsNil(err, fmt.Sprintf("router模版读取失败!错误详情:%s", err.Error()))
|
||||
|
||||
t5, err := template.ParseFiles("resource/template/js/api.template")
|
||||
biz.ErrIsNil(err, fmt.Sprintf("js模版读取失败!错误详情:%s", err.Error()))
|
||||
|
||||
t6, err := template.ParseFiles("resource/template/vue/list-vue.template")
|
||||
biz.ErrIsNil(err, fmt.Sprintf("vue列表模版读取失败!错误详情:%s", err.Error()))
|
||||
|
||||
t7, err := template.ParseFiles("resource/template/vue/edit-vue.template")
|
||||
biz.ErrIsNil(err, fmt.Sprintf("vue编辑模版读取失败!错误详情:%s", err.Error()))
|
||||
|
||||
tab := services.DevGenTableModelDao.FindOne(entity.DevGenTable{TableId: tableId}, false)
|
||||
|
||||
var b1 bytes.Buffer
|
||||
err = t1.Execute(&b1, tab)
|
||||
var b2 bytes.Buffer
|
||||
err = t2.Execute(&b2, tab)
|
||||
var b3 bytes.Buffer
|
||||
err = t3.Execute(&b3, tab)
|
||||
var b4 bytes.Buffer
|
||||
err = t4.Execute(&b4, tab)
|
||||
var b5 bytes.Buffer
|
||||
err = t5.Execute(&b5, tab)
|
||||
var b6 bytes.Buffer
|
||||
err = t6.Execute(&b6, tab)
|
||||
var b7 bytes.Buffer
|
||||
err = t7.Execute(&b7, tab)
|
||||
|
||||
mp := make(map[string]interface{})
|
||||
mp["template/entity.template"] = b1.String()
|
||||
mp["template/api.template"] = b2.String()
|
||||
mp["template/service.template"] = b3.String()
|
||||
mp["template/router.template"] = b4.String()
|
||||
mp["template/jsApi.template"] = b5.String()
|
||||
mp["template/listVue.template"] = b6.String()
|
||||
mp["template/editVue.template"] = b7.String()
|
||||
return mp
|
||||
}
|
||||
|
||||
// 生成 代码
|
||||
func GenCode(tableId int64) {
|
||||
|
||||
tab := services.DevGenTableModelDao.FindOne(entity.DevGenTable{TableId: tableId}, false)
|
||||
tab.ModuleName = strings.Replace(tab.TableName, "_", "-", -1)
|
||||
|
||||
t1, err := template.ParseFiles("resource/template/go/entity.template")
|
||||
biz.ErrIsNil(err, fmt.Sprintf("entity模版读取失败!错误详情:%s", err.Error()))
|
||||
|
||||
t2, err := template.ParseFiles("resource/template/go/api.template")
|
||||
biz.ErrIsNil(err, fmt.Sprintf("api模版读取失败!错误详情:%s", err.Error()))
|
||||
|
||||
t3, err := template.ParseFiles("resource/template/go/service.template")
|
||||
biz.ErrIsNil(err, fmt.Sprintf("service模版读取失败!错误详情:%s", err.Error()))
|
||||
|
||||
t4, err := template.ParseFiles("resource/template/go/router.template")
|
||||
biz.ErrIsNil(err, fmt.Sprintf("router模版读取失败!错误详情:%s", err.Error()))
|
||||
|
||||
t5, err := template.ParseFiles("resource/template/js/api.template")
|
||||
biz.ErrIsNil(err, fmt.Sprintf("js模版读取失败!错误详情:%s", err.Error()))
|
||||
|
||||
t6, err := template.ParseFiles("resource/template/vue/list-vue.template")
|
||||
biz.ErrIsNil(err, fmt.Sprintf("vue列表模版读取失败!错误详情:%s", err.Error()))
|
||||
t7, err := template.ParseFiles("resource/template/vue/edit-vue.template")
|
||||
biz.ErrIsNil(err, fmt.Sprintf("vue编辑模版读取失败!错误详情:%s", err.Error()))
|
||||
|
||||
kgo.KFile.Mkdir("./apps/"+tab.PackageName+"/api/", os.ModePerm)
|
||||
kgo.KFile.Mkdir("./apps/"+tab.PackageName+"/entity/", os.ModePerm)
|
||||
kgo.KFile.Mkdir("./apps/"+tab.PackageName+"/router/", os.ModePerm)
|
||||
kgo.KFile.Mkdir("./apps/"+tab.PackageName+"/services/", os.ModePerm)
|
||||
kgo.KFile.Mkdir(config.Conf.Gen.Frontpath+"/api/"+tab.PackageName+"/", os.ModePerm)
|
||||
kgo.KFile.Mkdir(config.Conf.Gen.Frontpath+"/views/"+tab.PackageName+"/"+tab.ModuleName+"/", os.ModePerm)
|
||||
kgo.KFile.Mkdir(config.Conf.Gen.Frontpath+"/views/"+tab.PackageName+"/"+tab.ModuleName+"/"+"component"+"/", os.ModePerm)
|
||||
|
||||
var b1 bytes.Buffer
|
||||
err = t1.Execute(&b1, tab)
|
||||
var b2 bytes.Buffer
|
||||
err = t2.Execute(&b2, tab)
|
||||
var b3 bytes.Buffer
|
||||
err = t3.Execute(&b3, tab)
|
||||
var b4 bytes.Buffer
|
||||
err = t4.Execute(&b4, tab)
|
||||
var b5 bytes.Buffer
|
||||
err = t5.Execute(&b5, tab)
|
||||
var b6 bytes.Buffer
|
||||
err = t6.Execute(&b6, tab)
|
||||
var b7 bytes.Buffer
|
||||
err = t7.Execute(&b7, tab)
|
||||
|
||||
kgo.KFile.WriteFile("./apps/"+tab.PackageName+"/entity/"+tab.TableName+".go", b1.Bytes())
|
||||
kgo.KFile.WriteFile("./apps/"+tab.PackageName+"/api/"+tab.TableName+".go", b2.Bytes())
|
||||
kgo.KFile.WriteFile("./apps/"+tab.PackageName+"/services/"+tab.TableName+".go", b3.Bytes())
|
||||
kgo.KFile.WriteFile("./apps/"+tab.PackageName+"/router/"+tab.TableName+".go", b4.Bytes())
|
||||
kgo.KFile.WriteFile(config.Conf.Gen.Frontpath+"/api/"+tab.PackageName+"/"+tab.ModuleName+".js", b5.Bytes())
|
||||
kgo.KFile.WriteFile(config.Conf.Gen.Frontpath+"/views/"+tab.PackageName+"/"+tab.ModuleName+"/index.vue", b6.Bytes())
|
||||
kgo.KFile.WriteFile(config.Conf.Gen.Frontpath+"/views/"+tab.PackageName+"/"+tab.ModuleName+"/"+"component"+"/index.vue", b7.Bytes())
|
||||
}
|
||||
32
apps/develop/router/gen.go
Normal file
32
apps/develop/router/gen.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"pandax/apps/develop/api"
|
||||
"pandax/apps/develop/services"
|
||||
"pandax/base/ctx"
|
||||
)
|
||||
|
||||
func InitGenRouter(router *gin.RouterGroup) {
|
||||
// 登录日志
|
||||
genApi := &api.GenApi{
|
||||
GenTableApp: services.DevGenTableModelDao,
|
||||
}
|
||||
gen := router.Group("gen")
|
||||
|
||||
genViewLog := ctx.NewLogInfo("获取生成代码视图")
|
||||
gen.GET("preview/:tableId", func(c *gin.Context) {
|
||||
ctx.NewReqCtxWithGin(c).WithLog(genViewLog).Handle(genApi.Preview)
|
||||
})
|
||||
|
||||
genCodeLog := ctx.NewLogInfo("生成代码")
|
||||
gen.GET("code/:tableId", func(c *gin.Context) {
|
||||
ctx.NewReqCtxWithGin(c).WithLog(genCodeLog).Handle(genApi.GenCode)
|
||||
})
|
||||
|
||||
genApiLog := ctx.NewLogInfo("生成菜单和api配置")
|
||||
gen.GET("menuAndApi/:tableId", func(c *gin.Context) {
|
||||
ctx.NewReqCtxWithGin(c).WithLog(genApiLog).Handle(genApi.AutoApi)
|
||||
})
|
||||
|
||||
}
|
||||
56
apps/develop/router/table.go
Normal file
56
apps/develop/router/table.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"pandax/apps/develop/api"
|
||||
"pandax/apps/develop/services"
|
||||
"pandax/base/ctx"
|
||||
)
|
||||
|
||||
func InitGenTableRouter(router *gin.RouterGroup) {
|
||||
// 登录日志
|
||||
genApi := &api.GenTableApi{
|
||||
GenTableApp: services.DevGenTableModelDao,
|
||||
}
|
||||
gen := router.Group("table")
|
||||
|
||||
genDbListLog := ctx.NewLogInfo("获取数据库列表")
|
||||
gen.GET("/db/list", func(c *gin.Context) {
|
||||
ctx.NewReqCtxWithGin(c).WithLog(genDbListLog).Handle(genApi.GetDBTableList)
|
||||
})
|
||||
|
||||
genListLog := ctx.NewLogInfo("获取表列表")
|
||||
gen.GET("list", func(c *gin.Context) {
|
||||
ctx.NewReqCtxWithGin(c).WithLog(genListLog).Handle(genApi.GetTablePage)
|
||||
})
|
||||
|
||||
genInfoNameLog := ctx.NewLogInfo("获取表信息By tableName")
|
||||
gen.GET("/info/tableName", func(c *gin.Context) {
|
||||
ctx.NewReqCtxWithGin(c).WithLog(genInfoNameLog).Handle(genApi.GetTableInfoByName)
|
||||
})
|
||||
|
||||
genInfoLog := ctx.NewLogInfo("获取表信息")
|
||||
gen.GET("info/:tableId", func(c *gin.Context) {
|
||||
ctx.NewReqCtxWithGin(c).WithLog(genInfoLog).Handle(genApi.GetTableInfo)
|
||||
})
|
||||
|
||||
genTreeLog := ctx.NewLogInfo("获取表树")
|
||||
gen.GET("tableTree", func(c *gin.Context) {
|
||||
ctx.NewReqCtxWithGin(c).WithLog(genTreeLog).Handle(genApi.GetTableTree)
|
||||
})
|
||||
|
||||
genInsterLog := ctx.NewLogInfo("新增表")
|
||||
gen.POST("", func(c *gin.Context) {
|
||||
ctx.NewReqCtxWithGin(c).WithLog(genInsterLog).Handle(genApi.Insert)
|
||||
})
|
||||
|
||||
genUpdateLog := ctx.NewLogInfo("修改表")
|
||||
gen.PUT("", func(c *gin.Context) {
|
||||
ctx.NewReqCtxWithGin(c).WithLog(genUpdateLog).Handle(genApi.Update)
|
||||
})
|
||||
|
||||
genDeleteLog := ctx.NewLogInfo("删除表")
|
||||
gen.DELETE(":tableId", func(c *gin.Context) {
|
||||
ctx.NewReqCtxWithGin(c).WithLog(genDeleteLog).Handle(genApi.Delete)
|
||||
})
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"pandax/base/biz"
|
||||
"pandax/base/config"
|
||||
"pandax/base/global"
|
||||
"pandax/base/utils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -17,16 +18,16 @@ import (
|
||||
|
||||
type (
|
||||
SysGenTableModel interface {
|
||||
FindDbTablesListPage(page, pageSize int, data entity.DevGenTable) (*[]entity.DevGenTable, int64)
|
||||
FindDbTableOne(tableName string) *entity.DevGenTable
|
||||
FindDbTablesListPage(page, pageSize int, data entity.DBTables) (*[]entity.DBTables, int64)
|
||||
FindDbTableOne(tableName string) *entity.DBTables
|
||||
|
||||
// 导入表数据
|
||||
Insert(data entity.DevGenTable)
|
||||
FindOne(dictCode int64) *entity.DevGenTable
|
||||
FindTree(data entity.DevGenTable) *[]entity.ToolsGenTableExtend
|
||||
FindOne(data entity.DevGenTable, exclude bool) *entity.DevGenTable
|
||||
FindTree(data entity.DevGenTable) *[]entity.DevGenTable
|
||||
FindListPage(page, pageSize int, data entity.DevGenTable) (*[]entity.DevGenTable, int64)
|
||||
Update(data entity.DevGenTable) *entity.DevGenTable
|
||||
Delete(dictCode []int64)
|
||||
Delete(tableIds []int64)
|
||||
}
|
||||
|
||||
devGenTableModelImpl struct {
|
||||
@@ -38,36 +39,52 @@ var DevGenTableModelDao SysGenTableModel = &devGenTableModelImpl{
|
||||
table: "dev_gen_tables",
|
||||
}
|
||||
|
||||
func (m *devGenTableModelImpl) FindDbTablesListPage(page, pageSize int, data entity.DevGenTable) (*[]entity.DevGenTable, int64) {
|
||||
list := make([]entity.DevGenTable, 0)
|
||||
func (m *devGenTableModelImpl) FindDbTablesListPage(page, pageSize int, data entity.DBTables) (*[]entity.DBTables, int64) {
|
||||
list := make([]entity.DBTables, 0)
|
||||
pgdata := make([]map[string]interface{}, 0)
|
||||
var total int64 = 0
|
||||
offset := pageSize * (page - 1)
|
||||
if config.Conf.Server.DbType != "mysql" && config.Conf.Server.DbType == "postgresql" {
|
||||
if config.Conf.Server.DbType != "mysql" && config.Conf.Server.DbType != "postgresql" {
|
||||
biz.ErrIsNil(errors.New("只支持mysql和postgresql数据库"), "只支持mysql和postgresql数据库")
|
||||
}
|
||||
|
||||
db := global.Db.Table("information_schema.tables")
|
||||
db = db.Where("TABLE_NAME not in (select table_name from `" + config.Conf.Gen.Dbname + "`.sys_tables) ")
|
||||
db = db.Where("table_schema= ? ", config.Conf.Gen.Dbname)
|
||||
|
||||
if data.TableName != "" {
|
||||
db = db.Where("table_name = ?", data.TableName)
|
||||
if config.Conf.Server.DbType == "mysql" {
|
||||
db = db.Where("table_schema= ? ", config.Conf.Gen.Dbname)
|
||||
}
|
||||
if config.Conf.Server.DbType == "postgresql" {
|
||||
db = db.Where("table_schema = ? ", "public")
|
||||
}
|
||||
db = db.Where("table_name NOT LIKE 'dev_%'")
|
||||
if data.TableName != "" {
|
||||
db = db.Where("table_name like ?", "%"+data.TableName+"%")
|
||||
}
|
||||
if config.Conf.Server.DbType == "mysql" {
|
||||
err := db.Limit(pageSize).Offset(offset).Find(&list).Offset(-1).Limit(-1).Count(&total).Error
|
||||
biz.ErrIsNil(err, "查询配置分页列表信息失败")
|
||||
return &list, total
|
||||
} else {
|
||||
err := db.Limit(pageSize).Offset(offset).Find(&pgdata).Offset(-1).Limit(-1).Count(&total).Error
|
||||
biz.ErrIsNil(err, "查询配置分页列表信息失败")
|
||||
for _, pd := range pgdata {
|
||||
list = append(list, entity.DBTables{TableName: utils.B2S(pd["table_name"].([]uint8))})
|
||||
}
|
||||
return &list, total
|
||||
}
|
||||
|
||||
err := db.Count(&total).Error
|
||||
err = db.Limit(pageSize).Offset(offset).Find(&list).Error
|
||||
biz.ErrIsNil(err, "查询配置分页列表信息失败")
|
||||
return &list, total
|
||||
}
|
||||
|
||||
func (m *devGenTableModelImpl) FindDbTableOne(tableName string) *entity.DevGenTable {
|
||||
resData := new(entity.DevGenTable)
|
||||
func (m *devGenTableModelImpl) FindDbTableOne(tableName string) *entity.DBTables {
|
||||
resData := new(entity.DBTables)
|
||||
if config.Conf.Server.DbType != "mysql" && config.Conf.Server.DbType == "postgresql" {
|
||||
biz.ErrIsNil(errors.New("只支持mysql和postgresql数据库"), "只支持mysql和postgresql数据库")
|
||||
}
|
||||
db := global.Db.Table("information_schema.tables")
|
||||
db = db.Where("table_schema= ? ", config.Conf.Gen.Dbname)
|
||||
biz.IsTrue(tableName != "", "table name cannot be empty!")
|
||||
|
||||
if config.Conf.Server.DbType != "mysql" {
|
||||
db = db.Where("table_schema= ? ", config.Conf.Gen.Dbname)
|
||||
}
|
||||
if config.Conf.Server.DbType != "postgresql" {
|
||||
db = db.Where("table_schema= ? ", "public")
|
||||
}
|
||||
db = db.Where("table_name = ?", tableName)
|
||||
err := db.First(&resData).Error
|
||||
biz.ErrIsNil(err, err.Error())
|
||||
@@ -79,19 +96,31 @@ func (m *devGenTableModelImpl) Insert(dgt entity.DevGenTable) {
|
||||
biz.ErrIsNil(err, "新增生成代码表失败")
|
||||
for i := 0; i < len(dgt.Columns); i++ {
|
||||
dgt.Columns[i].TableId = dgt.TableId
|
||||
SysSysConfigModelDao.Insert(dgt.Columns[i])
|
||||
DevTableColumnModelDao.Insert(dgt.Columns[i])
|
||||
}
|
||||
}
|
||||
|
||||
func (m *devGenTableModelImpl) FindOne(tableId int64) *entity.DevGenTable {
|
||||
func (m *devGenTableModelImpl) FindOne(data entity.DevGenTable, exclude bool) *entity.DevGenTable {
|
||||
resData := new(entity.DevGenTable)
|
||||
err := global.Db.Table(m.table).Where("`table_id` = ?", tableId).First(resData).Error
|
||||
db := global.Db.Table(m.table)
|
||||
if data.TableName != "" {
|
||||
db = db.Where("table_name = ?", data.TableName)
|
||||
}
|
||||
if data.TableId != 0 {
|
||||
db = db.Where("table_id = ?", data.TableId)
|
||||
}
|
||||
if data.TableComment != "" {
|
||||
db = db.Where("table_comment = ?", data.TableComment)
|
||||
}
|
||||
err := db.First(resData).Error
|
||||
biz.ErrIsNil(err, "查询配置信息失败")
|
||||
list := DevTableColumnModelDao.FindList(entity.DevGenTableColumn{TableId: resData.TableId}, exclude)
|
||||
resData.Columns = *list
|
||||
return resData
|
||||
}
|
||||
|
||||
func (m *devGenTableModelImpl) FindTree(data entity.DevGenTable) *[]entity.ToolsGenTableExtend {
|
||||
resData := make([]entity.ToolsGenTableExtend, 0)
|
||||
func (m *devGenTableModelImpl) FindTree(data entity.DevGenTable) *[]entity.DevGenTable {
|
||||
resData := make([]entity.DevGenTable, 0)
|
||||
db := global.Db.Table(m.table)
|
||||
|
||||
if data.TableName != "" {
|
||||
@@ -108,7 +137,7 @@ func (m *devGenTableModelImpl) FindTree(data entity.DevGenTable) *[]entity.Tools
|
||||
for i := 0; i < len(resData); i++ {
|
||||
var col entity.DevGenTableColumn
|
||||
col.TableId = resData[i].TableId
|
||||
columns := SysSysConfigModelDao.FindList(col)
|
||||
columns := DevTableColumnModelDao.FindList(col, false)
|
||||
resData[i].Columns = *columns
|
||||
}
|
||||
return &resData
|
||||
@@ -173,7 +202,7 @@ func (m *devGenTableModelImpl) Update(data entity.DevGenTable) *entity.DevGenTab
|
||||
}
|
||||
}
|
||||
}
|
||||
SysSysConfigModelDao.Update(data.Columns[i])
|
||||
DevTableColumnModelDao.Update(data.Columns[i])
|
||||
}
|
||||
return &data
|
||||
}
|
||||
@@ -200,7 +229,7 @@ func (e *devGenTableModelImpl) DeleteTables(tableId int64) bool {
|
||||
}
|
||||
|
||||
func (m *devGenTableModelImpl) Delete(configIds []int64) {
|
||||
err := global.Db.Table(m.table).Delete(&entity.DevGenTable{}, "`table_id` in (?)", configIds).Error
|
||||
err := global.Db.Table(m.table).Delete(&entity.DevGenTable{}, "table_id in (?)", configIds).Error
|
||||
biz.ErrIsNil(err, "删除生成代码信息失败")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -16,37 +16,25 @@ import (
|
||||
|
||||
type (
|
||||
SysGenTableColumnModel interface {
|
||||
FindDbTablesColumnListPage(page, pageSize int, data entity.DevGenTableColumn) (*[]entity.DevGenTableColumn, int64)
|
||||
FindDbTableColumnList(tableName string) *[]entity.DevGenTableColumn
|
||||
FindDbTablesColumnListPage(page, pageSize int, data entity.DBColumns) (*[]entity.DBColumns, int64)
|
||||
FindDbTableColumnList(tableName string) *[]entity.DBColumns
|
||||
|
||||
Insert(data entity.DevGenTableColumn) *entity.DevGenTableColumn
|
||||
FindList(data entity.DevGenTableColumn) *[]entity.DevGenTableColumn
|
||||
FindList(data entity.DevGenTableColumn, exclude bool) *[]entity.DevGenTableColumn
|
||||
Update(data entity.DevGenTableColumn) *entity.DevGenTableColumn
|
||||
}
|
||||
|
||||
devTableColumnModelImpl struct {
|
||||
table string
|
||||
ColumnTypeStr []string //数据库字符串类型
|
||||
ColumnTypeTime []string //数据库时间类型
|
||||
ColumnTypeNumber []string //数据库数字类型
|
||||
ColumnNameNotEdit []string //页面不需要编辑字段
|
||||
ColumnNameNotList []string //页面不需要显示的列表字段
|
||||
ColumnNameNotQuery []string //页面不需要查询字段
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
var SysSysConfigModelDao SysGenTableColumnModel = &devTableColumnModelImpl{
|
||||
table: "dev_gen_table_columns",
|
||||
ColumnTypeStr: []string{"char", "varchar", "narchar", "varchar2", "tinytext", "text", "mediumtext", "longtext"},
|
||||
ColumnTypeTime: []string{"datetime", "time", "date", "timestamp"},
|
||||
ColumnTypeNumber: []string{"tinyint", "smallint", "mediumint", "int", "number", "integer", "bigint", "float", "float", "double", "decimal"},
|
||||
ColumnNameNotEdit: []string{"id", "created_by", "created_at", "updated_by", "updated_at", "deleted_at"},
|
||||
ColumnNameNotList: []string{"id", "created_by", "updated_by", "created_at", "updated_at", "deleted_at"},
|
||||
ColumnNameNotQuery: []string{"id", "created_by", "updated_by", "created_at", "updated_at", "deleted_at", "remark"},
|
||||
var DevTableColumnModelDao SysGenTableColumnModel = &devTableColumnModelImpl{
|
||||
table: "dev_gen_table_columns",
|
||||
}
|
||||
|
||||
func (m *devTableColumnModelImpl) FindDbTablesColumnListPage(page, pageSize int, data entity.DevGenTableColumn) (*[]entity.DevGenTableColumn, int64) {
|
||||
list := make([]entity.DevGenTableColumn, 0)
|
||||
func (m *devTableColumnModelImpl) FindDbTablesColumnListPage(page, pageSize int, data entity.DBColumns) (*[]entity.DBColumns, int64) {
|
||||
list := make([]entity.DBColumns, 0)
|
||||
var total int64 = 0
|
||||
offset := pageSize * (page - 1)
|
||||
if config.Conf.Server.DbType != "mysql" && config.Conf.Server.DbType == "postgresql" {
|
||||
@@ -66,8 +54,8 @@ func (m *devTableColumnModelImpl) FindDbTablesColumnListPage(page, pageSize int,
|
||||
return &list, total
|
||||
}
|
||||
|
||||
func (m *devTableColumnModelImpl) FindDbTableColumnList(tableName string) *[]entity.DevGenTableColumn {
|
||||
resData := make([]entity.DevGenTableColumn, 0)
|
||||
func (m *devTableColumnModelImpl) FindDbTableColumnList(tableName string) *[]entity.DBColumns {
|
||||
resData := make([]entity.DBColumns, 0)
|
||||
if config.Conf.Server.DbType != "mysql" && config.Conf.Server.DbType == "postgresql" {
|
||||
biz.ErrIsNil(errors.New("只支持mysql和postgresql数据库"), "只支持mysql和postgresql数据库")
|
||||
}
|
||||
@@ -87,10 +75,20 @@ func (m *devTableColumnModelImpl) Insert(dgt entity.DevGenTableColumn) *entity.D
|
||||
return &dgt
|
||||
}
|
||||
|
||||
func (m *devTableColumnModelImpl) FindList(data entity.DevGenTableColumn) *[]entity.DevGenTableColumn {
|
||||
func (m *devTableColumnModelImpl) FindList(data entity.DevGenTableColumn, exclude bool) *[]entity.DevGenTableColumn {
|
||||
list := make([]entity.DevGenTableColumn, 0)
|
||||
err := global.Db.Table(m.table).Where("table_id = ?", data.TableId).Find(&list).Error
|
||||
|
||||
db := global.Db.Table(m.table).Where("table_id = ?", data.TableId)
|
||||
if exclude {
|
||||
notIn := make([]string, 6)
|
||||
notIn = append(notIn, "id")
|
||||
notIn = append(notIn, "create_by")
|
||||
notIn = append(notIn, "update_by")
|
||||
notIn = append(notIn, "created_at")
|
||||
notIn = append(notIn, "updated_at")
|
||||
notIn = append(notIn, "deleted_at")
|
||||
db = db.Where(" column_name not in(?)", notIn)
|
||||
}
|
||||
err := db.Find(&list).Error
|
||||
biz.ErrIsNil(err, "查询生成代码字段表信息失败")
|
||||
return &list
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user