---添加数据日志查询功能
This commit is contained in:
@ -48,10 +48,14 @@ import com.pictc.converts.ConverUtil;
|
||||
import com.pictc.datalog.LogFieldInfo;
|
||||
import com.pictc.datalog.LogJoinInfo;
|
||||
import com.pictc.datalog.LogTableInfo;
|
||||
import com.pictc.enums.BusinessCode;
|
||||
import com.pictc.enums.ExceptionCommonCode;
|
||||
import com.pictc.exceptions.OrmException;
|
||||
import com.pictc.jdbc.JdbcContext;
|
||||
import com.pictc.jdbc.ResultSetUtils;
|
||||
import com.pictc.jdbc.model.JdbcParam;
|
||||
import com.xjrsoft.common.enums.YesOrNoEnum;
|
||||
import com.xjrsoft.common.exception.BusinessException;
|
||||
import com.xjrsoft.common.utils.SecureUtil;
|
||||
import com.xjrsoft.module.datalog.entity.DataChangeLog;
|
||||
import com.xjrsoft.module.datalog.entity.json.FieldChange;
|
||||
@ -124,7 +128,7 @@ public class DataLogTools {
|
||||
|
||||
public static final String SQL_LIST = "SELECT * FROM ${TBL_NAME} WHERE entity_id = ? ORDER BY operation_time DESC, flow_id DESC";
|
||||
|
||||
public static final Set<String> excludeFields = SetUtils.of("tenantId","dataVersion","tenantId","tenantId","tenantId");
|
||||
public static final Set<String> excludeFields = SetUtils.of("tenantId","dataVersion","createUserId","createDate","modifyUserId","modifyDate","modifyDate","deleteMark");
|
||||
|
||||
|
||||
public static DataChangeLog createLog(Class<?> klazz,OperationType type) {
|
||||
@ -250,6 +254,13 @@ public class DataLogTools {
|
||||
}
|
||||
|
||||
|
||||
public static boolean deleteByIds(Class<?> klazz, @Valid List<Long> ids) {
|
||||
for (Long id : ids) {
|
||||
deleteById(klazz, id);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static <T>T deleteById(Class<T> klazz,Long id) {
|
||||
LogTableInfo tabInfo = getAnnotation(klazz);
|
||||
BaseMapper mapper = MybatisTools.getMapper(tabInfo.getEntityType());
|
||||
@ -274,13 +285,68 @@ public class DataLogTools {
|
||||
return entity;
|
||||
}
|
||||
|
||||
public static boolean deleteByIds(Class<?> klazz, @Valid List<Long> ids) {
|
||||
private static void delete(Object entity,LogTableInfo tableinfo,BaseMapper mapper){
|
||||
Long idValue = tableinfo.getIdValue(entity);
|
||||
if(idValue!=null && idValue > 0) {
|
||||
mapper.deleteById(idValue);
|
||||
}else {
|
||||
throw new BusinessException(BusinessCode.ofArgs(ExceptionCommonCode.DATA_DEL_ID_IS_NULL,tableinfo.getTableName()));
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean enable(Class<?> klazz, @Valid List<Long> ids) {
|
||||
for (Long id : ids) {
|
||||
deleteById(klazz, id);
|
||||
enableById(klazz, id);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static <T>T enableById(Class<T> klazz,Long id) {
|
||||
LogTableInfo tabInfo = getAnnotation(klazz);
|
||||
BaseMapper mapper = MybatisTools.getMapper(tabInfo.getEntityType());
|
||||
Object entity = findNativeById(klazz, id);
|
||||
if(entity==null) return null;
|
||||
T old = BeanUtils.newInstance(klazz);
|
||||
BeanUtil.copyProperties(entity,old,true);
|
||||
List<DataChangeLog> logs = CollectionUtils.newArrayList();
|
||||
tabInfo.setFieldValue(entity,"valid",YesOrNoEnum.YES.getTextCode());
|
||||
mapper.updateById(entity);
|
||||
T dto = (T)tabInfo.toDto(entity);
|
||||
DataChangeLog datalog = createLog(klazz,OperationType.UPDATE);
|
||||
datalog.setEntityId(id);
|
||||
buildFields(datalog,tabInfo,dto,null);
|
||||
logs.add(datalog);
|
||||
saveLogs(tabInfo,logs);
|
||||
return dto;
|
||||
}
|
||||
|
||||
public static boolean disable(Class<?> klazz, @Valid List<Long> ids) {
|
||||
for (Long id : ids) {
|
||||
enableById(klazz, id);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static <T>T disableById(Class<T> klazz,Long id) {
|
||||
LogTableInfo tabInfo = getAnnotation(klazz);
|
||||
BaseMapper mapper = MybatisTools.getMapper(tabInfo.getEntityType());
|
||||
Object entity = findNativeById(klazz, id);
|
||||
if(entity==null) return null;
|
||||
T old = BeanUtils.newInstance(klazz);
|
||||
BeanUtil.copyProperties(entity,old,true);
|
||||
List<DataChangeLog> logs = CollectionUtils.newArrayList();
|
||||
tabInfo.setFieldValue(entity,"valid",YesOrNoEnum.NO.getTextCode());
|
||||
mapper.updateById(entity);
|
||||
T dto = (T)tabInfo.toDto(entity);
|
||||
DataChangeLog datalog = createLog(klazz,OperationType.UPDATE);
|
||||
datalog.setEntityId(id);
|
||||
buildFields(datalog,tabInfo,dto,null);
|
||||
logs.add(datalog);
|
||||
saveLogs(tabInfo,logs);
|
||||
return dto;
|
||||
}
|
||||
|
||||
|
||||
public static <T>T findById(Class<T> klazz,Serializable id){
|
||||
return findById(klazz,id,false);
|
||||
}
|
||||
@ -294,7 +360,14 @@ public class DataLogTools {
|
||||
initJoinValues(tabInfo,dto,SetUtils.of(klazz), full);
|
||||
return dto;
|
||||
}
|
||||
|
||||
|
||||
private static Object findNativeById(Class<?> klazz, Serializable id) {
|
||||
LogTableInfo tabInfo = getAnnotation(klazz);
|
||||
BaseMapper mapper = MybatisTools.getMapper(tabInfo.getEntityType());
|
||||
return mapper.selectById(id);
|
||||
}
|
||||
|
||||
public static void initJoinValues(LogTableInfo tabInfo,Object entity,Set<Class<?>> classPath,boolean full){
|
||||
List<LogJoinInfo> joins = tabInfo.getJoins();
|
||||
BaseMapper mapper = MybatisTools.getMapper(tabInfo.getEntityType());
|
||||
@ -362,18 +435,8 @@ public class DataLogTools {
|
||||
}
|
||||
|
||||
|
||||
private static void delete(Object entity,LogTableInfo tableinfo,BaseMapper mapper){
|
||||
Long idValue = tableinfo.getIdValue(entity);
|
||||
if(idValue!=null && idValue > 0) {
|
||||
// QueryChainWrapper wrapper = new QueryChainWrapper(mapper);
|
||||
// wrapper.eq(tableinfo.getIdColumn(),tableinfo.getIdValue(entity));
|
||||
// mapper.delete(wrapper);
|
||||
mapper.deleteById(idValue);
|
||||
}else {
|
||||
throw new RuntimeException("删除数据时,表【"+tableinfo.getName()+"】的主键为空,删除失败!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static <T>void initJoinValue(T entity,LogTableInfo tabInfo,Set<Class<?>> classes) {
|
||||
if(classes==null) {
|
||||
@ -881,4 +944,6 @@ public class DataLogTools {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -18,8 +18,12 @@ public interface DatalogService {
|
||||
|
||||
public <T>T deleteById(Class<T> klazz,long id);
|
||||
|
||||
public <T>List<DataChangeLog> findByEntityId(Class<T> klazz,long id);
|
||||
public <T>List<DataChangeLog> findLogsByEntityId(Class<T> klazz,long id);
|
||||
|
||||
public <T>boolean deleteByIds(Class<T> class1, @Valid List<Long> ids);
|
||||
|
||||
public <T>boolean enable(Class<T> class1, @Valid List<Long> ids);
|
||||
|
||||
public <T>boolean disable(Class<T> class1, @Valid List<Long> ids);
|
||||
|
||||
}
|
||||
|
||||
@ -52,16 +52,26 @@ public class DatalogServiceImpl implements DatalogService{
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public <T> boolean deleteByIds(Class<T> class1, @Valid List<Long> ids) {
|
||||
return DataLogTools.deleteByIds(class1, ids);
|
||||
public <T> boolean deleteByIds(Class<T> klazz, @Valid List<Long> ids) {
|
||||
return DataLogTools.deleteByIds(klazz, ids);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@Override
|
||||
public <T> List<DataChangeLog> findByEntityId(Class<T> klazz, long dataId) {
|
||||
public <T> List<DataChangeLog> findLogsByEntityId(Class<T> klazz, long dataId) {
|
||||
return DataLogTools.findLogsByEntityId(klazz, dataId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> boolean enable(Class<T> klazz, @Valid List<Long> ids) {
|
||||
return DataLogTools.enable(klazz, ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> boolean disable(Class<T> klazz, @Valid List<Long> ids) {
|
||||
return DataLogTools.disable(klazz, ids);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user