Commit 24b7b8f7 authored by suhuiguang's avatar suhuiguang

fix(jg): 自动填充

1.BaseService子类调用更新updateById时,自动填充rec_date rec_user_id
parent 07965522
......@@ -11,18 +11,32 @@ import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.yeejoin.amos.boot.biz.common.entity.TzsBaseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.typroject.tyboot.core.foundation.context.RequestContext;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
import static com.yeejoin.amos.boot.module.jg.biz.edit.utils.ReflectiveFieldAccessor.getAllFields;
public class BaseService<M extends BaseMapper<T>, T extends TzsBaseEntity> extends ServiceImpl<M, T> {
private static final String COLUMN_NAME_REC_DATE = "\"REC_DATE\"";
private static final String COLUMN_NAME_REC_USER_ID = "\"REC_USER_ID\"";
private static final ImmutableSet<String> AUTO_FILL_FIELDS = ImmutableSet.of(COLUMN_NAME_REC_DATE, COLUMN_NAME_REC_USER_ID);
private static final ImmutableMap<String, Supplier<Object>> FIELD_PROCESSORS = ImmutableMap.<String, Supplier<Object>>builder().
put(COLUMN_NAME_REC_DATE, Date::new).
put(COLUMN_NAME_REC_USER_ID, RequestContext::getExeUserId).build();
@Transactional(rollbackFor = Exception.class)
public boolean saveOrUpdateWithNull(T entity) {
......@@ -48,7 +62,7 @@ public class BaseService<M extends BaseMapper<T>, T extends TzsBaseEntity> exten
// 动态设置非空字段到 SET 语句
allFields.stream().filter(field -> field.getAnnotation(TableField.class) != null && field.getAnnotation(TableField.class).exist()).forEach(field -> {
String columnName = getColumnName(field); // 获取字段名(处理 @TableField 注解)
wrapper.set(columnName, getFieldValue(field, entity));
wrapper.set(columnName, getFieldValue(columnName, field, entity));
});
// 设置主键条件(确保主键在父类也能获取)
String idColumn = getColumnName(getIdField(entity.getClass())); // 获取主键字段名
......@@ -82,12 +96,21 @@ public class BaseService<M extends BaseMapper<T>, T extends TzsBaseEntity> exten
}
// 安全获取字段值
private Object getFieldValue(Field field, Object obj) {
private Object getFieldValue(String columnName, Field field, Object obj) {
try {
field.setAccessible(true);
return field.get(obj);
Object value = field.get(obj);
// 特殊字段自动填充处理
String upperColumnName = columnName.toUpperCase();
if (value == null && AUTO_FILL_FIELDS.contains(upperColumnName)) {
Supplier<Object> processor = FIELD_PROCESSORS.get(upperColumnName);
if (processor != null) {
value = processor.get();
}
}
return value;
} catch (IllegalAccessException e) {
throw new RuntimeException("获取字段值失败", e);
throw new RuntimeException(String.format("获取字段[%s]值失败", field.getName()), e);
}
}
......
......@@ -3707,7 +3707,6 @@ public class IdxBizJgRegisterInfoServiceImpl extends BaseService<IdxBizJgRegiste
this.removeDescAfterCode(useInfo);
// 使用信息
useInfo.setRecord(record);
useInfo.setRecDate(date);
useInfo.setDataSource(dataSource);
useInfo.setIsIntoManagement(Boolean.FALSE);
useInfo.setSequenceNbr(OPERATESAVE.equals(operateType) ? null : String.valueOf(equipmentInfoForm.get("USEINFO_SEQ")));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment