优化代码格式

优化dict注解切面功能
dict注解使用dictTable时相当于for循环查询数据库
当数据量过大时效率极低故优化
组装成一个列表直接查一次就可完成所有数据的组装
暂不支持没有dictTable的情况
This commit is contained in:
yunxiao 2020-06-15 20:06:55 +08:00
parent dacfd16273
commit 2331ab475f
1 changed files with 72 additions and 68 deletions
jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/aspect

View File

@ -39,6 +39,7 @@ import lombok.extern.slf4j.Slf4j;
public class DictAspect {
@Autowired
private ISysDictService dictService;
// 定义切点Pointcut
@Pointcut("execution(public * org.jeecg.modules..*.*Controller.*(..))")
public void excudeService() {
@ -46,14 +47,14 @@ public class DictAspect {
@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");
long start=System.currentTimeMillis();
long time2 = System.currentTimeMillis();
log.debug("获取JSON数据 耗时:" + (time2 - time1) + "ms");
long start = System.currentTimeMillis();
this.parseDictText(result);
long end=System.currentTimeMillis();
log.debug("解析注入JSON数据 耗时"+(end-start)+"ms");
long end = System.currentTimeMillis();
log.debug("解析注入JSON数据 耗时" + (end - start) + "ms");
return result;
}
@ -63,47 +64,48 @@ public class DictAspect {
* 示例为SysUser 字段为sex 添加了注解@Dict(dicCode = "sex") 会在字典服务立马查出来对应的text 然后在请求list的时候将这个字典text已字段名称加_dictText形式返回到前端
* 例输入当前返回值的就会多出一个sex_dictText字段
* {
* sex:1,
* sex_dictText:""
* sex:1,
* sex_dictText:""
* }
* 前端直接取值sext_dictText在table里面无需再进行前端的字典转换了
* customRender:function (text) {
* if(text==1){
* return "";
* }else if(text==2){
* return "";
* }else{
* return text;
* }
* }
* 目前vue是这么进行字典渲染到table上的多了就很麻烦了 这个直接在服务端渲染完成前端可以直接用
* customRender:function (text) {
* if(text==1){
* return "";
* }else if(text==2){
* return "";
* }else{
* return text;
* }
* }
* 目前vue是这么进行字典渲染到table上的多了就很麻烦了 这个直接在服务端渲染完成前端可以直接用
*
* @param result
*/
private void parseDictText(Object result) {
if (result instanceof Result) {
if (((Result) result).getResult() instanceof IPage) {
List<JSONObject> items = new ArrayList<>();
Map<String,String> fieldItems=new HashMap<>();
Map<String,ArrayList<String>> keysList=new HashMap<>();
Set<String> keys=new HashSet<>();
Map<String, String> fieldItems = new HashMap<>();
Map<String, ArrayList<String>> keysList = new HashMap<>();
Set<String> keys = new HashSet<>();
for (Object record : ((IPage) ((Result) result).getResult()).getRecords()) {
ObjectMapper mapper = new ObjectMapper();
String json="{}";
String json = "{}";
try {
//解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
json = mapper.writeValueAsString(record);
json = mapper.writeValueAsString(record);
} catch (JsonProcessingException e) {
log.error("json解析失败"+e.getMessage(),e);
log.error("json解析失败" + e.getMessage(), e);
}
JSONObject item = JSONObject.parseObject(json);
//update-begin--Author:scott -- Date:20190603 ----for解决继承实体字段无法翻译问题------
//for (Field field : record.getClass().getDeclaredFields()) {
for (Field field : oConvertUtils.getAllFields(record)) {
if (field.getType().getName().equals("java.util.Date")&&field.getAnnotation(JsonFormat.class)==null&&item.get(field.getName())!=null){
SimpleDateFormat aDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (field.getType().getName().equals("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()))));
}
//update-end--Author:scott -- Date:20190603 ----for解决继承实体字段无法翻译问题------
//update-end--Author:scott -- Date:20190603 ----for解决继承实体字段无法翻译问题------
if (field.getAnnotation(Dict.class) != null) {
String code = field.getAnnotation(Dict.class).dicCode();
String text = field.getAnnotation(Dict.class).dicText();
@ -111,16 +113,16 @@ public class DictAspect {
String key = String.valueOf(item.get(field.getName()));
//组装sql关键字 ItemOrder功能启用
if(field.getAnnotation(ItemOrder.class)!=null){
String order=field.getAnnotation(ItemOrder.class).value();
if (field.getAnnotation(ItemOrder.class) != null) {
String order = field.getAnnotation(ItemOrder.class).value();
keys.add(order);
//实现单次加载
if(!fieldItems.containsKey("code"+order)){
keysList.put("index"+order, new ArrayList<>());
fieldItems.put("code"+order,code);
fieldItems.put("text"+order,text);
fieldItems.put("table"+order,table);
fieldItems.put("fieldName"+order,field.getName());
if (!fieldItems.containsKey("code" + order)) {
keysList.put("index" + order, new ArrayList<>());
fieldItems.put("code" + order, code);
fieldItems.put("text" + order, text);
fieldItems.put("table" + order, table);
fieldItems.put("fieldName" + order, field.getName());
}
continue;
}
@ -128,8 +130,8 @@ public class DictAspect {
//翻译字典值对应的txt
String textValue = translateDictValue(code, text, table, key);
log.debug(" 字典Val : "+ textValue);
log.debug(" __翻译字典字段__ "+field.getName() + CommonConstant.DICT_TEXT_SUFFIX+" "+ textValue);
log.debug(" 字典Val : " + textValue);
log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + " " + textValue);
item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
}
//date类型默认转换string格式化日期
@ -137,19 +139,19 @@ public class DictAspect {
}
//实现sql in里面的值组装
for (Field field : oConvertUtils.getAllFields(record)) {
if(field.getAnnotation(ItemOrder.class)!=null){
String order=field.getAnnotation(ItemOrder.class).value();
keysList.get("index"+order).add(String.valueOf(item.get(field.getName())));
if (field.getAnnotation(ItemOrder.class) != null) {
String order = field.getAnnotation(ItemOrder.class).value();
keysList.get("index" + order).add(String.valueOf(item.get(field.getName())));
}
}
items.add(item);
}
//for循环填充值
for (String key:keys){
Map<String,String> textValues=translateDictValues(fieldItems.get("code"+key), fieldItems.get("text"+key), fieldItems.get("table"+key), keysList.get("index"+key));
if(oConvertUtils.isNotEmpty(textValues)){
for (JSONObject itemFix:items){
itemFix.put(fieldItems.get("fieldName"+key) + CommonConstant.DICT_TEXT_SUFFIX,textValues.get(String.valueOf(itemFix.get(fieldItems.get("fieldName"+key)))));
for (String key : keys) {
Map<String, String> textValues = translateDictValues(fieldItems.get("code" + key), fieldItems.get("text" + key), fieldItems.get("table" + key), keysList.get("index" + key));
if (oConvertUtils.isNotEmpty(textValues)) {
for (JSONObject itemFix : items) {
itemFix.put(fieldItems.get("fieldName" + key) + CommonConstant.DICT_TEXT_SUFFIX, textValues.get(String.valueOf(itemFix.get(fieldItems.get("fieldName" + key)))));
}
}
}
@ -161,7 +163,8 @@ public class DictAspect {
}
/**
* 翻译字典文本
* 翻译字典文本
*
* @param code
* @param text
* @param table
@ -169,21 +172,21 @@ public class DictAspect {
* @return
*/
private String translateDictValue(String code, String text, String table, String key) {
if(oConvertUtils.isEmpty(key)) {
return null;
}
StringBuffer textValue=new StringBuffer();
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);
log.debug(" 字典 key : " + k);
if (k.trim().length() == 0) {
continue; //跳过循环
}
if (!StringUtils.isEmpty(table)){
log.debug("--DictAspect------dicTable="+ table+" ,dicText= "+text+" ,dicCode="+code);
tmpValue= dictService.queryTableDictTextByKey(table,text,code,k.trim());
}else {
if (!StringUtils.isEmpty(table)) {
log.debug("--DictAspect------dicTable=" + table + " ,dicText= " + text + " ,dicCode=" + code);
tmpValue = dictService.queryTableDictTextByKey(table, text, code, k.trim());
} else {
tmpValue = dictService.queryDictTextByKey(code, k.trim());
}
@ -199,29 +202,30 @@ public class DictAspect {
}
/**
* 翻译字典文本一次完成
* 翻译字典文本一次完成
*
* @param code
* @param text
* @param table
* @param keys
* @return
*/
private Map<String,String> translateDictValues(String code, String text, String table, List<String> keys) {
if(oConvertUtils.isEmpty(keys)) {
private Map<String, String> translateDictValues(String code, String text, String table, List<String> keys) {
if (oConvertUtils.isEmpty(keys)) {
return null;
}
List<ItemValue> itemValues=null;
Map<String,String> map=new HashMap<>();
if (!StringUtils.isEmpty(table)){
log.debug("--DictAspect------dicTable="+ table+" ,dicText= "+text+" ,dicCode="+code);
itemValues = dictService.queryTableDictTextByKeys(table,text,code,keys);
}
List<ItemValue> itemValues = null;
Map<String, String> map = new HashMap<>();
if (!StringUtils.isEmpty(table)) {
log.debug("--DictAspect------dicTable=" + table + " ,dicText= " + text + " ,dicCode=" + code);
itemValues = dictService.queryTableDictTextByKeys(table, text, code, keys);
}
if (oConvertUtils.isNotEmpty(itemValues)) {
for(ItemValue itemValue:itemValues){
map.put(itemValue.getCode(),itemValue.getText());
}
if (oConvertUtils.isNotEmpty(itemValues)) {
for (ItemValue itemValue : itemValues) {
map.put(itemValue.getCode(), itemValue.getText());
}
}
return map;
}