From 0d265f20733c841f15e0c266f75e6ebe8b659215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E7=90=A6=E6=B6=9B?= Date: Thu, 9 Mar 2023 12:02:36 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90fix=20sonar=E3=80=91DictAspect?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jero/common/aspect/DictAspect.java | 325 ++++++++++-------- 1 file changed, 175 insertions(+), 150 deletions(-) diff --git a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/aspect/DictAspect.java b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/aspect/DictAspect.java index cd0f2e0d..0f66d471 100644 --- a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/aspect/DictAspect.java +++ b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/aspect/DictAspect.java @@ -42,7 +42,7 @@ public class DictAspect { @Autowired private CommonAPI commonAPI; @Autowired - public RedisTemplate redisTemplate; + public RedisTemplate redisTemplate; private static final String SYS_CACHE_DICT_S_S = "sys:cache:dict::%s:%s"; @@ -96,91 +96,103 @@ public class DictAspect { // 字典数据列表, key = 字典code,value=数据列表 Map> dataListMap = new HashMap<>(); - for (Object record : ((IPage) ((Result) result).getResult()).getRecords()) { - ObjectMapper mapper = new ObjectMapper(); - String json="{}"; - try { - //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat - json = mapper.writeValueAsString(record); - } catch (JsonProcessingException e) { - log.error("json解析失败"+e.getMessage(),e); - } - JSONObject item = JSON.parseObject(json); - //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------ - //for (Field field : record.getClass().getDeclaredFields()) { - // 遍历所有字段,把字典Code取出来,放到 map 里 - for (Field field : oConvertUtils.getAllFields(record)) { - String value = item.getString(field.getName()); - if (oConvertUtils.isEmpty(value)) { - continue; - } - //update-end--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------ - if (field.getAnnotation(Dict.class) != null) { - if (!dictFieldList.contains(field)) { - dictFieldList.add(field); - } - String code = field.getAnnotation(Dict.class).dicCode(); - String text = field.getAnnotation(Dict.class).dicText(); - String table = field.getAnnotation(Dict.class).dictTable(); - - List dataList; - String dictCode = code; - if (!StringUtils.isEmpty(table)) { - dictCode = String.format("%s,%s,%s", table, text, code); - } - dataList = dataListMap.computeIfAbsent(dictCode, k -> new ArrayList<>()); - this.listAddAllDeduplicate(dataList, Arrays.asList(value.split(","))); - } - //date类型默认转换string格式化日期 - if (Date.class.isAssignableFrom(field.getType()) && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null){ - SimpleDateFormat aDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName())))); - } - } - items.add(item); - } + addItems((Result) result, items, dictFieldList, dataListMap); //step.2 调用翻译方法,一次性翻译 Map> 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 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); - } - } - } + putItems(items, dictFieldList, translText); ((IPage) ((Result) result).getResult()).setRecords(items); } } + private void putItems(List items, List dictFieldList, Map> 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 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 items, List dictFieldList, Map> dataListMap) { + for (Object obj : ((IPage) result.getResult()).getRecords()) { + ObjectMapper mapper = new ObjectMapper(); + String json="{}"; + try { + //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat + json = mapper.writeValueAsString(obj); + } catch (JsonProcessingException e) { + log.error("json解析失败"+e.getMessage(),e); + } + JSONObject item = JSON.parseObject(json); + //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------ + //for (Field field : record.getClass().getDeclaredFields()) { + // 遍历所有字段,把字典Code取出来,放到 map 里 + putItem(dictFieldList, dataListMap, obj, item); + items.add(item); + } + } + + private void putItem(List dictFieldList, Map> dataListMap, Object obj, JSONObject item) { + for (Field field : oConvertUtils.getAllFields(obj)) { + String value = item.getString(field.getName()); + if (oConvertUtils.isEmpty(value)) { + continue; + } + //update-end--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------ + if (field.getAnnotation(Dict.class) != null) { + if (!dictFieldList.contains(field)) { + dictFieldList.add(field); + } + String code = field.getAnnotation(Dict.class).dicCode(); + String text = field.getAnnotation(Dict.class).dicText(); + String table = field.getAnnotation(Dict.class).dictTable(); + + List dataList; + String dictCode = code; + if (!StringUtils.isEmpty(table)) { + dictCode = String.format("%s,%s,%s", table, text, code); + } + dataList = dataListMap.computeIfAbsent(dictCode, k -> new ArrayList<>()); + this.listAddAllDeduplicate(dataList, Arrays.asList(value.split(","))); + } + //date类型默认转换string格式化日期 + if (Date.class.isAssignableFrom(field.getType()) && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null){ + SimpleDateFormat aDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName())))); + } + } + } + /** * list 去重添加 */ @@ -205,51 +217,18 @@ public class DictAspect { //step.1 先通过redis中获取缓存字典数据 for (String dictCode : dataListMap.keySet()) { List dataList = dataListMap.get(dictCode); - if (dataList.size() == 0) { + if (dataList.isEmpty()) { continue; } // 表字典需要翻译的数据 List needTranslDataTable = new ArrayList<>(); - 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 (redisTemplate.hasKey(keyString)) { - try { - String text = oConvertUtils.getString(redisTemplate.opsForValue().get(keyString)); - List 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 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); - } - } - - } + addNeedTranslData(translText, needTranslData, dictCode, dataList, needTranslDataTable); //step.2 调用数据库翻译表字典 - if (needTranslDataTable.size() > 0) { + if (!needTranslDataTable.isEmpty()) { 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); log.info("translateDictFromTableByKeys.dictCode:" + dictCode); log.info("translateDictFromTableByKeys.values:" + values); @@ -257,22 +236,13 @@ public class DictAspect { log.info("translateDictFromTableByKeys.result:" + texts); List list = translText.computeIfAbsent(dictCode, k -> new ArrayList<>()); 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 调用数据库进行翻译普通字典 - if (needTranslData.size() > 0) { + if (!needTranslData.isEmpty()) { List dictCodeList = Arrays.asList(dataListMap.keySet().toArray(new String[]{})); // 将不包含逗号的字典code筛选出来,因为带逗号的是表字典,而不是普通的数据字典 List filterDictCodes = dictCodeList.stream().filter(key -> !key.contains(",")).collect(Collectors.toList()); @@ -301,6 +271,56 @@ public class DictAspect { return translText; } + private void setRedis(String dictCode, List 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> translText, List needTranslData, String dictCode, List dataList, List 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> translText, String dictCode, String data, String keyString) { + try { + String text = oConvertUtils.getString(redisTemplate.opsForValue().get(keyString)); + List 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; //跳过循环 } //update-begin--Author:scott -- Date:20210531 ----for: !56 优化微服务应用下存在表字段需要字典翻译时加载缓慢问题----- - 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 (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()); - } - } + tmpValue = getTmpValue(code, text, table, k, tmpValue); //update-end--Author:scott -- Date:20210531 ----for: !56 优化微服务应用下存在表字段需要字典翻译时加载缓慢问题----- if (tmpValue != null) { @@ -386,5 +383,33 @@ public class DictAspect { 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; + } + }