--修改新增时有id的时候,进行修改操作

This commit is contained in:
2026-01-14 17:43:05 +08:00
parent ad9778e473
commit 4953cfa283

View File

@ -131,22 +131,32 @@ public class DataLogTools {
public static <T>T insert(T entity,DataOperationListener<T> listener) { public static <T>T insert(T entity,DataOperationListener<T> listener) {
Class<? extends Object> klazz = entity.getClass(); Class<? extends Object> klazz = entity.getClass();
List<DataChangeLog> logs = CollectionUtils.newArrayList(); List<DataChangeLog> logs = CollectionUtils.newArrayList();
DataChangeLog datalog = createLog(klazz,OperationType.INSERT);
LogTableInfo tabInfo = getAnnotation(klazz); LogTableInfo tabInfo = getAnnotation(klazz);
initJoinValue(entity,tabInfo,null); initJoinValue(entity,tabInfo,null);
Long idValue = tabInfo.getIdValue(entity); Long idValue = tabInfo.getIdValue(entity);
BaseMapper mapper = MybatisTools.getMapper(tabInfo.getEntityType()); BaseMapper mapper = MybatisTools.getMapper(tabInfo.getEntityType());
T old = findById(klazz, idValue);
DataChangeLog datalog = old!=null ? createLog(klazz,OperationType.UPDATE) : createLog(klazz,OperationType.INSERT);
DataOperationContent<T> content = null; DataOperationContent<T> content = null;
if(listener!=null) { if(listener!=null) {
content = DataOperationContent.of(tabInfo,OperationType.INSERT,entity,null); if(old!=null) {
content = DataOperationContent.of(tabInfo,OperationType.UPDATE,entity,old);
}else {
content = DataOperationContent.of(tabInfo,OperationType.INSERT,entity,null);
}
listener.before(content); listener.before(content);
} }
try { try {
mapper.insert(BeanUtil.toBean(entity,tabInfo.getEntityType()));
saveAttrs(tabInfo,entity);
datalog.setEntityId(idValue); datalog.setEntityId(idValue);
buildFields(datalog,tabInfo,entity,null); if(old!=null) {
buildFields(datalog,tabInfo,entity,old);
mapper.updateById(tabInfo.toEntity(entity));
}else {
buildFields(datalog,tabInfo,entity,null);
mapper.insert(tabInfo.toEntity(entity));
}
saveAttrs(tabInfo,entity);
logs.add(datalog); logs.add(datalog);
List<LogJoinInfo> joins = tabInfo.getJoins(); List<LogJoinInfo> joins = tabInfo.getJoins();
if(CollectionUtils.isNotEmpty(joins)) { if(CollectionUtils.isNotEmpty(joins)) {
@ -647,7 +657,6 @@ public class DataLogTools {
if(CollectionUtils.isNotEmpty(listValue)) { if(CollectionUtils.isNotEmpty(listValue)) {
if(type==OperationType.INSERT || type==OperationType.DELETE) { if(type==OperationType.INSERT || type==OperationType.DELETE) {
if(type==OperationType.INSERT) { if(type==OperationType.INSERT) {
for (Object item : listValue) { for (Object item : listValue) {
Long idValue = joinTable.getIdValue(item); Long idValue = joinTable.getIdValue(item);
DataChangeLog datalog = createLog(join.getTargetClass(),type,parent); DataChangeLog datalog = createLog(join.getTargetClass(),type,parent);