| | |
| | | |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.yuanchu.mom.dto.DepartmentDto; |
| | | import com.yuanchu.mom.pojo.Department; |
| | | import com.yuanchu.mom.mapper.DepartmentMapper; |
| | | import com.yuanchu.mom.service.DepartmentService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.yuanchu.mom.dto.DepartmentDto; |
| | | import com.yuanchu.mom.mapper.DepartmentMapper; |
| | | import com.yuanchu.mom.pojo.Department; |
| | | import com.yuanchu.mom.service.DepartmentService; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.var; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.sql.Connection; |
| | | import java.sql.PreparedStatement; |
| | | import java.sql.SQLException; |
| | | import java.sql.Wrapper; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Service |
| | | @AllArgsConstructor |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Slf4j |
| | | public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService { |
| | | |
| | | DepartmentMapper departmentMapper; |
| | | |
| | | |
| | | |
| | | |
| | | //添加部门 |
| | |
| | | //获取部门树 |
| | | @Override |
| | | public List<DepartmentDto> selectDepartment() { |
| | | List<Department> departments = departmentMapper.selectDepartment(); |
| | | // 假设 resultSet 是从数据库查询返回的结果集 |
| | | List<DepartmentDto> departmentDtos = new ArrayList<>(); |
| | | Map<Integer, DepartmentDto> departmentMap = new HashMap<>(); |
| | | List<DepartmentDto> departments = departmentMapper.selectDepartment(); |
| | | //获取父节点 |
| | | List<DepartmentDto> collect = departments.stream().filter(m -> m.getFatherId() == null).peek( |
| | | (m) -> m.setChildren(getChildren(m, departments)) |
| | | ).collect(Collectors.toList()); |
| | | return collect; |
| | | } |
| | | |
| | | for (Department department : departments) { |
| | | DepartmentDto departmentDto = new DepartmentDto(department.getId(), department.getName(), department.getFatherId(), new ArrayList<DepartmentDto>()); |
| | | departmentMap.put(department.getId(), departmentDto); |
| | | if (department.getFatherId() == null) { |
| | | // 根部门 |
| | | departmentDtos.add(departmentDto); |
| | | } else { |
| | | // 将当前部门添加到父部门的 children 列表中 |
| | | DepartmentDto parent = departmentMap.get(department.getFatherId()); |
| | | if (parent != null) { |
| | | parent.getChildren().add(departmentDto); |
| | | } |
| | | } |
| | | } |
| | | return departmentDtos; |
| | | /** |
| | | * 递归查询子节点 |
| | | * @param root 根节点 |
| | | * @param all 所有节点 |
| | | * @return 根节点信息 |
| | | */ |
| | | private List<DepartmentDto> getChildren(DepartmentDto root, List<DepartmentDto> all) { |
| | | return all.stream().filter(m -> Objects.equals(m.getFatherId(), root.getId())).peek( |
| | | (m) -> m.setChildren(getChildren(m, all)) |
| | | ).collect(Collectors.toList()); |
| | | } |
| | | |
| | | //删除部门 |
| | |
| | | return removeBatchByIds(department); |
| | | } |
| | | |
| | | |
| | | //判断是否有子类,直到没有为止 |
| | | public List<Department> getDepartment(Integer id){ |
| | | public List<Department> getDepartment(Integer id) { |
| | | List<Department> list = new ArrayList<>(); |
| | | Department depart = baseMapper.selectById(id); |
| | | list.add(depart); |
| | | List<Department> departments = baseMapper.selectList(Wrappers.<Department>lambdaQuery().eq(Department::getFatherId, id)); |
| | | if (ObjectUtils.isNotEmpty(departments)){ |
| | | if (ObjectUtils.isNotEmpty(departments)) { |
| | | list.addAll(departments); |
| | | for (Department department : departments){ |
| | | for (Department department : departments) { |
| | | list.addAll(getDepartment(department.getId())); |
| | | } |
| | | } |