GetOrganizations

public async Task<CommonResponse> GetOrganizations()
        {
            //throw new NotImplementedException();
            var result = new CommonResponse();
            var rootNode = new JArray();
            var permissionsList = await _repository.QueryAsync<OrganizationEntity>(d => d.Is_Deleted == false);
            //GetOrganization(permissionsList, 0, "", rootNode);
            GetOrganization(permissionsList, "2c8b408c-09ca-4cc0-aea3-8a34d68a6c3f",rootNode);

            File.WriteAllText(@"C:\Users\l\Downloads\organization.json", JsonConvert.SerializeObject(rootNode, Formatting.Indented));

            result.SetSuccess(rootNode);
            return result;
        }

        public void GetOrganization(List<OrganizationEntity> organizationList, int index, string parenid, JArray root)
        {
            if (index >= organizationList.Count)
            {
                return;
            }

            var entity = organizationList[index];
            if (!string.IsNullOrEmpty(parenid))   //子菜单
            {
                entity = organizationList.Where(d => d.Id == parenid).FirstOrDefault();
            }
            else if (index == 0 && !string.IsNullOrEmpty(entity.Parent_id))       //首次不是一级菜单
            {
                GetOrganization(organizationList, ++index, "", root);
                return;
            }

            var node = new JObject();
            node.Add("id", entity.Id);
            //node.Add("path", entity.Path);
            node.Add("name", entity.Name);
            //node.Add("component", entity.Component);
            //if (!string.IsNullOrEmpty(entity.Redirect))
            //{
            //    node.Add("redirect", entity.Redirect);
            //}
            //var node2 = new JObject();
            //node2.Add("title", entity.Title);
            //node2.Add("icon", entity.Icon);
            //node.Add("meta", node2);
            //node.Add("ability", new JArray());
            var childeData = new JArray();
            var childeIDs = organizationList.Where(d => d.Parent_id == entity.Id).Select(d => d.Id).ToList();
            if (childeIDs.Count > 0)
            {
                foreach (var childID in childeIDs)
                {
                    GetOrganization(organizationList, 0, childID, childeData);
                }
                node.Add("children", childeData);
            }
            root.Add(node);
            var currentIndex = organizationList.IndexOf(entity);
            organizationList.RemoveAt(currentIndex); //删除已添加节点项

            if (!string.IsNullOrEmpty(parenid) && childeIDs.Count == 0)  //子菜单下没有子菜单了
                return;
            GetOrganization(organizationList, currentIndex, "", root);
        }


        public void GetOrganization(List<OrganizationEntity> dataList,string parenID, JArray root) 
        {
            if (string.IsNullOrEmpty(parenID))
            {
                return;
            }
            var currentEntity= dataList.Where(d => d.Is_Deleted == false && d.Id == parenID).FirstOrDefault();
            if (currentEntity==null|| currentEntity.Id== currentEntity.Parent_id)
            {
                return;
            }
            var node = new JObject();
            node.Add("name", currentEntity.Name);
            Console.WriteLine("name :"+currentEntity.Name);
            Console.WriteLine("id :"+currentEntity.Id);
            var pids = _repository.Query<OrganizationEntity>(d => d.Is_Deleted == false && d.Parent_id == parenID).Select(d=>d.Id);
            if (pids.Count()>0)
            {
                var childeData = new JArray();
                foreach (var id in pids)
                {
                    GetOrganization(dataList,id, childeData);
                }
                node.Add("children", childeData);
            }

            root.Add(node);
        }

 

posted @ 2021-03-04 15:44  dreamw  阅读(105)  评论(0编辑  收藏  举报