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