using Attribute; using Dto; using Extensions; using Infrastructure; using Model; using Service; using Vo; namespace Services { /// /// 部门管理 /// [AppService(ServiceType = typeof(ISysDeptService), ServiceLifetime = LifeTime.Transient)] public class SysDeptService : BaseService, ISysDeptService { public ISysRoleDeptService RoleDeptRepository; public SysDeptService(ISysRoleDeptService roleDeptRepository) { RoleDeptRepository = roleDeptRepository; } /// /// 查询部门管理数据 /// /// /// public List GetSysDepts(SysDeptQueryDto dept) { var predicate = Expressionable.Create(); predicate = predicate.And(it => it.DelFlag == "0"); predicate = predicate.AndIF(dept.deptName.IfNotEmpty(), it => it.name.Contains(dept.deptName)); 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.name == dept.name && 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 != info?.status) // { // throw new CustomException("部门停用,不允许新增"); // } // dept.ancestors = ""; // if (info != null) // { // dept.ancestors = info.ancestors + "," + dept.parentId; // } dept.CreateTime = DateTime.Now; dept.UpdateTime = DateTime.Now; return Add(dept); } /// /// 修改保存部门信息 /// /// /// public int UpdateDept(SysDept dept) { dept.UpdateTime = DateTime.Now; // 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); // } int result = Update(dept, true); 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 SysDeptQueryDto()), 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); // }); return new List(); } /// /// 构建前端所需要树结构 /// /// 部门列表 /// 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(); } } }