---数据变更日志查询修复

This commit is contained in:
2025-12-08 15:45:58 +08:00
parent 0669ea6d4f
commit 08243b2974
3 changed files with 85 additions and 40 deletions

View File

@ -30,11 +30,15 @@ import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
@ -87,6 +91,10 @@ public class DataLogTools {
public static final Set<String> excludeFields = SetUtils.of("id","tenantId","dataVersion","createUserId","createDate","modifyUserId","modifyDate","modifyDate","deleteMark");
public static final String SQL_LIST = "SELECT * FROM ${TBL_NAME} WHERE entity_id = ? ORDER BY operation_time DESC, flow_id DESC";
public static final String SQL_PLACEHOLDER = "\\$\\{TBL_NAME\\}";
private static DatalogMapper logDbService;
@Autowired
@ -569,7 +577,7 @@ public class DataLogTools {
tabInfo.setIdValue(entity,idValue);
}
classes.add(tabInfo.getEntityType());
if(!CollectionUtils.isNotEmpty(joins)) {
if(CollectionUtils.isNotEmpty(joins)) {
for (LogJoinInfo join : joins) {
LogJoinColumn[] columns = join.getJoin().columns();
LogTableInfo joinTable = join.getTableInfo();
@ -891,7 +899,18 @@ public class DataLogTools {
// executeBatch(sql, batchParams);
for (DataChangeLog item : logs) {
if(item.hasFieldChanges()) {
logDbService.insertDataLog(tableName,item);
logDbService.insertDataLog(tableName,item.getId(),
item.getFlowId(), // 2. flow_id
item.getPid(), // 3. pid
item.getEntityClassName(), // 4. entity_class_name
item.getBusName(), // 5. bus_name
item.getEntityId(), // 6. entity_id
JSON.toJSONString(item.getFieldChanges()), // 7. field_changes可传 JSON 字符串)
item.getOperationType().name(), // 8. operation_type如 "INSERT/UPDATE/DELETE"
item.getOperatorId(), // 9. operator_id
item.getOperatorName(), // 10. operator_name
item.getOperationTime(), // 11. operation_timeDate 类型MyBatis 自动转数据库时间类型)
item.getOperationIp());
}
}
}
@ -955,41 +974,40 @@ public class DataLogTools {
if(initTable(tableName)) {
return null;
}
List<DataChangeLog> logs = logDbService.selectByDataId(tableName,dataId);
return toVo(logs);
// String sql = parseSql(SQL_LIST, tableName);
// JdbcContext context = null;
// try {
// context = new JdbcContext(true,true);
// context.init(sql);
// context.setParams(ListUtils.ofArray(JdbcParam.ofLong(dataId)));
// ResultSet set = context.executeQuery();
// Map<String, String> names = ResultSetUtils.getMapColumns(set);
// while (set.next()) {
// DataChangeLog log = new DataChangeLog();
// log.setId(set.getString("id"));
// log.setFlowId(set.getString("flow_id"));
// log.setPid(set.getString("pid"));
// log.setEntityClassName(set.getString("entity_class_name"));
// log.setBusName(set.getString("bus_name"));
// log.setEntityId(set.getLong("entity_id"));
// log.setOperationType(OperationType.valueOf(set.getString("operation_type")));
// log.setOperationIp(set.getString("operation_ip"));
// log.setOperatorId(set.getLong("operator_id"));
// log.setOperatorName(set.getString("operator_name"));
// log.setOperationTime(ResultSetUtils.getObject(set,"operation_time",LocalDateTime.class));
// String json = set.getString("field_changes");
// if(StringUtils.isNotEmpty(json)) {
// log.setFieldChanges(JSON.parseArray(json).toJavaList(FieldChange.class));
// }
// result.add(log);
// }
// return toVo(result);
// } catch (SQLException e) {
// throw context.createException(e);
// } finally {
// context.end();
// }
String sql = parseSql(SQL_LIST, tableName);
JdbcContext context = null;
try {
List<DataChangeLog> result = CollectionUtils.newArrayList();
context = new JdbcContext(true,true);
context.init(sql);
context.setParams(ListUtils.ofArray(JdbcParam.ofLong(dataId)));
ResultSet set = context.executeQuery();
Map<String, String> names = ResultSetUtils.getMapColumns(set);
while (set.next()) {
DataChangeLog log = new DataChangeLog();
log.setId(set.getString("id"));
log.setFlowId(set.getString("flow_id"));
log.setPid(set.getString("pid"));
log.setEntityClassName(set.getString("entity_class_name"));
log.setBusName(set.getString("bus_name"));
log.setEntityId(set.getLong("entity_id"));
log.setOperationType(OperationType.valueOf(set.getString("operation_type")));
log.setOperationIp(set.getString("operation_ip"));
log.setOperatorId(set.getLong("operator_id"));
log.setOperatorName(set.getString("operator_name"));
log.setOperationTime(ResultSetUtils.getObject(set,"operation_time",LocalDateTime.class));
String json = set.getString("field_changes");
if(StringUtils.isNotEmpty(json)) {
log.setFieldChanges(JSON.parseArray(json).toJavaList(FieldChange.class));
}
result.add(log);
}
return toVo(result);
} catch (SQLException e) {
throw context.createException(e);
} finally {
context.end();
}
}
private static List<DataChangeLogVo> toVo(List<DataChangeLog> list){
@ -1161,6 +1179,10 @@ public class DataLogTools {
context.end();
}
}
private static String parseSql(String sql,String tableName) {
return sql.replaceAll(SQL_PLACEHOLDER,tableName);
}
public static void main(String[] args) {