56 lines
1.6 KiB
C#
56 lines
1.6 KiB
C#
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);
|
||
}
|
||
});
|
||
}
|
||
}
|
||
} |