Skip to content

Commit

Permalink
feat: 调整树结构泛型和生成方法
Browse files Browse the repository at this point in the history
  • Loading branch information
piexlMax(奇淼 committed Nov 18, 2024
1 parent 8ddc545 commit 062f798
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 50 deletions.
2 changes: 1 addition & 1 deletion server/model/common/basetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions server/model/system/request/sys_auto_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
Expand Down
20 changes: 10 additions & 10 deletions server/resource/package/server/model/model.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
}
Expand All @@ -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 }}

Expand Down
22 changes: 2 additions & 20 deletions server/resource/package/server/service/service.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}记录
Expand Down
6 changes: 3 additions & 3 deletions server/resource/package/web/view/table.vue.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ getDataSourceFunc()
>
<el-table-column type="selection" width="55" />
{{ if .GvaModel }}
<el-table-column align="left" label="日期" prop="createdAt" width="180">
<el-table-column align="left" label="日期" prop="createdAt" {{- if .IsTree }}min-{{- end }}width="180">
<template #default="scope">{{ "{{ formatDate(scope.row.CreatedAt) }}" }}</template>
</el-table-column>
{{ end }}
Expand Down Expand Up @@ -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
Expand Down
32 changes: 16 additions & 16 deletions server/utils/fmt_plus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions web/src/view/systemTools/autoCode/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,22 @@
<el-checkbox v-model="form.autoCreateResource" />
</el-form-item>
</el-col>
<el-col :span="3">
<el-form-item>
<template #label>
<el-tooltip
content="注:会自动创建parentID来进行父子关系关联,仅支持主键为int类型"
placement="bottom"
effect="light"
>
<div>
树型结构 <el-icon><QuestionFilled /></el-icon>
</div>
</el-tooltip>
</template>
<el-checkbox v-model="form.isTree" />
</el-form-item>
</el-col>
<el-col :span="3">
<el-form-item>
<template #label>
Expand Down Expand Up @@ -1024,6 +1040,7 @@
gvaModel: true,
autoCreateResource: false,
onlyTemplate: false,
isTree: false,
fields: []
})
const rules = ref({
Expand Down Expand Up @@ -1445,6 +1462,7 @@
gvaModel: true,
autoCreateResource: false,
onlyTemplate: false,
isTree: false,
fields: []
}
await nextTick()
Expand Down

0 comments on commit 062f798

Please sign in to comment.