递归查询所有部门和人员树形数据

    /// <summary>
    /// 查询所有部门和人员树形数据
    /// </summary>
    public class Tree
    {
        public long? id { get; set; }
        public string label { get; set; }
        public List<Tree> children { get; set; }
    }
        /// <summary>
        /// 查询所有部门和人员树形数据
        /// </summary>
        /// <returns></returns>
        public List<Tree> GetDepartmentUserTree()
        {
            var DepartmentIdList = _repository.Change<Department>().AsQueryable().ToList();
            return CreateTree(DepartmentIdList, 0);
        }

        public List<Tree> CreateTree(List<Department> DepartmentIdList, long? ParentId)
        {
            List<Tree> nodes = new List<Tree>();
            var children = DepartmentIdList.Where(p => p.ParentId == ParentId).ToList();
            foreach (var item in children)
            {
                Tree tree = new Tree();
                tree.id = item.Id;
                tree.label = item.Name;
                if (item.HasChildren == 1)
                {
                    List<Tree> tr = CreateTree(DepartmentIdList, item.Id);
                    tree.children = tr;
                    nodes.Add(tree);
                }
                else
                {
                    if (_repository.Change<SystemUser>().Where(d => d.DepartmentId == item.Id).Count() > 0)
                    {
                        tree.children = new List<Tree>();
                        var userlist = _repository.Change<SystemUser>().Where(d => d.DepartmentId == item.Id).ToList();
                        foreach (var u in userlist)
                        {
                            tree.children.Add(new Tree { id = u.Id, label = u.Name, children = new List<Tree>() });
                        }
                        nodes.Add(tree);
                    }
                    else
                    {
                        tree.children = new List<Tree>();
                        nodes.Add(tree);
                    }
                }
            }
            return nodes;
        }
    /// <summary>
    /// 部门
    /// </summary>
    [Table("department")]
    public class Department : EntityBase
    {
        /// <summary>
        /// 名称
        /// </summary>
        /// <value></value>
        [Column("name")]
        public string Name { get; set; }

        /// <summary>
        /// 上级部门编号
        /// </summary>
        /// <value></value>
        [Column("parent_id")]
        public long? ParentId { get; set; }

        /// <summary>
        /// 下级部门编号
        /// </summary>
        /// <value></value>
        [Column("superior_id")]
        public string SuperiorId { get; set; }

        /// <summary>
        /// 部门负责人
        /// </summary>
        [Column("principal")]
        public long? Principal { get; set; }

        /// <summary>
        /// 部门负责人姓名
        /// </summary>
        [Column("principal_name")]
        public string PrincipalName { get; set; }

        /// <summary>
        /// 状态 1-启用 2-停用
        /// </summary>
        [Column("status")]
        public int Status { get; set; }

        /// <summary>
        /// 顺序
        /// </summary>
        /// <value></value>
        [Column("sort")]
        public int? Sort { get; set; }

        /// <summary>
        /// 是否有子项 1-有 2-无
        /// </summary>
        [Column("has_children")]
        public int? HasChildren { get; set; }
    }
    /// <summary>
    /// 系统用户
    /// </summary>
    [Table("system_user")]
    public class SystemUser : EntityBase
    {
        /// <summary>
        /// 用户名
        /// </summary>
        [Column("user_name")]
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [Column("password")]
        public string Password { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        [Column("name")]
        public string Name { get; set; }

        /// <summary>
        /// 部门名称
        /// </summary>
        /// <value></value>
        [Column("department_name")]
        public string DepartmentName { get; set; }

        /// <summary>
        /// 部门编号
        /// </summary>
        /// <value></value>
        [Column("department_id")]
        public long? DepartmentId { get; set; }

        /// <summary>
        /// 角色名称
        /// </summary>
        /// <value></value>
        [Column("role_name")]
        public string RoleName { get; set; }

        /// <summary>
        /// 角色编号
        /// </summary>
        /// <value></value>
        [Column("role_id")]
        public long? RoleId { get; set; }

        /// <summary>
        /// 职务名称
        /// </summary>
        /// <value></value>
        [Column("position_name")]
        public string PositionName { get; set; }

        /// <summary>
        /// 职位编号
        /// </summary>
        /// <value></value>
        [Column("position_id")]
        public long? PositionId { get; set; }

        /// <summary>
        /// 最后登录时间
        /// </summary>
        /// <value></value>
        [Column("last_login_time")]
        public long? LastLoginTime { get; set; }

        /// <summary>
        /// 状态 1-正常 2-停用
        /// </summary>
        [Column("status")]
        public int? Status { get; set; }
    }

 

posted @ 2022-07-25 10:41  哪啊哪啊神去村  阅读(5)  评论(0编辑  收藏  举报