package BaseMenuService import ( "dsBaseWeb/Business/BaseMenu/BaseMenuProto" "dsBaseWeb/Utils/CommonUtil" "encoding/json" "strings" ) func PageBaseMenuInfo(appId string, identityId int32) (*BaseMenuProto.Reply, error) { Reply, err := PageBaseMenu(BaseMenuProto.QueryArg{AppId: appId, IdentityId: identityId}) Reply.List = convertPositionTree(Reply.List) return Reply, err } func GetBaseMenuInfo(memuId string) (*BaseMenuProto.Reply, error) { Reply, err := GetBaseMenu(BaseMenuProto.ModelArg{MenuId: memuId}) return Reply, err } func AddBaseMenuInfo(appId string, parentId string, identityId int32, menuName string, menuCode string, menuUrl string, menuIcon string) (*BaseMenuProto.Reply, error) { Reply, err := AddBaseMenu(BaseMenuProto.ModelArg{AppId: appId, ParentId: parentId, IdentityId: identityId, MenuName: menuName, MenuCode: menuCode, MenuUrl: menuUrl, MenuIcon: menuIcon}) return Reply, err } func DeleteBaseMenuInfo(ids string) (*BaseMenuProto.Reply, error) { idsArr := strings.Split(ids, ",") Reply, err := DeleteBaseMenu(BaseMenuProto.DeleteIdsArg{Ids: idsArr}) return Reply, err } func UpdateBaseMenuInfo(menuId string, menuName string, menuCode string, menuUrl string, menuIcon string) (*BaseMenuProto.Reply, error) { Reply, err := UpdateBaseMenu(BaseMenuProto.ModelArg{MenuId: menuId, MenuName: menuName, MenuCode: menuCode, MenuUrl: menuUrl, MenuIcon: menuIcon}) return Reply, err } func SetMenuSortInfo(menuId string, direction int32) (*BaseMenuProto.Reply, error) { Reply, err := SetMenuSort(BaseMenuProto.SetMenuSortArg{MenuId: menuId, Direction: direction}) 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{ MenuId: record["menu_id"].(string), MenuName: record["menu_name"].(string), ParentId: record["parent_id"].(string), MenuCode: record["menu_code"].(string), MenuIcon: record["menu_icon"].(string), AppId: record["app_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 { MenuId string `json:"menu_id"` MenuName string `json:"menu_name"` ParentId string `json:"parent_id"` MenuCode string `json:"menu_code"` MenuIcon string `json:"menu_icon"` AppId string `json:"app_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.MenuId { children = append(children, v) } } if children != nil { yes = true } return }