| | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.lang.tree.Tree; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.ruoyi.common.constant.UserConstants; |
| | | 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.system.domain.SysDept; |
| | | import com.ruoyi.system.domain.SysRole; |
| | | import com.ruoyi.system.domain.SysUser; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.helper.DataBaseHelper; |
| | | import com.ruoyi.common.helper.LoginHelper; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.TreeBuildUtils; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import com.ruoyi.system.mapper.SysDeptMapper; |
| | | import com.ruoyi.system.mapper.SysRoleMapper; |
| | | import com.ruoyi.system.mapper.SysUserMapper; |
| | |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | |
| | | */ |
| | | @Override |
| | | public List<SysDept> selectDeptList(SysDept dept) { |
| | | return baseMapper.selectDeptList(dept); |
| | | LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>(); |
| | | lqw.eq(SysDept::getDelFlag, "0") |
| | | .eq(ObjectUtil.isNotNull(dept.getDeptId()), SysDept::getDeptId, dept.getDeptId()) |
| | | .eq(ObjectUtil.isNotNull(dept.getParentId()), SysDept::getParentId, dept.getParentId()) |
| | | .like(StringUtils.isNotBlank(dept.getDeptName()), SysDept::getDeptName, dept.getDeptName()) |
| | | .eq(StringUtils.isNotBlank(dept.getStatus()), SysDept::getStatus, dept.getStatus()) |
| | | .orderByAsc(SysDept::getParentId) |
| | | .orderByAsc(SysDept::getOrderNum); |
| | | return baseMapper.selectDeptList(lqw); |
| | | } |
| | | |
| | | /** |
| | | * 查询部门树结构信息 |
| | | * |
| | | * @param dept 部门信息 |
| | | * @return 部门树信息集合 |
| | | */ |
| | | @Override |
| | | public List<Tree<Long>> selectDeptTreeList(SysDept dept) { |
| | | List<SysDept> depts = this.selectDeptList(dept); |
| | | return buildDeptTreeSelect(depts); |
| | | } |
| | | |
| | | /** |
| | |
| | | if (CollUtil.isEmpty(depts)) { |
| | | return CollUtil.newArrayList(); |
| | | } |
| | | Long parentId = depts.get(0).getParentId(); |
| | | return TreeBuildUtils.build(depts, parentId, (dept, tree) -> |
| | | return TreeBuildUtils.build(depts, (dept, tree) -> |
| | | tree.setId(dept.getDeptId()) |
| | | .setParentId(dept.getParentId()) |
| | | .setName(dept.getDeptName()) |
| | |
| | | @Override |
| | | public List<Long> selectDeptListByRoleId(Long roleId) { |
| | | SysRole role = roleMapper.selectById(roleId); |
| | | return baseMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly()); |
| | | return baseMapper.selectDeptListByRoleId(roleId, role.getDeptCheckStrictly()); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public SysDept selectDeptById(Long deptId) { |
| | | return baseMapper.selectById(deptId); |
| | | SysDept dept = baseMapper.selectById(deptId); |
| | | SysDept parentDept = baseMapper.selectOne(new LambdaQueryWrapper<SysDept>() |
| | | .select(SysDept::getDeptName).eq(SysDept::getDeptId, dept.getParentId())); |
| | | dept.setParentName(ObjectUtil.isNotNull(parentDept) ? parentDept.getDeptName() : null); |
| | | return dept; |
| | | } |
| | | |
| | | /** |
| | | * 根据ID查询所有子部门(正常状态) |
| | | * 根据ID查询所有子部门数(正常状态) |
| | | * |
| | | * @param deptId 部门ID |
| | | * @return 子部门数 |
| | |
| | | @Override |
| | | public long selectNormalChildrenDeptById(Long deptId) { |
| | | return baseMapper.selectCount(new LambdaQueryWrapper<SysDept>() |
| | | .eq(SysDept::getStatus, 0) |
| | | .apply("find_in_set({0}, ancestors)", deptId)); |
| | | .eq(SysDept::getStatus, UserConstants.DEPT_NORMAL) |
| | | .apply(DataBaseHelper.findInSet(deptId, "ancestors"))); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public String checkDeptNameUnique(SysDept dept) { |
| | | Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId(); |
| | | boolean count = baseMapper.exists(new LambdaQueryWrapper<SysDept>() |
| | | boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysDept>() |
| | | .eq(SysDept::getDeptName, dept.getDeptName()) |
| | | .eq(SysDept::getParentId, dept.getParentId()) |
| | | .ne(SysDept::getDeptId, deptId)); |
| | | if (count) { |
| | | .ne(ObjectUtil.isNotNull(dept.getDeptId()), SysDept::getDeptId, dept.getDeptId())); |
| | | if (exist) { |
| | | return UserConstants.NOT_UNIQUE; |
| | | } |
| | | return UserConstants.UNIQUE; |
| | |
| | | */ |
| | | @Override |
| | | public void checkDeptDataScope(Long deptId) { |
| | | if (!SysUser.isAdmin(LoginHelper.getUserId())) { |
| | | if (!LoginHelper.isAdmin()) { |
| | | SysDept dept = new SysDept(); |
| | | dept.setDeptId(deptId); |
| | | List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept); |
| | | if (StringUtils.isEmpty(depts)) { |
| | | List<SysDept> depts = this.selectDeptList(dept); |
| | | if (CollUtil.isEmpty(depts)) { |
| | | throw new ServiceException("没有权限访问部门数据!"); |
| | | } |
| | | } |
| | |
| | | public int updateDept(SysDept dept) { |
| | | SysDept newParentDept = baseMapper.selectById(dept.getParentId()); |
| | | SysDept oldDept = baseMapper.selectById(dept.getDeptId()); |
| | | if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept)) { |
| | | if (ObjectUtil.isNotNull(newParentDept) && ObjectUtil.isNotNull(oldDept)) { |
| | | String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId(); |
| | | String oldAncestors = oldDept.getAncestors(); |
| | | dept.setAncestors(newAncestors); |
| | |
| | | } |
| | | int result = baseMapper.updateById(dept); |
| | | if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors()) |
| | | && !StringUtils.equals("0", dept.getAncestors())) { |
| | | && !StringUtils.equals(UserConstants.DEPT_NORMAL, dept.getAncestors())) { |
| | | // 如果该部门是启用状态,则启用该部门的所有上级部门 |
| | | updateParentDeptStatusNormal(dept); |
| | | } |
| | |
| | | String ancestors = dept.getAncestors(); |
| | | Long[] deptIds = Convert.toLongArray(ancestors); |
| | | baseMapper.update(null, new LambdaUpdateWrapper<SysDept>() |
| | | .set(SysDept::getStatus, "0") |
| | | .set(SysDept::getStatus, UserConstants.DEPT_NORMAL) |
| | | .in(SysDept::getDeptId, Arrays.asList(deptIds))); |
| | | } |
| | | |
| | |
| | | */ |
| | | public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) { |
| | | List<SysDept> children = baseMapper.selectList(new LambdaQueryWrapper<SysDept>() |
| | | .apply("find_in_set({0},ancestors)", deptId)); |
| | | .apply(DataBaseHelper.findInSet(deptId, "ancestors"))); |
| | | List<SysDept> list = new ArrayList<>(); |
| | | for (SysDept child : children) { |
| | | child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors)); |
| | | SysDept dept = new SysDept(); |
| | | dept.setDeptId(child.getDeptId()); |
| | | dept.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors)); |
| | | list.add(dept); |
| | | } |
| | | if (children.size() > 0) { |
| | | baseMapper.updateDeptChildren(children); |
| | | if (list.size() > 0) { |
| | | baseMapper.updateBatchById(list); |
| | | } |
| | | } |
| | | |