fix: 数据字典不赋值bug
This commit is contained in:
+145
-59
@@ -1,8 +1,10 @@
|
||||
package com.jero.common.aspect;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.jero.common.api.CommonAPI;
|
||||
@@ -22,10 +24,7 @@ import org.springframework.util.StringUtils;
|
||||
import javax.annotation.Resource;
|
||||
import java.lang.reflect.Field;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -56,54 +55,106 @@ public class TranslationAspect {
|
||||
|
||||
@Around("annotationPointcut()")
|
||||
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
Object result = joinPoint.proceed();
|
||||
this.parseDictText(result);
|
||||
return result;
|
||||
Object Results = joinPoint.proceed();
|
||||
this.parseDictText(Results);
|
||||
return Results;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在切入点return内容之后切入内容(可以用来对处理返回值做一些加工处理)
|
||||
*
|
||||
* @param joinPoint
|
||||
*/
|
||||
@AfterReturning(returning="rvt",pointcut = "annotationPointcut()")
|
||||
@AfterReturning(returning = "rvt", pointcut = "annotationPointcut()")
|
||||
public void doAfterReturning(JoinPoint joinPoint, Object rvt) {
|
||||
|
||||
}
|
||||
|
||||
private void parseDictText(Object result) {
|
||||
private void parseDictText(Object Results) {
|
||||
List<SysDictItemCore> listDict = commonAPI.getDictItemAll();
|
||||
if(CollectionUtils.isEmpty(listDict)){
|
||||
if (CollectionUtils.isEmpty(listDict)) {
|
||||
return;
|
||||
}
|
||||
if (result instanceof Results) {
|
||||
if (((Results) result).getResult() instanceof IPage) {
|
||||
if (Results instanceof Results) {
|
||||
if (((Results) Results).getResult() instanceof IPage) {
|
||||
List<Object> items = new ArrayList<>();
|
||||
|
||||
for (Object record : ((IPage) ((Results) result).getResult()).getRecords()) {
|
||||
Object item = translateObject(record,listDict);
|
||||
for (Object record : ((IPage) ((Results) Results).getResult()).getRecords()) {
|
||||
Object item = translateObject(record, listDict);
|
||||
items.add(item);
|
||||
}
|
||||
((IPage) ((Results) result).getResult()).setRecords(items);
|
||||
}else if (((Results) result).getResult() instanceof List) {
|
||||
((IPage) ((Results) Results).getResult()).setRecords(items);
|
||||
} else if (((Results) Results).getResult() instanceof List) {
|
||||
List<Object> items = new ArrayList<>();
|
||||
for (Object record : ((List) ((Results) result).getResult())) {
|
||||
Object item = translateObject(record,listDict);
|
||||
for (Object record : ((List) ((Results) Results).getResult())) {
|
||||
Object item = translateObject(record, listDict);
|
||||
items.add(item);
|
||||
}
|
||||
((Results) result).setResult(items);
|
||||
}else{
|
||||
Object record = (Object) ((Results) result).getResult();
|
||||
Object item = translateObject(record,listDict);
|
||||
((Results) result).setResult(item);
|
||||
((Results) Results).setResult(items);
|
||||
} else {
|
||||
Object record = (Object) ((Results) Results).getResult();
|
||||
Object item = translateObject(record, listDict);
|
||||
((Results) Results).setResult(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Object translateObject(Object record,List<SysDictItemCore> listDict){
|
||||
/**
|
||||
* 手动发起流程
|
||||
* 流程发起将数据翻译后保存为json以供强制撤回后使用
|
||||
* @param data
|
||||
* @param user
|
||||
* @return
|
||||
*/
|
||||
public String manualParseDictText(Object data, Object user) {
|
||||
List<SysDictItemCore> listDict = commonAPI.getDictItemAll();
|
||||
if (CollectionUtils.isEmpty(listDict)) {
|
||||
return "";
|
||||
}
|
||||
JSONObject dataJsonObject = (JSONObject) translateObject(data, listDict);
|
||||
dataJsonObject.put("personnelObj", user);
|
||||
return dataJsonObject.toJSONString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动发起流程的
|
||||
* 流程发起将数据翻译后保存为json以供强制撤回后使用
|
||||
* @param data
|
||||
* @param user
|
||||
* @return
|
||||
*/
|
||||
public String autoParseDictText(Object data, Map<String, Object> user) {
|
||||
List<SysDictItemCore> listDict = commonAPI.getDictItemAll();
|
||||
if (CollectionUtils.isEmpty(listDict)) {
|
||||
return "";
|
||||
}
|
||||
JSONObject dataJsonObject = (JSONObject) translateObject(data, listDict);
|
||||
// 对人员进行翻译
|
||||
dataJsonObject.put("personnelObj", this.translateUser(user, listDict));
|
||||
return dataJsonObject.toJSONString();
|
||||
}
|
||||
|
||||
private Object translateUser(Map<String, Object> user, List<SysDictItemCore> listDict) {
|
||||
Map<String, Object> Results = new HashMap<>();
|
||||
for (Map.Entry<String, Object> entry : user.entrySet()) {
|
||||
String value = (String) entry.getValue();
|
||||
if (StrUtil.isBlank(value)) {
|
||||
continue;
|
||||
}
|
||||
String code = "id";
|
||||
String text = "CONCAT(realname, '(', username, ')')";
|
||||
String table = "sys_user";
|
||||
String textValue = translateDictValue(code, text, table, value, listDict);
|
||||
Results.put(entry.getKey(), value);
|
||||
Results.put(entry.getKey() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
|
||||
}
|
||||
return Results;
|
||||
}
|
||||
|
||||
private Object translateObject(Object record, List<SysDictItemCore> listDict) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String json;
|
||||
try {
|
||||
//解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
|
||||
json = mapper.writeValueAsString(record);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("json解析失败" + e.getMessage(), e);
|
||||
@@ -112,44 +163,79 @@ public class TranslationAspect {
|
||||
JSONObject item;
|
||||
try {
|
||||
item = JSONObject.parseObject(json);
|
||||
//update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
|
||||
//for (Field field : record.getClass().getDeclaredFields()) {
|
||||
|
||||
for (Field field : oConvertUtils.getAllFields(record)) {
|
||||
//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();
|
||||
String table = field.getAnnotation(Dict.class).dictTable();
|
||||
String key = String.valueOf(item.get(field.getName()));
|
||||
|
||||
//翻译字典值对应的txt
|
||||
String textValue = translateDictValue(code, text, table, key, listDict);
|
||||
item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
|
||||
}
|
||||
//date类型默认转换string格式化日期
|
||||
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()))));
|
||||
}
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.info("########################此返回类型不支持字典翻译########################");
|
||||
} catch (Exception e) {
|
||||
log.error("json解析失败" + e.getMessage(), e);
|
||||
return record;
|
||||
}
|
||||
|
||||
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 (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<Object> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private boolean isComplexObject(Class<?> type) {
|
||||
return !(
|
||||
type.isPrimitive() ||
|
||||
type.equals(String.class) ||
|
||||
Number.class.isAssignableFrom(type) ||
|
||||
Date.class.isAssignableFrom(type)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 翻译字典文本
|
||||
* @param code 编码
|
||||
* @param text 值
|
||||
* @param table 表名
|
||||
* @param key 键
|
||||
* 翻译字典文本
|
||||
*
|
||||
* @param code 编码
|
||||
* @param text 值
|
||||
* @param table 表名
|
||||
* @param key 键
|
||||
* @param listDict 字典集合
|
||||
*/
|
||||
private String translateDictValue(String code, String text, String table, String key, List<SysDictItemCore> listDict) {
|
||||
if(oConvertUtils.isEmpty(key)) {
|
||||
if (oConvertUtils.isEmpty(key)) {
|
||||
return null;
|
||||
}
|
||||
StringBuilder textValue = new StringBuilder();
|
||||
@@ -159,11 +245,11 @@ public class TranslationAspect {
|
||||
if (k.trim().length() == 0) {
|
||||
continue; //跳过循环
|
||||
}
|
||||
if (!StringUtils.isEmpty(table)){
|
||||
tmpValue = commonAPI.queryTableDictTextByKey(table,text,code,k.trim());
|
||||
}else {
|
||||
List<SysDictItemCore> listNew = listDict.stream().filter(o-> Objects.equals(o.getDictCode(),code) && Objects.equals(o.getItemValue(),k.trim())).collect(Collectors.toList());
|
||||
if(!CollectionUtils.isEmpty(listNew)){
|
||||
if (!StringUtils.isEmpty(table)) {
|
||||
tmpValue = commonAPI.queryTableDictTextByKey(table, text, code, k.trim());
|
||||
} else {
|
||||
List<SysDictItemCore> 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).getItemText();
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -1,6 +1,7 @@
|
||||
package com.jero.modules.system.entity;
|
||||
|
||||
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 lombok.Data;
|
||||
@@ -76,5 +77,6 @@ public class SysDictItem implements Serializable {
|
||||
|
||||
private Date updateTime;
|
||||
|
||||
|
||||
@TableField(exist = false)
|
||||
private String dictCode;
|
||||
}
|
||||
|
||||
+3
@@ -18,6 +18,9 @@ public interface SysDictItemMapper extends BaseMapper<SysDictItem> {
|
||||
@Select("SELECT * FROM sys_dict_item WHERE DICT_ID = #{mainId} order by sort_order asc, item_value asc")
|
||||
public List<SysDictItem> selectItemsByMainId(String mainId);
|
||||
|
||||
@Select("SELECT sys_dict.dict_code,sys_dict_item.* from sys_dict_item LEFT JOIN sys_dict ON sys_dict_item.dict_id = sys_dict.id where status = 1")
|
||||
List<SysDictItem> selectItemsAll();
|
||||
|
||||
|
||||
// /**
|
||||
// * 获取字典所有数据
|
||||
|
||||
+1
-2
@@ -30,7 +30,6 @@ public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDi
|
||||
|
||||
@Override
|
||||
public List<SysDictItem> getDictItemAll() {
|
||||
// return sysDictItemMapper.getDictItemAll();
|
||||
return null;
|
||||
return sysDictItemMapper.selectItemsAll();
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -3,6 +3,7 @@ package com.jero.modules.controller;
|
||||
import java.util.List;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import com.jero.common.api.vo.Results;
|
||||
import com.jero.common.aspect.annotation.Translation;
|
||||
import com.jero.modules.entity.KhSystemSetting;
|
||||
import com.jero.modules.service.IKhSystemSettingService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -41,6 +42,7 @@ public class KhSystemSettingController extends JeroController<KhSystemSetting, I
|
||||
@AutoLog(value = "系统设置-获取设置")
|
||||
@ApiOperation(value="系统设置-获取设置", notes="系统设置-获取设置")
|
||||
@GetMapping(value = "/getSetting")
|
||||
@Translation
|
||||
public Results<KhSystemSetting> queryList(KhSystemSetting khSystemSetting, HttpServletRequest req) {
|
||||
List<KhSystemSetting> list = khSystemSettingService.queryList(khSystemSetting, req);
|
||||
return Results.OK(list.get(0));
|
||||
|
||||
Reference in New Issue
Block a user