From 39963e6ae2ef4c76e84dd002a4ce5a26c9c34ac3 Mon Sep 17 00:00:00 2001 From: chenbowen Date: Tue, 23 Sep 2025 14:57:06 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E6=96=B0=E5=BB=BA=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E7=9A=84=E6=9F=A5=E8=AF=A2=E7=94=A8=E6=88=B7=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=202.=20=E6=96=B0=E5=A2=9E=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1=E9=99=84=E4=BB=B6=E5=B8=A6=20url=20=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=20=E5=88=9B=E5=BB=BA=E4=BA=BA=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/util/user/UserNameEnrichUtils.java | 123 ++++++++++++++++++ .../dto/BusinessFileWithUrlRespDTO.java | 10 ++ .../businessfile/vo/BusinessFileRespVO.java | 14 +- .../vo/BusinessFileWithUrlRespVO.java | 3 + .../businessfile/BusinessFileServiceImpl.java | 43 +++++- 5 files changed, 184 insertions(+), 9 deletions(-) create mode 100644 zt-framework/zt-common/src/main/java/com/zt/plat/framework/common/util/user/UserNameEnrichUtils.java diff --git a/zt-framework/zt-common/src/main/java/com/zt/plat/framework/common/util/user/UserNameEnrichUtils.java b/zt-framework/zt-common/src/main/java/com/zt/plat/framework/common/util/user/UserNameEnrichUtils.java new file mode 100644 index 00000000..e5ea33a7 --- /dev/null +++ b/zt-framework/zt-common/src/main/java/com/zt/plat/framework/common/util/user/UserNameEnrichUtils.java @@ -0,0 +1,123 @@ +package com.zt.plat.framework.common.util.user; + +import cn.hutool.core.collection.CollUtil; + +import java.util.*; +import java.util.function.BiConsumer; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * 用户名称填充工具类 + * 通用的用户信息填充工具,可在各个模块中使用 + * + * @author ZT + */ +public class UserNameEnrichUtils { + + /** + * 批量设置创建人名称 + * + * @param items 需要设置创建人名称的对象列表 + * @param creatorExtractor 创建人ID提取器 + * @param creatorNameSetter 创建人名称设置器 + * @param userNicknameProvider 用户昵称提供者函数 + */ + public static void setCreatorNames(List items, + Function creatorExtractor, + BiConsumer creatorNameSetter, + Function, Map> userNicknameProvider) { + if (CollUtil.isEmpty(items) || userNicknameProvider == null) { + return; + } + + // 提取所有创建人ID + Set creatorIds = items.stream() + .map(creatorExtractor) + .filter(Objects::nonNull) + .filter(creator -> !creator.isEmpty()) + .map(creatorId -> { + try { + return Long.parseLong(creatorId); + } catch (NumberFormatException e) { + return null; + } + }) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); + + if (creatorIds.isEmpty()) { + return; + } + + // 批量获取用户昵称 + Map userNicknameMap = userNicknameProvider.apply(creatorIds); + + // 设置创建人名称 + items.forEach(item -> { + String creatorId = creatorExtractor.apply(item); + if (creatorId != null && !creatorId.isEmpty()) { + try { + Long id = Long.parseLong(creatorId); + String nickname = userNicknameMap.get(id); + if (nickname != null) { + creatorNameSetter.accept(item, nickname); + } + } catch (NumberFormatException e) { + // 忽略无效的ID格式 + } + } + }); + } + + /** + * 设置单个对象的创建人名称 + * 直接复用批量设置的逻辑 + * + * @param item 需要设置创建人名称的对象 + * @param creatorExtractor 创建人ID提取器 + * @param creatorNameSetter 创建人名称设置器 + * @param userNicknameProvider 用户昵称提供者函数 + */ + public static void setCreatorName(T item, + Function creatorExtractor, + BiConsumer creatorNameSetter, + Function, Map> userNicknameProvider) { + if (item == null) { + return; + } + // 直接复用批量设置的逻辑 + setCreatorNames(Collections.singletonList(item), creatorExtractor, creatorNameSetter, userNicknameProvider); + } + + /** + * 创建通用的用户昵称提供者 + * 这是一个通用的实现,可以直接在各个模块中使用 + * + * @param userDataProvider 用户数据提供者,接收用户ID集合,返回用户数据Map + * @param nicknameExtractor 从用户数据中提取昵称的函数 + * @return 用户昵称提供者函数 + */ + public static Function, Map> createUserNicknameProvider( + Function, Map> userDataProvider, + Function nicknameExtractor) { + return userIds -> { + if (CollUtil.isEmpty(userIds)) { + return Collections.emptyMap(); + } + + Map userDataMap = userDataProvider.apply(userIds); + if (userDataMap == null || userDataMap.isEmpty()) { + return Collections.emptyMap(); + } + + return userDataMap.entrySet().stream() + .filter(entry -> entry.getValue() != null) + .collect(Collectors.toMap( + Map.Entry::getKey, + entry -> nicknameExtractor.apply(entry.getValue()), + (existing, replacement) -> existing // 处理重复key的情况 + )); + }; + } +} \ No newline at end of file diff --git a/zt-module-infra/zt-module-infra-api/src/main/java/com/zt/plat/module/infra/api/businessfile/dto/BusinessFileWithUrlRespDTO.java b/zt-module-infra/zt-module-infra-api/src/main/java/com/zt/plat/module/infra/api/businessfile/dto/BusinessFileWithUrlRespDTO.java index 650ba4b5..a9293dea 100644 --- a/zt-module-infra/zt-module-infra-api/src/main/java/com/zt/plat/module/infra/api/businessfile/dto/BusinessFileWithUrlRespDTO.java +++ b/zt-module-infra/zt-module-infra-api/src/main/java/com/zt/plat/module/infra/api/businessfile/dto/BusinessFileWithUrlRespDTO.java @@ -46,4 +46,14 @@ public class BusinessFileWithUrlRespDTO extends BusinessFileRespDTO { */ private Integer fileSize; + /** + * 创建人 + */ + private String creator; + + /** + * 创建人名称 + */ + private String creatorName; + } \ No newline at end of file diff --git a/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/controller/admin/businessfile/vo/BusinessFileRespVO.java b/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/controller/admin/businessfile/vo/BusinessFileRespVO.java index c0e485ab..f5be615b 100644 --- a/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/controller/admin/businessfile/vo/BusinessFileRespVO.java +++ b/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/controller/admin/businessfile/vo/BusinessFileRespVO.java @@ -1,11 +1,11 @@ package com.zt.plat.module.infra.controller.admin.businessfile.vo; +import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; +import com.alibaba.excel.annotation.ExcelProperty; import io.swagger.v3.oas.annotations.media.Schema; -import lombok.*; -import java.util.*; -import org.springframework.format.annotation.DateTimeFormat; +import lombok.Data; + import java.time.LocalDateTime; -import com.alibaba.excel.annotation.*; @Schema(description = "管理后台 - 业务附件关联 Response VO") @Data @@ -40,4 +40,10 @@ public class BusinessFileRespVO { @ExcelProperty("创建时间") private LocalDateTime createTime; + @Schema(description = "创建人", example = "1024") + private String creator; + + @Schema(description = "创建人名称", example = "张三") + private String creatorName; + } \ No newline at end of file diff --git a/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/controller/admin/businessfile/vo/BusinessFileWithUrlRespVO.java b/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/controller/admin/businessfile/vo/BusinessFileWithUrlRespVO.java index 0fb9dc84..95717360 100644 --- a/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/controller/admin/businessfile/vo/BusinessFileWithUrlRespVO.java +++ b/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/controller/admin/businessfile/vo/BusinessFileWithUrlRespVO.java @@ -43,6 +43,9 @@ public class BusinessFileWithUrlRespVO extends BusinessFileRespVO { @Schema(description = "文件大小", example = "2048") private Integer fileSize; + @Schema(description = "创建人名称", example = "张三") + private String creatorName; + /** * 获取文件 URL(参考 FileRespVO 的实现) */ diff --git a/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/service/businessfile/BusinessFileServiceImpl.java b/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/service/businessfile/BusinessFileServiceImpl.java index 1377ebee..e53b539a 100644 --- a/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/service/businessfile/BusinessFileServiceImpl.java +++ b/zt-module-infra/zt-module-infra-server/src/main/java/com/zt/plat/module/infra/service/businessfile/BusinessFileServiceImpl.java @@ -3,14 +3,18 @@ package com.zt.plat.module.infra.service.businessfile; import cn.hutool.core.collection.CollUtil; import com.zt.plat.framework.common.pojo.PageResult; import com.zt.plat.framework.common.util.object.BeanUtils; +import com.zt.plat.framework.common.util.user.UserNameEnrichUtils; import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX; import com.zt.plat.module.infra.controller.admin.businessfile.vo.BusinessFilePageReqVO; +import com.zt.plat.module.infra.controller.admin.businessfile.vo.BusinessFileRespVO; import com.zt.plat.module.infra.controller.admin.businessfile.vo.BusinessFileSaveReqVO; import com.zt.plat.module.infra.controller.admin.businessfile.vo.BusinessFileWithUrlRespVO; import com.zt.plat.module.infra.dal.dataobject.businessfile.BusinessFileDO; import com.zt.plat.module.infra.dal.dataobject.file.FileDO; import com.zt.plat.module.infra.dal.mysql.businessfile.BusinessFileMapper; import com.zt.plat.module.infra.service.file.FileService; +import com.zt.plat.module.system.api.user.AdminUserApi; +import com.zt.plat.module.system.api.user.dto.AdminUserRespDTO; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -35,6 +39,9 @@ public class BusinessFileServiceImpl implements BusinessFileService { @Resource private FileService fileService; + @Resource + private AdminUserApi adminUserApi; + @Override public Long createBusinessFile(BusinessFileSaveReqVO createReqVO) { // 插入 @@ -97,13 +104,40 @@ public class BusinessFileServiceImpl implements BusinessFileService { PageResult pageResult = getBusinessFilePage(pageReqVO); PageResult result = BeanUtils.toBean(pageResult, BusinessFileWithUrlRespVO.class); - // 批量获取文件信息并设置URL - List fileIds = result.getList().stream() + if (CollUtil.isEmpty(result.getList())) { + return result; + } + + // 批量设置文件信息 + setFileInfo(result.getList()); + + // 批量设置创建人名称 + UserNameEnrichUtils.setCreatorNames(result.getList(), + BusinessFileRespVO::getCreator, + BusinessFileWithUrlRespVO::setCreatorName, + UserNameEnrichUtils.createUserNicknameProvider( + userIds -> adminUserApi.getUserMap(userIds), + AdminUserRespDTO::getNickname + )); + + return result; + } + + /** + * 批量设置文件信息 + */ + private void setFileInfo(List voList) { + // 批量获取文件信息 + List fileIds = voList.stream() .map(BusinessFileWithUrlRespVO::getFileId) .filter(Objects::nonNull) .distinct() .toList(); + if (fileIds.isEmpty()) { + return; + } + // 批量查询文件信息 Map fileMap = new HashMap<>(); for (Long fileId : fileIds) { @@ -114,7 +148,7 @@ public class BusinessFileServiceImpl implements BusinessFileService { } // 设置文件相关信息 - for (BusinessFileWithUrlRespVO vo : result.getList()) { + voList.forEach(vo -> { if (vo.getFileId() != null) { FileDO fileDO = fileMap.get(vo.getFileId()); if (fileDO != null) { @@ -125,8 +159,7 @@ public class BusinessFileServiceImpl implements BusinessFileService { vo.setFileSize(fileDO.getSize()); } } - } - return result; + }); } @Override