//----------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
{
///
/// 部门表Service
///
public partial class SysDeptService : BaseServices, ISysDeptService
{
IBaseRepository dal;
ISysDeptRepository _sysDeptRepository;
ISysRoleDeptRepository _sysRoleDeptRepository;
ISysUserRoleRepository _sysUserRoleRepository;
IMapper _mapper;
public SysDeptService(IBaseRepository dal, ISysDeptRepository sysDeptRepository, ISysRoleDeptRepository sysRoleDeptRepository,
ISysUserRoleRepository sysUserRoleRepository, IMapper mapper)
{
this.dal = dal;
BaseDal = dal;
_sysDeptRepository = sysDeptRepository;
_sysRoleDeptRepository = sysRoleDeptRepository;
_sysUserRoleRepository = sysUserRoleRepository;
_mapper = mapper;
}
///
/// 查询部门管理数据
///
/// 部门信息
/// 部门信息集合
public async Task> SelectDeptList(PageQuery pageQuery)
{
Expression> 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>(data);
return model;
}
///
/// 构建前端所需要树结构
///
/// 部门列表
/// 树结构列表
public List BuildDeptTree(List depts)
{
List returnList = new List();
var tempList = depts.Select(x => x.DeptID);
for (IEnumerator 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;
}
///
/// 构建前端所需要下拉树结构
///
/// 部门列表
/// 下拉树结构列表
public List BuildDeptTreeSelect(List depts)
{
var depView = _mapper.Map>(depts);
var deptTrees = BuildDeptTree(depView);
return _mapper.Map>(deptTrees);
}
///
/// 根据角色ID查询部门树信息
///
/// 角色ID
/// 选中部门列表
public async Task> SelectDeptListByRoleId(int roleId)
{
var depts = await _sysRoleDeptRepository.QueryAsync(x => x.RoleID == roleId);
return depts.Select(x => (int)x.DeptID).ToList();
}
///
/// 根据部门ID查询信息
///
/// 部门ID
/// 部门信息
public async Task SelectDeptById(int deptId)
{
return await dal.QueryByIdAsync(deptId);
}
///
/// 查询部门全部有效数据
///
/// 部门信息
public async Task> SelectDeptAll()
{
return await dal.QueryAsync(x => x.DelFlag == false && x.Status == SysConst.ENABLE);
}
///
/// 根据ID查询所有子部门(正常状态)
///
/// 部门ID
/// 子部门数
public async Task SelectNormalChildrenDeptById(int deptId)
{
var children = await _sysDeptRepository.SelectChildrenDeptById(deptId);
var data = children.FindAll(x => x.DelFlag == false && x.Status == SysConst.ENABLE);
return data.Count;
}
///
/// 根据ID查询所有子部门(正常状态)
///
/// 部门ID
/// 子部ID及当前选中节点部门ID的List
public async Task> SelectNormalChildrenIDsById(int deptId)
{
return await _sysDeptRepository.SelectNormalChildrenIDsById(deptId);
}
///
/// 是否存在子节点
///
/// 部门ID
/// 结果
public async Task HasChildByDeptId(int deptId)
{
var result = await dal.QueryAsync(x => x.ParentID == deptId && x.DelFlag == false);
return result.Count > 0;
}
///
/// 查询部门是否存在用户
///
/// 部门ID
/// 结果 true 存在 false 不存在
public async Task 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;
}
///
/// 校验部门名称是否唯一
///
/// 部门信息
/// 结果
public async Task CheckDeptNameUnique(SysDept dept)
{
var data = await dal.QueryAsync(x => x.DeptName == dept.DeptName && x.DeptID != dept.DeptID);
return data.Count == 0;
}
///
/// 新增保存部门信息
///
/// 部门信息
/// 结果
public async Task 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);
}
///
/// 修改保存部门信息
///
/// 部门信息
/// 结果
public async Task 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;
}
///
/// 修改该部门的父级部门状态
///
/// 当前部门
/// 修改状态
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);
}
///
/// 修改子元素关系
///
/// 被修改的部门ID
/// 新的父ID集合
/// 旧的父ID集合
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);
}
}
///
/// 删除部门管理信息
///
/// 部门ID
/// 结果
public async Task 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);
}
///
/// 递归列表
///
public void RecursionFn(List list, DeptView t)
{
// 得到子节点列表
List childList = GetChildList(list, t);
t.Children = childList;
foreach (var tChild in childList)
{
if (HasChild(list, tChild))
{
RecursionFn(list, tChild);
}
}
}
///
/// 得到子节点列表
///
///
///
///
public List GetChildList(List list, DeptView t)
{
List tlist = new List();
IEnumerator it = list.GetEnumerator();
while (it.MoveNext())
{
var n = it.Current;
if (n.ParentID.IsNotEmptyOrNull() && n.ParentID == t.DeptID)
{
tlist.Add(n);
}
}
return tlist;
}
///
/// 判断是否有子节点
///
///
///
///
public bool HasChild(List list, DeptView t)
{
return GetChildList(list, t).Count > 0 ? true : false;
}
}
}
//----------SysDept结束----------