Skip to content

Commit

Permalink
feat: 自动化树形结构完结
Browse files Browse the repository at this point in the history
  • Loading branch information
piexlMax(奇淼 committed Nov 19, 2024
1 parent f684ce1 commit 321201e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 23 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(in *T, children T)
SetChildren(children T)
GetID() int
GetParentID() int
}
12 changes: 6 additions & 6 deletions server/resource/package/server/model/model.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type {{.StructName}} struct {
DeletedBy uint `gorm:"column:deleted_by;comment:删除者"`
{{- end }}
{{- if .IsTree }}
Children []{{.StructName}} `json:"children" gorm:"-"` //子节点
Children []*{{.StructName}} `json:"children" gorm:"-"` //子节点
ParentID int `json:"parentID" gorm:"column:parent_id;comment:父节点"`
{{- end }}
{{- end }}
Expand All @@ -88,22 +88,22 @@ func ({{.StructName}}) TableName() string {

{{if .IsTree }}
// GetChildren 实现TreeNode接口
func (s {{.StructName}}) GetChildren() []{{.StructName}} {
func (s *{{.StructName}}) GetChildren() []*{{.StructName}} {
return s.Children
}

// SetChildren 实现TreeNode接口
func (s {{.StructName}}) SetChildren(in *{{.StructName}},children {{.StructName}}) {
in.Children = append(in.Children, children)
func (s *{{.StructName}}) SetChildren(children *{{.StructName}}) {
s.Children = append(s.Children, children)
}

// GetID 实现TreeNode接口
func (s {{.StructName}}) GetID() int {
func (s *{{.StructName}}) GetID() int {
return int({{if not .GvaModel}}*{{- end }}s.{{.PrimaryField.FieldName}})
}

// GetParentID 实现TreeNode接口
func (s {{.StructName}}) GetParentID() int {
func (s *{{.StructName}}) GetParentID() int {
return s.ParentID
}
{{ end }}
Expand Down
8 changes: 3 additions & 5 deletions server/resource/package/server/service/service.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,13 @@ import (
{{- if not .IsTree}}
{{.Package}}Req "{{.Module}}/model/{{.Package}}/request"
{{- else }}
"{{.Module}}/utils"
"errors"
{{- end }}
{{- if .AutoCreateResource }}
"gorm.io/gorm"
{{- end}}
{{- end }}
{{- if .IsTree }}
"{{.Module}}/utils"
{{- end }}
)

type {{.StructName}}Service struct {}
Expand Down Expand Up @@ -151,10 +149,10 @@ 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}}
var {{.Abbreviation}}s []*{{.Package}}.{{.StructName}}

err = db.Find(&{{.Abbreviation}}s).Error

Expand Down
14 changes: 7 additions & 7 deletions server/resource/plugin/server/model/model.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type {{.StructName}} struct {
DeletedBy uint `gorm:"column:deleted_by;comment:删除者"`
{{- end }}
{{- if .IsTree }}
Children []{{.StructName}} `json:"children" gorm:"-"` //子节点
Children []*{{.StructName}} `json:"children" gorm:"-"` //子节点
ParentID int `json:"parentID" gorm:"column:parent_id;comment:父节点"`
{{- end }}
{{- end }}
Expand All @@ -86,24 +86,24 @@ func ({{.StructName}}) TableName() string {
{{ end }}


{{ if .IsTree }}
{{if .IsTree }}
// GetChildren 实现TreeNode接口
func (s {{.StructName}}) GetChildren() []{{.StructName}} {
func (s *{{.StructName}}) GetChildren() []*{{.StructName}} {
return s.Children
}

// SetChildren 实现TreeNode接口
func (s {{.StructName}}) SetChildren(in *{{.StructName}},children {{.StructName}}) {
in.Children = append(in.Children, children)
func (s *{{.StructName}}) SetChildren(children *{{.StructName}}) {
s.Children = append(s.Children, children)
}

// GetID 实现TreeNode接口
func (s {{.StructName}}) GetID() int {
func (s *{{.StructName}}) GetID() int {
return int({{if not .GvaModel}}*{{- end }}s.{{.PrimaryField.FieldName}})
}

// GetParentID 实现TreeNode接口
func (s {{.StructName}}) GetParentID() int {
func (s *{{.StructName}}) GetParentID() int {
return s.ParentID
}
{{ end }}
Expand Down
4 changes: 2 additions & 2 deletions server/resource/plugin/server/service/service.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ func (s *{{.Abbreviation}}) Get{{.StructName}}({{.PrimaryField.FieldJson}} strin
{{- if .IsTree }}
// Get{{.StructName}}InfoList 分页获取{{.Description}}记录,Tree模式下不添加分页和搜索
// Author [yourname](https://github.com/yourname)
func (s *{{.Abbreviation}}) Get{{.StructName}}InfoList() (list []model.{{.StructName}},err error) {
func (s *{{.Abbreviation}}) Get{{.StructName}}InfoList() (list []*model.{{.StructName}},err error) {
// 创建db
db := {{$db}}.Model(&model.{{.StructName}}{})
var {{.Abbreviation}}s []model.{{.StructName}}
var {{.Abbreviation}}s []*model.{{.StructName}}

err = db.Find(&{{.Abbreviation}}s).Error

Expand Down
3 changes: 1 addition & 2 deletions server/utils/fmt_plus.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ func BuildTree[T common.TreeNode[T]](nodes []T) []T {
for i := range nodes {
if nodes[i].GetParentID() != 0 {
parent := nodeMap[nodes[i].GetParentID()]
parent.SetChildren(&parent, nodes[i])
nodeMap[nodes[i].GetParentID()] = parent
parent.SetChildren(nodes[i])
}
}

Expand Down

0 comments on commit 321201e

Please sign in to comment.