Files
zszq-trs/YLErpWeb/Models/DepartmentViewModel.cs
2024-05-09 14:06:26 +08:00

56 lines
1.6 KiB
C#
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace YLErp.Web.Models
{
public class DepartmentViewModel
{
public static List<SelectListItem> GetDepartmentListByName(string depName)
{
var dep = DepartmentBLL.Departments.FirstOrDefault(d => d.Name == depName);
if (dep == null)
{
return new List<SelectListItem>();
}
return GetDepartmentList(dep.Id);
}
static public List<SelectListItem> GetDepartmentList(int deptid)
{
var list = new List<SelectListItem>();
Department root = null;
if (deptid > 0)
{
root = (from o in DepartmentBLL.Departments where o.Id == deptid select o).First();
}
else
{
root = DepartmentBLL.Departments.First();
}
list_findchildren(list, root, 0, 0, false);
return list;
}
static void list_findchildren(List<SelectListItem> list, Department parent, int level, int selectValue, bool withParent = true)
{
var pre = "";
for (var i = 0; i < level; i++)
{
pre += " ";
}
if (withParent)
{
list.Add(new SelectListItem { Value = parent.Id.ToString(), Text = pre + parent.Name, Selected = (selectValue == parent.Id) });
}
DepartmentBLL.Departments.ForEach(o =>
{
if (o.PId == parent.Id)
{
list_findchildren(list, o, level + 1, selectValue);
}
});
}
}
}