最近接到了一个业务是,审计客户和合同的字段变化,要明细到使用系统的人员能看懂(大概就是我们存入数据库是1 2什么的进行翻译)
返回的信息大概就是:
客户A的 客户状态 从 客户状态A 修改成了 客户状态B ,客户性别 从 客户性别A 变成了客户性别B。
我实现的思路大概就是:
1、获取到客户对象的前后变化
2、通过枚举Enum或者数据字典来翻译 客户对象的 字段名称,对应字段名称下的类型也进行翻译
我这里使用的是枚举
一、操作准备:
客户对象类:
@Data
@RequiredArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;//姓名
private String sex;//性别
private String address;//用户地址
private String status;//用户状态
}
客户所需的字段枚举:
public enum clientFieldEnum {
CLIENT_TYPE("status","用户状态"),
CLIENT_LEVEL("level","用户等级"),
CLIENT_SEX("sex","用户性别"),
CLIENT_ADDRESS("address","用户地址");
private final String fieldName;
private final String fieldRemark;
clientFieldEnum(String fieldName, String fieldRemark) {
this.fieldName = fieldName;
this.fieldRemark = fieldRemark;
}
public String getFieldName() {
return fieldName;
}
public String getFieldRemark() {
return fieldRemark;
}
public static String getRemarkBybussisName(String fieldName) {
for (clientFieldEnum bussis : clientFieldEnum.values()) {
if (bussis.getFieldName().equals(fieldName)) {
return bussis.getFieldRemark();
}
}
// 如果没有匹配的 code,返回默认值或抛出异常
return "未知字段";
}
}
客户的状态枚举:
public enum clientStatusEnum {
NEWCLIENT("1","新进客户"),
MIDCLIENT("2","跟进客户");
private final String type;
private final Object enumObject;
clientStatusEnum(String type, Object enumObject) {
this.type = type;
this.enumObject = enumObject;
}
public String getType() {
return type;
}
public Object getEnumObject() {
return enumObject;
}
// 根据输入的 code 返回对应的描述
public static Object getObjectByStatus(String type) {
for (clientStatusEnum client : clientStatusEnum.values()) {
if (client.getType().equals(type)) {
return client.getEnumObject();
}
}
// 如果没有匹配的 code,返回默认值或抛出异常
return "未知客户";
}
}
客户的性别枚举:
public enum clientSexEnum {
MAN("1","男"),
WOMAN("2","女");
private final String sexCode;
private final String sexName;
clientSexEnum(String sexCode, String sexName) {
this.sexCode = sexCode;
this.sexName = sexName;
}
public String getSexCode() {
return sexCode;
}
public String getSexName() {
return sexName;
}
public static Object getObjectBySex(String sexCode) {
for (clientSexEnum sex : clientSexEnum.values()) {
if (sex.getSexCode().equals(sexCode)) {
return sex.getSexName();
}
}
// 如果没有匹配的 code,返回默认值或抛出异常
return "未知性别";
}
客户的业务实际操作枚举:
public enum tranformClientEnum {
KEY1("status"){
@Override
public Map<String,Object> execute(Object oldValue,Object newValue) {
Map<String,Object> object = new HashMap();
object.put("fieldName","用户状态");
object.put("oldValue", clientStatusEnum.getObjectByStatus((String) oldValue));
object.put("newValue", clientStatusEnum.getObjectByStatus((String) newValue));
return object;
}
},
KEY2("sex"){
@Override
public Map<String,Object> execute(Object oldValue,Object newValue) {
Map<String,Object> object = new HashMap();
object.put("fieldName","用户性别");
object.put("oldValue", clientSexEnum.getObjectBySex((String) oldValue));
object.put("newValue", clientSexEnum.getObjectBySex((String) newValue));
return object;
}
};
private final String key;
tranformClientEnum(String key) {
this.key = key;
}
public String getKey() {
return key;
}
// 抽象方法,由每个枚举实例实现
public abstract Map<String,Object> execute(Object oldValue,Object newValue);
// 根据 code 获取对应的 ActionType 并执行方法
public static Map<String,Object> executeByCode(String key,Object oldValue,Object newValue) {
//如何key里面存在则进行枚举实现
for (tranformClientEnum action : tranformClientEnum.values()) {
if (action.getKey().equals(key)) {
Map<String, Object> execute = action.execute(oldValue, newValue);
return execute;
}
}
//如果key里面没有则直接输出
Map<String,Object> map = new HashMap<>();
map.put("fieldName",clientFieldEnum.getRemarkBybussisName(key));
map.put("oldValue", oldValue);
map.put("newValue", newValue);
return map;
}
二、测试代码:
@SpringBootTest
public class diffUtilsTest {
@DisplayName("diffUtils测试")
@Test
public void auditChanges() {
User oldEntity = new User(1L,"小明", "1", "广州","1");
User newEntity = new User(1L,"小亮", "2", "深圳","4");
// 获取对象的属性
Map<String, Object> oldMap = BeanUtil.beanToMap(oldEntity);
Map<String, Object> newMap = BeanUtil.beanToMap(newEntity);
System.out.println("-----对象User字段的变化记录------");
oldMap.forEach((key, oldValue) -> {
Object newValue = newMap.get(key);
if (newValue != null && !oldValue.equals(newValue)) {
Map<String, Object> stringObjectMap = tranformClientEnum.executeByCode(key, oldValue, newValue);
System.out.println("字段名称:"+stringObjectMap.get("fieldName")+" 由:"+stringObjectMap.get("oldValue")+" 变为:"+stringObjectMap.get("newValue"));
}
});
}
}
三、运行结果:
ps:里面使用到的BeanUtil是导入了hutool工具包,没有引入的同学可以看看我的上一篇文章
完结撒花~~❀
你们有没有更好的方法做呢?
标签:return,String,Object,newValue,字段,oldValue,JAVA,Enums,public From: https://blog.csdn.net/TinpeaV/article/details/141196323