1、添加附件注解

2、完善附件保存
This commit is contained in:
2025-12-09 14:57:14 +08:00
parent b615df5612
commit a6aa52c318
16 changed files with 246 additions and 585 deletions

View File

@ -0,0 +1,24 @@
package com.pictc.datalog;
import java.lang.reflect.Field;
import com.pictc.utils.ClassUtils;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class LogAttrFieldInfo {
private String tableName;
private Field field;
private boolean list;
public static LogAttrFieldInfo ofField(String tableName,Field field) {
return new LogAttrFieldInfo().setField(field).setTableName(tableName).setList(ClassUtils.isList(field.getType()));
}
}

View File

@ -9,6 +9,7 @@ import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.pictc.annotations.datalog.LogAttrField;
import com.pictc.annotations.datalog.LogField;
import com.pictc.annotations.datalog.LogJoin;
import com.pictc.annotations.datalog.LogTable;
@ -34,6 +35,8 @@ public class LogTableInfo {
private List<LogFieldInfo> fields = CollectionUtils.newArrayList();
private List<LogAttrFieldInfo> attrs = CollectionUtils.newArrayList();
private Map<String,LogFieldInfo> fieldMap = CollectionUtils.newConcurrentHashMap();
private Map<String,LogFieldInfo> columnMap = CollectionUtils.newConcurrentHashMap();
@ -49,9 +52,11 @@ public class LogTableInfo {
info = MybatisTools.getTableInfo(table.source());
initId();
initFields();
initAttrs();
initJoins();
}
public String getName() {
return table.name();
}
@ -68,6 +73,17 @@ public class LogTableInfo {
idField = BeanUtils.getField(idKey,klazz);
}
private void initAttrs() {
if(isValid()) {
List<Field> _fields = BeanUtils.getFields(klazz, LogAttrField.class);
if(CollectionUtils.isNotEmpty(_fields)) {
for (Field field : _fields) {
attrs.add(LogAttrFieldInfo.ofField(table.source(),field));
}
}
}
}
private void initFields() {
if(isValid()) {
List<Field> _fields = BeanUtils.getFields(klazz, LogField.class);
@ -127,7 +143,7 @@ public class LogTableInfo {
return fieldInfo==null?null:BeanUtils.getFieldValue(fieldInfo.getField(),entity);
}
public Object getFieldValue(Object entity,Field field) {
public <T>T getFieldValue(Object entity,Field field) {
if(entity==null) return null;
return BeanUtils.getFieldValue(field,entity);
}

View File

@ -39,6 +39,7 @@ 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.Lists;
import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
@ -52,6 +53,7 @@ import com.pictc.converts.ConverUtil;
import com.pictc.datalog.DataOperationContent;
import com.pictc.datalog.DataOperationListener;
import com.pictc.datalog.DefaultDataOperationListener;
import com.pictc.datalog.LogAttrFieldInfo;
import com.pictc.datalog.LogFieldInfo;
import com.pictc.datalog.LogJoinInfo;
import com.pictc.datalog.LogTableInfo;
@ -70,6 +72,10 @@ import com.xjrsoft.module.datalog.mapper.DatalogMapper;
import com.xjrsoft.module.datalog.vo.DataChangeLogVo;
import com.xjrsoft.module.datalog.vo.OperationType;
import com.xjrsoft.module.organization.dto.UserDto;
import com.xjrsoft.module.system.client.IFileClient;
import com.xjrsoft.module.system.dto.LngFileUploadBindDto;
import com.xjrsoft.module.system.dto.UpdateLngFileUploadDto;
import com.xjrsoft.module.system.vo.LngFileUploadVo;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
@ -97,10 +103,17 @@ public class DataLogTools {
private static DatalogMapper logDbService;
private static IFileClient fileClient;
@Autowired
public void setLogDbService(DatalogMapper logDbService) {
DataLogTools.logDbService = logDbService;
}
@Autowired
public void setFileClient(IFileClient fileClient) {
DataLogTools.fileClient = fileClient;
}
public static DataChangeLog createLog(Class<?> klazz,OperationType type) {
DataChangeLog createLog = createLog(klazz, type, null);
@ -163,6 +176,7 @@ public class DataLogTools {
try {
mapper.insert(BeanUtil.toBean(entity,tabInfo.getEntityType()));
saveAttrs(tabInfo,entity);
datalog.setEntityId(idValue);
buildFields(datalog,tabInfo,entity,null);
logs.add(datalog);
@ -193,6 +207,32 @@ public class DataLogTools {
return entity;
}
private static <T> void saveAttrs(LogTableInfo tabInfo, T entity) {
saveAttrs(tabInfo, entity, false);
}
private static <T> void saveAttrs(LogTableInfo tabInfo, T entity,boolean remove) {
List<LogAttrFieldInfo> attrs = tabInfo.getAttrs();
if(CollectionUtils.isNotEmpty(attrs)) {
for (LogAttrFieldInfo item : attrs) {
LngFileUploadBindDto bindDto = new LngFileUploadBindDto();
bindDto.setTableId(tabInfo.getIdValue(entity));
bindDto.setTableName(item.getTableName());
bindDto.setColumnName(item.getField().getName());
bindDto.setRemove(remove);
if(item.isList()) {
bindDto.setFiles(tabInfo.getFieldValue(entity,item.getField()));
}else {
UpdateLngFileUploadDto vo = tabInfo.getFieldValue(entity,item.getField());
if(vo!=null) {
bindDto.setFiles(Lists.newArrayList(vo));
}
}
fileClient.bindTableData(bindDto);
}
}
}
public static <T>T update(T dto) {
return update(dto, new DefaultDataOperationListener<T>());
}
@ -215,8 +255,8 @@ public class DataLogTools {
List<DataChangeLog> logs = CollectionUtils.newArrayList();
DataChangeLog datalog = createLog(klazz,OperationType.UPDATE);
initJoinValue(dto,tabInfo,null);
mapper.updateById(tabInfo.toEntity(dto));
saveAttrs(tabInfo,dto);
datalog.setEntityId(idValue);
buildFields(datalog,tabInfo,dto,old);
logs.add(datalog);
@ -331,6 +371,7 @@ public class DataLogTools {
List<DataChangeLog> logs = CollectionUtils.newArrayList();
try {
delete(entity, tabInfo, mapper);
saveAttrs(tabInfo,entity,true);
DataChangeLog datalog = createLog(klazz,OperationType.DELETE);
datalog.setEntityId(id);
buildFields(datalog,tabInfo,entity,null);
@ -640,29 +681,32 @@ public class DataLogTools {
datalog.setEntityId(idValue);
Object bean = BeanUtil.toBean(item,joinTable.getEntityType());
mapper.insert(bean);
saveAttrs(joinTable,item);
buildFields(datalog,joinTable,item,null);
logs.add(datalog);
}
}else {
for (Object item : listValue) {
Long idValue = joinTable.getIdValue(item);
Object voOjb = BeanUtil.toBean(item,joinTable.getKlazz());
DataChangeLog datalog = createLog(join.getTargetClass(),type,parent);
datalog.setEntityId(idValue);
delete(voOjb, joinTable, mapper);
buildFields(datalog,joinTable,voOjb,null);
Object pojo = BeanUtil.toBean(item,joinTable.getKlazz());
delete(pojo, joinTable, mapper);
saveAttrs(joinTable,item,true);
buildFields(datalog,joinTable,item,null);
logs.add(datalog);
}
}
}else {
for (Object item : listValue) {
Long idValue = joinTable.getIdValue(item);
Object nval = BeanUtil.toBean(item,joinTable.getKlazz());
DataChangeLog datalog = createLog(join.getTargetClass(),type,parent);
datalog.setEntityId(idValue);
Object old = findById(join.getTargetClass(), idValue);
mapper.updateById(nval);
buildFields(datalog,joinTable,nval,old);
Object pojo = BeanUtil.toBean(item,joinTable.getKlazz());
mapper.updateById(pojo);
saveAttrs(joinTable,item);
buildFields(datalog,joinTable,item,old);
logs.add(datalog);
}
}
@ -671,12 +715,13 @@ public class DataLogTools {
List oldList = getJoinList(joinTable, join, mapper, parentInfo, entity);
if(oldList!=null) {
for (Object item : oldList) {
Object voOjb = BeanUtil.toBean(item,joinTable.getKlazz());
Long idValue = joinTable.getIdValue(item);
DataChangeLog datalog = createLog(join.getTargetClass(),OperationType.DELETE,parent);
datalog.setEntityId(idValue);
delete(voOjb, joinTable, mapper);
buildFields(datalog,joinTable,voOjb,null);
Object pojo = BeanUtil.toBean(item,joinTable.getKlazz());
delete(pojo, joinTable, mapper);
saveAttrs(joinTable,item,true);
buildFields(datalog,joinTable,item,null);
logs.add(datalog);
}
}
@ -688,29 +733,33 @@ public class DataLogTools {
DataChangeLog datalog = createLog(join.getTargetClass(),type,parent);
logs.add(datalog);
datalog.setEntityId(idValue);
Object pojo = BeanUtil.toBean(val,joinTable.getKlazz());
if(type==OperationType.INSERT || type==OperationType.DELETE) {
if(type==OperationType.INSERT) {
mapper.insert(val);
mapper.insert(pojo);
saveAttrs(joinTable,val);
}else {
delete(val, joinTable, mapper);
delete(pojo, joinTable, mapper);
saveAttrs(joinTable,val,true);
}
buildFields(datalog,joinTable, val, null);
}else {
Object old = mapper.selectById(idValue);
mapper.updateById(pojo);
saveAttrs(joinTable,val,true);
buildFields(datalog,joinTable,val,old);
Object bean = BeanUtil.toBean(val,joinTable.getEntityType());
mapper.updateById(bean);
}
} else if(type==OperationType.UPDATE || type==OperationType.DELETE){
//进行修改或者删除时,级联对象为空时,需要删除旧数据
Object old = getJoinObj(joinTable, join, mapper, parentInfo, entity);
if(old!=null) {
Object voOjb = BeanUtil.toBean(old,joinTable.getKlazz());
Object dto = BeanUtil.toBean(old,joinTable.getKlazz());
Long idValue = joinTable.getIdValue(old);
DataChangeLog datalog = createLog(join.getTargetClass(),OperationType.DELETE,parent);
datalog.setEntityId(idValue);
delete(voOjb, joinTable, mapper);
buildFields(datalog,joinTable,voOjb,null);
delete(dto, joinTable, mapper);
saveAttrs(joinTable,dto,true);
buildFields(datalog,joinTable,dto,null);
}
}
}
@ -1185,11 +1234,6 @@ public class DataLogTools {
}
public static void main(String[] args) {
long id = IdWorker.getId();
System.out.println(""+id);
System.out.println((""+id).length());
}
}