| | |
| | | |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.yuanchu.mom.common.PrintChina; |
| | | import com.yuanchu.mom.dto.DepartmentDto; |
| | | import com.yuanchu.mom.dto.UserDto; |
| | | import com.yuanchu.mom.dto.UserPageDto; |
| | | import com.yuanchu.mom.pojo.Department; |
| | | import com.yuanchu.mom.mapper.DepartmentMapper; |
| | | import com.yuanchu.mom.pojo.Device; |
| | | import com.yuanchu.mom.pojo.User; |
| | | import com.yuanchu.mom.service.DepartmentService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.yuanchu.mom.utils.QueryWrappers; |
| | | 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.extern.slf4j.Slf4j; |
| | | import lombok.var; |
| | | 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; |
| | | |
| | | /** |
| | |
| | | DepartmentMapper departmentMapper; |
| | | |
| | | |
| | | |
| | | |
| | | //添加部门 |
| | | @Override |
| | | public int addDepartment(Department department) { |
| | |
| | | //获取部门树 |
| | | @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(); |
| | | departments.addAll(departmentMapper.selectCustomList()); |
| | | //获取父节点 |
| | | 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()); |
| | | } |
| | | |
| | | //删除部门 |
| | |
| | | List<Department> department = getDepartment(id); |
| | | return removeBatchByIds(department); |
| | | } |
| | | |
| | | //根据选择的树展示相关的人员 |
| | | @Override |
| | | public Map<String, Object> showUserById(Page page, UserDto user) { |
| | | //根据部门id,查询他的所有子类id |
| | | List<Integer> list = departmentMapper.selectSonById(Integer.parseInt(user.getDepartId())); |
| | | List<String> ids = list.stream() |
| | | .map(Object::toString) |
| | | //.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))); |
| | | return map; |
| | | } |
| | | |
| | | |
| | | //判断是否有子类,直到没有为止 |
| | | public List<Department> getDepartment(Integer id) { |
| | |
| | | return list; |
| | | } |
| | | |
| | | @Override |
| | | public List<Department> selectDepartmentEnum() { |
| | | return departmentMapper.selectList(Wrappers.<Department>lambdaQuery().isNotNull(Department::getFatherId).select(Department::getId,Department::getName)); |
| | | } |
| | | } |