| | |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.yuanchu.mom.utils.QueryWrappers; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import lombok.var; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | |
| | | 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.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | @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()); |
| | | } |
| | | |
| | | //删除部门 |
| | |
| | | public Map<String, Object> showUserById(Page page, UserDto user) { |
| | | //根据部门id,查询他的所有子类id |
| | | List<Integer> list = departmentMapper.selectSonById(Integer.parseInt(user.getDepartId())); |
| | | String ids = list.stream() |
| | | List<String> ids = list.stream() |
| | | .map(Object::toString) |
| | | .collect(Collectors.joining(",")); |
| | | //.collect(Collectors.joining(",")); |
| | | .collect(Collectors.toList()); |
| | | log.info(ids+"\n"); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("head", PrintChina.printChina(UserDto.class)); |
| | | map.put("body", departmentMapper.showUserById(page, ids, QueryWrappers.queryWrappers(user))); |