123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- using Attribute;
- using Dto;
- using Extensions;
- using Infrastructure;
- using Model;
- using Service;
- using Vo;
- namespace Services
- {
- /// <summary>
- /// 部门管理
- /// </summary>
- [AppService(ServiceType = typeof(ISysDeptService), ServiceLifetime = LifeTime.Transient)]
- public class SysDeptService : BaseService<SysDept>, ISysDeptService
- {
- public ISysRoleDeptService RoleDeptRepository;
- public SysDeptService(ISysRoleDeptService roleDeptRepository)
- {
- RoleDeptRepository = roleDeptRepository;
- }
- /// <summary>
- /// 查询部门管理数据
- /// </summary>
- /// <param name="dept"></param>
- /// <returns></returns>
- public List<SysDept> GetSysDepts(SysDeptQueryDto dept)
- {
- var predicate = Expressionable.Create<SysDept>();
- 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;
- }
- /// <summary>
- /// 校验部门名称是否唯一
- /// </summary>
- /// <param name="dept"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 新增保存部门信息
- /// </summary>
- /// <param name="dept"></param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 修改保存部门信息
- /// </summary>
- /// <param name="dept"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 修改该部门的父级部门状态
- /// </summary>
- /// <param name="dept">当前部门</param>
- 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));
- }
- /// <summary>
- /// 修改子元素关系
- /// </summary>
- /// <param name="deptId">被修改的部门ID</param>
- /// <param name="newAncestors">新的父ID集合</param>
- /// <param name="oldAncestors">旧的父ID集合</param>
- public void UpdateDeptChildren(long deptId, string newAncestors, string oldAncestors)
- {
- List<SysDept> 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();
- // }
- }
- /// <summary>
- /// 获取所有子部门
- /// </summary>
- /// <param name="depts"></param>
- /// <param name="deptId"></param>
- /// <returns></returns>
- public List<SysDept> GetChildrenDepts(List<SysDept> depts, long deptId)
- {
- // return depts.FindAll(delegate (SysDept item)
- // {
- // long[] pid = Tools.SpitLongArrary(item.Ancestors);
- // return pid.Contains(deptId);
- // });
- return new List<SysDept>();
- }
- /// <summary>
- /// 构建前端所需要树结构
- /// </summary>
- /// <param name="depts">部门列表</param>
- /// <returns></returns>
- public List<SysDept> BuildDeptTree(List<SysDept> depts)
- {
- List<SysDept> returnList = new List<SysDept>();
- List<long> 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;
- }
- /// <summary>
- /// 构建前端所需下拉树结构
- /// </summary>
- /// <param name="depts"></param>
- /// <returns></returns>
- public List<TreeSelectVo> BuildDeptTreeSelect(List<SysDept> depts)
- {
- List<SysDept> menuTrees = BuildDeptTree(depts);
- List<TreeSelectVo> treeMenuVos = new List<TreeSelectVo>();
- foreach (var item in menuTrees)
- {
- treeMenuVos.Add(new TreeSelectVo(item));
- }
- return treeMenuVos;
- }
- /// <summary>
- /// 递归列表
- /// </summary>
- /// <param name="list"></param>
- /// <param name="t"></param>
- private void RecursionFn(List<SysDept> list, SysDept t)
- {
- //得到子节点列表
- List<SysDept> childList = GetChildList(list, t);
- t.children = childList;
- foreach (var item in childList)
- {
- if (GetChildList(list, item).Count() > 0)
- {
- RecursionFn(list, item);
- }
- }
- }
- /// <summary>
- /// 递归获取子菜单
- /// </summary>
- /// <param name="list">所有菜单</param>
- /// <param name="dept"></param>
- /// <returns></returns>
- private List<SysDept> GetChildList(List<SysDept> list, SysDept dept)
- {
- return list.Where(p => p.parentId == dept.deptId).ToList();
- }
- #region 角色部门
- /// <summary>
- /// 根据角色获取菜单id
- /// </summary>
- /// <param name="roleId"></param>
- /// <returns></returns>
- public List<SysRoleDept> SelectRoleDeptByRoleId(long roleId)
- {
- return RoleDeptRepository.SelectRoleDeptByRoleId(roleId);
- }
- /// <summary>
- /// 获取角色部门id集合
- /// </summary>
- /// <param name="roleId"></param>
- /// <returns></returns>
- public List<long> SelectRoleDepts(long roleId)
- {
- var list = SelectRoleDeptByRoleId(roleId);
- return list.Select(x => x.DeptId).Distinct().ToList();
- }
- /// <summary>
- /// 删除角色部门数据
- /// </summary>
- /// <param name="roleId"></param>
- /// <returns></returns>
- public bool DeleteRoleDeptByRoleId(long roleId)
- {
- return RoleDeptRepository.Delete(f => f.RoleId == roleId);
- }
- /// <summary>
- /// 批量插入角色部门
- /// </summary>
- /// <param name="role"></param>
- /// <returns></returns>
- public int InsertRoleDepts(SysRole role)
- {
- int rows = 1;
- // List<SysRoleDept> 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
- }
- /// <summary>
- /// 角色部门
- /// </summary>
- [AppService(ServiceType = typeof(ISysRoleDeptService), ServiceLifetime = LifeTime.Transient)]
- public class SysRoleDeptService : BaseService<SysRoleDept>, ISysRoleDeptService
- {
- public List<SysRoleDept> SelectRoleDeptByRoleId(long roleId)
- {
- return GetList(it => it.RoleId == roleId).ToList();
- }
- }
- }
|