using Edu.Model.ViewModel.User;
using Edu.Repository.User;
using System.Collections.Generic;
using System.Linq;
using VT.FW.DB;
namespace Edu.Module.User
{
///
/// 部门处理类
///
public class DepartmentModule
{
///
/// 部门仓储层对象
///
private readonly RB_DepartmentRepository departmentRepository = new RB_DepartmentRepository();
///
/// 账号仓储层对象
///
private readonly RB_AccountRepository accountRepository = new RB_AccountRepository();
///
/// 岗位仓储层对象
///
private readonly RB_PostRepository postRepository = new RB_PostRepository();
///
/// 部门岗位关联表仓储层对象
///
private readonly RB_Department_PostRepository department_PostRepository = new RB_Department_PostRepository();
///
/// 获取部门分页列表
///
///
///
///
///
///
public List GetDepartmentPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Department_ViewModel query)
{
var list = departmentRepository.GetDepartmentPageListRepository(pageIndex, pageSize, out rowsCount, query);
List parentList = new List();
List empList = new List();
List deptPostList = new List();
if (list != null && list.Count > 0)
{
//查询部门列表
string Ids = string.Join(",", list.Where(qitem => qitem.ParentId > 0).Select(qitem => qitem.ParentId));
if (!string.IsNullOrEmpty(Ids))
{
parentList = GetDepartmentListModule(new RB_Department_ViewModel() { QDeptIds = Ids });
}
//查询员工列表
string persion = string.Join(",", list.Where(qitem => !string.IsNullOrEmpty(qitem.ManagerIds)).Select(qitem => qitem.ManagerIds));
if (!string.IsNullOrEmpty(persion))
{
empList = accountRepository.GetEmployeeListRepository(new Employee_ViewModel() { QIds = persion });
}
string qDeptIds = string.Join(",", list.Select(qitem => qitem.DeptId));
if (!string.IsNullOrEmpty(qDeptIds))
{
deptPostList = department_PostRepository.GetDepartmentPostListRepository(new RB_Department_Post_ViewModel() { QDeptIds = qDeptIds });
}
}
foreach (var item in list)
{
//部门
item.ParentDeptName = parentList?.Where(qitem => qitem.DeptId == item.ParentId)?.FirstOrDefault()?.DeptName ?? "";
//负责人
string persionName = "";
if (item.ManagerList != null && item.ManagerList.Count > 0)
{
foreach (var subItem in item.ManagerList)
{
persionName += "、" + empList?.Where(qitem => qitem.Id == subItem)?.FirstOrDefault()?.EmployeeName ?? "";
}
}
if (!string.IsNullOrEmpty(persionName) && persionName != "")
{
persionName = persionName.Substring(1);
}
item.ManagerName = persionName;
item.DeptPostList = deptPostList?.Where(qitem => qitem.Dept_Id == item.DeptId)?.ToList() ?? new List();
}
return list;
}
///
/// 获取部门列表
///
///
///
public List GetDepartmentListModule(RB_Department_ViewModel query)
{
return departmentRepository.GetDepartmentListRepository(query);
}
///
/// 获取当前部门的所有上级部门
///
///
///
public List GetAllSuperiorDepartmentListModule(object DeptId)
{
return departmentRepository.GetAllSuperiorDepartmentListRepository(DeptId);
}
///
/// 获取当前部门的所有上级部门编号【例如:1,2,3...】
///
///
///
public string GetAllSuperiorDepartmentIdsModule(object DeptId)
{
return departmentRepository.GetAllSuperiorDepartmentIdsRepository(DeptId);
}
///
/// 获取当前部门和当前部门所有的下级部门列表
///
///
///
public List GetCurrentAndChildDepartmentListModule(object DeptId)
{
return departmentRepository.GetCurrentAndChildDepartmentListRepository(DeptId);
}
///
/// 获取当前部门和当前部门所有的下级部门【例如:1,2,3...】
///
///
///
public string GetCurrentAndChildDepartmentIdsModule(object DeptId)
{
return departmentRepository.GetCurrentAndChildDepartmentIdsRepository(DeptId);
}
///
/// 获取部门结构树
///
///
/// 是否查询员工
/// 是否查询岗位
///
public List GetDepartmentTreeModule(RB_Department_ViewModel query,bool isQueryEmployee=false,bool isQueryPost=false)
{
//树形结构列表
List list = new List();
//所有的部门列表
var deptList = GetDepartmentListModule(query);
//员工列表
var empList = new List();
//岗位列表
var postList = new List();
if (deptList == null)
{
deptList = new List();
}
//查询员工
if (isQueryEmployee)
{
empList = accountRepository.GetEmployeeListRepository(new Employee_ViewModel()
{
Group_Id = query.Group_Id,
School_Id = query.School_Id
});
}
if (isQueryPost)
{
postList= postRepository.GetPostListExtRepository(new RB_Post_ViewModel()
{
Group_Id = query.Group_Id,
});
}
if (deptList != null && deptList.Count > 0)
{
var firstList = new List();
if (query.School_Id == 0)
{
firstList = deptList.Where(qitem => qitem.ParentId == 0 && qitem.School_Id == 0).ToList();
}
else
{
var minParentId = deptList.Where(qitem => qitem.School_Id == query.School_Id).Min(qitem => qitem.ParentId);
firstList = deptList.Where(qitem => qitem.ParentId == minParentId && qitem.School_Id == query.School_Id).ToList();
}
if (firstList != null && firstList.Count > 0)
{
foreach (var fItem in firstList)
{
DepartmentTree_ViewModel tModel = new DepartmentTree_ViewModel()
{
DeptId = fItem.DeptId,
DeptName = fItem.DeptName,
ParentId = fItem.ParentId,
ChildList = new List(),
School_Id = fItem.School_Id,
IsCompany = fItem.IsCompany,
DataType=1,
};
#region 添加员工信息
if (isQueryEmployee)
{
var tempEmpList = empList?.Where(qitem => qitem.Dept_Id == fItem.DeptId)?.ToList();
if (tempEmpList != null && tempEmpList.Count > 0)
{
foreach (var eItem in tempEmpList)
{
tModel.ChildList.Add(new DepartmentTree_ViewModel()
{
DeptId = eItem.Id,
DeptName = eItem.EmployeeName,
ParentId = 0,
ChildList = new List(),
School_Id = eItem.School_Id,
DataType = 2,
});
}
}
}
#endregion
#region 添加岗位
if (isQueryPost)
{
var tempPostList = postList?.Where(qitem => qitem.Dept_Id == fItem.DeptId)?.ToList();
if (tempPostList != null && tempPostList.Count > 0)
{
foreach (var pItem in tempPostList)
{
tModel.ChildList.Add(new DepartmentTree_ViewModel()
{
DeptId = pItem.PostId,
DeptName = pItem.PostName,
ParentId = 0,
ChildList = new List(),
School_Id = fItem.School_Id,
DataType = 3,
});
}
}
}
#endregion
var childList = GetDeptTreeList(fItem.DeptId, deptList, empList: empList,postList:postList);
if (childList != null && childList.Count > 0)
{
tModel.ChildList.AddRange(childList);
}
list.Add(tModel);
}
}
}
return list;
}
///
/// 递归生成树形结构
///
/// 父节点编号
/// 数据源列表
/// 员工列表
private List GetDeptTreeList(int parentId, List sourceList, List empList=null, List postList=null)
{
List treeList = new List();
foreach (var item in sourceList.Where(qitem => qitem.ParentId == parentId))
{
DepartmentTree_ViewModel model = new DepartmentTree_ViewModel()
{
DeptId = item.DeptId,
DeptName = item.DeptName,
ParentId = item.ParentId,
ChildList = new List(),
School_Id=item.School_Id,
IsCompany = item.IsCompany,
DataType = 1,
};
#region 添加员工信息
if (empList != null && empList.Count > 0)
{
var tempEmpList = empList?.Where(qitem => qitem.Dept_Id == item.DeptId)?.ToList();
if (tempEmpList != null && tempEmpList.Count > 0)
{
foreach (var eItem in tempEmpList)
{
model.ChildList.Add(new DepartmentTree_ViewModel()
{
DeptId = eItem.Id,
DeptName = eItem.EmployeeName,
ParentId = 0,
School_Id=eItem.School_Id,
ChildList = new List(),
DataType = 2,
});
}
}
}
#endregion
#region 添加岗位
if (postList != null && postList.Count > 0)
{
var tempPostList = postList?.Where(qitem => qitem.Dept_Id == item.DeptId)?.ToList();
if (tempPostList != null && tempPostList.Count > 0)
{
foreach (var pItem in tempPostList)
{
model.ChildList.Add(new DepartmentTree_ViewModel()
{
DeptId = pItem.PostId,
DeptName = pItem.PostName,
ParentId = 0,
ChildList = new List(),
School_Id = item.School_Id,
DataType = 3,
});
}
}
}
#endregion
//添加下级部门
var childList = GetDeptTreeList(item.DeptId, sourceList, empList: empList, postList: postList);
if (childList != null && childList.Count > 0)
{
model.ChildList.AddRange(childList);
}
treeList.Add(model);
}
return treeList;
}
///
/// 获取组织机构
///
///
///
///
public object GetOrganizationChartModule(RB_Group_ViewModel groupModel, RB_Department_ViewModel query)
{
List