---数据变更日志查询修复
This commit is contained in:
@ -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_time(Date 类型,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){
|
||||
@ -1162,6 +1180,10 @@ public class DataLogTools {
|
||||
}
|
||||
}
|
||||
|
||||
private static String parseSql(String sql,String tableName) {
|
||||
return sql.replaceAll(SQL_PLACEHOLDER,tableName);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
long id = IdWorker.getId();
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
package com.xjrsoft.module.datalog.mapper;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.ibatis.annotations.*;
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.pictc.common.mybatis.JsonTypeHandler;
|
||||
import com.xjrsoft.module.datalog.entity.DataChangeLog;
|
||||
|
||||
/**
|
||||
@ -20,8 +22,8 @@ public interface DatalogMapper extends BaseMapper<DataChangeLog> {
|
||||
" id, flow_id, pid, entity_class_name, bus_name, \r\n" +
|
||||
" entity_id, field_changes, operation_type, operator_id, \r\n" +
|
||||
" operator_name, operation_time, operation_ip\r\n" +
|
||||
") VALUES (#{data.id},#{data.flowId},#{data.pid},#{data.entityClassName},#{data.busName},#{data.entityId},"
|
||||
+ "#{data.fieldChanges},#{data.operationType},#{data.operatorId},#{data.operatorName},#{data.operationTime},#{data.operationIp});";
|
||||
") VALUES (#{id},#{flowId},#{pid},#{entityClassName},#{busName},#{entityId},"
|
||||
+ "#{fieldChanges},#{operationType},#{operatorId},#{operatorName},#{operationTime},#{operationIp});";
|
||||
|
||||
public static final String SQL_CREATE_TABLE =
|
||||
"CREATE TABLE \"data_log_sit\".${TBL_NAME} (\r\n" +
|
||||
@ -63,9 +65,21 @@ public interface DatalogMapper extends BaseMapper<DataChangeLog> {
|
||||
@Select("select tbl_name FROM \"data_log_sit\".log_table_info")
|
||||
List<String> getTableList();
|
||||
|
||||
|
||||
// String SQL_INSERT = "INSERT INTO \"data_log_sit\".${TBL_NAME} (\r\n" +
|
||||
// " id, flow_id, pid, entity_class_name, bus_name, \r\n" +
|
||||
// " entity_id, field_changes, operation_type, operator_id, \r\n" +
|
||||
// " operator_name, operation_time, operation_ip\r\n" +
|
||||
// ") VALUES (#{id},#{flowId},#{pid},#{entityClassName},#{busName},#{entityId},"
|
||||
// + "#{fieldChanges},#{operationType},#{operatorId},#{operatorName},#{operationTime},#{operationIp});";
|
||||
|
||||
@DS("datalog")
|
||||
@Insert(SQL_INSERT)
|
||||
void insertDataLog(@Param("TBL_NAME") String tableName,@Param("data") DataChangeLog data);
|
||||
void insertDataLog(@Param("TBL_NAME") String tableName
|
||||
,@Param("id") String id,@Param("flowId") String flowId,@Param("pid") String pid
|
||||
,@Param("entityClassName") String entityClassName,@Param("busName") String busName,@Param("entityId") Long entityId
|
||||
,@Param("fieldChanges") String fieldChanges,@Param("operationType") String operationType,@Param("operatorId") Long operatorId
|
||||
,@Param("operatorName") String operatorName,@Param("operationTime") LocalDateTime operationTime,@Param("operationIp") String operationIp);
|
||||
|
||||
@DS("datalog")
|
||||
@Update(SQL_CREATE_TABLE)
|
||||
@ -73,6 +87,14 @@ public interface DatalogMapper extends BaseMapper<DataChangeLog> {
|
||||
|
||||
@DS("datalog")
|
||||
@Select("SELECT * FROM \"data_log_sit\".${TBL_NAME} WHERE entity_id = #{id} ORDER BY operation_time DESC, flow_id DESC")
|
||||
@Results({
|
||||
// 其他字段无需显式配置(MyBatis 自动下划线转驼峰),仅配置需要特殊处理的 fieldChanges
|
||||
@Result(
|
||||
column = "field_changes", // 数据库列名(必须与表中 JSON 列名一致,如 field_changes)
|
||||
property = "fieldChanges", // 实体字段名
|
||||
typeHandler = JsonTypeHandler.class // 你的 JSON 类型处理器
|
||||
)
|
||||
})
|
||||
List<DataChangeLog> selectByDataId(@Param("TBL_NAME") String tableName,@Param("id")Long id);
|
||||
|
||||
|
||||
|
||||
@ -15,6 +15,7 @@ import com.xjrsoft.common.annotation.UniqueNameGenerator;
|
||||
* 主数据服务---开发使用
|
||||
* 使用时集成到其它模块使用,不作为单独的服务
|
||||
*/
|
||||
//@EnableSaToken
|
||||
@MapperScan("com.xjrsoft.module.*.mapper")
|
||||
@SpringBootApplication
|
||||
@EnableAspectJAutoProxy(exposeProxy = true)
|
||||
|
||||
Reference in New Issue
Block a user