refactor(notification): 优化质量通知用户关联逻辑

- 移除手动选择用户的条件判断,统一调用用户关联保存方法
- 更新业务错误验证逻辑,检查用户列表和部门列表均为空的情况
- 重构saveNotificationUsers方法,区分全部用户和手动选择用户的处理流程
- 修复查询条件中的空格格式问题
- 优化代码结构,提高可读性和维护性
This commit is contained in:
曹鹏飞 2026-06-13 14:25:14 +08:00
parent 5e88f5c36e
commit 8d3d446df2
1 changed files with 71 additions and 53 deletions

View File

@ -100,10 +100,7 @@ public class QualityNotificationControllerService {
qualityNotificationFileService.saveBatch(attachments);
}
// 如果是手动选择用户保存用户关联
if (request.getTargetType() == 2) {
saveNotificationUsers(entity.getId(), request, operator, operatorId, now);
}
saveNotificationUsers(entity.getId(), request, operator, operatorId, now);
}
/**
@ -116,8 +113,10 @@ public class QualityNotificationControllerService {
// 如果是手动选择用户必须提供用户ID列表
if (request.getTargetType() == 2) {
VUtil.trueThrowBusinessError(CollectionUtil.isEmpty(request.getUserIds()))
.throwMessage("手动选择用户时,用户列表不能为空");
VUtil.trueThrowBusinessError(CollectionUtil.isEmpty(request.getUserIds())
&& CollectionUtil.isEmpty(request.getDepartmentIds())
)
.throwMessage("手动选择用户时,用户列表和部门列表不能为空");
}
String operator = UserUtil.getUserName();
@ -141,9 +140,7 @@ public class QualityNotificationControllerService {
.remove();
// 如果是手动选择用户重新保存用户关联
if (request.getTargetType() == 2 && CollectionUtil.isNotEmpty(request.getUserIds())) {
saveNotificationUsers(request.getId(), request, operator, operatorId, now);
}
saveNotificationUsers(request.getId(), request, operator, operatorId, now);
if (CollectionUtil.isNotEmpty(request.getAttachments())) {
qualityNotificationFileService.lambdaUpdate()
@ -218,7 +215,7 @@ public class QualityNotificationControllerService {
if (entity.getTargetType() == 2) {
List<QmsQualityNotificationTarget> users = notificationUserService.lambdaQuery()
.eq(QmsQualityNotificationTarget::getNotificationId, id)
.eq(QmsQualityNotificationTarget::getSourceType,0)
.eq(QmsQualityNotificationTarget::getSourceType, 0)
.list();
if (CollectionUtil.isNotEmpty(users)) {
List<Long> userIds = users.stream()
@ -272,49 +269,14 @@ public class QualityNotificationControllerService {
*/
private void saveNotificationUsers(Long notificationId, QmsQualityNotificationAddQO request,
String operator, Long operatorId, LocalDateTime now) {
List<QmsQualityNotificationTarget> datas = new ArrayList<>();
if (CollectionUtil.isNotEmpty(request.getUserIds())) {
datas.addAll(
request.getUserIds().stream()
.map(userId -> new QmsQualityNotificationTarget()
.setNotificationId(notificationId)
.setSourceId(userId)
.setSourceType(0)
.setCreateById(operatorId)
.setCreateBy(operator)
.setCreateTime(now))
.toList()
);
}
if (CollectionUtil.isNotEmpty(request.getDepartmentIds())) {
datas.addAll(
request.getDepartmentIds().stream()
.map(departmentId -> new QmsQualityNotificationTarget()
.setNotificationId(notificationId)
.setSourceId(departmentId)
.setSourceType(1)
.setCreateById(operatorId)
.setCreateBy(operator)
.setCreateTime(now))
.toList()
);
}
List<Long> userIds = request.getUserIds();
if (CollectionUtil.isNotEmpty(request.getDepartmentIds())) {
if (CollectionUtil.isEmpty(userIds)) {
userIds = new ArrayList<>();
}
userIds.addAll(
userInteriorService.lambdaQuery()
.in(UserInterior::getDeptId, request.getDepartmentIds())
.list()
if (request.getTargetType() == 1) {
userService.lambdaQuery()
.in(User::getId, userInteriorService.list()
.stream()
.map(UserInterior::getUserId)
.toList()
);
}
if (CollectionUtil.isNotEmpty(userIds)) {
userService.listByIds(userIds)
)
.list()
.forEach(user -> {
todoItemService.add(new QmsTodoItem()
.setTitle(request.getTitle())
@ -326,8 +288,64 @@ public class QualityNotificationControllerService {
.setCreateTime(LocalDateTime.now())
);
});
} else if (request.getTargetType() == 2) {
List<QmsQualityNotificationTarget> datas = new ArrayList<>();
if (CollectionUtil.isNotEmpty(request.getUserIds())) {
datas.addAll(
request.getUserIds().stream()
.map(userId -> new QmsQualityNotificationTarget()
.setNotificationId(notificationId)
.setSourceId(userId)
.setSourceType(0)
.setCreateById(operatorId)
.setCreateBy(operator)
.setCreateTime(now))
.toList()
);
}
if (CollectionUtil.isNotEmpty(request.getDepartmentIds())) {
datas.addAll(
request.getDepartmentIds().stream()
.map(departmentId -> new QmsQualityNotificationTarget()
.setNotificationId(notificationId)
.setSourceId(departmentId)
.setSourceType(1)
.setCreateById(operatorId)
.setCreateBy(operator)
.setCreateTime(now))
.toList()
);
}
List<Long> userIds = request.getUserIds();
if (CollectionUtil.isNotEmpty(request.getDepartmentIds())) {
if (CollectionUtil.isEmpty(userIds)) {
userIds = new ArrayList<>();
}
userIds.addAll(
userInteriorService.lambdaQuery()
.in(UserInterior::getDeptId, request.getDepartmentIds())
.list()
.stream()
.map(UserInterior::getUserId)
.toList()
);
}
if (CollectionUtil.isNotEmpty(userIds)) {
userService.listByIds(userIds)
.forEach(user -> {
todoItemService.add(new QmsTodoItem()
.setTitle(request.getTitle())
.setCode(basdeSerialNumberControllerService.generateSerialNumber(47))
.setSourceId(notificationId)
.setSourceTypeId(dictionaryItemService.getIdByCode("MessageType", "QualityNotification"))
.setCreateUserId(user.getId())
.setCreateUserName(user.getUserName())
.setCreateTime(LocalDateTime.now())
);
});
}
notificationUserService.saveBatch(datas);
}
notificationUserService.saveBatch(datas);
}
/**
@ -341,7 +359,7 @@ public class QualityNotificationControllerService {
vo.setDepartmentIds(
notificationUserService.lambdaQuery()
.eq(QmsQualityNotificationTarget::getNotificationId, vo.getId())
.eq(QmsQualityNotificationTarget::getSourceType,1)
.eq(QmsQualityNotificationTarget::getSourceType, 1)
.list()
.stream()
.map(QmsQualityNotificationTarget::getSourceId)
@ -349,7 +367,7 @@ public class QualityNotificationControllerService {
);
List<QmsQualityNotificationTarget> users = notificationUserService.lambdaQuery()
.eq(QmsQualityNotificationTarget::getNotificationId, vo.getId())
.eq(QmsQualityNotificationTarget::getSourceType,0)
.eq(QmsQualityNotificationTarget::getSourceType, 0)
.list();
if (CollectionUtil.isNotEmpty(users)) {
List<Long> userIds = users.stream()