private List <DepartmentTreeModel> process2TreeModel(List <DepartmentDto> source, DepartmentTreeModel curRootNode, int did)
        {
            List <DepartmentTreeModel> resultlist = new List <DepartmentTreeModel>();

            if (source != null && source.Count() > 0)//出口1
            {
                if (curRootNode == null)
                {
                    return(resultlist);//出口2
                }
                //当前节点的直接子节点list
                IEnumerable <DepartmentDto> sublist = source.Where(m => m.ParentID == curRootNode.id);
                if (sublist == null || sublist.Count() == 0)
                {
                    return(resultlist);
                }
                // var pmodel=sublist.FirstOrDefault();

                foreach (var dep in sublist)//出口3
                {
                    if (did != dep.DepartmentID)
                    {
                        DepartmentTreeModel t = new DepartmentTreeModel()
                        {
                            id       = dep.DepartmentID,
                            text     = dep.Name,
                            children = process2TreeModel(source, dep, did)
                        };
                        curRootNode.children.Add(t);
                    }
                }
                return(curRootNode.children);
            }
            return(new List <DepartmentTreeModel>());
        }
        //返回编辑时的树结构,弃用
        public ActionResult GetMyDepartment(string did)
        {
            var result = new StandardJsonResult <List <DepartmentTreeModel> >();

            result.Try(() => {
                var service = ML.BC.Infrastructure.Ioc.GetService <IEnterpriseDepartmentManagementService>();
                List <DepartmentDto> list = service.GetMyDepartment(BCSession.User.EnterpriseID, BCSession.User.DepartmentID);
                list[0].ParentID          = 0;
                if (list == null)
                {
                    throw new ArgumentNullException("func list get from service is null!");
                }
                DepartmentTreeModel root = new DepartmentTreeModel();
                root.id       = list[0].DepartmentID;
                root.text     = list[0].Name;
                root.children = new List <DepartmentTreeModel>();
                root.children = process2TreeModel(list, root, Convert.ToInt32(did));
                result.Value  = new List <DepartmentTreeModel>();
                result.Value.Add(root);
            });
            return(Json(result.Value, JsonRequestBehavior.AllowGet));
        }
Exemple #3
0
        //当前企业的部门列表
        public ActionResult GetDepartList()
        {
            string _enterpriseID = GetSession().User.EnterpriseID;
            var    result        = new StandardJsonResult <List <DepartmentTreeModel> >();

            result.Try(() =>
            {
                //企业所有部门

                List <DepartmentDto> list = _entDepartService.GetAllAvaliableDepartmentByEnterpriseId(_enterpriseID);
                //DepartmentTreeModel root = list.Where(obj => obj.ParentID == 0).FirstOrDefault();// 根节点
                //virtual root
                DepartmentTreeModel root = new DepartmentTreeModel
                {
                    id       = 0,
                    text     = "",
                    children = new List <DepartmentTreeModel>()
                };
                //处理成树形结构 递归入口
                root.children = process2TreeModel(list, root);

                result.Value = new List <DepartmentTreeModel>();
                result.Value = root.children;
            });
            if (result.Success)
            {
                return(new OringinalJsonResult <List <DepartmentTreeModel> > {
                    Value = result.Value
                });
            }
            else
            {
                return(new OringinalJsonResult <List <DepartmentTreeModel> > {
                    Value = new List <DepartmentTreeModel>()
                });
            }
        }