fix:封装标签查询
This commit is contained in:
@@ -8,8 +8,8 @@ import lombok.Getter;
|
||||
* 固定配置文件元数据响应中需要填充的字典标签字段映射关系
|
||||
*
|
||||
* 映射关系:
|
||||
* - disciplineTypeDictClass/disciplineDictValue → DISCIPLINE_TYPE
|
||||
* - fileTypeDictClass/fileTypeDictValue → ALL_FILE_TYPE
|
||||
* - disciplineTypeDictClass/disciplineDictValue/disciplineDictName → DISCIPLINE_TYPE
|
||||
* - fileTypeDictClass/fileTypeDictValue/fileTypeDictName → ALL_FILE_TYPE
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
@@ -20,14 +20,14 @@ public enum FileDictTagEnum {
|
||||
* 字段: disciplineTypeDictClass, disciplineDictValue
|
||||
* dictClass: DISCIPLINE_TYPE
|
||||
*/
|
||||
DISCIPLINE_TYPE("DISCIPLINE_TYPE", "disciplineTypeDictClass", "disciplineDictValue"),
|
||||
DISCIPLINE_TYPE("DISCIPLINE_TYPE", "disciplineTypeDictClass", "disciplineDictValue", "disciplineDictName"),
|
||||
|
||||
/**
|
||||
* 文件类型标签
|
||||
* 字段: fileTypeDictClass, fileTypeDictValue
|
||||
* dictClass: ALL_FILE_TYPE
|
||||
*/
|
||||
FILE_TYPE("ALL_FILE_TYPE", "fileTypeDictClass", "fileTypeDictValue");
|
||||
FILE_TYPE("ALL_FILE_TYPE", "fileTypeDictClass", "fileTypeDictValue", "fileTypeDictName");
|
||||
|
||||
/**
|
||||
* 字典分类(对应数据库中的 dictClass)
|
||||
@@ -43,6 +43,11 @@ public enum FileDictTagEnum {
|
||||
* dictValue 字段名(完整字段名,如 disciplineDictValue)
|
||||
*/
|
||||
private final String dictValueFieldName;
|
||||
|
||||
/**
|
||||
* dictName 字段名(完整字段名,如 disciplineDictName)
|
||||
*/
|
||||
private final String dictNameFieldName;
|
||||
|
||||
/**
|
||||
* 根据 dictClass 获取对应的枚举
|
||||
|
||||
@@ -64,11 +64,17 @@ public class BaseResp {
|
||||
@Schema(description = "文件类型字典值")
|
||||
private String fileTypeDictValue;
|
||||
|
||||
@Schema(description = "文件类型字典名称")
|
||||
private String fileTypeDictName;
|
||||
|
||||
@Schema(description = "学科类型字典类")
|
||||
private String disciplineTypeDictClass;
|
||||
|
||||
@Schema(description = "学科类型字典值")
|
||||
private String disciplineDictValue;
|
||||
|
||||
@Schema(description = "学科类型字典名称")
|
||||
private String disciplineDictName;
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
}
|
||||
|
||||
@@ -22,10 +22,16 @@ public class FileDictTagsAggDTO implements Serializable {
|
||||
@Schema(description = "文件类型字典值(逗号分隔)")
|
||||
private String fileTypeDictValue;
|
||||
|
||||
@Schema(description = "文件类型字典名称(逗号分隔)")
|
||||
private String fileTypeDictName;
|
||||
|
||||
@Schema(description = "学科类型字典类")
|
||||
private String disciplineTypeDictClass;
|
||||
|
||||
@Schema(description = "学科类型字典值(逗号分隔)")
|
||||
private String disciplineDictValue;
|
||||
|
||||
@Schema(description = "学科类型字典名称(逗号分隔)")
|
||||
private String disciplineDictName;
|
||||
}
|
||||
|
||||
|
||||
@@ -81,8 +81,8 @@ public class FileDictTagQueryServiceImpl implements IFileDictTagQueryService {
|
||||
(v1, v2) -> v1
|
||||
));
|
||||
|
||||
// 3) fileId -> (dictClass -> Set<dictValue>)
|
||||
Map<Long, Map<String, LinkedHashSet<String>>> agg = new HashMap<>();
|
||||
// 3) fileId -> (dictClass -> 聚合数据:Set<dictValue> + Set<dictName>)
|
||||
Map<Long, Map<String, DictAggData>> agg = new HashMap<>();
|
||||
for (FileTagRel rel : fileTagRels) {
|
||||
if (rel == null || rel.getFileId() == null || rel.getTagId() == null) {
|
||||
continue;
|
||||
@@ -94,9 +94,13 @@ public class FileDictTagQueryServiceImpl implements IFileDictTagQueryService {
|
||||
if (!FileDictTagEnum.isConfigured(dict.dictClass)) {
|
||||
continue;
|
||||
}
|
||||
agg.computeIfAbsent(rel.getFileId(), k -> new HashMap<>())
|
||||
.computeIfAbsent(dict.dictClass, k -> new LinkedHashSet<>())
|
||||
.add(dict.dictValue);
|
||||
DictAggData data = agg
|
||||
.computeIfAbsent(rel.getFileId(), k -> new HashMap<>())
|
||||
.computeIfAbsent(dict.dictClass, k -> new DictAggData());
|
||||
data.getDictValues().add(dict.dictValue);
|
||||
if (dict.dictName != null) {
|
||||
data.getDictNames().add(dict.dictName);
|
||||
}
|
||||
}
|
||||
|
||||
if (agg.isEmpty()) {
|
||||
@@ -105,9 +109,9 @@ public class FileDictTagQueryServiceImpl implements IFileDictTagQueryService {
|
||||
|
||||
// 4) 转为 DTO(目前只输出枚举配置的两类字段)
|
||||
Map<Long, FileDictTagsAggDTO> result = new HashMap<>();
|
||||
for (Map.Entry<Long, Map<String, LinkedHashSet<String>>> e : agg.entrySet()) {
|
||||
for (Map.Entry<Long, Map<String, DictAggData>> e : agg.entrySet()) {
|
||||
Long fileId = e.getKey();
|
||||
Map<String, LinkedHashSet<String>> classMap = e.getValue();
|
||||
Map<String, DictAggData> classMap = e.getValue();
|
||||
if (classMap == null || classMap.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
@@ -115,9 +119,16 @@ public class FileDictTagQueryServiceImpl implements IFileDictTagQueryService {
|
||||
FileDictTagsAggDTO dto = new FileDictTagsAggDTO();
|
||||
dto.setFileId(fileId);
|
||||
|
||||
for (Map.Entry<String, LinkedHashSet<String>> classEntry : classMap.entrySet()) {
|
||||
for (Map.Entry<String, DictAggData> classEntry : classMap.entrySet()) {
|
||||
String dictClass = classEntry.getKey();
|
||||
String dictValues = String.join(",", classEntry.getValue());
|
||||
DictAggData data = classEntry.getValue();
|
||||
if (data == null || data.getDictValues().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
String dictValues = String.join(",", data.getDictValues());
|
||||
String dictNames = data.getDictNames().isEmpty()
|
||||
? null
|
||||
: String.join(",", data.getDictNames());
|
||||
FileDictTagEnum tagEnum = FileDictTagEnum.getByDictClass(dictClass);
|
||||
if (tagEnum == null) {
|
||||
continue;
|
||||
@@ -125,9 +136,11 @@ public class FileDictTagQueryServiceImpl implements IFileDictTagQueryService {
|
||||
if (tagEnum == FileDictTagEnum.FILE_TYPE) {
|
||||
dto.setFileTypeDictClass(tagEnum.getDictClass());
|
||||
dto.setFileTypeDictValue(dictValues);
|
||||
dto.setFileTypeDictName(dictNames);
|
||||
} else if (tagEnum == FileDictTagEnum.DISCIPLINE_TYPE) {
|
||||
dto.setDisciplineTypeDictClass(tagEnum.getDictClass());
|
||||
dto.setDisciplineDictValue(dictValues);
|
||||
dto.setDisciplineDictName(dictNames);
|
||||
}
|
||||
}
|
||||
result.put(fileId, dto);
|
||||
@@ -170,21 +183,31 @@ public class FileDictTagQueryServiceImpl implements IFileDictTagQueryService {
|
||||
}
|
||||
for (FileDictTagEnum tagEnum : FileDictTagEnum.values()) {
|
||||
String dictValues = getDictValuesByEnum(tags, tagEnum);
|
||||
if (dictValues == null) {
|
||||
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
|
||||
String valueSetterName = "set" + Character.toUpperCase(dictValueFieldName.charAt(0)) + dictValueFieldName.substring(1);
|
||||
resp.getClass().getMethod(valueSetterName, String.class)
|
||||
.invoke(resp, dictValues);
|
||||
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());
|
||||
@@ -207,4 +230,35 @@ public class FileDictTagQueryServiceImpl implements IFileDictTagQueryService {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据枚举类型,从聚合结果中取出对应的字典名称
|
||||
*/
|
||||
private String getDictNamesByEnum(FileDictTagsAggDTO tags, FileDictTagEnum tagEnum) {
|
||||
if (tags == null || tagEnum == null) {
|
||||
return null;
|
||||
}
|
||||
if (tagEnum == FileDictTagEnum.FILE_TYPE) {
|
||||
return tags.getFileTypeDictName();
|
||||
} else if (tagEnum == FileDictTagEnum.DISCIPLINE_TYPE) {
|
||||
return tags.getDisciplineDictName();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 内部聚合数据结构:同时持有 dictValue 和 dictName 的去重集合
|
||||
*/
|
||||
private static class DictAggData {
|
||||
private final LinkedHashSet<String> dictValues = new LinkedHashSet<>();
|
||||
private final LinkedHashSet<String> dictNames = new LinkedHashSet<>();
|
||||
|
||||
public LinkedHashSet<String> getDictValues() {
|
||||
return dictValues;
|
||||
}
|
||||
|
||||
public LinkedHashSet<String> getDictNames() {
|
||||
return dictNames;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user