package com.yuanchu.mom.service.impl; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.yuanchu.mom.dto.DepartmentDto; import com.yuanchu.mom.mapper.DepartmentLimsMapper; import com.yuanchu.mom.pojo.DepartmentLims; import com.yuanchu.mom.service.DepartmentLimsService; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * @author z1292 * @description 针对表【department_lims(部门明细)】的数据库操作Service实现 * @createDate 2024-05-22 14:08:17 */ @Service @AllArgsConstructor public class DepartmentLimsServiceImpl extends ServiceImpl implements DepartmentLimsService{ DepartmentLimsMapper departmentMapper; @Override public int addDepartment(DepartmentLims department) { departmentMapper.insert(department); return department.getId(); } //获取部门树 @Override public List selectDepartment() { List departments = departmentMapper.selectDepartment(); //获取父节点 List collect = departments.stream().filter(m -> m.getFatherId() == null).peek( (m) -> m.setChildren(getChildren(m, departments)) ).collect(Collectors.toList()); return collect; } /** * 递归查询子节点 * @param root 根节点 * @param all 所有节点 * @return 根节点信息 */ private List getChildren(DepartmentDto root, List all) { return all.stream().filter(m -> Objects.equals(m.getFatherId(), root.getId())).peek( (m) -> m.setChildren(getChildren(m, all)) ).collect(Collectors.toList()); } //删除部门 @Override public boolean delDepartment(Integer id) { //判断是否有子类,直到没有为止 List department = getDepartment(id); return removeBatchByIds(department); } //判断是否有子类,直到没有为止 public List getDepartment(Integer id) { List list = new ArrayList<>(); DepartmentLims depart = baseMapper.selectById(id); list.add(depart); List departments = baseMapper.selectList(Wrappers.lambdaQuery().eq(DepartmentLims::getFatherId, id)); if (ObjectUtils.isNotEmpty(departments)) { list.addAll(departments); for (DepartmentLims department : departments) { list.addAll(getDepartment(department.getId())); } } return list; } }