diff --git a/itc-pcitc-dependencies/itc-pcitc-dependencies-service/src/main/java/com/pictc/utils/DataLogTools.java b/itc-pcitc-dependencies/itc-pcitc-dependencies-service/src/main/java/com/pictc/utils/DataLogTools.java index 475d69a..dff5a6b 100644 --- a/itc-pcitc-dependencies/itc-pcitc-dependencies-service/src/main/java/com/pictc/utils/DataLogTools.java +++ b/itc-pcitc-dependencies/itc-pcitc-dependencies-service/src/main/java/com/pictc/utils/DataLogTools.java @@ -131,22 +131,32 @@ public class DataLogTools { public static T insert(T entity,DataOperationListener listener) { Class klazz = entity.getClass(); List logs = CollectionUtils.newArrayList(); - DataChangeLog datalog = createLog(klazz,OperationType.INSERT); LogTableInfo tabInfo = getAnnotation(klazz); initJoinValue(entity,tabInfo,null); Long idValue = tabInfo.getIdValue(entity); BaseMapper mapper = MybatisTools.getMapper(tabInfo.getEntityType()); + T old = findById(klazz, idValue); + DataChangeLog datalog = old!=null ? createLog(klazz,OperationType.UPDATE) : createLog(klazz,OperationType.INSERT); DataOperationContent content = 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); } try { - mapper.insert(BeanUtil.toBean(entity,tabInfo.getEntityType())); - saveAttrs(tabInfo,entity); 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); List joins = tabInfo.getJoins(); if(CollectionUtils.isNotEmpty(joins)) { @@ -647,7 +657,6 @@ public class DataLogTools { if(CollectionUtils.isNotEmpty(listValue)) { if(type==OperationType.INSERT || type==OperationType.DELETE) { if(type==OperationType.INSERT) { - for (Object item : listValue) { Long idValue = joinTable.getIdValue(item); DataChangeLog datalog = createLog(join.getTargetClass(),type,parent);