mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
[修复]代码生成
This commit is contained in:
@@ -34,3 +34,16 @@ func (e *GenApi) GenCode(rc *ctx.ReqCtx) {
|
||||
tableId := ginx.PathParamInt(rc.GinCtx, "tableId")
|
||||
gen.GenCode(int64(tableId))
|
||||
}
|
||||
|
||||
// @Summary 配置生成
|
||||
// @Description 生成API和菜单
|
||||
// @Tags 工具 / 生成工具
|
||||
// @Param tableId path int true "tableId"
|
||||
// @Success 200 {string} string "{"code": 200, "data": [...]}"
|
||||
// @Router /develop/code/gen/configure/{tableId} [get]
|
||||
// @Security X-TOKEN
|
||||
func (e *GenApi) GenConfigure(rc *ctx.ReqCtx) {
|
||||
tableId := ginx.PathParamInt(rc.GinCtx, "tableId")
|
||||
menuId := ginx.QueryInt(rc.GinCtx, "menuId", 0)
|
||||
gen.GenConfigure(tableId, menuId)
|
||||
}
|
||||
|
||||
@@ -13,11 +13,8 @@ type DevGenTable struct {
|
||||
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"` // 备注
|
||||
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"`
|
||||
|
||||
@@ -2,11 +2,14 @@ package gen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/kakuilan/kgo"
|
||||
"log"
|
||||
"os"
|
||||
"pandax/apps/develop/entity"
|
||||
"pandax/apps/develop/services"
|
||||
sysEntity "pandax/apps/system/entity"
|
||||
sysServices "pandax/apps/system/services"
|
||||
"pandax/base/biz"
|
||||
"pandax/base/config"
|
||||
"pandax/base/global"
|
||||
@@ -411,8 +414,8 @@ func GenCode(tableId int64) {
|
||||
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)
|
||||
kgo.KFile.Mkdir(config.Conf.Gen.Frontpath+"/views/"+tab.PackageName+"/"+tab.BusinessName+"/", os.ModePerm)
|
||||
kgo.KFile.Mkdir(config.Conf.Gen.Frontpath+"/views/"+tab.PackageName+"/"+tab.BusinessName+"/"+"component"+"/", os.ModePerm)
|
||||
|
||||
var b1 bytes.Buffer
|
||||
err = t1.Execute(&b1, tab)
|
||||
@@ -433,7 +436,105 @@ func GenCode(tableId int64) {
|
||||
kgo.KFile.WriteFile("./apps/"+tab.PackageName+"/services/"+tab.TableName+".go", b2.Bytes())
|
||||
kgo.KFile.WriteFile("./apps/"+tab.PackageName+"/api/"+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"+"/editModule.vue", b7.Bytes())
|
||||
kgo.KFile.WriteFile(config.Conf.Gen.Frontpath+"/api/"+tab.PackageName+"/"+tab.BusinessName+".js", b5.Bytes())
|
||||
kgo.KFile.WriteFile(config.Conf.Gen.Frontpath+"/views/"+tab.PackageName+"/"+tab.BusinessName+"/index.vue", b6.Bytes())
|
||||
kgo.KFile.WriteFile(config.Conf.Gen.Frontpath+"/views/"+tab.PackageName+"/"+tab.BusinessName+"/"+"component"+"/editModule.vue", b7.Bytes())
|
||||
}
|
||||
|
||||
// GenConfigure 生成菜单,api
|
||||
func GenConfigure(tableId, parentId int) {
|
||||
tab := services.DevGenTableModelDao.FindOne(entity.DevGenTable{TableId: int64(tableId)}, false)
|
||||
|
||||
//生成菜单 一个菜单 三个按钮
|
||||
component := "Layout"
|
||||
if parentId != 0 {
|
||||
component = fmt.Sprintf("/%s/%s/index", tab.PackageName, tab.BusinessName)
|
||||
}
|
||||
menu := sysEntity.SysMenu{
|
||||
ParentId: int64(parentId),
|
||||
MenuName: tab.TableComment,
|
||||
MenuType: "C",
|
||||
Sort: 1,
|
||||
Icon: "elementSetting",
|
||||
Path: fmt.Sprintf("/%s/%s", tab.PackageName, tab.BusinessName),
|
||||
Component: component,
|
||||
IsIframe: "1",
|
||||
IsHide: "0",
|
||||
IsKeepAlive: "1",
|
||||
IsAffix: "1",
|
||||
Permission: fmt.Sprintf("%s:%s:list", tab.PackageName, tab.BusinessName),
|
||||
Status: "0",
|
||||
CreateBy: "admin",
|
||||
}
|
||||
insert := sysServices.SysMenuModelDao.Insert(menu)
|
||||
log.Println("insert", insert.MenuId)
|
||||
//新增按钮
|
||||
menuA := sysEntity.SysMenu{
|
||||
ParentId: insert.MenuId,
|
||||
MenuName: "新增" + tab.TableComment,
|
||||
MenuType: "F",
|
||||
Sort: 1,
|
||||
Permission: fmt.Sprintf("%s:%s:add", tab.PackageName, tab.BusinessName),
|
||||
Status: "0",
|
||||
CreateBy: "admin",
|
||||
}
|
||||
go sysServices.SysMenuModelDao.Insert(menuA)
|
||||
//修改按钮
|
||||
menuE := sysEntity.SysMenu{
|
||||
ParentId: insert.MenuId,
|
||||
MenuName: "修改" + tab.TableComment,
|
||||
MenuType: "F",
|
||||
Sort: 2,
|
||||
Permission: fmt.Sprintf("%s:%s:edit", tab.PackageName, tab.BusinessName),
|
||||
Status: "0",
|
||||
CreateBy: "admin",
|
||||
}
|
||||
go sysServices.SysMenuModelDao.Insert(menuE)
|
||||
//删除按钮
|
||||
menuD := sysEntity.SysMenu{
|
||||
ParentId: insert.MenuId,
|
||||
MenuName: "删除" + tab.TableComment,
|
||||
MenuType: "F",
|
||||
Sort: 3,
|
||||
Permission: fmt.Sprintf("%s:%s:delete", tab.PackageName, tab.BusinessName),
|
||||
Status: "0",
|
||||
CreateBy: "admin",
|
||||
}
|
||||
go sysServices.SysMenuModelDao.Insert(menuD)
|
||||
//生成api
|
||||
apiL := sysEntity.SysApi{
|
||||
Path: fmt.Sprintf("/%s/%s/list", tab.PackageName, tab.BusinessName),
|
||||
Description: fmt.Sprintf("查询%s列表(分页)", tab.TableComment),
|
||||
ApiGroup: tab.BusinessName,
|
||||
Method: "GET",
|
||||
}
|
||||
go sysServices.SysApiModelDao.Insert(apiL)
|
||||
apiG := sysEntity.SysApi{
|
||||
Path: fmt.Sprintf("/%s/%s/:%s", tab.PackageName, tab.BusinessName, tab.PkJsonField),
|
||||
Description: fmt.Sprintf("获取%s信息", tab.TableComment),
|
||||
ApiGroup: tab.BusinessName,
|
||||
Method: "GET",
|
||||
}
|
||||
go sysServices.SysApiModelDao.Insert(apiG)
|
||||
apiA := sysEntity.SysApi{
|
||||
Path: fmt.Sprintf("/%s/%s", tab.PackageName, tab.BusinessName),
|
||||
Description: fmt.Sprintf("添加%s信息", tab.TableComment),
|
||||
ApiGroup: tab.BusinessName,
|
||||
Method: "POST",
|
||||
}
|
||||
go sysServices.SysApiModelDao.Insert(apiA)
|
||||
apiE := sysEntity.SysApi{
|
||||
Path: fmt.Sprintf("/%s/%s", tab.PackageName, tab.BusinessName),
|
||||
Description: fmt.Sprintf("修改%s信息", tab.TableComment),
|
||||
ApiGroup: tab.BusinessName,
|
||||
Method: "PUT",
|
||||
}
|
||||
go sysServices.SysApiModelDao.Insert(apiE)
|
||||
apiD := sysEntity.SysApi{
|
||||
Path: fmt.Sprintf("/%s/%s/:%s", tab.PackageName, tab.BusinessName, tab.PkJsonField),
|
||||
Description: fmt.Sprintf("删除%s信息", tab.TableComment),
|
||||
ApiGroup: tab.BusinessName,
|
||||
Method: "DELETE",
|
||||
}
|
||||
go sysServices.SysApiModelDao.Insert(apiD)
|
||||
}
|
||||
|
||||
@@ -26,6 +26,6 @@ func InitGenRouter(router *gin.RouterGroup) {
|
||||
|
||||
genConfigureLog := ctx.NewLogInfo("生成配置")
|
||||
gen.GET("configure/:tableId", func(c *gin.Context) {
|
||||
ctx.NewReqCtxWithGin(c).WithLog(genConfigureLog).Handle(genApi.GenCode)
|
||||
ctx.NewReqCtxWithGin(c).WithLog(genConfigureLog).Handle(genApi.GenConfigure)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"pandax/base/config"
|
||||
"pandax/base/global"
|
||||
"pandax/base/utils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -178,7 +177,7 @@ func (m *devGenTableModelImpl) Update(data entity.DevGenTable) *entity.DevGenTab
|
||||
tableMap := make(map[string]*entity.DevGenTable)
|
||||
if len(tableNames) > 0 {
|
||||
err = global.Db.Table(m.table).Where("table_name in (?)", tableNames).Find(&tables).Error
|
||||
biz.ErrIsNil(err, err.Error())
|
||||
biz.ErrIsNil(err, "关联表不存在")
|
||||
for i := range tables {
|
||||
tableMap[tables[i].TableName] = &tables[i]
|
||||
}
|
||||
@@ -189,17 +188,9 @@ func (m *devGenTableModelImpl) Update(data entity.DevGenTable) *entity.DevGenTab
|
||||
t, ok := tableMap[data.Columns[i].LinkTableName]
|
||||
if ok {
|
||||
data.Columns[i].LinkTableClass = t.ClassName
|
||||
data.Columns[i].LinkTablePackage = t.ModuleName
|
||||
} else {
|
||||
tableNameList := strings.Split(data.Columns[i].LinkTableName, "_")
|
||||
data.Columns[i].LinkTableClass = ""
|
||||
data.Columns[i].LinkTablePackage = ""
|
||||
for a := 0; a < len(tableNameList); a++ {
|
||||
strStart := string([]byte(tableNameList[a])[:1])
|
||||
strEnd := string([]byte(tableNameList[a])[1:])
|
||||
data.Columns[i].LinkTableClass += strings.ToUpper(strStart) + strEnd
|
||||
data.Columns[i].LinkTablePackage += strings.ToLower(strStart) + strings.ToLower(strEnd)
|
||||
}
|
||||
data.Columns[i].LinkTablePackage = t.PackageName
|
||||
data.Columns[i].LinkLabelId = t.PkColumn
|
||||
data.Columns[i].LinkLabelName = t.PkGoField
|
||||
}
|
||||
}
|
||||
DevTableColumnModelDao.Update(data.Columns[i])
|
||||
|
||||
Reference in New Issue
Block a user