|
|
|
|
|
|
|
|
//----------SysDept开始----------
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Linq.Expressions;
|
|
|
using System.Threading.Tasks;
|
|
|
using Admin.Core.IRepository;
|
|
|
using Admin.Core.Model;
|
|
|
using Admin.Core.Common;
|
|
|
using SqlSugar;
|
|
|
using AutoMapper;
|
|
|
using Admin.Core.IRepository.ISys;
|
|
|
using Admin.Core.IService.ISys;
|
|
|
using Admin.Core.Model.Sys;
|
|
|
|
|
|
namespace Admin.Core.Service.Sys
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 部门表Service
|
|
|
/// </summary>
|
|
|
public partial class SysDeptService : BaseServices<SysDept>, ISysDeptService
|
|
|
{
|
|
|
IBaseRepository<SysDept> dal;
|
|
|
ISysDeptRepository _sysDeptRepository;
|
|
|
ISysRoleDeptRepository _sysRoleDeptRepository;
|
|
|
ISysUserRoleRepository _sysUserRoleRepository;
|
|
|
IMapper _mapper;
|
|
|
public SysDeptService(IBaseRepository<SysDept> dal, ISysDeptRepository sysDeptRepository, ISysRoleDeptRepository sysRoleDeptRepository,
|
|
|
ISysUserRoleRepository sysUserRoleRepository, IMapper mapper)
|
|
|
{
|
|
|
this.dal = dal;
|
|
|
BaseDal = dal;
|
|
|
_sysDeptRepository = sysDeptRepository;
|
|
|
_sysRoleDeptRepository = sysRoleDeptRepository;
|
|
|
_sysUserRoleRepository = sysUserRoleRepository;
|
|
|
_mapper = mapper;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 查询部门管理数据
|
|
|
/// </summary>
|
|
|
/// <param name="pageQuery"> 部门信息 </param>
|
|
|
/// <returns> 部门信息集合 </returns>
|
|
|
public async Task<PageModel<DeptView>> SelectDeptList(PageQuery<SysDept> pageQuery)
|
|
|
{
|
|
|
Expression<Func<SysDept, bool>> whereExpression = x => (bool)x.DelFlag == false;
|
|
|
if (pageQuery.Query.DeptName.IsNotEmptyOrNull())
|
|
|
{
|
|
|
whereExpression = whereExpression.And(x => x.DeptName.Contains(pageQuery.Query.DeptName));
|
|
|
}
|
|
|
if (pageQuery.Query.Status.IsNotEmptyOrNull())
|
|
|
{
|
|
|
whereExpression = whereExpression.And(x => x.Status == pageQuery.Query.Status);
|
|
|
}
|
|
|
|
|
|
var data = await dal.QueryPageAsync(whereExpression, pageQuery.Page, pageQuery.PageSize, "OrderNum asc");
|
|
|
|
|
|
var model = _mapper.Map<PageModel<DeptView>>(data);
|
|
|
|
|
|
return model;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 构建前端所需要树结构
|
|
|
/// </summary>
|
|
|
/// <param name="depts"> 部门列表 </param>
|
|
|
/// <returns> 树结构列表 </returns>
|
|
|
public List<DeptView> BuildDeptTree(List<DeptView> depts)
|
|
|
{
|
|
|
List<DeptView> returnList = new List<DeptView>();
|
|
|
var tempList = depts.Select(x => x.DeptID);
|
|
|
|
|
|
for (IEnumerator<DeptView> iterator = depts.GetEnumerator(); iterator.MoveNext();)
|
|
|
{
|
|
|
DeptView dept = iterator.Current;
|
|
|
// 如果是顶级节点, 遍历该父节点的所有子节点
|
|
|
if (!tempList.Contains((int)dept.ParentID))
|
|
|
{
|
|
|
RecursionFn(depts, dept);
|
|
|
returnList.Add(dept);
|
|
|
}
|
|
|
}
|
|
|
if (returnList.Count == 0)
|
|
|
{
|
|
|
returnList = depts;
|
|
|
}
|
|
|
return returnList;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 构建前端所需要下拉树结构
|
|
|
/// </summary>
|
|
|
/// <param name="depts"> 部门列表 </param>
|
|
|
/// <returns> 下拉树结构列表 </returns>
|
|
|
public List<TreeSelect> BuildDeptTreeSelect(List<SysDept> depts)
|
|
|
{
|
|
|
var depView = _mapper.Map<List<DeptView>>(depts);
|
|
|
var deptTrees = BuildDeptTree(depView);
|
|
|
return _mapper.Map<List<TreeSelect>>(deptTrees);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 根据角色ID查询部门树信息
|
|
|
/// </summary>
|
|
|
/// <param name="roleId"> 角色ID </param>
|
|
|
/// <returns> 选中部门列表 </returns>
|
|
|
public async Task<List<int>> SelectDeptListByRoleId(int roleId)
|
|
|
{
|
|
|
var depts = await _sysRoleDeptRepository.QueryAsync(x => x.RoleID == roleId);
|
|
|
return depts.Select(x => (int)x.DeptID).ToList();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 根据部门ID查询信息
|
|
|
/// </summary>
|
|
|
/// <param name="deptId"> 部门ID </param>
|
|
|
/// <returns> 部门信息 </returns>
|
|
|
public async Task<SysDept> SelectDeptById(int deptId)
|
|
|
{
|
|
|
return await dal.QueryByIdAsync(deptId);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 查询部门全部有效数据
|
|
|
/// </summary>
|
|
|
/// <returns> 部门信息 </returns>
|
|
|
public async Task<List<SysDept>> SelectDeptAll()
|
|
|
{
|
|
|
return await dal.QueryAsync(x => x.DelFlag == false && x.Status == SysConst.ENABLE);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 根据ID查询所有子部门(正常状态)
|
|
|
/// </summary>
|
|
|
/// <param name="deptId"> 部门ID </param>
|
|
|
/// <returns> 子部门数 </returns>
|
|
|
public async Task<int> SelectNormalChildrenDeptById(int deptId)
|
|
|
{
|
|
|
var children = await _sysDeptRepository.SelectChildrenDeptById(deptId);
|
|
|
var data = children.FindAll(x => x.DelFlag == false && x.Status == SysConst.ENABLE);
|
|
|
return data.Count;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 根据ID查询所有子部门(正常状态)
|
|
|
/// </summary>
|
|
|
/// <param name="deptId"> 部门ID </param>
|
|
|
/// <returns> 子部ID及当前选中节点部门ID的List </returns>
|
|
|
public async Task<List<int>> SelectNormalChildrenIDsById(int deptId)
|
|
|
{
|
|
|
return await _sysDeptRepository.SelectNormalChildrenIDsById(deptId);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 是否存在子节点
|
|
|
/// </summary>
|
|
|
/// <param name="deptId"> 部门ID </param>
|
|
|
/// <returns> 结果 </returns>
|
|
|
public async Task<bool> HasChildByDeptId(int deptId)
|
|
|
{
|
|
|
var result = await dal.QueryAsync(x => x.ParentID == deptId && x.DelFlag == false);
|
|
|
return result.Count > 0;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 查询部门是否存在用户
|
|
|
/// </summary>
|
|
|
/// <param name="deptId"> 部门ID </param>
|
|
|
/// <returns> 结果 true 存在 false 不存在 </returns>
|
|
|
public async Task<bool> CheckDeptExistUser(int deptId)
|
|
|
{
|
|
|
var roles = await _sysRoleDeptRepository.QueryAsync(x => x.DeptID == deptId);
|
|
|
var roleIds = roles.Select(x => x.RoleID);
|
|
|
var result = await _sysUserRoleRepository.QueryAsync(x => roleIds.Contains(x.RoleID));
|
|
|
return result.Count > 0;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 校验部门名称是否唯一
|
|
|
/// </summary>
|
|
|
/// <param name="dept"> 部门信息 </param>
|
|
|
/// <returns> 结果 </returns>
|
|
|
public async Task<bool> CheckDeptNameUnique(SysDept dept)
|
|
|
{
|
|
|
var data = await dal.QueryAsync(x => x.DeptName == dept.DeptName && x.DeptID != dept.DeptID);
|
|
|
return data.Count == 0;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 新增保存部门信息
|
|
|
/// </summary>
|
|
|
/// <param name="dept"> 部门信息 </param>
|
|
|
/// <returns> 结果 </returns>
|
|
|
public async Task<int> InsertDept(SysDept dept)
|
|
|
{
|
|
|
var info = await dal.QueryByIdAsync(dept.ParentID);
|
|
|
// 如果父节点不为正常状态,则不允许新增子节点
|
|
|
if (info.Status == SysConst.DISABLE)
|
|
|
{
|
|
|
return SysConst.FLAGINT;
|
|
|
}
|
|
|
dept.Ancestors = info.Ancestors + "," + dept.ParentID;
|
|
|
dept.CreateTime = DateTime.Now;
|
|
|
dept.UpdateTime = DateTime.Now;
|
|
|
dept.DelFlag = false;
|
|
|
return await dal.AddAsync(dept);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 修改保存部门信息
|
|
|
/// </summary>
|
|
|
/// <param name="dept"> 部门信息 </param>
|
|
|
/// <returns> 结果 </returns>
|
|
|
public async Task<bool> UpdateDept(SysDept dept)
|
|
|
{
|
|
|
var newParentDept = await dal.QueryByIdAsync(dept.ParentID);
|
|
|
var oldDept = await dal.QueryByIdAsync(dept.DeptID);
|
|
|
if (newParentDept.IsNotEmptyOrNull() && oldDept.IsNotEmptyOrNull())
|
|
|
{
|
|
|
string newAncestors = newParentDept.Ancestors + "," + newParentDept.DeptID;
|
|
|
string oldAncestors = oldDept.Ancestors;
|
|
|
dept.Ancestors = newAncestors;
|
|
|
await UpdateDeptChildren(dept.DeptID, newAncestors, oldAncestors);
|
|
|
}
|
|
|
dept.UpdateTime = DateTime.Now;
|
|
|
var result = await dal.UpdateAsync(dept);
|
|
|
if (dept.Status == SysConst.ENABLE && dept.Ancestors.IsNotEmptyOrNull() && dept.Ancestors != "0")
|
|
|
{
|
|
|
// 如果该部门是启用状态,则启用该部门的所有上级部门
|
|
|
await UpdateParentDeptStatusNormal(dept, SysConst.ENABLE);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 修改该部门的父级部门状态
|
|
|
/// </summary>
|
|
|
/// <param name="dept"> 当前部门 </param>
|
|
|
/// <param name="status"> 修改状态 </param>
|
|
|
public async Task UpdateParentDeptStatusNormal(SysDept dept, int status)
|
|
|
{
|
|
|
var deptIds = dept.Ancestors.Split(",");
|
|
|
var depts = await dal.QueryByIDsAsync(deptIds);
|
|
|
depts.ForEach(x => x.Status = status);
|
|
|
await dal.UpdateAsync(depts);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 修改子元素关系
|
|
|
/// </summary>
|
|
|
/// <param name="deptId"> 被修改的部门ID </param>
|
|
|
/// <param name="newAncestors"> 新的父ID集合 </param>
|
|
|
/// <param name="oldAncestors"> 旧的父ID集合 </param>
|
|
|
public async Task UpdateDeptChildren(int deptId, string newAncestors, string oldAncestors)
|
|
|
{
|
|
|
var children = await _sysDeptRepository.SelectChildrenDeptById(deptId);
|
|
|
foreach (var child in children)
|
|
|
{
|
|
|
child.Ancestors = child.Ancestors.Replace(oldAncestors, newAncestors);
|
|
|
}
|
|
|
if (children.Count > 0)
|
|
|
{
|
|
|
await dal.UpdateAsync(children);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 删除部门管理信息
|
|
|
/// </summary>
|
|
|
/// <param name="deptId"> 部门ID </param>
|
|
|
/// <returns> 结果 </returns>
|
|
|
public async Task<bool> DeleteDeptById(int deptId, string updateBy)
|
|
|
{
|
|
|
var dept = await dal.QueryByIdAsync(deptId);
|
|
|
dept.UpdateBy = updateBy;
|
|
|
dept.UpdateTime = DateTime.Now;
|
|
|
dept.DelFlag = true;
|
|
|
return await dal.UpdateAsync(dept);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 递归列表
|
|
|
/// </summary>
|
|
|
public void RecursionFn(List<DeptView> list, DeptView t)
|
|
|
{
|
|
|
// 得到子节点列表
|
|
|
List<DeptView> childList = GetChildList(list, t);
|
|
|
t.Children = childList;
|
|
|
foreach (var tChild in childList)
|
|
|
{
|
|
|
if (HasChild(list, tChild))
|
|
|
{
|
|
|
RecursionFn(list, tChild);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 得到子节点列表
|
|
|
/// </summary>
|
|
|
/// <param name="list"></param>
|
|
|
/// <param name="t"></param>
|
|
|
/// <returns></returns>
|
|
|
public List<DeptView> GetChildList(List<DeptView> list, DeptView t)
|
|
|
{
|
|
|
List<DeptView> tlist = new List<DeptView>();
|
|
|
IEnumerator<DeptView> it = list.GetEnumerator();
|
|
|
while (it.MoveNext())
|
|
|
{
|
|
|
var n = it.Current;
|
|
|
if (n.ParentID.IsNotEmptyOrNull() && n.ParentID == t.DeptID)
|
|
|
{
|
|
|
tlist.Add(n);
|
|
|
}
|
|
|
}
|
|
|
return tlist;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 判断是否有子节点
|
|
|
/// </summary>
|
|
|
/// <param name="list"></param>
|
|
|
/// <param name="t"></param>
|
|
|
/// <returns></returns>
|
|
|
public bool HasChild(List<DeptView> list, DeptView t)
|
|
|
{
|
|
|
return GetChildList(list, t).Count > 0 ? true : false;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//----------SysDept结束----------
|
|
|
|