fix:文件更新,复制原文件标签

This commit is contained in:
2026-02-11 16:21:41 +08:00
parent a312da00af
commit 162ca3d385
4 changed files with 151 additions and 90 deletions

View File

@@ -32,6 +32,75 @@ public class FileDictTagQueryServiceImpl implements IFileDictTagQueryService {
private final IFileTagRelService fileTagRelService;
private final ISysConfigFeignClient sysConfigFeignClient;
@Override
public <T> void fillFileTagsForRespList(List<T> respList, Function<T, Long> idGetter) {
if (CollectionUtils.isEmpty(respList)) {
return;
}
// 收集文件ID
List<Long> fileIds = respList.stream()
.map(idGetter)
.filter(Objects::nonNull)
.distinct()
.toList();
if (CollectionUtils.isEmpty(fileIds)) {
return;
}
// 查询聚合标签
Map<Long, FileDictTagsAggDTO> fileIdToTagsMap = queryByFileIds(fileIds);
if (MapUtils.isEmpty(fileIdToTagsMap)) {
return;
}
// 通过反射,将标签字段填充到 Resp 对象中(基于 FileDictTagEnum 的配置)
for (T resp : respList) {
Long fileId = idGetter.apply(resp);
if (fileId == null) {
continue;
}
FileDictTagsAggDTO tags = fileIdToTagsMap.get(fileId);
if (tags == null) {
continue;
}
for (FileDictTagEnum tagEnum : FileDictTagEnum.values()) {
String dictValues = getDictValuesByEnum(tags, tagEnum);
String dictNames = getDictNamesByEnum(tags, tagEnum);
if (dictValues == null && dictNames == null) {
continue;
}
String dictClass = tagEnum.getDictClass();
String dictClassFieldName = tagEnum.getDictClassFieldName();
String dictValueFieldName = tagEnum.getDictValueFieldName();
String dictNameFieldName = tagEnum.getDictNameFieldName();
try {
// setXxxDictClass
String classSetterName = "set" + Character.toUpperCase(dictClassFieldName.charAt(0)) + dictClassFieldName.substring(1);
resp.getClass().getMethod(classSetterName, String.class)
.invoke(resp, dictClass);
// setXxxDictValue
if (dictValues != null) {
String valueSetterName = "set" + Character.toUpperCase(dictValueFieldName.charAt(0)) + dictValueFieldName.substring(1);
resp.getClass().getMethod(valueSetterName, String.class)
.invoke(resp, dictValues);
}
// setXxxDictName如果存在
if (dictNames != null) {
String nameSetterName = "set" + Character.toUpperCase(dictNameFieldName.charAt(0)) + dictNameFieldName.substring(1);
resp.getClass().getMethod(nameSetterName, String.class)
.invoke(resp, dictNames);
}
} catch (Exception e) {
log.warn("填充文件标签信息失败, fileId: {}, respClass: {}, dictClass: {}, error: {}",
fileId, resp.getClass().getName(), dictClass, e.getMessage());
}
}
}
}
private Map<Long, FileDictTagsAggDTO> queryByFileIds(List<Long> fileIds) {
if (CollectionUtils.isEmpty(fileIds)) {
return Collections.emptyMap();
@@ -149,73 +218,6 @@ public class FileDictTagQueryServiceImpl implements IFileDictTagQueryService {
return result;
}
@Override
public <T> void fillFileTagsForRespList(List<T> respList, Function<T, Long> idGetter) {
if (CollectionUtils.isEmpty(respList)) {
return;
}
// 收集文件ID
List<Long> fileIds = respList.stream()
.map(idGetter)
.filter(Objects::nonNull)
.distinct()
.toList();
if (CollectionUtils.isEmpty(fileIds)) {
return;
}
// 查询聚合标签
Map<Long, FileDictTagsAggDTO> fileIdToTagsMap = queryByFileIds(fileIds);
if (MapUtils.isEmpty(fileIdToTagsMap)) {
return;
}
// 通过反射,将标签字段填充到 Resp 对象中(基于 FileDictTagEnum 的配置)
for (T resp : respList) {
Long fileId = idGetter.apply(resp);
if (fileId == null) {
continue;
}
FileDictTagsAggDTO tags = fileIdToTagsMap.get(fileId);
if (tags == null) {
continue;
}
for (FileDictTagEnum tagEnum : FileDictTagEnum.values()) {
String dictValues = getDictValuesByEnum(tags, tagEnum);
String dictNames = getDictNamesByEnum(tags, tagEnum);
if (dictValues == null && dictNames == null) {
continue;
}
String dictClass = tagEnum.getDictClass();
String dictClassFieldName = tagEnum.getDictClassFieldName();
String dictValueFieldName = tagEnum.getDictValueFieldName();
String dictNameFieldName = tagEnum.getDictNameFieldName();
try {
// setXxxDictClass
String classSetterName = "set" + Character.toUpperCase(dictClassFieldName.charAt(0)) + dictClassFieldName.substring(1);
resp.getClass().getMethod(classSetterName, String.class)
.invoke(resp, dictClass);
// setXxxDictValue
if (dictValues != null) {
String valueSetterName = "set" + Character.toUpperCase(dictValueFieldName.charAt(0)) + dictValueFieldName.substring(1);
resp.getClass().getMethod(valueSetterName, String.class)
.invoke(resp, dictValues);
}
// setXxxDictName如果存在
if (dictNames != null) {
String nameSetterName = "set" + Character.toUpperCase(dictNameFieldName.charAt(0)) + dictNameFieldName.substring(1);
resp.getClass().getMethod(nameSetterName, String.class)
.invoke(resp, dictNames);
}
} catch (Exception e) {
log.warn("填充文件标签信息失败, fileId: {}, respClass: {}, dictClass: {}, error: {}",
fileId, resp.getClass().getName(), dictClass, e.getMessage());
}
}
}
}
/**
* 根据枚举类型,从聚合结果中取出对应的字典值
*/

View File

@@ -2443,6 +2443,27 @@ public class MinioFileIDataFileServiceImpl implements IDataFileService {
}
}
// 如果未传递新的字典标签,则从原有文件获取并填充
if (CollectionUtils.isEmpty(req.getDictTags())) {
fileDictTagQueryService.fillFileTagsForRespList(List.of(req), UpdateFileReq::getId);
// 如果成功填充了标签值,则设置 dictTags 列表,以便 dictTagHelper 能够识别
List<String> dictTags = buildDictTagsFromEnum(req);
if (!dictTags.isEmpty()) {
req.setDictTags(dictTags);
// 强制刷新缓存确保后续逻辑能获取到ID
// 保存标签缓存到 tempFileMetadataInfo如果有
if (CollectionUtils.isNotEmpty(req.getDictTags())) {
Map<String, Map<String, Integer>> dictIdMap = req.getDictTagIdsCache();
if (dictIdMap == null || dictIdMap.isEmpty()) {
dictIdMap = dictTagHelper.queryAndCacheDictTagIds(req);
}
req.setDictTagIdsCache(dictIdMap);
}
}
}
FileMetadataInfo fileMetadataInfo = fileMetadataInfoService.lambdaQuery().eq(FileMetadataInfo::getId, req.getId()).one();
if (fileMetadataInfo == null) {
return SdmResponse.failed("文件不存在");
@@ -2480,7 +2501,7 @@ public class MinioFileIDataFileServiceImpl implements IDataFileService {
tempFileMetadataInfo.setDictTagIdsCache(dictIdMap);
}
fileMetadataInfo.setTempMetadata(JSONObject.toJSONString(tempFileMetadataInfo));
fileMetadataInfo.setUpdateTime(LocalDateTime.now());
fileMetadataInfo.setUpdaterId(ThreadLocalContext.getUserId());
@@ -2581,6 +2602,9 @@ public class MinioFileIDataFileServiceImpl implements IDataFileService {
FileMetadataInfo newFileInfo = createFileMetadata(newFileMinioObjectKey, req.getFileName(),
req.getProjectId(), req.getAnalysisDirectionId(), req.getRemarks(), oldFileMetadataInfo.getParentId(), req.getFile().getSize()
);
newFileInfo.setRemarks(oldFileMetadataInfo.getRemarks());
newFileInfo.setProjectId(oldFileMetadataInfo.getProjectId());
newFileInfo.setAnalysisDirectionName(oldFileMetadataInfo.getAnalysisDirectionName());
newFileInfo.setFileGroupId(fileGroupId);
newFileInfo.setVersionNo(versionNo + 1);
@@ -2690,6 +2714,35 @@ public class MinioFileIDataFileServiceImpl implements IDataFileService {
}
}
private List<String> buildDictTagsFromEnum(UpdateFileReq req) {
List<String> dictTags = new ArrayList<>();
for (FileDictTagEnum tagEnum : FileDictTagEnum.values()) {
String classFieldName = tagEnum.getDictClassFieldName();
String valueFieldName = tagEnum.getDictValueFieldName();
String classValue = getFieldValue(req, classFieldName);
String valueValue = getFieldValue(req, valueFieldName);
if (StringUtils.hasText(classValue) && StringUtils.hasText(valueValue)) {
dictTags.add(classFieldName);
dictTags.add(valueFieldName);
}
}
return dictTags;
}
private String getFieldValue(Object obj, String fieldName) {
try {
Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
Object value = field.get(obj);
return value != null ? value.toString() : null;
} catch (Exception e) {
log.warn("Failed to get field value for {}", fieldName);
return null;
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public SdmResponse uploadAvatar(MultipartFile avatar) {
@@ -3535,6 +3588,9 @@ public class MinioFileIDataFileServiceImpl implements IDataFileService {
fileSimulationMappingService.lambdaUpdate()
.in(FileSimulationMapping::getFileId, failBusinessIds)
.remove();
// 6. 删除文件标签关系表
fileTagRelService.lambdaUpdate().in(FileTagRel::getFileId, failBusinessIds).remove();
return true;
} catch (Exception e) {
CoreLogger.error("handleFailFiles error{}", e.getMessage());
@@ -4712,7 +4768,7 @@ public class MinioFileIDataFileServiceImpl implements IDataFileService {
metadata.setUpdateTime(LocalDateTime.now());
fileMetadataInfoService.updateById(metadata);
// 6. 如果是文件,更新 Storage fullPath
// 6. 如果是文件,更新 fileStorage
if (Objects.equals(metadata.getDataType(), DataTypeEnum.FILE.getValue())) {
fileStorageService.lambdaUpdate()
.eq(FileStorage::getFileId, metadata.getId())