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.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<String, Object> dataMap = (Map<String, Object>) objectToMap(object);
log.info("新增的数据:{}" + dataMap.toString());
}
}*/
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
if (operateLog.needDefaultCompare()) {
//比较新数据与数据库原数据
List<Map<String, Object>> 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<String, Object> 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<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) {
try {
List<Map<String, Object>> 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())) {

View File

@ -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();