From 4c5b1e99a25061bdbf33698eae2f9063b814869e Mon Sep 17 00:00:00 2001 From: caihaohan Date: Fri, 26 Jul 2024 14:57:38 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E7=BF=BB=E8=AF=91=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E9=80=92=E5=BD=92=E5=AF=BC=E8=87=B4=E7=9A=84=E6=A0=88=E6=BA=A2?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jero/common/aspect/TranslationAspect.java | 108 ++++++++++-------- 1 file changed, 59 insertions(+), 49 deletions(-) diff --git a/laws-base/laws-base-core/src/main/java/com/jero/common/aspect/TranslationAspect.java b/laws-base/laws-base-core/src/main/java/com/jero/common/aspect/TranslationAspect.java index c6093d29..068f5620 100644 --- a/laws-base/laws-base-core/src/main/java/com/jero/common/aspect/TranslationAspect.java +++ b/laws-base/laws-base-core/src/main/java/com/jero/common/aspect/TranslationAspect.java @@ -151,69 +151,79 @@ public class TranslationAspect { return result; } + private final Set processedObjects = new HashSet<>(); + private Object translateObject(Object record, List listDict) { - ObjectMapper mapper = new ObjectMapper(); - String json; - try { - json = mapper.writeValueAsString(record); - } catch (JsonProcessingException e) { - log.error("json解析失败" + e.getMessage(), e); + if (record == null || processedObjects.contains(record)) { return record; } - JSONObject item; + processedObjects.add(record); + try { - item = JSONObject.parseObject(json); - } catch (Exception e) { - log.error("json解析失败" + e.getMessage(), e); - return record; - } + ObjectMapper mapper = new ObjectMapper(); + String json; + try { + json = mapper.writeValueAsString(record); + } catch (JsonProcessingException e) { + log.error("json解析失败" + e.getMessage(), e); + return record; + } + JSONObject item; + try { + item = JSONObject.parseObject(json); + } catch (Exception e) { + log.error("json解析失败" + e.getMessage(), e); + return record; + } - for (Field field : oConvertUtils.getAllFields(record)) { - field.setAccessible(true); + for (Field field : oConvertUtils.getAllFields(record)) { + field.setAccessible(true); - // 如果字段是复杂对象类型或者是List,进行递归翻译 - if (isComplexObject(field.getType()) || List.class.isAssignableFrom(field.getType())) { - Object nestedRecord; - try { - nestedRecord = field.get(record); - } catch (IllegalAccessException e) { - continue; - } + if (isComplexObject(field.getType()) || List.class.isAssignableFrom(field.getType())) { + Object nestedRecord; + try { + nestedRecord = field.get(record); + } catch (IllegalAccessException e) { + continue; + } - if (nestedRecord != null) { - JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class); - String jsonFieldName = (jsonProperty != null) ? jsonProperty.value() : field.getName(); + if (nestedRecord != null) { + JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class); + String jsonFieldName = (jsonProperty != null) ? jsonProperty.value() : field.getName(); - if (List.class.isAssignableFrom(field.getType())) { - List objList = (List) nestedRecord; - List translatedList = new ArrayList<>(); - for (Object obj : objList) { - translatedList.add(translateObject(obj, listDict)); + if (List.class.isAssignableFrom(field.getType())) { + List objList = (List) nestedRecord; + List translatedList = new ArrayList<>(); + for (Object obj : objList) { + translatedList.add(translateObject(obj, listDict)); + } + item.put(jsonFieldName, translatedList); + } else { + Object translatedNestedRecord = translateObject(nestedRecord, listDict); + item.put(jsonFieldName, translatedNestedRecord); } - item.put(jsonFieldName, translatedList); - } else { - Object translatedNestedRecord = translateObject(nestedRecord, listDict); - item.put(jsonFieldName, translatedNestedRecord); } } + + if (field.getAnnotation(Dict.class) != null) { + String code = field.getAnnotation(Dict.class).dicCode(); + String text = field.getAnnotation(Dict.class).dicText(); + String table = field.getAnnotation(Dict.class).dictTable(); + String key = String.valueOf(item.get(field.getName())); + String textValue = translateDictValue(code, text, table, key, listDict); + item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue); + } + + if (Objects.equals(field.getType().getName(), "java.util.Date") && 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())))); + } } - if (field.getAnnotation(Dict.class) != null) { - String code = field.getAnnotation(Dict.class).dicCode(); - String text = field.getAnnotation(Dict.class).dicText(); - String table = field.getAnnotation(Dict.class).dictTable(); - String key = String.valueOf(item.get(field.getName())); - String textValue = translateDictValue(code, text, table, key, listDict); - item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue); - } - - if (Objects.equals(field.getType().getName(), "java.util.Date") && 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())))); - } + return item; + } finally { + processedObjects.remove(record); } - - return item; } private boolean isComplexObject(Class type) {