From 23855599412c4d61b38d78f0f3abd3430a48b5b1 Mon Sep 17 00:00:00 2001 From: zhangherong <571457620@qq.com> Date: 星期三, 25 六月 2025 11:51:38 +0800 Subject: [PATCH] Merge branch 'mdc_hyjs_master' --- lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/DictAspect.java | 429 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 429 insertions(+), 0 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 new file mode 100644 index 0000000..a4ed593 --- /dev/null +++ b/lxzn-boot-base-core/src/main/java/org/jeecg/common/aspect/DictAspect.java @@ -0,0 +1,429 @@ +package org.jeecg.common.aspect; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.parser.Feature; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.jeecg.common.api.CommonAPI; +import org.jeecg.common.api.vo.Result; +import org.jeecg.common.aspect.annotation.Dict; +import org.jeecg.common.constant.CommonConstant; +import org.jeecg.common.system.vo.DictModel; +import org.jeecg.common.util.oConvertUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; + +import java.lang.reflect.Field; +import java.util.*; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * @Description: 瀛楀吀aop绫� + * @Author: dangzhenghui + * @Date: 2019-3-17 21:50 + * @Version: 1.0 + */ +@Aspect +@Component +@Slf4j +public class DictAspect { + @Lazy + @Autowired + private CommonAPI commonApi; + @Autowired + public RedisTemplate redisTemplate; + + @Autowired + private ObjectMapper objectMapper; + + private static final String JAVA_UTIL_DATE = "java.util.Date"; + + /** + * 瀹氫箟鍒囩偣Pointcut + */ + @Pointcut("execution(public * org.jeecg.modules..*.*Controller.*(..)) || @annotation(org.jeecg.common.aspect.annotation.AutoDict)") + public void excudeService() { + } + + @Around("excudeService()") + public Object doAround(ProceedingJoinPoint pjp) throws Throwable { + long time1=System.currentTimeMillis(); + Object result = pjp.proceed(); + long time2=System.currentTimeMillis(); + log.debug("鑾峰彇JSON鏁版嵁 鑰楁椂锛�"+(time2-time1)+"ms"); + long start=System.currentTimeMillis(); + result=this.parseDictText(result); + long end=System.currentTimeMillis(); + log.debug("娉ㄥ叆瀛楀吀鍒癑SON鏁版嵁 鑰楁椂"+(end-start)+"ms"); + return result; + } + + /** + * 鏈柟娉曢拡瀵硅繑鍥炲璞′负Result 鐨処Page鐨勫垎椤靛垪琛ㄦ暟鎹�丩ist鏁版嵁锛堥泦鍚堥噷闈㈠張濂椾簡涓�灞傞泦鍚堟槸涓嶆敮鎸侊級杩涜鍔ㄦ�佸瓧鍏告敞鍏� + * 瀛楀吀娉ㄥ叆瀹炵幇 閫氳繃瀵瑰疄浣撶被娣诲姞娉ㄨВ@dict 鏉ユ爣璇嗛渶瑕佺殑瀛楀吀鍐呭,瀛楀吀鍒嗕负鍗曞瓧鍏竎ode鍗冲彲 锛宼able瀛楀吀 code table text閰嶅悎浣跨敤涓庡師鏉eecg鐨勭敤娉曠浉鍚� + * 绀轰緥涓篠ysUser 瀛楁涓簊ex 娣诲姞浜嗘敞瑙Dict(dicCode = "sex") 浼氬湪瀛楀吀鏈嶅姟绔嬮┈鏌ュ嚭鏉ュ搴旂殑text 鐒跺悗鍦ㄨ姹俵ist鐨勬椂鍊欏皢杩欎釜瀛楀吀text锛屽凡瀛楁鍚嶇О鍔燺dictText褰㈠紡杩斿洖鍒板墠绔� + * 渚嬭緭鍏ュ綋鍓嶈繑鍥炲�肩殑灏变細澶氬嚭涓�涓猻ex_dictText瀛楁 + * { + * sex:1, + * sex_dictText:"鐢�" + * } + * 鍓嶇鐩存帴鍙栧�約ext_dictText鍦╰able閲岄潰鏃犻渶鍐嶈繘琛屽墠绔殑瀛楀吀杞崲浜� + * customRender:function (text) { + * if(text==1){ + * return "鐢�"; + * }else if(text==2){ + * return "濂�"; + * }else{ + * return text; + * } + * } + * 鐩墠vue鏄繖涔堣繘琛屽瓧鍏告覆鏌撳埌table涓婄殑澶氫簡灏卞緢楹荤儲浜� 杩欎釜鐩存帴鍦ㄦ湇鍔$娓叉煋瀹屾垚鍓嶇鍙互鐩存帴鐢� + * @param result + */ + 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銆� 鍒ゆ柇鏄惁鍚湁瀛楀吀娉ㄨВ,娌℃湁娉ㄨВ杩斿洖----- + Boolean hasDict= checkHasDict(records); + 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); + } + + } + return result; + } + + /** + * list 鍘婚噸娣诲姞 + */ + private void listAddAllDeduplicate(List<String> dataList, List<String> addList) { + // 绛涢�夊嚭dataList涓病鏈夌殑鏁版嵁 + List<String> filterList = addList.stream().filter(i -> !dataList.contains(i)).collect(Collectors.toList()); + dataList.addAll(filterList); + } + + /** + * 涓�娆℃�ф妸鎵�鏈夌殑瀛楀吀閮界炕璇戜簡 + * 1. 鎵�鏈夌殑鏅�氭暟鎹瓧鍏哥殑鎵�鏈夋暟鎹彧鎵ц涓�娆QL + * 2. 琛ㄥ瓧鍏哥浉鍚岀殑鎵�鏈夋暟鎹彧鎵ц涓�娆QL + * @param dataListMap + * @return + */ + private Map<String, List<DictModel>> translateAllDict(Map<String, List<String>> dataListMap) { + // 缈昏瘧鍚庣殑瀛楀吀鏂囨湰锛宬ey=dictCode + Map<String, List<DictModel>> translText = new HashMap<>(5); + // 闇�瑕佺炕璇戠殑鏁版嵁锛堟湁浜涘彲浠ヤ粠redis缂撳瓨涓幏鍙栵紝灏变笉璧版暟鎹簱鏌ヨ锛� + List<String> needTranslData = new ArrayList<>(); + //step.1 鍏堥�氳繃redis涓幏鍙栫紦瀛樺瓧鍏告暟鎹� + for (String dictCode : dataListMap.keySet()) { + List<String> dataList = dataListMap.get(dictCode); + if (dataList.size() == 0) { + continue; + } + // 琛ㄥ瓧鍏搁渶瑕佺炕璇戠殑鏁版嵁 + List<String> 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<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 璋冪敤鏁版嵁搴撶炕璇戣〃瀛楀吀 + if (needTranslDataTable.size() > 0) { + String[] arr = dictCode.split(","); + String table = arr[0], text = arr[1], code = arr[2]; + String values = String.join(",", needTranslDataTable); + log.debug("translateDictFromTableByKeys.dictCode:" + dictCode); + log.debug("translateDictFromTableByKeys.values:" + values); + List<DictModel> texts = commonApi.translateDictFromTableByKeys(table, text, code, values); + log.debug("translateDictFromTableByKeys.result:" + texts); + List<DictModel> list = translText.computeIfAbsent(dictCode, k -> new ArrayList<>()); + list.addAll(texts); + + // 鍋� redis 缂撳瓨 + for (DictModel dict : texts) { + String redisKey = String.format("sys:cache:dictTable::SimpleKey [%s,%s]", dictCode, dict.getValue()); + try { + // update-begin-author:taoyan date:20211012 for: 瀛楀吀琛ㄧ炕璇戞敞瑙g紦瀛樻湭鏇存柊 issues/3061 + // 淇濈暀5鍒嗛挓 + redisTemplate.opsForValue().set(redisKey, dict.getText(), 300, TimeUnit.SECONDS); + // update-end-author:taoyan date:20211012 for: 瀛楀吀琛ㄧ炕璇戞敞瑙g紦瀛樻湭鏇存柊 issues/3061 + } catch (Exception e) { + log.warn(e.getMessage(), e); + } + } + } + } + + //step.3 璋冪敤鏁版嵁搴撹繘琛岀炕璇戞櫘閫氬瓧鍏� + if (needTranslData.size() > 0) { + List<String> dictCodeList = Arrays.asList(dataListMap.keySet().toArray(new String[]{})); + // 灏嗕笉鍖呭惈閫楀彿鐨勫瓧鍏竎ode绛涢�夊嚭鏉ワ紝鍥犱负甯﹂�楀彿鐨勬槸琛ㄥ瓧鍏革紝鑰屼笉鏄櫘閫氱殑鏁版嵁瀛楀吀 + List<String> filterDictCodes = dictCodeList.stream().filter(key -> !key.contains(",")).collect(Collectors.toList()); + String dictCodes = String.join(",", filterDictCodes); + String values = String.join(",", needTranslData); + log.debug("translateManyDict.dictCodes:" + dictCodes); + log.debug("translateManyDict.values:" + values); + Map<String, List<DictModel>> manyDict = commonApi.translateManyDict(dictCodes, values); + log.debug("translateManyDict.result:" + manyDict); + for (String dictCode : manyDict.keySet()) { + List<DictModel> list = translText.computeIfAbsent(dictCode, k -> new ArrayList<>()); + List<DictModel> newList = manyDict.get(dictCode); + list.addAll(newList); + + // 鍋� redis 缂撳瓨 + for (DictModel dict : newList) { + String redisKey = String.format("sys:cache:dict::%s:%s", dictCode, dict.getValue()); + try { + redisTemplate.opsForValue().set(redisKey, dict.getText()); + } catch (Exception e) { + log.warn(e.getMessage(), e); + } + } + } + } + return translText; + } + + /** + * 瀛楀吀鍊兼浛鎹㈡枃鏈� + * + * @param dictModels + * @param values + * @return + */ + private String translDictText(List<DictModel> dictModels, String values) { + List<String> result = new ArrayList<>(); + + // 鍏佽澶氫釜閫楀彿鍒嗛殧锛屽厑璁镐紶鏁扮粍瀵硅薄 + String[] splitVal = values.split(","); + for (String val : splitVal) { + String dictText = val; + for (DictModel dict : dictModels) { + if (val.equals(dict.getValue())) { + dictText = dict.getText(); + break; + } + } + result.add(dictText); + } + return String.join(",", result); + } + + /** + * 缈昏瘧瀛楀吀鏂囨湰 + * @param code + * @param text + * @param table + * @param key + * @return + */ + @Deprecated + private String translateDictValue(String code, String text, String table, String key) { + if(oConvertUtils.isEmpty(key)) { + return null; + } + StringBuffer textValue=new StringBuffer(); + String[] keys = key.split(","); + for (String k : keys) { + String tmpValue = null; + log.debug(" 瀛楀吀 key : "+ k); + if (k.trim().length() == 0) { + continue; //璺宠繃寰幆 + } + //update-begin--Author:scott -- Date:20210531 ----for锛� !56 浼樺寲寰湇鍔″簲鐢ㄤ笅瀛樺湪琛ㄥ瓧娈甸渶瑕佸瓧鍏哥炕璇戞椂鍔犺浇缂撴參闂----- + if (!StringUtils.isEmpty(table)){ + log.debug("--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 浼樺寲寰湇鍔″簲鐢ㄤ笅瀛樺湪琛ㄥ瓧娈甸渶瑕佸瓧鍏哥炕璇戞椂鍔犺浇缂撴參闂----- + + if (tmpValue != null) { + if (!"".equals(textValue.toString())) { + textValue.append(","); + } + textValue.append(tmpValue); + } + + } + return textValue.toString(); + } + + /** + * 妫�娴嬭繑鍥炵粨鏋滈泦涓槸鍚﹀寘鍚獶ict娉ㄨВ + * @param records + * @return + */ + private Boolean checkHasDict(List<Object> records){ + if(oConvertUtils.isNotEmpty(records) && records.size()>0){ + for (Field field : oConvertUtils.getAllFields(records.get(0))) { + if (oConvertUtils.isNotEmpty(field.getAnnotation(Dict.class))) { + return true; + } + } + } + return false; + } + +} -- Gitblit v1.9.3