| | |
| | | package com.ycl.system.service.impl; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import com.ycl.common.base.Result; |
| | | import com.ycl.common.core.domain.StringTreeSelect; |
| | | import com.ycl.system.domain.base.BaseSelect; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import com.ycl.common.annotation.DataScope; |
| | |
| | | * @author ycl |
| | | */ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class SysDeptServiceImpl implements ISysDeptService |
| | | { |
| | | @Autowired |
| | | private SysDeptMapper deptMapper; |
| | | |
| | | @Autowired |
| | | private SysRoleMapper roleMapper; |
| | | private final SysDeptMapper deptMapper; |
| | | private final SysRoleMapper roleMapper; |
| | | |
| | | /** |
| | | * 查询部门管理数据 |
| | |
| | | return deptMapper.selectDeptList(dept); |
| | | } |
| | | |
| | | @Override |
| | | public List<SysDept> selectDeptListNoAuth(SysDept dept) { |
| | | return deptMapper.selectDeptListNoAuth(dept); |
| | | } |
| | | |
| | | /** |
| | | * 查询部门树结构信息 |
| | | * |
| | |
| | | { |
| | | List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept); |
| | | return buildDeptTreeSelect(depts); |
| | | } |
| | | |
| | | @Override |
| | | public List<TreeSelect> deptTreeNoDataAuth(SysDept dept) { |
| | | List<SysDept> depts = deptMapper.selectDeptList(dept);; |
| | | return buildDeptTreeSelect(depts); |
| | | } |
| | | |
| | | @Override |
| | | public List<StringTreeSelect> flowDeptTree(SysDept dept) { |
| | | List<SysDept> depts = deptMapper.selectDeptList(dept); |
| | | List<StringTreeSelect> list = depts.stream().map(item -> { |
| | | StringTreeSelect d = new StringTreeSelect(); |
| | | d.setId("dept:" + item.getDeptId()); |
| | | d.setLabel(item.getDeptName()); |
| | | d.setParentId("dept:" + item.getParentId()); |
| | | return d; |
| | | }).collect(Collectors.toList()); |
| | | |
| | | |
| | | Map<String, StringTreeSelect> nodeMap = new HashMap<>(); |
| | | // 将所有节点放入Map中,方便后续查找 |
| | | for (StringTreeSelect node : list) { |
| | | nodeMap.put(node.getId(), node); |
| | | } |
| | | List<StringTreeSelect> treeList = new ArrayList<>(); |
| | | // 构建树结构 |
| | | for (StringTreeSelect node : list) { |
| | | StringTreeSelect root = null; |
| | | if ("dept:0".equals(node.getParentId()) || node.getParentId().isEmpty()) { |
| | | // 根节点 |
| | | root = node; |
| | | treeList.add(root); |
| | | } else { |
| | | // 找到父节点,并将当前节点添加到父节点的children列表中 |
| | | StringTreeSelect parentNode = nodeMap.get(node.getParentId()); |
| | | if (parentNode != null) { |
| | | parentNode.getChildren().add(node); |
| | | } |
| | | } |
| | | } |
| | | return treeList; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | return returnList; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 构建前端所需要下拉树结构 |
| | |
| | | public SysDept selectDeptById(Long deptId) |
| | | { |
| | | return deptMapper.selectDeptById(deptId); |
| | | } |
| | | |
| | | @Override |
| | | public List<SysDept> selectDeptByIds(List<Long> deptIds) { |
| | | return deptMapper.selectDeptByIds(deptIds); |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public int insertDept(SysDept dept) |
| | | { |
| | | SysDept info = deptMapper.selectDeptById(dept.getParentId()); |
| | | // 如果父节点不为正常状态,则不允许新增子节点 |
| | | if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) |
| | | { |
| | | throw new ServiceException("部门停用,不允许新增"); |
| | | if (Objects.nonNull(dept.getParentId())) { |
| | | SysDept info = deptMapper.selectDeptById(dept.getParentId()); |
| | | // 如果父节点不为正常状态,则不允许新增子节点 |
| | | if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) |
| | | { |
| | | throw new ServiceException("上级部门已停用,不允许新增下级部门"); |
| | | } |
| | | dept.setAncestors(info.getAncestors() + "," + dept.getParentId()); |
| | | } else { |
| | | dept.setParentId(0L); |
| | | dept.setAncestors(""); |
| | | } |
| | | dept.setAncestors(info.getAncestors() + "," + dept.getParentId()); |
| | | return deptMapper.insertDept(dept); |
| | | } |
| | | |
| | |
| | | @Override |
| | | public int updateDept(SysDept dept) |
| | | { |
| | | SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId()); |
| | | SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId()); |
| | | if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept)) |
| | | { |
| | | String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId(); |
| | | String oldAncestors = oldDept.getAncestors(); |
| | | dept.setAncestors(newAncestors); |
| | | updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors); |
| | | if (Objects.isNull(oldDept)) { |
| | | throw new RuntimeException("修改部门被删除或不存在"); |
| | | } |
| | | if (Objects.nonNull(dept.getParentId())) { |
| | | SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId()); |
| | | if (Objects.nonNull(newParentDept)) { |
| | | String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId(); |
| | | String oldAncestors = oldDept.getAncestors(); |
| | | dept.setAncestors(newAncestors); |
| | | updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors); |
| | | } |
| | | } else { |
| | | dept.setParentId(0L); |
| | | dept.setAncestors(""); |
| | | } |
| | | int result = deptMapper.updateDept(dept); |
| | | if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors()) |
| | | && !StringUtils.equals("0", dept.getAncestors())) |
| | | { |
| | | // 如果该部门是启用状态,则启用该部门的所有上级部门 |
| | | updateParentDeptStatusNormal(dept); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | |
| | | ).collect(Collectors.toList()); |
| | | return Result.ok().data(vos); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public List<StringTreeSelect> flowableAll() { |
| | | List<StringTreeSelect> list = deptMapper.selectDeptList(new SysDept()).stream().map(sysDept -> { |
| | | StringTreeSelect stringTreeSelect = new StringTreeSelect(); |
| | | stringTreeSelect.setId("dept:" + sysDept.getDeptId()); |
| | | stringTreeSelect.setLabel(sysDept.getDeptName()); |
| | | return stringTreeSelect; |
| | | }).collect(Collectors.toList()); |
| | | return list; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public List<Long> getChildIds(Long deptId) { |
| | | return deptMapper.getChildIds(deptId); |
| | | } |
| | | } |