/// <summary>
/// 将所有部门根据上下级关系显示在一个列表中,用于展示到下拉框中
/// </summary>
/// <returns></returns>
public async Task<List<Department>> GetTreeDepartment()
{
await Task.Yield();
var listAll = GetAllDepartment();
List<Department> tree = new List<Department>();
//一级
var list_01 = listAll.Where(x => string.IsNullOrWhiteSpace(x.Parent_id));
Action<Department, List<Department>> func = null;
func = (q, list) =>
{
//查找子集
var child = listAll.Where(x => x.Parent_id == q.Id);
//是否有子集,1=有,0=无
q.E2=child?.Count()>0? "1":"0";
list.Add(q);
if (child != null && child.Count() > 0)
{
foreach (var item in child)
{
//计算层级
item.E7 = q.E7 + 1;
func(item, list);
}
}
};
foreach (var item in list_01)
{
//计算层级
item.E7 = 1;
func(item, tree);
}
return tree;
}
/// <summary>
/// 部门
/// </summary>
public class Department : BaseEntity
{
/// <summary>
/// 主键
/// </summary>
[Key]
public string Id { get; set; }
/// <summary>
/// 部门名称
/// </summary>
public string Department_name { get; set; }
/// <summary>
/// 父级id
/// </summary>
public string Parent_id { get; set; }
/// <summary>
/// 排序
/// </summary>
public int Sno { get; set; }
/// <summary>
/// 是否启用,1=启用,0=禁用
/// </summary>
public byte? State { get; set; }
}
/// <summary>
/// 扩展字段
/// </summary>
public class BaseEntity
{
/// <summary>
/// 扩展字段
/// </summary>
[NotMapped]
public string E1 { get; set; }
/// <summary>
/// 扩展字段
/// </summary>
[NotMapped]
public string E2 { get; set; }
/// <summary>
/// 扩展字段
/// </summary>
[NotMapped]
public string E3 { get; set; }
[NotMapped]
public string E4 { get; set; }
[NotMapped]
public string E5 { get; set; }
[NotMapped]
public string E6 { get; set; }
/// <summary>
/// 扩展字段
/// </summary>
[NotMapped]
public int E7 { get; set; }
[NotMapped]
public List<string> List1 { get; set; } = new List<string>();
}