---数据服务操作增加操作对象生命周期
This commit is contained in:
@ -0,0 +1,75 @@
|
||||
package com.xjrsoft.module.common.db.utils;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.xjrsoft.module.common.db.service.CommonCallService;
|
||||
|
||||
/**
|
||||
* @author 张福财
|
||||
* @date 2025年10月30日 上午9:58:16
|
||||
* @Description: TODO(这里用一句话描述这个类的作用)
|
||||
*/
|
||||
@Component
|
||||
public class CommonCallUtils {
|
||||
|
||||
private static CommonCallService callService;
|
||||
|
||||
@Autowired
|
||||
public void setCallService(CommonCallService callService) {
|
||||
CommonCallUtils.callService = callService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description:
|
||||
一、新增、修改记录时,在同一个事务里执行如下过程:
|
||||
1、更新表;
|
||||
2、调用数据库函数pc_表名.f_save(表主键);
|
||||
3、函数返回非空、或者数据库异常时rollback,函数返回空时commit;
|
||||
* @param table 表名
|
||||
* @param id 表主键
|
||||
* @return String 返回类型 函数返回非空、或者数据库异常时rollback,函数返回空时commit;
|
||||
*/
|
||||
public static String saveAfter(String table,long id) {
|
||||
return callService.saveAfter(table, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description:
|
||||
二、删除记录时,在同一个事务里执行如下过程:
|
||||
1、调用数据库函数pc_表名.f_delete(表主键);
|
||||
2、函数返回非空、或者数据库异常时rollback,函数返回空时commit;
|
||||
* @return
|
||||
* @return String 返回类型
|
||||
*/
|
||||
public static String deleteBefore(String table,Long id) {
|
||||
return callService.deleteBefore(table, id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Description:
|
||||
二、停用时,在同一个事务里执行如下过程:
|
||||
1、调用数据库函数pc_表名.f_off(表主键);
|
||||
2、函数返回非空、或者数据库异常时rollback,函数返回空时commit;
|
||||
* @return
|
||||
* @return String 返回类型
|
||||
*/
|
||||
public static String disableBefore(String table,Long id) {
|
||||
return callService.disableBefore(table, id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Description:
|
||||
1、调用数据库函数pc_表名.f_on(表主键);
|
||||
2、函数返回非空、或者数据库异常时rollback,函数返回空时commit;
|
||||
* @return
|
||||
* @return String 返回类型
|
||||
*/
|
||||
public static String enableBefore(String table,Long id) {
|
||||
return callService.enableBefore(table, id);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -77,7 +77,7 @@ public class DataChangeLog implements Serializable{
|
||||
/**
|
||||
* 操作类型:INSERT-新增,UPDATE-修改,DELETE-删除
|
||||
*/
|
||||
@ApiModelProperty("操作类型:INSERT-新增,UPDATE-修改,DELETE-删除")
|
||||
@ApiModelProperty("操作类型:INSERT-新增,UPDATE-修改,DELETE-删除 ENABLE-启用 DISABLE-禁用")
|
||||
private OperationType operationType;
|
||||
|
||||
/**
|
||||
|
||||
@ -0,0 +1,80 @@
|
||||
package com.xjrsoft.module.datalog.mapper;
|
||||
|
||||
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.xjrsoft.module.datalog.entity.DataChangeLog;
|
||||
|
||||
/**
|
||||
* @author 张福财
|
||||
* @date 2025年10月30日 下午2:17:47
|
||||
* @Description: 数据日志操作
|
||||
*/
|
||||
@DS("datalog") //"data_log_sit"
|
||||
@Mapper
|
||||
public interface DatalogMapper extends BaseMapper<DataChangeLog> {
|
||||
|
||||
public static final 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 (#{data.id},#{data.flowId},#{data.pid},#{data.entityClassName},#{data.busName},#{data.entityId},"
|
||||
+ "#{data.fieldChanges},#{data.operationType},#{data.operatorId},#{data.operatorName},#{data.operationTime},#{data.operationIp});";
|
||||
|
||||
public static final String SQL_CREATE_TABLE =
|
||||
"CREATE TABLE \"data_log_sit\".${TBL_NAME} (\r\n" +
|
||||
" id VARCHAR(32) NOT NULL,\r\n" +
|
||||
" flow_id VARCHAR(32),\r\n" +
|
||||
" pid VARCHAR(32) DEFAULT '#',\r\n" +
|
||||
" entity_class_name VARCHAR(255),\r\n" +
|
||||
" bus_name VARCHAR(100),\r\n" +
|
||||
" entity_id BIGINT,\r\n" +
|
||||
" field_changes TEXT,\r\n" +
|
||||
" operation_type VARCHAR(20),\r\n" +
|
||||
" operator_id BIGINT,\r\n" +
|
||||
" operator_name VARCHAR(100),\r\n" +
|
||||
" operation_time TIMESTAMPTZ,\r\n" +
|
||||
" operation_ip VARCHAR(50),\r\n" +
|
||||
" PRIMARY KEY (id));\r\n" +
|
||||
"COMMENT ON COLUMN \"data_log_sit\".${TBL_NAME}.\"flow_id\" IS '流水id';\r\n" +
|
||||
"COMMENT ON COLUMN \"data_log_sit\".${TBL_NAME}.\"pid\" IS '父表ID,根节点#';\r\n" +
|
||||
"COMMENT ON COLUMN \"data_log_sit\".${TBL_NAME}.\"entity_class_name\" IS '实体类名称(全类名)';\r\n" +
|
||||
"COMMENT ON COLUMN \"data_log_sit\".${TBL_NAME}.\"bus_name\" IS '业务名称';\r\n" +
|
||||
"COMMENT ON COLUMN \"data_log_sit\".${TBL_NAME}.\"entity_id\" IS '操作的实体ID(主键值)';\r\n" +
|
||||
"COMMENT ON COLUMN \"data_log_sit\".${TBL_NAME}.\"field_changes\" IS '属性值记录(JSON格式存储字段变更详情)';\r\n" +
|
||||
"COMMENT ON COLUMN \"data_log_sit\".${TBL_NAME}.\"operation_type\" IS '操作类型:INSERT-新增,UPDATE-修改,DELETE-删除';\r\n" +
|
||||
"COMMENT ON COLUMN \"data_log_sit\".${TBL_NAME}.\"operator_id\" IS '操作人ID';\r\n" +
|
||||
"COMMENT ON COLUMN \"data_log_sit\".${TBL_NAME}.\"operator_name\" IS '操作人姓名';\r\n" +
|
||||
"COMMENT ON COLUMN \"data_log_sit\".${TBL_NAME}.\"operation_time\" IS '操作时间';\r\n" +
|
||||
"COMMENT ON COLUMN \"data_log_sit\".${TBL_NAME}.\"operation_ip\" IS '操作IP地址';\r\n" +
|
||||
"CREATE INDEX idx_${TBL_NAME}_enityt_id ON \"data_log_sit\".${TBL_NAME} (entity_id);";
|
||||
|
||||
@DS("datalog")
|
||||
@Select("select count(*) from \"data_log_sit\".log_table_info where tbl_name = #{tableName}")
|
||||
long getCountByTableName(@Param("tableName") String name);
|
||||
|
||||
@DS("datalog")
|
||||
@Insert("INSERT INTO \"data_log_sit\".log_table_info (tbl_name) VALUES (#{tableName});")
|
||||
void addTableInfo(@Param("tableName") String tableName);
|
||||
|
||||
@DS("datalog")
|
||||
@Select("select tbl_name FROM \"data_log_sit\".log_table_info")
|
||||
List<String> getTableList();
|
||||
|
||||
@DS("datalog")
|
||||
@Insert(SQL_INSERT)
|
||||
void insertDataLog(@Param("TBL_NAME") String tableName,@Param("data") DataChangeLog data);
|
||||
|
||||
@DS("datalog")
|
||||
@Update(SQL_CREATE_TABLE)
|
||||
void createDatalogTable(@Param("TBL_NAME") String tableName);
|
||||
|
||||
@DS("datalog")
|
||||
@Select("SELECT * FROM \"data_log_sit\".${TBL_NAME} WHERE entity_id = #{id} ORDER BY operation_time DESC, flow_id DESC")
|
||||
List<DataChangeLog> selectByDataId(@Param("TBL_NAME") String tableName,@Param("id")Long id);
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -4,6 +4,7 @@ import java.util.List;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.apache.seata.spring.annotation.GlobalTransactional;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@ -16,12 +17,14 @@ import com.xjrsoft.module.datalog.vo.DataChangeLogVo;
|
||||
public class DatalogServiceImpl implements DatalogService{
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@GlobalTransactional(rollbackFor = Exception.class) // Seata全局事务注解
|
||||
@Override
|
||||
public <T> T insert(T entity) {
|
||||
return DataLogTools.insert(entity);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@GlobalTransactional(rollbackFor = Exception.class) // Seata全局事务注解
|
||||
@Override
|
||||
public <T> List<T> insertBatch(List<T> entitys) {
|
||||
List<T> res = Lists.newArrayList();
|
||||
@ -32,6 +35,7 @@ public class DatalogServiceImpl implements DatalogService{
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@GlobalTransactional(rollbackFor = Exception.class) // Seata全局事务注解
|
||||
@Override
|
||||
public <T> boolean updateById(T entity) {
|
||||
DataLogTools.update(entity);
|
||||
@ -39,34 +43,42 @@ public class DatalogServiceImpl implements DatalogService{
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@GlobalTransactional(rollbackFor = Exception.class) // Seata全局事务注解
|
||||
@Override
|
||||
public <T> T delete(T entity) {
|
||||
return DataLogTools.delete(entity);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@GlobalTransactional(rollbackFor = Exception.class) // Seata全局事务注解
|
||||
@Override
|
||||
public <T> T deleteById(Class<T> klazz, long id) {
|
||||
return DataLogTools.deleteById(klazz, id);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@GlobalTransactional(rollbackFor = Exception.class) // Seata全局事务注解
|
||||
@Override
|
||||
public <T> boolean deleteByIds(Class<T> klazz, @Valid List<Long> ids) {
|
||||
return DataLogTools.deleteByIds(klazz, ids);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@GlobalTransactional(rollbackFor = Exception.class) // Seata全局事务注解
|
||||
@Override
|
||||
public <T> List<DataChangeLogVo> findLogsByEntityId(Class<T> klazz, long dataId) {
|
||||
return DataLogTools.findLogsByEntityId(klazz, dataId);
|
||||
}
|
||||
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@GlobalTransactional(rollbackFor = Exception.class) // Seata全局事务注解
|
||||
@Override
|
||||
public <T> boolean enable(Class<T> klazz, @Valid List<Long> ids) {
|
||||
return DataLogTools.enable(klazz, ids);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@GlobalTransactional(rollbackFor = Exception.class) // Seata全局事务注解
|
||||
@Override
|
||||
public <T> boolean disable(Class<T> klazz, @Valid List<Long> ids) {
|
||||
return DataLogTools.disable(klazz, ids);
|
||||
|
||||
Reference in New Issue
Block a user