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.

95 lines
2.6 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 BasePositionService
import (
BasePosition "dsSupport/MyModel/AccessSystem/AccessSystemProto/BasePositionProto"
"dsSupport/Utils/CommonUtil"
"encoding/json"
)
func TreePositionInfo() (*BasePosition.Reply, error) {
Reply, err := TreeBasePosition()
Reply.List = convertPositionTree(Reply.List)
return Reply, err
}
func convertPositionTree(listStr string) string {
list := CommonUtil.ConvertJsonStringToMapArray(listStr)
ListOrgNode := make([]*OrgNode, 0)
for i := range list {
record := list[i]
//添加数据
a := OrgNode{
Id: record["id"].(string),
NodeName: record["node_name"].(string),
ParentId: record["parent_id"].(string),
OrgType: record["org_type"].(float64),
AreaLevel: record["area_level"].(float64),
Xxbxlxm: record["xxbxlxm"].(string),
MaintainId: record["maintain_id"].(string),
SortId: record["sort_id"].(float64),
}
ListOrgNode = append(ListOrgNode, &a)
}
//结构体的SortId根据数据库获取时使用order by方法获取有序的子元素集合这里就不在代码中进行排序了。
//从根节点开始进行构建,递归调用生成tree
startOrgNode := ListOrgNode[0]
makeTree(ListOrgNode, startOrgNode)
//根据结构体生成json数据
jsonByte, _ := json.Marshal(startOrgNode)
return string(jsonByte)
}
/**
功能部门实体Struct
作者:黄海
时间2020-06-03
*/
type OrgNode struct {
Id string `json:"id"`
NodeName string `json:"node_name"`
ParentId string `json:"parent_id"`
OrgType float64 `json:"org_type"`
AreaLevel float64 `json:"area_level"`
Xxbxlxm string `json:"xxbxlxm"`
MaintainId string `json:"maintain_id"`
SortId float64 `json:"sort_id"`
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.Id {
children = append(children, v)
}
}
if children != nil {
yes = true
}
return
}