package com.example.service.impl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.example.entity.Department;
|
import com.example.mapper.DepartmentMapper;
|
import com.example.service.DepartmentService;
|
import org.springframework.stereotype.Service;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
@Service
|
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
|
|
@Override
|
public List<Department> getDepartmentTree() {
|
return baseMapper.selectByParentCode("0");
|
}
|
|
@Override
|
public Department getByDeptCode(String deptCode) {
|
return baseMapper.selectByDeptCode(deptCode);
|
}
|
|
@Override
|
public boolean checkDeptCodeExists(String deptCode) {
|
return baseMapper.selectByDeptCode(deptCode) != null;
|
}
|
|
@Override
|
public List<String> getChildDeptCodes(String deptCode) {
|
List<String> result = new ArrayList<>();
|
result.add(deptCode);
|
collectChildDeptCodes(deptCode, result);
|
return result;
|
}
|
|
private void collectChildDeptCodes(String parentCode, List<String> result) {
|
if (parentCode == null || parentCode.isEmpty()) {
|
return;
|
}
|
List<Department> children = baseMapper.selectByParentCode(parentCode);
|
for (Department child : children) {
|
if (child.getDeptCode() != null && !child.getDeptCode().equals(parentCode)) {
|
result.add(child.getDeptCode());
|
collectChildDeptCodes(child.getDeptCode(), result);
|
}
|
}
|
}
|
|
@Override
|
public List<Department> getDepartmentTreeWithPermission(String deptCode) {
|
Department currentDept = getByDeptCode(deptCode);
|
if (currentDept == null) {
|
return new ArrayList<>();
|
}
|
List<Department> result = new ArrayList<>();
|
result.add(currentDept);
|
collectChildDepartments(deptCode, result);
|
return result;
|
}
|
|
private void collectChildDepartments(String parentCode, List<Department> result) {
|
if (parentCode == null || parentCode.isEmpty()) {
|
return;
|
}
|
List<Department> children = baseMapper.selectByParentCode(parentCode);
|
for (Department child : children) {
|
if (child.getDeptCode() != null && !child.getDeptCode().equals(parentCode)) {
|
result.add(child);
|
collectChildDepartments(child.getDeptCode(), result);
|
}
|
}
|
}
|
}
|