From 062f7981ddb76e7e1b35d0b9e4b95a3332be4f83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?piexlMax=28=E5=A5=87=E6=B7=BC?= Date: Mon, 18 Nov 2024 17:22:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4=E6=A0=91=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E6=B3=9B=E5=9E=8B=E5=92=8C=E7=94=9F=E6=88=90=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/model/common/basetypes.go | 2 +- server/model/system/request/sys_auto_code.go | 1 + .../package/server/model/model.go.tpl | 20 ++++++------ .../package/server/service/service.go.tpl | 22 ++----------- .../resource/package/web/view/table.vue.tpl | 6 ++-- server/utils/fmt_plus.go | 32 +++++++++---------- web/src/view/systemTools/autoCode/index.vue | 18 +++++++++++ 7 files changed, 51 insertions(+), 50 deletions(-) diff --git a/server/model/common/basetypes.go b/server/model/common/basetypes.go index 3966a4cc75..3c27ed0242 100644 --- a/server/model/common/basetypes.go +++ b/server/model/common/basetypes.go @@ -37,7 +37,7 @@ func (m *JSONMap) Scan(value interface{}) error { type TreeNode[T any] interface { GetChildren() []T - SetChildren(children []T) + SetChildren(in *T, children T) GetID() int GetParentID() int } diff --git a/server/model/system/request/sys_auto_code.go b/server/model/system/request/sys_auto_code.go index ef70b8fcba..9fb943759f 100644 --- a/server/model/system/request/sys_auto_code.go +++ b/server/model/system/request/sys_auto_code.go @@ -27,6 +27,7 @@ type AutoCode struct { AutoCreateMenuToSql bool `json:"autoCreateMenuToSql" example:"false"` // 是否自动创建menu AutoCreateBtnAuth bool `json:"autoCreateBtnAuth" example:"false"` // 是否自动创建按钮权限 OnlyTemplate bool `json:"onlyTemplate" example:"false"` // 是否只生成模板 + IsTree bool `json:"isTree" example:"false"` // 是否树形结构 IsAdd bool `json:"isAdd" example:"false"` // 是否新增 Fields []*AutoCodeField `json:"fields"` Module string `json:"-"` diff --git a/server/resource/package/server/model/model.go.tpl b/server/resource/package/server/model/model.go.tpl index 701e6b6cb9..ccb7f1dde3 100644 --- a/server/resource/package/server/model/model.go.tpl +++ b/server/resource/package/server/model/model.go.tpl @@ -73,8 +73,8 @@ type {{.StructName}} struct { DeletedBy uint `gorm:"column:deleted_by;comment:删除者"` {{- end }} {{- if .IsTree }} - Children []{{.StructName}} `json:"children"` - ParentID int `json:"parentID" gorm:"column:parent_id;comment:父节点ID"` + Children []{{.StructName}} `json:"children" gorm:"-"` //子节点 + ParentID int `json:"parentID" gorm:"column:parent_id;comment:父节点"` {{- end }} {{- end }} } @@ -88,23 +88,23 @@ func ({{.StructName}}) TableName() string { {{if .IsTree }} // GetChildren 实现TreeNode接口 -func ({{.StructName}}) GetChildren() []{{.StructName}} { - return {{.StructName}}.Children +func (s {{.StructName}}) GetChildren() []{{.StructName}} { + return s.Children } // SetChildren 实现TreeNode接口 -func ({{.StructName}}) SetChildren(children []{{.StructName}}) { - {{.StructName}}.Children = children +func (s {{.StructName}}) SetChildren(in *{{.StructName}},children {{.StructName}}) { + in.Children = append(in.Children, children) } // GetID 实现TreeNode接口 -func ({{.StructName}}) GetID() int { - return {{- if .GvaModel}}*{{- end }}{{.StructName}}.{{.PrimaryField.FieldName}} +func (s {{.StructName}}) GetID() int { + return int({{if not .GvaModel}}*{{- end }}s.{{.PrimaryField.FieldName}}) } // GetParentID 实现TreeNode接口 -func ({{.StructName}}) GetParentID() int { - return {{.StructName}}.ParentID +func (s {{.StructName}}) GetParentID() int { + return s.ParentID } {{ end }} diff --git a/server/resource/package/server/service/service.go.tpl b/server/resource/package/server/service/service.go.tpl index d1e2b098a2..58e0bd00b1 100644 --- a/server/resource/package/server/service/service.go.tpl +++ b/server/resource/package/server/service/service.go.tpl @@ -136,32 +136,14 @@ func ({{.Abbreviation}}Service *{{.StructName}}Service)Get{{.StructName}}({{.Pri {{- if .IsTree }} // Get{{.StructName}}InfoList 分页获取{{.Description}}记录,Tree模式下不添加分页和搜索 // Author [yourname](https://github.com/yourname) -func ({{.Abbreviation}}Service *{{.StructName}}Service)Get{{.StructName}}InfoList() (list []{{.Package}}.{{.StructName}} err error) { +func ({{.Abbreviation}}Service *{{.StructName}}Service)Get{{.StructName}}InfoList() (list []{{.Package}}.{{.StructName}},err error) { // 创建db db := {{$db}}.Model(&{{.Package}}.{{.StructName}}{}) var {{.Abbreviation}}s []{{.Package}}.{{.StructName}} - // 排序条件 - {{- if .NeedSort}} - var OrderStr string - orderMap := make(map[string]bool) - {{- range .Fields}} - {{- if .Sort}} - orderMap["{{.ColumnName}}"] = true - {{- end}} - {{- end}} - if orderMap[info.Sort] { - OrderStr = info.Sort - if info.Order == "descending" { - OrderStr = OrderStr + " desc" - } - db = db.Order(OrderStr) - } - {{- end}} - err = db.Find(&{{.Abbreviation}}s).Error - return utils.BuildTree(&{{.Abbreviation}}s), total, err + return utils.BuildTree({{.Abbreviation}}s), err } {{- else }} // Get{{.StructName}}InfoList 分页获取{{.Description}}记录 diff --git a/server/resource/package/web/view/table.vue.tpl b/server/resource/package/web/view/table.vue.tpl index 6b87da66bc..74b2a6cd51 100644 --- a/server/resource/package/web/view/table.vue.tpl +++ b/server/resource/package/web/view/table.vue.tpl @@ -540,7 +540,7 @@ getDataSourceFunc() > {{ if .GvaModel }} - + {{ end }} @@ -1185,9 +1185,9 @@ const delete{{.StructName}}Func = async (row) => { const dialogFormVisible = ref(false) // 打开弹窗 -const openDialog = ({{- if .Tree -}}row{{- end -}}) => { +const openDialog = ({{- if .IsTree -}}row{{- end -}}) => { type.value = 'create' - {{- if .Tree }} + {{- if .IsTree }} formData.value.parentID = row ? row.{{.PrimaryField.FieldJson}} : 0 {{- end }} dialogFormVisible.value = true diff --git a/server/utils/fmt_plus.go b/server/utils/fmt_plus.go index 2b6d877016..6b24010104 100644 --- a/server/utils/fmt_plus.go +++ b/server/utils/fmt_plus.go @@ -85,25 +85,25 @@ func RandomInt(min, max int) int { // BuildTree 用于构建一个树形结构 func BuildTree[T common.TreeNode[T]](nodes []T) []T { nodeMap := make(map[int]T) - var roots []T - // 创建一个基本map - for _, node := range nodes { - nodeMap[node.GetID()] = node + for i := range nodes { + nodeMap[nodes[i].GetID()] = nodes[i] } - // 递归node给map增加children - for _, node := range nodes { - if node.GetParentID() == 0 { - // 捕获根节点 - roots = append(roots, node) - } else { - parent := nodeMap[node.GetParentID()] - children := parent.GetChildren() - children = append(children, node) - parent.SetChildren(children) - nodeMap[parent.GetID()] = parent + for i := range nodes { + if nodes[i].GetParentID() != 0 { + parent := nodeMap[nodes[i].GetParentID()] + parent.SetChildren(&parent, nodes[i]) + nodeMap[nodes[i].GetParentID()] = parent + } + } + + var rootNodes []T + + for i := range nodeMap { + if nodeMap[i].GetParentID() == 0 { + rootNodes = append(rootNodes, nodeMap[i]) } } - return roots + return rootNodes } diff --git a/web/src/view/systemTools/autoCode/index.vue b/web/src/view/systemTools/autoCode/index.vue index 1a9055044e..6c0df6e514 100644 --- a/web/src/view/systemTools/autoCode/index.vue +++ b/web/src/view/systemTools/autoCode/index.vue @@ -400,6 +400,22 @@ + + + + + +