From 9cbd8c9b66776f427b260902ad5fbb7301794d2c Mon Sep 17 00:00:00 2001 From: lyh <925863403@qq.com> Date: 星期一, 20 一月 2025 11:57:49 +0800 Subject: [PATCH] 新增字典翻译返回值List,新增DictList深度翻译 --- lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/DictAspect.java | 241 ++++++++++++++++++++++++++++-------------------- 1 files changed, 141 insertions(+), 100 deletions(-) diff --git a/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/DictAspect.java b/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/DictAspect.java index c5c2897..bbf19f1 100644 --- a/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/DictAspect.java +++ b/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/DictAspect.java @@ -58,7 +58,7 @@ @Around("excudeService()") public Object doAround(ProceedingJoinPoint pjp) throws Throwable { - long time1=System.currentTimeMillis(); + long time1=System.currentTimeMillis(); Object result = pjp.proceed(); long time2=System.currentTimeMillis(); log.debug("鑾峰彇JSON鏁版嵁 鑰楁椂锛�"+(time2-time1)+"ms"); @@ -70,7 +70,7 @@ } /** - * 鏈柟娉曢拡瀵硅繑鍥炲璞′负Result 鐨処Page鐨勫垎椤靛垪琛ㄦ暟鎹繘琛屽姩鎬佸瓧鍏告敞鍏� + * 鏈柟娉曢拡瀵硅繑鍥炲璞′负Result 鐨処Page鐨勫垎椤靛垪琛ㄦ暟鎹�丩ist鏁版嵁锛堥泦鍚堥噷闈㈠張濂椾簡涓�灞傞泦鍚堟槸涓嶆敮鎸侊級杩涜鍔ㄦ�佸瓧鍏告敞鍏� * 瀛楀吀娉ㄥ叆瀹炵幇 閫氳繃瀵瑰疄浣撶被娣诲姞娉ㄨВ@dict 鏉ユ爣璇嗛渶瑕佺殑瀛楀吀鍐呭,瀛楀吀鍒嗕负鍗曞瓧鍏竎ode鍗冲彲 锛宼able瀛楀吀 code table text閰嶅悎浣跨敤涓庡師鏉eecg鐨勭敤娉曠浉鍚� * 绀轰緥涓篠ysUser 瀛楁涓簊ex 娣诲姞浜嗘敞瑙Dict(dicCode = "sex") 浼氬湪瀛楀吀鏈嶅姟绔嬮┈鏌ュ嚭鏉ュ搴旂殑text 鐒跺悗鍦ㄨ姹俵ist鐨勬椂鍊欏皢杩欎釜瀛楀吀text锛屽凡瀛楁鍚嶇О鍔燺dictText褰㈠紡杩斿洖鍒板墠绔� * 渚嬭緭鍏ュ綋鍓嶈繑鍥炲�肩殑灏变細澶氬嚭涓�涓猻ex_dictText瀛楁 @@ -94,12 +94,6 @@ private Object parseDictText(Object result) { if (result instanceof Result) { if (((Result) result).getResult() instanceof IPage) { - List<JSONObject> items = new ArrayList<>(); - - //step.1 绛涢�夊嚭鍔犱簡 Dict 娉ㄨВ鐨勫瓧娈靛垪琛� - List<Field> dictFieldList = new ArrayList<>(); - // 瀛楀吀鏁版嵁鍒楄〃锛� key = 瀛楀吀code锛寁alue=鏁版嵁鍒楄〃 - Map<String, List<String>> dataListMap = new HashMap<>(5); //鍙栧嚭缁撴灉闆� List<Object> records=((IPage) ((Result) result).getResult()).getRecords(); //update-begin--Author:zyf -- Date:20220606 ----for锛氥�怴UEN-1230銆� 鍒ゆ柇鏄惁鍚湁瀛楀吀娉ㄨВ,娌℃湁娉ㄨВ杩斿洖----- @@ -107,102 +101,149 @@ if(!hasDict){ return result; } - - log.debug(" __ 杩涘叆瀛楀吀缈昏瘧鍒囬潰 DictAspect 鈥斺�� " ); - //update-end--Author:zyf -- Date:20220606 ----for锛氥�怴UEN-1230銆� 鍒ゆ柇鏄惁鍚湁瀛楀吀娉ㄨВ,娌℃湁娉ㄨВ杩斿洖----- - for (Object record : records) { - String json="{}"; - try { - //update-begin--Author:zyf -- Date:20220531 ----for锛氥�恑ssues/#3629銆� DictAspect Jackson搴忓垪鍖栨姤閿�----- - //瑙e喅@JsonFormat娉ㄨВ瑙f瀽涓嶄簡鐨勯棶棰樿瑙丼ysAnnouncement绫荤殑@JsonFormat - json = objectMapper.writeValueAsString(record); - //update-end--Author:zyf -- Date:20220531 ----for锛氥�恑ssues/#3629銆� DictAspect Jackson搴忓垪鍖栨姤閿�----- - } catch (JsonProcessingException e) { - log.error("json瑙f瀽澶辫触"+e.getMessage(),e); - } - //update-begin--Author:scott -- Date:20211223 ----for锛氥�恑ssues/3303銆憆estcontroller杩斿洖json鏁版嵁鍚巏ey椤哄簭閿欎贡 ----- - JSONObject item = JSONObject.parseObject(json, Feature.OrderedField); - //update-end--Author:scott -- Date:20211223 ----for锛氥�恑ssues/3303銆憆estcontroller杩斿洖json鏁版嵁鍚巏ey椤哄簭閿欎贡 ----- - - //update-begin--Author:scott -- Date:20190603 ----for锛氳В鍐崇户鎵垮疄浣撳瓧娈垫棤娉曠炕璇戦棶棰�------ - //for (Field field : record.getClass().getDeclaredFields()) { - // 閬嶅巻鎵�鏈夊瓧娈碉紝鎶婂瓧鍏窩ode鍙栧嚭鏉ワ紝鏀惧埌 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<String> 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鏍煎紡鍖栨棩鏈� - //update-begin--Author:zyf -- Date:20220531 ----for锛氥�恑ssues/#3629銆� DictAspect Jackson搴忓垪鍖栨姤閿�----- - //if (JAVA_UTIL_DATE.equals(field.getType().getName())&&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())))); - //} - //update-end--Author:zyf -- Date:20220531 ----for锛氥�恑ssues/#3629銆� DictAspect Jackson搴忓垪鍖栨姤閿�----- - } - 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); + List<JSONObject> dictText = getDictText(records); + ((IPage) ((Result) result).getResult()).setRecords(dictText); } - + else { + //鍙栧嚭缁撴灉闆� + Object object= (Object) ((Result) result).getResult(); + Class<?> aClass = ((Result) result).getResult().getClass(); + if ("java.util.HashMap".equals(aClass.getName())){ + //灏唌ap杞崲涓洪泦鍚堝湪杞崲涓簃ap + Map<String, Object> mapset = new HashMap<>(); + Map<String,Object> map= (Map<String,Object>) object; + for (String s : map.keySet()) { + Object a1 = map.get(s); + List<Object> records=new ArrayList<>(); + Class<?> aClass1 = a1.getClass(); + if ("java.util.ArrayList".equals(aClass1.getName())){ + records = (ArrayList)map.get(s); + }else{ + records = oConvertUtils.castList(a1, Object.class); + } + Boolean hasDict= checkHasDict(records); + if(!hasDict){ + return result; + } + List<JSONObject> dictText = getDictText(records); + mapset.put(s,dictText); + } + ((Result) result).setResult(mapset); + }else { + List<Object> records = oConvertUtils.castList(object, Object.class); + //update-begin--Author:zyf -- Date:20220606 ----for锛氥�怴UEN-1230銆� 鍒ゆ柇鏄惁鍚湁瀛楀吀娉ㄨВ,娌℃湁娉ㄨВ杩斿洖----- + Boolean hasDict= checkHasDict(records); + if(!hasDict){ + return result; + } + List<JSONObject> dictText = getDictText(records); + ((Result) result).setResult(dictText); + } + } } return result; } + /** + 浼犲叆闆嗗悎 缈昏瘧瀛楀吀鍊� + **/ + private List<JSONObject> getDictText(List<Object> records){ + List<JSONObject> items = new ArrayList<>(); + //step.1 绛涢�夊嚭鍔犱簡 Dict 娉ㄨВ鐨勫瓧娈靛垪琛� + List<Field> dictFieldList = new ArrayList<>(); + // 瀛楀吀鏁版嵁鍒楄〃锛� key = 瀛楀吀code锛寁alue=鏁版嵁鍒楄〃 + Map<String, List<String>> dataListMap = new HashMap<>(5); + + + log.debug(" __ 杩涘叆瀛楀吀缈昏瘧鍒囬潰 DictAspect 鈥斺�� " ); + //update-end--Author:zyf -- Date:20220606 ----for锛氥�怴UEN-1230銆� 鍒ゆ柇鏄惁鍚湁瀛楀吀娉ㄨВ,娌℃湁娉ㄨВ杩斿洖----- + for (Object record : records) { + String json="{}"; + try { + //update-begin--Author:zyf -- Date:20220531 ----for锛氥�恑ssues/#3629銆� DictAspect Jackson搴忓垪鍖栨姤閿�----- + //瑙e喅@JsonFormat娉ㄨВ瑙f瀽涓嶄簡鐨勯棶棰樿瑙丼ysAnnouncement绫荤殑@JsonFormat + json = objectMapper.writeValueAsString(record); + //update-end--Author:zyf -- Date:20220531 ----for锛氥�恑ssues/#3629銆� DictAspect Jackson搴忓垪鍖栨姤閿�----- + } catch (JsonProcessingException e) { + log.error("json瑙f瀽澶辫触"+e.getMessage(),e); + } + //update-begin--Author:scott -- Date:20211223 ----for锛氥�恑ssues/3303銆憆estcontroller杩斿洖json鏁版嵁鍚巏ey椤哄簭閿欎贡 ----- + JSONObject item = JSONObject.parseObject(json, Feature.OrderedField); + //update-end--Author:scott -- Date:20211223 ----for锛氥�恑ssues/3303銆憆estcontroller杩斿洖json鏁版嵁鍚巏ey椤哄簭閿欎贡 ----- + + //update-begin--Author:scott -- Date:20190603 ----for锛氳В鍐崇户鎵垮疄浣撳瓧娈垫棤娉曠炕璇戦棶棰�------ + //for (Field field : record.getClass().getDeclaredFields()) { + // 閬嶅巻鎵�鏈夊瓧娈碉紝鎶婂瓧鍏窩ode鍙栧嚭鏉ワ紝鏀惧埌 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<String> 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鏍煎紡鍖栨棩鏈� + //update-begin--Author:zyf -- Date:20220531 ----for锛氥�恑ssues/#3629銆� DictAspect Jackson搴忓垪鍖栨姤閿�----- + //if (JAVA_UTIL_DATE.equals(field.getType().getName())&&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())))); + //} + //update-end--Author:zyf -- Date:20220531 ----for锛氥�恑ssues/#3629銆� DictAspect Jackson搴忓垪鍖栨姤閿�----- + } + 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); + } + } + } + return items; + } /** * list 鍘婚噸娣诲姞 -- Gitblit v1.9.3