From 9f059a097a0c38496dd79937900d6be0a8751a9f Mon Sep 17 00:00:00 2001 From: xd <844539747@qq.com> Date: Fri, 26 Jul 2024 15:18:17 +0800 Subject: [PATCH] '11' --- .../changeRecord/ChangeRecordAspect.java | 65 +++++++++++++++++-- .../changeRecord/DefaultContentParse.java | 7 +- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/changeRecord/ChangeRecordAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/changeRecord/ChangeRecordAspect.java index bb7063b..dc70e67 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/changeRecord/ChangeRecordAspect.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/changeRecord/ChangeRecordAspect.java @@ -9,6 +9,7 @@ import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.domain.SysChangeRecord; import com.ruoyi.system.mapper.SysChangeRecordMapper; +import jodd.util.StringUtil; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; @@ -24,7 +25,10 @@ import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; @Aspect @Component @@ -89,13 +93,19 @@ public class ChangeRecordAspect { e.printStackTrace(); log.error("service加载失败:", e); } - if ("add".equals(operateLog.type())) { +/* if ("add".equals(operateLog.type())) { Map dataMap = (Map) objectToMap(object); log.info("新增的数据:{}" + dataMap.toString()); - } + }*/ + + List> list = new ArrayList>(); if (operateLog.needDefaultCompare()) { - //比较新数据与数据库原数据 - List> list = defaultDealUpdate(object, oldMap, operateLog.tableId(),operateLog.codeName()); + String id = (String) oldMap.get(operateLog.tableId()); + if(StringUtils.isEmpty(id)){ + list = defaultDealAdd(object, operateLog.tableId(),operateLog.codeName()); + }else{ + list = defaultDealUpdate(object, oldMap, operateLog.tableId(),operateLog.codeName()); + } for (Map dataMap : list) { changeRecord.setChangeField(String.valueOf(dataMap.get("filedName"))); changeRecord.setBeforeChange(String.valueOf(dataMap.get("oldValue"))); @@ -112,6 +122,47 @@ public class ChangeRecordAspect { } + private List> defaultDealAdd(Object newObject, String tableId, String codeName) { + try { + List> list = new ArrayList<>(); + Map newMap = (Map) objectToMap(newObject); + Object finalNewObject = newObject; + newMap.forEach((k, v) -> { + Object newResult = newMap.get(k); + v = v==null?"":v; + newResult = newResult==null?"":newResult; + if(!StringUtil.isEmpty(String.valueOf(newResult))){ + Field field = ReflectionUtils.getAccessibleField(finalNewObject, k); + DataName dataName = field.getAnnotation(DataName.class); + if (null != dataName && StringUtils.isNotEmpty(dataName.name())) { + //翻译表达式 0=男,1=女 + String readConverterExp = dataName.readConverterExp(); + Map result = new HashMap(); + result.put("filedName", dataName.name()); + result.put(tableId, newMap.get(tableId)); + result.put(codeName, newMap.get(codeName)); + if (StringUtils.isNotEmpty(dataName.readConverterExp())) { + String newValue = convertByExp( + String.valueOf(newResult), dataName.readConverterExp(), ","); + result.put("oldValue", ""); + result.put("newValue", newValue); + } else { + result.put("oldValue", ""); + result.put("newValue", newResult); + } + list.add(result); + } + } + }); + log.info("比较的数据哈:{}" + list.toString()); + return list; + } catch (Exception e) { + log.error("比较异常", e); + e.printStackTrace(); + throw new RuntimeException("比较异常", e); + } + } + private List> defaultDealUpdate(Object newObject, Map oldMap, String tableId, String codeName) { try { List> list = new ArrayList<>(); @@ -119,7 +170,9 @@ public class ChangeRecordAspect { Object finalNewObject = newObject; oldMap.forEach((k, v) -> { Object newResult = newMap.get(k); - if (null != v && !v.equals(newResult)) { + v = v==null?"":v; + newResult = newResult==null?"":newResult; + if (!v.equals(newResult)) { Field field = ReflectionUtils.getAccessibleField(finalNewObject, k); DataName dataName = field.getAnnotation(DataName.class); if (null != dataName && StringUtils.isNotEmpty(dataName.name())) { diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/changeRecord/DefaultContentParse.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/changeRecord/DefaultContentParse.java index 669b710..afb227a 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/changeRecord/DefaultContentParse.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/changeRecord/DefaultContentParse.java @@ -4,7 +4,7 @@ import org.aspectj.lang.JoinPoint; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; -import org.springframework.util.Assert; + import java.lang.reflect.Method; @Component @@ -17,7 +17,10 @@ public class DefaultContentParse implements ContentParser { public Object getResult(JoinPoint joinPoint, ChangeRecordLog operateLog, String tableId) throws Exception { Object info = joinPoint.getArgs()[0]; Object id = ReflectionUtils.getFieldValue(info, tableId); - Assert.notNull(id, "id不能为空"); + if(id==null){ + return info; + } + String serviceImplName = operateLog.serviceImplclass(); String serviceImplMethod = operateLog.serviceImplMethod();