You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
3.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package main
import (
"encoding/json"
"fmt"
)
/**
功能部门实体Struct
*/
type OrgNode struct {
OrgId int `json:"org_id"`
ParentId int `json:"parent_id"`
SortId int `json:"sort_id"`
OrgName string `json:"org_name"`
Children []*OrgNode `json:"children,omitempty"`
}
/**
功能:从指定节点开始构建树状结构
作者:黄海
时间2020-06-03
*/
func makeTree(allNode []*OrgNode, node *OrgNode) {
children, _ := haveChild(allNode, node) //是不是有子节点
if children != nil {
node.Children = append(node.Children, children[0:]...) //添加子节点
for _, v := range children { //查询子节点的子节点,并添加到子节点
_, has := haveChild(allNode, v)
if has {
makeTree(allNode, v) //递归添加节点
}
}
}
}
/**
功能:判断是不是有子节点
作者:黄海
时间2020-06-03
*/
func haveChild(AllNode []*OrgNode, node *OrgNode) (children []*OrgNode, yes bool) {
for _, v := range AllNode {
if v.ParentId == node.OrgId {
children = append(children, v)
}
}
if children != nil {
yes = true
}
return
}
/**
功能json转TreeStruct
作者:黄海
时间2020-06-04
*/
func jsonToStruct(jsonData []byte) *OrgNode {
var treeNode *OrgNode
err := json.Unmarshal(jsonData, &treeNode)
if err != nil {
fmt.Println(err)
}
return treeNode
}
/**
功能:递归输出结构体
作者:黄海
时间2020-06-04
*/
func printTree(treeNode *OrgNode) {
for _, v := range treeNode.Children {
fmt.Printf("%d,%d,%s", v.OrgId, v.ParentId, v.OrgName)
if len(v.Children) != 0 {
printTree(v)
}
}
}
func main() {
//申请数组的内存空间
ListOrgNode := make([]*OrgNode, 0)
//添加数据
a := OrgNode{
OrgId: 0,
ParentId: -1,
SortId: 1,
OrgName: "长春市第一中学",
}
ListOrgNode = append(ListOrgNode, &a)
b := OrgNode{
OrgId: 1,
ParentId: 0,
SortId: 1,
OrgName: "一年级组",
}
ListOrgNode = append(ListOrgNode, &b)
c := OrgNode{
OrgId: 2,
ParentId: 1,
SortId: 1,
OrgName: "一年级语文组",
}
ListOrgNode = append(ListOrgNode, &c)
d := OrgNode{
OrgId: 3,
ParentId: 1,
SortId: 2,
OrgName: "一年级数学组",
}
ListOrgNode = append(ListOrgNode, &d)
e := OrgNode{
OrgId: 4,
ParentId: 0,
SortId: 2,
OrgName: "二年级组",
}
ListOrgNode = append(ListOrgNode, &e)
f := OrgNode{
OrgId: 5,
ParentId: 4,
SortId: 1,
OrgName: "二年级语文组",
}
ListOrgNode = append(ListOrgNode, &f)
g := OrgNode{
OrgId: 6,
ParentId: 4,
SortId: 2,
OrgName: "二年级数学组",
}
ListOrgNode = append(ListOrgNode, &g)
h := OrgNode{
OrgId: 7,
ParentId: 6,
SortId: 1,
OrgName: "二年级数学(奥数)组",
}
ListOrgNode = append(ListOrgNode, &h)
i := OrgNode{
OrgId: 8,
ParentId: 4,
SortId: 3,
OrgName: "二年级化学组",
}
ListOrgNode = append(ListOrgNode, &i)
j := OrgNode{
OrgId: 9,
ParentId: 0,
SortId: 3,
OrgName: "三年级组",
}
ListOrgNode = append(ListOrgNode, &j)
/*************************===================================******/
//结构体的SortId根据数据库获取时使用order by方法获取有序的子元素集合这里就不在代码中进行排序了。
//从根节点开始进行构建,递归调用生成tree
startOrgNode := ListOrgNode[0]
makeTree(ListOrgNode, startOrgNode)
//1、根据结构体生成json数据
jsonByte, _ := json.MarshalIndent(startOrgNode, "", "\t")
fmt.Println(string(jsonByte))
//2、将得到的json转回struct
s1 := jsonToStruct(jsonByte)
printTree(s1)
/*************************===================================******/
}