using Infrastructure; using Infrastructure.Attribute; using Infrastructure.Extensions; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using ZR.Common; using ZR.Model.System; using ZR.Model.System.Vo; using ZR.Service.System.IService; namespace ZR.Service.System { /// /// 部门管理 /// [AppService(ServiceType = typeof(ISysDeptService), ServiceLifetime = LifeTime.Transient)] public class SysDeptService : BaseService, ISysDeptService { public ISysRoleDeptService RoleDeptRepository; public SysDeptService(ISysRoleDeptService roleDeptRepository) { RoleDeptRepository = roleDeptRepository; } /// /// 查询部门管理数据 /// /// /// public List GetSysDepts(SysDept dept) { //开始拼装查询条件 var predicate = Expressionable.Create(); predicate = predicate.And(it => it.DelFlag == "0"); predicate = predicate.AndIF(dept.DeptName.IfNotEmpty(), it => it.DeptName.Contains(dept.DeptName)); predicate = predicate.AndIF(dept.Status.IfNotEmpty(), it => it.Status == dept.Status); var response = GetList(predicate.ToExpression()); return response; } /// /// 校验部门名称是否唯一 /// /// /// public string CheckDeptNameUnique(SysDept dept) { long deptId = dept.DeptId == 0 ? -1L : dept.DeptId; SysDept info = GetFirst(it => it.DeptName == dept.DeptName && it.ParentId == dept.ParentId); if (info != null && info.DeptId != deptId) { return UserConstants.NOT_UNIQUE; } return UserConstants.UNIQUE; } /// /// 新增保存部门信息 /// /// /// public int InsertDept(SysDept dept) { SysDept info = GetFirst(it => it.DeptId == dept.ParentId); //如果父节点不为正常状态,则不允许新增子节点 if (info != null && !UserConstants.DEPT_NORMAL.Equals(info?.Status)) { throw new CustomException("部门停用,不允许新增"); } dept.Ancestors = ""; if (info != null) { dept.Ancestors = info.Ancestors + "," + dept.ParentId; } return Add(dept); } /// /// 修改保存部门信息 /// /// /// public int UpdateDept(SysDept dept) { SysDept newParentDept = GetFirst(it => it.DeptId == dept.ParentId); SysDept oldDept = GetFirst(m => m.DeptId == dept.DeptId); if (newParentDept != null && oldDept != null) { string newAncestors = newParentDept.Ancestors + "," + newParentDept.DeptId; string oldAncestors = oldDept.Ancestors; dept.Ancestors = newAncestors; UpdateDeptChildren(dept.DeptId, newAncestors, oldAncestors); } int result = Context.Updateable(dept).ExecuteCommand(); if (UserConstants.DEPT_NORMAL.Equals(dept.Status) && dept.Ancestors.IfNotEmpty() && !"0".Equals(dept.Ancestors)) { // 如果该部门是启用状态,则启用该部门的所有上级部门 UpdateParentDeptStatusNormal(dept); } return result; } /// /// 修改该部门的父级部门状态 /// /// 当前部门 private void UpdateParentDeptStatusNormal(SysDept dept) { long[] depts = Tools.SpitLongArrary(dept.Ancestors); dept.Status = "0"; dept.Update_time = DateTime.Now; Update(dept, it => new { it.Update_by, it.Update_time, it.Status }, f => depts.Contains(f.DeptId)); } /// /// 修改子元素关系 /// /// 被修改的部门ID /// 新的父ID集合 /// 旧的父ID集合 public void UpdateDeptChildren(long deptId, string newAncestors, string oldAncestors) { List children = GetChildrenDepts(GetSysDepts(new SysDept()), deptId); foreach (var child in children) { string ancestors = child.Ancestors.ReplaceFirst(oldAncestors, newAncestors); long[] ancestorsArr = Tools.SpitLongArrary(ancestors).Distinct().ToArray(); child.Ancestors = string.Join(",", ancestorsArr); } if (children.Any()) { Context.Updateable(children).WhereColumns(f => new { f.DeptId }) .UpdateColumns(it => new { it.Ancestors }).ExecuteCommand(); } } /// /// 获取所有子部门 /// /// /// /// public List GetChildrenDepts(List depts, long deptId) { return depts.FindAll(delegate (SysDept item) { long[] pid = Tools.SpitLongArrary(item.Ancestors); return pid.Contains(deptId); }); } /// /// 构建前端所需要树结构 /// /// 部门列表 /// public List BuildDeptTree(List depts) { List returnList = new List(); List tempList = depts.Select(f => f.DeptId).ToList(); foreach (var dept in depts) { // 如果是顶级节点, 遍历该父节点的所有子节点 if (!tempList.Contains(dept.ParentId)) { RecursionFn(depts, dept); returnList.Add(dept); } } if (!returnList.Any()) { returnList = depts; } return returnList; } /// /// 构建前端所需下拉树结构 /// /// /// public List BuildDeptTreeSelect(List depts) { List menuTrees = BuildDeptTree(depts); List treeMenuVos = new List(); foreach (var item in menuTrees) { treeMenuVos.Add(new TreeSelectVo(item)); } return treeMenuVos; } /// /// 递归列表 /// /// /// private void RecursionFn(List list, SysDept t) { //得到子节点列表 List childList = GetChildList(list, t); t.children = childList; foreach (var item in childList) { if (GetChildList(list, item).Count() > 0) { RecursionFn(list, item); } } } /// /// 递归获取子菜单 /// /// 所有菜单 /// /// private List GetChildList(List list, SysDept dept) { return list.Where(p => p.ParentId == dept.DeptId).ToList(); } #region 角色部门 /// /// 根据角色获取菜单id /// /// /// public List SelectRoleDeptByRoleId(long roleId) { return RoleDeptRepository.SelectRoleDeptByRoleId(roleId); } /// /// 获取角色部门id集合 /// /// /// public List SelectRoleDepts(long roleId) { var list = SelectRoleDeptByRoleId(roleId); return list.Select(x => x.DeptId).Distinct().ToList(); } /// /// 删除角色部门数据 /// /// /// public bool DeleteRoleDeptByRoleId(long roleId) { return RoleDeptRepository.Delete(f => f.RoleId == roleId); } /// /// 批量插入角色部门 /// /// /// public int InsertRoleDepts(SysRole role) { int rows = 1; List list = new(); foreach (var item in role.DeptIds) { list.Add(new SysRoleDept() { DeptId = item, RoleId = role.RoleId }); } if (list.Count > 0) { rows = RoleDeptRepository.Insert(list); } return rows; } #endregion } /// /// 角色部门 /// [AppService(ServiceType = typeof(ISysRoleDeptService), ServiceLifetime = LifeTime.Transient)] public class SysRoleDeptService : BaseService, ISysRoleDeptService { public List SelectRoleDeptByRoleId(long roleId) { return GetList(it => it.RoleId == roleId).ToList(); } } }