1. 新建统一的查询用户名称工具类
2. 新增业务附件带 url 查询返回 创建人信息
This commit is contained in:
@@ -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 <T> void setCreatorNames(List<T> items,
|
||||||
|
Function<T, String> creatorExtractor,
|
||||||
|
BiConsumer<T, String> creatorNameSetter,
|
||||||
|
Function<Collection<Long>, Map<Long, String>> userNicknameProvider) {
|
||||||
|
if (CollUtil.isEmpty(items) || userNicknameProvider == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提取所有创建人ID
|
||||||
|
Set<Long> 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<Long, String> 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 <T> void setCreatorName(T item,
|
||||||
|
Function<T, String> creatorExtractor,
|
||||||
|
BiConsumer<T, String> creatorNameSetter,
|
||||||
|
Function<Collection<Long>, Map<Long, String>> userNicknameProvider) {
|
||||||
|
if (item == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 直接复用批量设置的逻辑
|
||||||
|
setCreatorNames(Collections.singletonList(item), creatorExtractor, creatorNameSetter, userNicknameProvider);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建通用的用户昵称提供者
|
||||||
|
* 这是一个通用的实现,可以直接在各个模块中使用
|
||||||
|
*
|
||||||
|
* @param userDataProvider 用户数据提供者,接收用户ID集合,返回用户数据Map
|
||||||
|
* @param nicknameExtractor 从用户数据中提取昵称的函数
|
||||||
|
* @return 用户昵称提供者函数
|
||||||
|
*/
|
||||||
|
public static <UserData> Function<Collection<Long>, Map<Long, String>> createUserNicknameProvider(
|
||||||
|
Function<Collection<Long>, Map<Long, UserData>> userDataProvider,
|
||||||
|
Function<UserData, String> nicknameExtractor) {
|
||||||
|
return userIds -> {
|
||||||
|
if (CollUtil.isEmpty(userIds)) {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<Long, UserData> 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的情况
|
||||||
|
));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -46,4 +46,14 @@ public class BusinessFileWithUrlRespDTO extends BusinessFileRespDTO {
|
|||||||
*/
|
*/
|
||||||
private Integer fileSize;
|
private Integer fileSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建人
|
||||||
|
*/
|
||||||
|
private String creator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建人名称
|
||||||
|
*/
|
||||||
|
private String creatorName;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
package com.zt.plat.module.infra.controller.admin.businessfile.vo;
|
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 io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.*;
|
import lombok.Data;
|
||||||
import java.util.*;
|
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import com.alibaba.excel.annotation.*;
|
|
||||||
|
|
||||||
@Schema(description = "管理后台 - 业务附件关联 Response VO")
|
@Schema(description = "管理后台 - 业务附件关联 Response VO")
|
||||||
@Data
|
@Data
|
||||||
@@ -40,4 +40,10 @@ public class BusinessFileRespVO {
|
|||||||
@ExcelProperty("创建时间")
|
@ExcelProperty("创建时间")
|
||||||
private LocalDateTime createTime;
|
private LocalDateTime createTime;
|
||||||
|
|
||||||
|
@Schema(description = "创建人", example = "1024")
|
||||||
|
private String creator;
|
||||||
|
|
||||||
|
@Schema(description = "创建人名称", example = "张三")
|
||||||
|
private String creatorName;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -43,6 +43,9 @@ public class BusinessFileWithUrlRespVO extends BusinessFileRespVO {
|
|||||||
@Schema(description = "文件大小", example = "2048")
|
@Schema(description = "文件大小", example = "2048")
|
||||||
private Integer fileSize;
|
private Integer fileSize;
|
||||||
|
|
||||||
|
@Schema(description = "创建人名称", example = "张三")
|
||||||
|
private String creatorName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取文件 URL(参考 FileRespVO 的实现)
|
* 获取文件 URL(参考 FileRespVO 的实现)
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -3,14 +3,18 @@ package com.zt.plat.module.infra.service.businessfile;
|
|||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import com.zt.plat.framework.common.pojo.PageResult;
|
import com.zt.plat.framework.common.pojo.PageResult;
|
||||||
import com.zt.plat.framework.common.util.object.BeanUtils;
|
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.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.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.BusinessFileSaveReqVO;
|
||||||
import com.zt.plat.module.infra.controller.admin.businessfile.vo.BusinessFileWithUrlRespVO;
|
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.businessfile.BusinessFileDO;
|
||||||
import com.zt.plat.module.infra.dal.dataobject.file.FileDO;
|
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.dal.mysql.businessfile.BusinessFileMapper;
|
||||||
import com.zt.plat.module.infra.service.file.FileService;
|
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 jakarta.annotation.Resource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
@@ -35,6 +39,9 @@ public class BusinessFileServiceImpl implements BusinessFileService {
|
|||||||
@Resource
|
@Resource
|
||||||
private FileService fileService;
|
private FileService fileService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private AdminUserApi adminUserApi;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Long createBusinessFile(BusinessFileSaveReqVO createReqVO) {
|
public Long createBusinessFile(BusinessFileSaveReqVO createReqVO) {
|
||||||
// 插入
|
// 插入
|
||||||
@@ -97,13 +104,40 @@ public class BusinessFileServiceImpl implements BusinessFileService {
|
|||||||
PageResult<BusinessFileDO> pageResult = getBusinessFilePage(pageReqVO);
|
PageResult<BusinessFileDO> pageResult = getBusinessFilePage(pageReqVO);
|
||||||
PageResult<BusinessFileWithUrlRespVO> result = BeanUtils.toBean(pageResult, BusinessFileWithUrlRespVO.class);
|
PageResult<BusinessFileWithUrlRespVO> result = BeanUtils.toBean(pageResult, BusinessFileWithUrlRespVO.class);
|
||||||
|
|
||||||
// 批量获取文件信息并设置URL
|
if (CollUtil.isEmpty(result.getList())) {
|
||||||
List<Long> fileIds = result.getList().stream()
|
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<BusinessFileWithUrlRespVO> voList) {
|
||||||
|
// 批量获取文件信息
|
||||||
|
List<Long> fileIds = voList.stream()
|
||||||
.map(BusinessFileWithUrlRespVO::getFileId)
|
.map(BusinessFileWithUrlRespVO::getFileId)
|
||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
.distinct()
|
.distinct()
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
|
if (fileIds.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// 批量查询文件信息
|
// 批量查询文件信息
|
||||||
Map<Long, FileDO> fileMap = new HashMap<>();
|
Map<Long, FileDO> fileMap = new HashMap<>();
|
||||||
for (Long fileId : fileIds) {
|
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) {
|
if (vo.getFileId() != null) {
|
||||||
FileDO fileDO = fileMap.get(vo.getFileId());
|
FileDO fileDO = fileMap.get(vo.getFileId());
|
||||||
if (fileDO != null) {
|
if (fileDO != null) {
|
||||||
@@ -125,8 +159,7 @@ public class BusinessFileServiceImpl implements BusinessFileService {
|
|||||||
vo.setFileSize(fileDO.getSize());
|
vo.setFileSize(fileDO.getSize());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user