From 3b595f90b0f677e8277e38edcff1f3116f4b9b7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E7=90=A6=E6=B6=9B?= Date: Wed, 7 Aug 2024 15:17:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BF=BB=E8=AF=91=E5=88=87?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jero/common/aspect/DictAspect.java | 59 ++++++++++++++++++- .../jero/common/aspect/TranslationAspect.java | 45 +++++++++++++- .../common/system/vo/SysDictItemCore.java | 9 +++ 3 files changed, 111 insertions(+), 2 deletions(-) diff --git a/laws-base/laws-base-core/src/main/java/com/jero/common/aspect/DictAspect.java b/laws-base/laws-base-core/src/main/java/com/jero/common/aspect/DictAspect.java index 1b769f83..42b8cfb1 100644 --- a/laws-base/laws-base-core/src/main/java/com/jero/common/aspect/DictAspect.java +++ b/laws-base/laws-base-core/src/main/java/com/jero/common/aspect/DictAspect.java @@ -1,5 +1,6 @@ package com.jero.common.aspect; +import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; @@ -14,6 +15,7 @@ import com.jero.common.aspect.annotation.Dict; import com.jero.common.constant.CacheConstant; import com.jero.common.constant.CommonConstant; import com.jero.common.system.vo.DictModel; +import com.jero.common.system.vo.SysDictItemCore; import com.jero.common.util.oConvertUtils; import lombok.extern.slf4j.Slf4j; import org.apache.poi.ss.formula.functions.T; @@ -24,6 +26,7 @@ import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import java.lang.reflect.Field; @@ -129,11 +132,17 @@ public class DictAspect { } private void putItems(List items, List dictFieldList, Map> translText) { + List listDict = commonAPI.getDictItemAll(); + if (CollectionUtils.isEmpty(listDict)) { + return; + } 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 enText = field.getAnnotation(Dict.class).dicEnText(); + String key = String.valueOf(obj.get(field.getName())); String fieldDictCode = code; if (!StringUtils.isEmpty(table)) { @@ -158,11 +167,56 @@ public class DictAspect { log.debug(" ---- dictModels: " + JSON.toJSONString(dictModels)); obj.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue); + String enValue = translateDictValueByEn(code, enText, table, key, listDict); + obj.put(field.getName() + CommonConstant.DICT_TEXT_EN_SUFFIX, enValue); } } } } + /** + * 翻译字典文本 + * + * @param code 编码 + * @param text 值 + * @param table 表名 + * @param key 键 + * @param listDict 字典集合 + */ + private String translateDictValueByEn(String code, String text, String table, String key, List listDict) { + if (oConvertUtils.isEmpty(key)) { + return null; + } + StringBuilder textValue = new StringBuilder(); + String[] keys = key.split(","); + for (String k : keys) { + String tmpValue = null; + if (k.trim().length() == 0) { + continue; //跳过循环 + } + if (!StringUtils.isEmpty(table) && StrUtil.isNotBlank(text)) { + tmpValue = commonAPI.queryTableDictTextByKey(table, text, code, k.trim()); + } else { + List listNew = listDict.stream() + .filter(o -> Objects.equals(o.getDictCode(), code) && + Objects.equals(o.getItemValue(), k.trim())) + .collect(Collectors.toList()); + if (!CollectionUtils.isEmpty(listNew)) { + tmpValue = listNew.get(0).getEnName(); + } + } + + if (tmpValue != null) { + if (!"".equals(textValue.toString())) { + textValue.append(","); + } + textValue.append(tmpValue); + } + } + return textValue.toString(); + } + + private void addItems(Result> result, List items, List dictFieldList, Map> dataListMap) { for (Object obj : result.getResult().getRecords()) { ObjectMapper mapper = new ObjectMapper(); @@ -410,7 +464,7 @@ public class DictAspect { } private String getTmpValue(String code, String text, String table, String k, String tmpValue) { - if (!StringUtils.isEmpty(table)){ + if (!StringUtils.isEmpty(table) && !StringUtils.isEmpty(text)){ 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))){ @@ -485,6 +539,7 @@ public class DictAspect { String code = field.getAnnotation(Dict.class).dicCode(); String text = field.getAnnotation(Dict.class).dicText(); String table = field.getAnnotation(Dict.class).dictTable(); + String enText = field.getAnnotation(Dict.class).dicEnText(); String key = String.valueOf(item.get(field.getName())); //翻译字典值对应的txt @@ -493,6 +548,8 @@ public class DictAspect { log.debug(" 字典Val : " + textValue); log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + ": " + textValue); item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue); + String enValue = translateDictValue(code, enText, table, key); + item.put(field.getName() + CommonConstant.DICT_TEXT_EN_SUFFIX, enValue); } //date类型默认转换string格式化日期 if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) { 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 e98b9f47..1248af6b 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 @@ -212,7 +212,7 @@ public class TranslationAspect { String enText = field.getAnnotation(Dict.class).dicEnText(); String key = String.valueOf(item.get(field.getName())); String textValue = translateDictValue(code, text, table, key, listDict); - String enValue = translateDictValue(code, enText, table, key, listDict); + String enValue = translateDictValueByEn(code, enText, table, key, listDict); item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue); item.put(field.getName() + CommonConstant.DICT_TEXT_EN_SUFFIX, enValue); } @@ -279,4 +279,47 @@ public class TranslationAspect { } return textValue.toString(); } + + + /** + * 翻译字典文本 + * + * @param code 编码 + * @param text 值 + * @param table 表名 + * @param key 键 + * @param listDict 字典集合 + */ + private String translateDictValueByEn(String code, String text, String table, String key, List listDict) { + if (oConvertUtils.isEmpty(key)) { + return null; + } + StringBuilder textValue = new StringBuilder(); + String[] keys = key.split(","); + for (String k : keys) { + String tmpValue = null; + if (k.trim().length() == 0) { + continue; //跳过循环 + } + if (!StringUtils.isEmpty(table) && StrUtil.isNotBlank(text)) { + tmpValue = commonAPI.queryTableDictTextByKey(table, text, code, k.trim()); + } else { + List listNew = listDict.stream() + .filter(o -> Objects.equals(o.getDictCode(), code) && + Objects.equals(o.getItemValue(), k.trim())) + .collect(Collectors.toList()); + if (!CollectionUtils.isEmpty(listNew)) { + tmpValue = listNew.get(0).getEnName(); + } + } + + if (tmpValue != null) { + if (!"".equals(textValue.toString())) { + textValue.append(","); + } + textValue.append(tmpValue); + } + } + return textValue.toString(); + } } diff --git a/laws-base/laws-base-core/src/main/java/com/jero/common/system/vo/SysDictItemCore.java b/laws-base/laws-base-core/src/main/java/com/jero/common/system/vo/SysDictItemCore.java index 19769671..5ad0808f 100644 --- a/laws-base/laws-base-core/src/main/java/com/jero/common/system/vo/SysDictItemCore.java +++ b/laws-base/laws-base-core/src/main/java/com/jero/common/system/vo/SysDictItemCore.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.jero.common.aspect.annotation.Dict; +import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; @@ -62,6 +63,14 @@ public class SysDictItemCore implements Serializable { private Integer sortOrder; + /** + * 字典英文名称 + */ + @Excel(name = "英文名称", width = 15) + @ApiModelProperty(value = "字典英文名称") + private String enName; + + /** * 状态(1启用 0不启用) */