【fix sonar】DictAspect文件

This commit is contained in:
梁琦涛
2023-03-03 10:39:42 +08:00
parent 2820d112d6
commit 59b39f57ff
@@ -6,23 +6,21 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; 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 com.jero.common.api.CommonAPI; import com.jero.common.api.CommonAPI;
import com.jero.common.api.vo.Result; import com.jero.common.api.vo.Result;
import com.jero.common.aspect.annotation.Dict; import com.jero.common.aspect.annotation.Dict;
import com.jero.common.constant.CommonConstant; import com.jero.common.constant.CommonConstant;
import com.jero.common.system.vo.DictModel; import com.jero.common.system.vo.DictModel;
import com.jero.common.util.oConvertUtils; import com.jero.common.util.oConvertUtils;
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.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@@ -46,9 +44,12 @@ public class DictAspect {
@Autowired @Autowired
public RedisTemplate redisTemplate; public RedisTemplate redisTemplate;
private static final String SYS_CACHE_DICT_S_S = "sys:cache:dict::%s:%s";
// 定义切点Pointcut // 定义切点Pointcut
@Pointcut("execution(public * com.jero.modules..*.*Controller.*(..))") @Pointcut("execution(public * com.jero.modules..*.*Controller.*(..))")
public void excudeService() { public void excudeService() {
// do other
} }
@Around("excudeService()") @Around("excudeService()")
@@ -87,8 +88,7 @@ public class DictAspect {
* @param result * @param result
*/ */
private void parseDictText(Object result) { private void parseDictText(Object result) {
if (result instanceof Result) { if (result instanceof Result && ((Result) result).getResult() instanceof IPage) {
if (((Result) result).getResult() instanceof IPage) {
List<JSONObject> items = new ArrayList<>(); List<JSONObject> items = new ArrayList<>();
//step.1 筛选出加了 Dict 注解的字段列表 //step.1 筛选出加了 Dict 注解的字段列表
@@ -105,7 +105,7 @@ public class DictAspect {
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
log.error("json解析失败"+e.getMessage(),e); log.error("json解析失败"+e.getMessage(),e);
} }
JSONObject item = JSONObject.parseObject(json); JSONObject item = JSON.parseObject(json);
//update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------ //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
//for (Field field : record.getClass().getDeclaredFields()) { //for (Field field : record.getClass().getDeclaredFields()) {
// 遍历所有字段,把字典Code取出来,放到 map 里 // 遍历所有字段,把字典Code取出来,放到 map 里
@@ -132,7 +132,7 @@ public class DictAspect {
this.listAddAllDeduplicate(dataList, Arrays.asList(value.split(","))); this.listAddAllDeduplicate(dataList, Arrays.asList(value.split(",")));
} }
//date类型默认转换string格式化日期 //date类型默认转换string格式化日期
if (field.getType().getName().equals("java.util.Date")&&field.getAnnotation(JsonFormat.class)==null&&item.get(field.getName())!=null){ if (Date.class.isAssignableFrom(field.getType()) && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null){
SimpleDateFormat aDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat aDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName())))); item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
} }
@@ -179,8 +179,6 @@ public class DictAspect {
((IPage) ((Result) result).getResult()).setRecords(items); ((IPage) ((Result) result).getResult()).setRecords(items);
} }
}
} }
/** /**
@@ -232,7 +230,7 @@ public class DictAspect {
needTranslDataTable.add(data); needTranslDataTable.add(data);
} }
} else { } else {
String keyString = String.format("sys:cache:dict::%s:%s", dictCode, data); String keyString = String.format(SYS_CACHE_DICT_S_S, dictCode, data);
if (redisTemplate.hasKey(keyString)) { if (redisTemplate.hasKey(keyString)) {
try { try {
String text = oConvertUtils.getString(redisTemplate.opsForValue().get(keyString)); String text = oConvertUtils.getString(redisTemplate.opsForValue().get(keyString));
@@ -291,7 +289,7 @@ public class DictAspect {
// 做 redis 缓存 // 做 redis 缓存
for (DictModel dict : newList) { for (DictModel dict : newList) {
String redisKey = String.format("sys:cache:dict::%s:%s", dictCode, dict.getValue()); String redisKey = String.format(SYS_CACHE_DICT_S_S, dictCode, dict.getValue());
try { try {
redisTemplate.opsForValue().set(redisKey, dict.getText()); redisTemplate.opsForValue().set(redisKey, dict.getText());
} catch (Exception e) { } catch (Exception e) {
@@ -363,7 +361,7 @@ public class DictAspect {
tmpValue= commonAPI.translateDictFromTable(table,text,code,k.trim()); tmpValue= commonAPI.translateDictFromTable(table,text,code,k.trim());
} }
}else { }else {
String keyString = String.format("sys:cache:dict::%s:%s",code,k.trim()); String keyString = String.format(SYS_CACHE_DICT_S_S,code,k.trim());
if (redisTemplate.hasKey(keyString)){ if (redisTemplate.hasKey(keyString)){
try { try {
tmpValue = oConvertUtils.getString(redisTemplate.opsForValue().get(keyString)); tmpValue = oConvertUtils.getString(redisTemplate.opsForValue().get(keyString));