This commit is contained in:
xd 2024-07-26 15:18:17 +08:00
parent 84b539a2bd
commit 9f059a097a
2 changed files with 64 additions and 8 deletions

View File

@ -9,6 +9,7 @@ import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysChangeRecord; import com.ruoyi.system.domain.SysChangeRecord;
import com.ruoyi.system.mapper.SysChangeRecordMapper; import com.ruoyi.system.mapper.SysChangeRecordMapper;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.After;
@ -24,7 +25,10 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; 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 @Aspect
@Component @Component
@ -89,13 +93,19 @@ public class ChangeRecordAspect {
e.printStackTrace(); e.printStackTrace();
log.error("service加载失败:", e); log.error("service加载失败:", e);
} }
if ("add".equals(operateLog.type())) { /* if ("add".equals(operateLog.type())) {
Map<String, Object> dataMap = (Map<String, Object>) objectToMap(object); Map<String, Object> dataMap = (Map<String, Object>) objectToMap(object);
log.info("新增的数据:{}" + dataMap.toString()); log.info("新增的数据:{}" + dataMap.toString());
} }*/
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
if (operateLog.needDefaultCompare()) { if (operateLog.needDefaultCompare()) {
//比较新数据与数据库原数据 String id = (String) oldMap.get(operateLog.tableId());
List<Map<String, Object>> list = defaultDealUpdate(object, oldMap, operateLog.tableId(),operateLog.codeName()); if(StringUtils.isEmpty(id)){
list = defaultDealAdd(object, operateLog.tableId(),operateLog.codeName());
}else{
list = defaultDealUpdate(object, oldMap, operateLog.tableId(),operateLog.codeName());
}
for (Map<String, Object> dataMap : list) { for (Map<String, Object> dataMap : list) {
changeRecord.setChangeField(String.valueOf(dataMap.get("filedName"))); changeRecord.setChangeField(String.valueOf(dataMap.get("filedName")));
changeRecord.setBeforeChange(String.valueOf(dataMap.get("oldValue"))); changeRecord.setBeforeChange(String.valueOf(dataMap.get("oldValue")));
@ -112,6 +122,47 @@ public class ChangeRecordAspect {
} }
private List<Map<String, Object>> defaultDealAdd(Object newObject, String tableId, String codeName) {
try {
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> newMap = (Map<String, Object>) 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<Map<String, Object>> defaultDealUpdate(Object newObject, Map<String, Object> oldMap, String tableId, String codeName) { private List<Map<String, Object>> defaultDealUpdate(Object newObject, Map<String, Object> oldMap, String tableId, String codeName) {
try { try {
List<Map<String, Object>> list = new ArrayList<>(); List<Map<String, Object>> list = new ArrayList<>();
@ -119,7 +170,9 @@ public class ChangeRecordAspect {
Object finalNewObject = newObject; Object finalNewObject = newObject;
oldMap.forEach((k, v) -> { oldMap.forEach((k, v) -> {
Object newResult = newMap.get(k); 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); Field field = ReflectionUtils.getAccessibleField(finalNewObject, k);
DataName dataName = field.getAnnotation(DataName.class); DataName dataName = field.getAnnotation(DataName.class);
if (null != dataName && StringUtils.isNotEmpty(dataName.name())) { if (null != dataName && StringUtils.isNotEmpty(dataName.name())) {

View File

@ -4,7 +4,7 @@ import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@Component @Component
@ -17,7 +17,10 @@ public class DefaultContentParse implements ContentParser {
public Object getResult(JoinPoint joinPoint, ChangeRecordLog operateLog, String tableId) throws Exception { public Object getResult(JoinPoint joinPoint, ChangeRecordLog operateLog, String tableId) throws Exception {
Object info = joinPoint.getArgs()[0]; Object info = joinPoint.getArgs()[0];
Object id = ReflectionUtils.getFieldValue(info, tableId); Object id = ReflectionUtils.getFieldValue(info, tableId);
Assert.notNull(id, "id不能为空"); if(id==null){
return info;
}
String serviceImplName = operateLog.serviceImplclass(); String serviceImplName = operateLog.serviceImplclass();
String serviceImplMethod = operateLog.serviceImplMethod(); String serviceImplMethod = operateLog.serviceImplMethod();