| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.lang.Validator; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.annotation.DataScope; |
| | | import com.ruoyi.common.constant.UserConstants; |
| | | import com.ruoyi.common.core.domain.TreeSelect; |
| | | import com.ruoyi.common.core.domain.entity.SysDept; |
| | | import com.ruoyi.common.core.domain.entity.SysRole; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl; |
| | | import com.ruoyi.common.exception.CustomException; |
| | | import com.ruoyi.system.mapper.SysDeptMapper; |
| | | import com.ruoyi.system.mapper.SysRoleMapper; |
| | |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | * @author ruoyi |
| | | */ |
| | | @Service |
| | | public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService { |
| | | public class SysDeptServiceImpl extends ServicePlusImpl<SysDeptMapper, SysDept, SysDept> implements ISysDeptService { |
| | | |
| | | @Autowired |
| | | private SysRoleMapper roleMapper; |
| | |
| | | @Override |
| | | @DataScope(deptAlias = "d") |
| | | public List<SysDept> selectDeptList(SysDept dept) { |
| | | Object dataScope = dept.getParams().get("dataScope"); |
| | | return list(new LambdaQueryWrapper<SysDept>() |
| | | .eq(dept.getParentId() != null && dept.getParentId() != 0, |
| | | SysDept::getParentId, dept.getParentId()) |
| | | .like(StrUtil.isNotBlank(dept.getDeptName()), SysDept::getDeptName, dept.getDeptName()) |
| | | .eq(StrUtil.isNotBlank(dept.getStatus()), SysDept::getStatus, dept.getStatus()) |
| | | .apply(dataScope != null, dataScope != null ? dataScope.toString() : null) |
| | | .orderByAsc(SysDept::getParentId) |
| | | .orderByAsc(SysDept::getOrderNum)); |
| | | return baseMapper.selectDeptList(dept); |
| | | } |
| | | |
| | | /** |
| | |
| | | for (SysDept dept : depts) { |
| | | tempList.add(dept.getDeptId()); |
| | | } |
| | | for (Iterator<SysDept> iterator = depts.iterator(); iterator.hasNext(); ) { |
| | | SysDept dept = (SysDept) iterator.next(); |
| | | // 如果是顶级节点, 遍历该父节点的所有子节点 |
| | | if (!tempList.contains(dept.getParentId())) { |
| | | recursionFn(depts, dept); |
| | | returnList.add(dept); |
| | | } |
| | | } |
| | | for (SysDept dept : depts) { |
| | | // 如果是顶级节点, 遍历该父节点的所有子节点 |
| | | if (!tempList.contains(dept.getParentId())) { |
| | | recursionFn(depts, dept); |
| | | returnList.add(dept); |
| | | } |
| | | } |
| | | if (returnList.isEmpty()) { |
| | | returnList = depts; |
| | | } |
| | |
| | | */ |
| | | @Override |
| | | public List<Integer> selectDeptListByRoleId(Long roleId) { |
| | | SysRole role = roleMapper.selectRoleById(roleId); |
| | | SysRole role = roleMapper.selectById(roleId); |
| | | return baseMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly()); |
| | | } |
| | | |
| | |
| | | int result = count(new LambdaQueryWrapper<SysDept>() |
| | | .eq(SysDept::getParentId, deptId) |
| | | .last("limit 1")); |
| | | return result > 0 ? true : false; |
| | | return result > 0; |
| | | } |
| | | |
| | | /** |
| | |
| | | public boolean checkDeptExistUser(Long deptId) { |
| | | int result = userMapper.selectCount(new LambdaQueryWrapper<SysUser>() |
| | | .eq(SysUser::getDeptId, deptId)); |
| | | return result > 0 ? true : false; |
| | | return result > 0; |
| | | } |
| | | |
| | | /** |
| | |
| | | updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors); |
| | | } |
| | | int result = baseMapper.updateById(dept); |
| | | if (UserConstants.DEPT_NORMAL.equals(dept.getStatus())) { |
| | | if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors()) |
| | | && !StringUtils.equals("0", dept.getAncestors())) { |
| | | // 如果该部门是启用状态,则启用该部门的所有上级部门 |
| | | updateParentDeptStatus(dept); |
| | | updateParentDeptStatusNormal(dept); |
| | | } |
| | | return result; |
| | | } |
| | |
| | | * |
| | | * @param dept 当前部门 |
| | | */ |
| | | private void updateParentDeptStatus(SysDept dept) { |
| | | String updateBy = dept.getUpdateBy(); |
| | | dept = getById(dept.getDeptId()); |
| | | dept.setUpdateBy(updateBy); |
| | | update(null,new LambdaUpdateWrapper<SysDept>() |
| | | .set(StrUtil.isNotBlank(dept.getStatus()), |
| | | SysDept::getStatus,dept.getStatus()) |
| | | .set(StrUtil.isNotBlank(dept.getUpdateBy()), |
| | | SysDept::getUpdateBy,dept.getUpdateBy()) |
| | | .in(SysDept::getDeptId, Arrays.asList(dept.getAncestors().split(",")))); |
| | | private void updateParentDeptStatusNormal(SysDept dept) { |
| | | String ancestors = dept.getAncestors(); |
| | | Long[] deptIds = Convert.toLongArray(ancestors); |
| | | update(null, new LambdaUpdateWrapper<SysDept>() |
| | | .set(SysDept::getStatus, "0") |
| | | .in(SysDept::getDeptId, Arrays.asList(deptIds))); |
| | | } |
| | | |
| | | /** |
| | | * 修改子元素关系 |
| | | * |
| | | * @param deptId 被修改的部门ID |
| | | * @param deptId 被修改的部门ID |
| | | * @param newAncestors 新的父ID集合 |
| | | * @param oldAncestors 旧的父ID集合 |
| | | */ |
| | |
| | | List<SysDept> children = list(new LambdaQueryWrapper<SysDept>() |
| | | .apply("find_in_set({0},ancestors)",deptId)); |
| | | for (SysDept child : children) { |
| | | child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors)); |
| | | child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors)); |
| | | } |
| | | if (children.size() > 0) { |
| | | updateBatchById(children); |
| | | baseMapper.updateDeptChildren(children); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | private List<SysDept> getChildList(List<SysDept> list, SysDept t) { |
| | | List<SysDept> tlist = new ArrayList<SysDept>(); |
| | | Iterator<SysDept> it = list.iterator(); |
| | | while (it.hasNext()) { |
| | | SysDept n = (SysDept) it.next(); |
| | | if (Validator.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue()) { |
| | | tlist.add(n); |
| | | } |
| | | } |
| | | for (SysDept n : list) { |
| | | if (Validator.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue()) { |
| | | tlist.add(n); |
| | | } |
| | | } |
| | | return tlist; |
| | | } |
| | | |
| | |
| | | * 判断是否有子节点 |
| | | */ |
| | | private boolean hasChild(List<SysDept> list, SysDept t) { |
| | | return getChildList(list, t).size() > 0 ? true : false; |
| | | return getChildList(list, t).size() > 0; |
| | | } |
| | | } |