fix: exclude inactive parent departments from publishing

This commit is contained in:
Your Name 2026-03-30 18:38:36 +08:00
parent f0387a6a65
commit 6635a248e6
3 changed files with 56 additions and 1 deletions

View File

@ -868,7 +868,8 @@ public class DepartmentService {
continue;
}
if ("demand".equals(dept.getDeptType()) || "primary_demand".equals(dept.getDeptType())) {
if (isDepartmentActiveWithAncestors(dept.getId())
&& ("demand".equals(dept.getDeptType()) || "primary_demand".equals(dept.getDeptType()))) {
result.add(dept);
}
@ -881,6 +882,7 @@ public class DepartmentService {
);
childDepts.stream()
.filter(child -> isDepartmentActiveWithAncestors(child.getId()))
.filter(child -> "demand".equals(child.getDeptType()) || "primary_demand".equals(child.getDeptType()))
.forEach(result::add);
}
@ -893,4 +895,29 @@ public class DepartmentService {
map -> new ArrayList<>(map.values())
));
}
/**
* 部门自身及其所有父级都必须是启用且未删除,才视为可用部门
*/
public boolean isDepartmentActiveWithAncestors(Long deptId) {
if (deptId == null) {
return false;
}
Department current = getDepartmentById(deptId);
while (current != null) {
if (current.getStatus() == null || current.getStatus() != 1) {
return false;
}
if (current.getDeleted() != null && current.getDeleted() == 1) {
return false;
}
if (current.getParentId() == null || current.getParentId() == 0) {
break;
}
current = getDepartmentById(current.getParentId());
}
return true;
}
}

View File

@ -1332,6 +1332,7 @@ public class TaskService {
.filter(java.util.Objects::nonNull)
.filter(dept -> dept.getStatus() != null && dept.getStatus() == 1)
.filter(dept -> dept.getDeleted() == null || dept.getDeleted() == 0)
.filter(dept -> departmentService.isDepartmentActiveWithAncestors(dept.getId()))
.filter(dept -> "demand".equals(dept.getDeptType()) || "primary_demand".equals(dept.getDeptType()))
.distinct()
.collect(Collectors.toList());
@ -1350,6 +1351,9 @@ public class TaskService {
if (requestedDept == null) {
throw new BusinessException(ResultCode.PARAM_ERROR, "发起部门不存在");
}
if (!departmentService.isDepartmentActiveWithAncestors(requestedDeptId)) {
throw new BusinessException(ResultCode.PARAM_ERROR, "发起部门已停用,请选择其他部门");
}
if (!"demand".equals(requestedDept.getDeptType()) && !"primary_demand".equals(requestedDept.getDeptType())) {
throw new BusinessException(ResultCode.PARAM_ERROR, "发起部门必须是需求部门或一级需求部门");
}

View File

@ -698,6 +698,7 @@ const availablePublishingDepartments = computed<PublishingDepartmentOption[]>(()
departments.forEach((dept) => {
if (!dept?.deptId || seen.has(dept.deptId)) return
if (dept.deptType !== 'demand' && dept.deptType !== 'primary_demand') return
if (!isDepartmentActiveWithAncestors(dept.deptId)) return
result.push({
deptId: dept.deptId,
deptName: dept.deptName,
@ -711,6 +712,7 @@ const availablePublishingDepartments = computed<PublishingDepartmentOption[]>(()
managedDepartments.forEach((dept) => {
if (!dept?.deptId || seen.has(dept.deptId)) return
if (dept.deptType !== 'demand' && dept.deptType !== 'primary_demand') return
if (!isDepartmentActiveWithAncestors(dept.deptId)) return
result.push({
deptId: dept.deptId,
deptName: dept.deptName,
@ -723,6 +725,7 @@ const availablePublishingDepartments = computed<PublishingDepartmentOption[]>(()
if (
userInfo.deptId &&
!seen.has(userInfo.deptId) &&
isDepartmentActiveWithAncestors(userInfo.deptId) &&
(userInfo.deptType === 'demand' || userInfo.deptType === 'primary_demand')
) {
result.unshift({
@ -1048,6 +1051,27 @@ const getDepartmentById = (deptId?: number | null) => {
return departmentList.value.find(d => d.id === deptId)
}
const isDepartmentActiveWithAncestors = (deptId?: number | null) => {
if (!deptId) return false
if (!departmentList.value.length) return true
let currentDept = getDepartmentById(deptId)
while (currentDept) {
if (currentDept.status !== undefined && currentDept.status !== 1) {
return false
}
if ((currentDept as any).deleted === 1) {
return false
}
if (!currentDept.parentId) {
break
}
currentDept = getDepartmentById(currentDept.parentId)
}
return true
}
const getDepartmentDisplayName = (
dept?: departmentApi.DepartmentOverview | null,
duplicateNames?: Set<string>