【fix sonar】DictAspect文件
This commit is contained in:
+145
-120
@@ -42,7 +42,7 @@ public class DictAspect {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private CommonAPI commonAPI;
|
private CommonAPI commonAPI;
|
||||||
@Autowired
|
@Autowired
|
||||||
public RedisTemplate redisTemplate;
|
public RedisTemplate<String,Object> redisTemplate;
|
||||||
|
|
||||||
private static final String SYS_CACHE_DICT_S_S = "sys:cache:dict::%s:%s";
|
private static final String SYS_CACHE_DICT_S_S = "sys:cache:dict::%s:%s";
|
||||||
|
|
||||||
@@ -96,12 +96,60 @@ public class DictAspect {
|
|||||||
// 字典数据列表, key = 字典code,value=数据列表
|
// 字典数据列表, key = 字典code,value=数据列表
|
||||||
Map<String, List<String>> dataListMap = new HashMap<>();
|
Map<String, List<String>> dataListMap = new HashMap<>();
|
||||||
|
|
||||||
for (Object record : ((IPage) ((Result) result).getResult()).getRecords()) {
|
addItems((Result) result, items, dictFieldList, dataListMap);
|
||||||
|
|
||||||
|
//step.2 调用翻译方法,一次性翻译
|
||||||
|
Map<String, List<DictModel>> translText = this.translateAllDict(dataListMap);
|
||||||
|
|
||||||
|
//step.3 将翻译结果填充到返回结果里
|
||||||
|
putItems(items, dictFieldList, translText);
|
||||||
|
|
||||||
|
((IPage) ((Result) result).getResult()).setRecords(items);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void putItems(List<JSONObject> items, List<Field> dictFieldList, Map<String, List<DictModel>> translText) {
|
||||||
|
for (JSONObject obj : items) {
|
||||||
|
for (Field field : dictFieldList) {
|
||||||
|
String code = field.getAnnotation(Dict.class).dicCode();
|
||||||
|
String text = field.getAnnotation(Dict.class).dicText();
|
||||||
|
String table = field.getAnnotation(Dict.class).dictTable();
|
||||||
|
|
||||||
|
String fieldDictCode = code;
|
||||||
|
if (!StringUtils.isEmpty(table)) {
|
||||||
|
fieldDictCode = String.format("%s,%s,%s", table, text, code);
|
||||||
|
}
|
||||||
|
|
||||||
|
String value = obj.getString(field.getName());
|
||||||
|
if (oConvertUtils.isNotEmpty(value)) {
|
||||||
|
List<DictModel> dictModels = translText.get(fieldDictCode);
|
||||||
|
if(dictModels==null || dictModels.isEmpty()){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String textValue = this.translDictText(dictModels, value);
|
||||||
|
log.debug(" 字典Val : " + textValue);
|
||||||
|
log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);
|
||||||
|
|
||||||
|
// TODO-sun 测试输出,待删
|
||||||
|
log.debug(" ---- dictCode: " + fieldDictCode);
|
||||||
|
log.debug(" ---- value: " + value);
|
||||||
|
log.debug(" ----- text: " + textValue);
|
||||||
|
log.debug(" ---- dictModels: " + JSON.toJSONString(dictModels));
|
||||||
|
|
||||||
|
obj.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addItems(Result result, List<JSONObject> items, List<Field> dictFieldList, Map<String, List<String>> dataListMap) {
|
||||||
|
for (Object obj : ((IPage) result.getResult()).getRecords()) {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
String json="{}";
|
String json="{}";
|
||||||
try {
|
try {
|
||||||
//解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
|
//解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
|
||||||
json = mapper.writeValueAsString(record);
|
json = mapper.writeValueAsString(obj);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
log.error("json解析失败"+e.getMessage(),e);
|
log.error("json解析失败"+e.getMessage(),e);
|
||||||
}
|
}
|
||||||
@@ -109,7 +157,13 @@ public class DictAspect {
|
|||||||
//update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
|
//update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
|
||||||
//for (Field field : record.getClass().getDeclaredFields()) {
|
//for (Field field : record.getClass().getDeclaredFields()) {
|
||||||
// 遍历所有字段,把字典Code取出来,放到 map 里
|
// 遍历所有字段,把字典Code取出来,放到 map 里
|
||||||
for (Field field : oConvertUtils.getAllFields(record)) {
|
putItem(dictFieldList, dataListMap, obj, item);
|
||||||
|
items.add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void putItem(List<Field> dictFieldList, Map<String, List<String>> dataListMap, Object obj, JSONObject item) {
|
||||||
|
for (Field field : oConvertUtils.getAllFields(obj)) {
|
||||||
String value = item.getString(field.getName());
|
String value = item.getString(field.getName());
|
||||||
if (oConvertUtils.isEmpty(value)) {
|
if (oConvertUtils.isEmpty(value)) {
|
||||||
continue;
|
continue;
|
||||||
@@ -137,48 +191,6 @@ public class DictAspect {
|
|||||||
item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
|
item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
items.add(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
//step.2 调用翻译方法,一次性翻译
|
|
||||||
Map<String, List<DictModel>> translText = this.translateAllDict(dataListMap);
|
|
||||||
|
|
||||||
//step.3 将翻译结果填充到返回结果里
|
|
||||||
for (JSONObject record : items) {
|
|
||||||
for (Field field : dictFieldList) {
|
|
||||||
String code = field.getAnnotation(Dict.class).dicCode();
|
|
||||||
String text = field.getAnnotation(Dict.class).dicText();
|
|
||||||
String table = field.getAnnotation(Dict.class).dictTable();
|
|
||||||
|
|
||||||
String fieldDictCode = code;
|
|
||||||
if (!StringUtils.isEmpty(table)) {
|
|
||||||
fieldDictCode = String.format("%s,%s,%s", table, text, code);
|
|
||||||
}
|
|
||||||
|
|
||||||
String value = record.getString(field.getName());
|
|
||||||
if (oConvertUtils.isNotEmpty(value)) {
|
|
||||||
List<DictModel> dictModels = translText.get(fieldDictCode);
|
|
||||||
if(dictModels==null || dictModels.size()==0){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
String textValue = this.translDictText(dictModels, value);
|
|
||||||
log.debug(" 字典Val : " + textValue);
|
|
||||||
log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue);
|
|
||||||
|
|
||||||
// TODO-sun 测试输出,待删
|
|
||||||
log.debug(" ---- dictCode: " + fieldDictCode);
|
|
||||||
log.debug(" ---- value: " + value);
|
|
||||||
log.debug(" ----- text: " + textValue);
|
|
||||||
log.debug(" ---- dictModels: " + JSON.toJSONString(dictModels));
|
|
||||||
|
|
||||||
record.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
((IPage) ((Result) result).getResult()).setRecords(items);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -205,51 +217,18 @@ public class DictAspect {
|
|||||||
//step.1 先通过redis中获取缓存字典数据
|
//step.1 先通过redis中获取缓存字典数据
|
||||||
for (String dictCode : dataListMap.keySet()) {
|
for (String dictCode : dataListMap.keySet()) {
|
||||||
List<String> dataList = dataListMap.get(dictCode);
|
List<String> dataList = dataListMap.get(dictCode);
|
||||||
if (dataList.size() == 0) {
|
if (dataList.isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// 表字典需要翻译的数据
|
// 表字典需要翻译的数据
|
||||||
List<String> needTranslDataTable = new ArrayList<>();
|
List<String> needTranslDataTable = new ArrayList<>();
|
||||||
for (String s : dataList) {
|
addNeedTranslData(translText, needTranslData, dictCode, dataList, needTranslDataTable);
|
||||||
String data = s.trim();
|
|
||||||
if (data.length() == 0) {
|
|
||||||
continue; //跳过循环
|
|
||||||
}
|
|
||||||
if (dictCode.contains(",")) {
|
|
||||||
String keyString = String.format("sys:cache:dictTable::SimpleKey [%s,%s]", dictCode, data);
|
|
||||||
if (redisTemplate.hasKey(keyString)) {
|
|
||||||
try {
|
|
||||||
String text = oConvertUtils.getString(redisTemplate.opsForValue().get(keyString));
|
|
||||||
List<DictModel> list = translText.computeIfAbsent(dictCode, k -> new ArrayList<>());
|
|
||||||
list.add(new DictModel(data, text));
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn(e.getMessage());
|
|
||||||
}
|
|
||||||
} else if (!needTranslDataTable.contains(data)) {
|
|
||||||
// 去重添加
|
|
||||||
needTranslDataTable.add(data);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
String keyString = String.format(SYS_CACHE_DICT_S_S, dictCode, data);
|
|
||||||
if (redisTemplate.hasKey(keyString)) {
|
|
||||||
try {
|
|
||||||
String text = oConvertUtils.getString(redisTemplate.opsForValue().get(keyString));
|
|
||||||
List<DictModel> list = translText.computeIfAbsent(dictCode, k -> new ArrayList<>());
|
|
||||||
list.add(new DictModel(data, text));
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn(e.getMessage());
|
|
||||||
}
|
|
||||||
} else if (!needTranslData.contains(data)) {
|
|
||||||
// 去重添加
|
|
||||||
needTranslData.add(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
//step.2 调用数据库翻译表字典
|
//step.2 调用数据库翻译表字典
|
||||||
if (needTranslDataTable.size() > 0) {
|
if (!needTranslDataTable.isEmpty()) {
|
||||||
String[] arr = dictCode.split(",");
|
String[] arr = dictCode.split(",");
|
||||||
String table = arr[0], text = arr[1], code = arr[2];
|
String table = arr[0];
|
||||||
|
String text = arr[1];
|
||||||
|
String code = arr[2];
|
||||||
String values = String.join(",", needTranslDataTable);
|
String values = String.join(",", needTranslDataTable);
|
||||||
log.info("translateDictFromTableByKeys.dictCode:" + dictCode);
|
log.info("translateDictFromTableByKeys.dictCode:" + dictCode);
|
||||||
log.info("translateDictFromTableByKeys.values:" + values);
|
log.info("translateDictFromTableByKeys.values:" + values);
|
||||||
@@ -257,22 +236,13 @@ public class DictAspect {
|
|||||||
log.info("translateDictFromTableByKeys.result:" + texts);
|
log.info("translateDictFromTableByKeys.result:" + texts);
|
||||||
List<DictModel> list = translText.computeIfAbsent(dictCode, k -> new ArrayList<>());
|
List<DictModel> list = translText.computeIfAbsent(dictCode, k -> new ArrayList<>());
|
||||||
list.addAll(texts);
|
list.addAll(texts);
|
||||||
|
setRedis(dictCode, texts);
|
||||||
|
|
||||||
// 做 redis 缓存
|
|
||||||
for (DictModel dict : texts) {
|
|
||||||
String redisKey = String.format("sys:cache:dictTable::SimpleKey [%s,%s]", dictCode, dict.getValue());
|
|
||||||
try {
|
|
||||||
// 保留10分钟
|
|
||||||
redisTemplate.opsForValue().set(redisKey, dict.getText(), 600, TimeUnit.SECONDS);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//step.3 调用数据库进行翻译普通字典
|
//step.3 调用数据库进行翻译普通字典
|
||||||
if (needTranslData.size() > 0) {
|
if (!needTranslData.isEmpty()) {
|
||||||
List<String> dictCodeList = Arrays.asList(dataListMap.keySet().toArray(new String[]{}));
|
List<String> dictCodeList = Arrays.asList(dataListMap.keySet().toArray(new String[]{}));
|
||||||
// 将不包含逗号的字典code筛选出来,因为带逗号的是表字典,而不是普通的数据字典
|
// 将不包含逗号的字典code筛选出来,因为带逗号的是表字典,而不是普通的数据字典
|
||||||
List<String> filterDictCodes = dictCodeList.stream().filter(key -> !key.contains(",")).collect(Collectors.toList());
|
List<String> filterDictCodes = dictCodeList.stream().filter(key -> !key.contains(",")).collect(Collectors.toList());
|
||||||
@@ -301,6 +271,56 @@ public class DictAspect {
|
|||||||
return translText;
|
return translText;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setRedis(String dictCode, List<DictModel> texts) {
|
||||||
|
// 做 redis 缓存
|
||||||
|
for (DictModel dict : texts) {
|
||||||
|
String redisKey = String.format("sys:cache:dictTable::SimpleKey [%s,%s]", dictCode, dict.getValue());
|
||||||
|
try {
|
||||||
|
// 保留10分钟
|
||||||
|
redisTemplate.opsForValue().set(redisKey, dict.getText(), 600, TimeUnit.SECONDS);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addNeedTranslData(Map<String, List<DictModel>> translText, List<String> needTranslData, String dictCode, List<String> dataList, List<String> needTranslDataTable) {
|
||||||
|
for (String s : dataList) {
|
||||||
|
String data = s.trim();
|
||||||
|
if (data.length() == 0) {
|
||||||
|
continue; //跳过循环
|
||||||
|
}
|
||||||
|
if (dictCode.contains(",")) {
|
||||||
|
String keyString = String.format("sys:cache:dictTable::SimpleKey [%s,%s]", dictCode, data);
|
||||||
|
if (Boolean.TRUE.equals(redisTemplate.hasKey(keyString))) {
|
||||||
|
addList(translText, dictCode, data, keyString);
|
||||||
|
} else if (!needTranslDataTable.contains(data)) {
|
||||||
|
// 去重添加
|
||||||
|
needTranslDataTable.add(data);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
String keyString = String.format(SYS_CACHE_DICT_S_S, dictCode, data);
|
||||||
|
if (Boolean.TRUE.equals(redisTemplate.hasKey(keyString))) {
|
||||||
|
addList(translText, dictCode, data, keyString);
|
||||||
|
} else if (!needTranslData.contains(data)) {
|
||||||
|
// 去重添加
|
||||||
|
needTranslData.add(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addList(Map<String, List<DictModel>> translText, String dictCode, String data, String keyString) {
|
||||||
|
try {
|
||||||
|
String text = oConvertUtils.getString(redisTemplate.opsForValue().get(keyString));
|
||||||
|
List<DictModel> list = translText.computeIfAbsent(dictCode, k -> new ArrayList<>());
|
||||||
|
list.add(new DictModel(data, text));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字典值替换文本
|
* 字典值替换文本
|
||||||
*
|
*
|
||||||
@@ -349,30 +369,7 @@ public class DictAspect {
|
|||||||
continue; //跳过循环
|
continue; //跳过循环
|
||||||
}
|
}
|
||||||
//update-begin--Author:scott -- Date:20210531 ----for: !56 优化微服务应用下存在表字段需要字典翻译时加载缓慢问题-----
|
//update-begin--Author:scott -- Date:20210531 ----for: !56 优化微服务应用下存在表字段需要字典翻译时加载缓慢问题-----
|
||||||
if (!StringUtils.isEmpty(table)){
|
tmpValue = getTmpValue(code, text, table, k, tmpValue);
|
||||||
log.info("--DictAspect------dicTable="+ table+" ,dicText= "+text+" ,dicCode="+code);
|
|
||||||
String keyString = String.format("sys:cache:dictTable::SimpleKey [%s,%s,%s,%s]",table,text,code,k.trim());
|
|
||||||
if (redisTemplate.hasKey(keyString)){
|
|
||||||
try {
|
|
||||||
tmpValue = oConvertUtils.getString(redisTemplate.opsForValue().get(keyString));
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn(e.getMessage());
|
|
||||||
}
|
|
||||||
}else {
|
|
||||||
tmpValue= commonAPI.translateDictFromTable(table,text,code,k.trim());
|
|
||||||
}
|
|
||||||
}else {
|
|
||||||
String keyString = String.format(SYS_CACHE_DICT_S_S,code,k.trim());
|
|
||||||
if (redisTemplate.hasKey(keyString)){
|
|
||||||
try {
|
|
||||||
tmpValue = oConvertUtils.getString(redisTemplate.opsForValue().get(keyString));
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn(e.getMessage());
|
|
||||||
}
|
|
||||||
}else {
|
|
||||||
tmpValue = commonAPI.translateDict(code, k.trim());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//update-end--Author:scott -- Date:20210531 ----for: !56 优化微服务应用下存在表字段需要字典翻译时加载缓慢问题-----
|
//update-end--Author:scott -- Date:20210531 ----for: !56 优化微服务应用下存在表字段需要字典翻译时加载缓慢问题-----
|
||||||
|
|
||||||
if (tmpValue != null) {
|
if (tmpValue != null) {
|
||||||
@@ -386,5 +383,33 @@ public class DictAspect {
|
|||||||
return textValue.toString();
|
return textValue.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getTmpValue(String code, String text, String table, String k, String tmpValue) {
|
||||||
|
if (!StringUtils.isEmpty(table)){
|
||||||
|
log.info("--DictAspect------dicTable="+ table+" ,dicText= "+text+" ,dicCode="+code);
|
||||||
|
String keyString = String.format("sys:cache:dictTable::SimpleKey [%s,%s,%s,%s]",table,text,code,k.trim());
|
||||||
|
if (Boolean.TRUE.equals(redisTemplate.hasKey(keyString))){
|
||||||
|
try {
|
||||||
|
tmpValue = oConvertUtils.getString(redisTemplate.opsForValue().get(keyString));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn(e.getMessage());
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
tmpValue= commonAPI.translateDictFromTable(table,text,code,k.trim());
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
String keyString = String.format(SYS_CACHE_DICT_S_S,code,k.trim());
|
||||||
|
if (Boolean.TRUE.equals(redisTemplate.hasKey(keyString))){
|
||||||
|
try {
|
||||||
|
tmpValue = oConvertUtils.getString(redisTemplate.opsForValue().get(keyString));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn(e.getMessage());
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
tmpValue = commonAPI.translateDict(code, k.trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tmpValue;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user