You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.9 KiB

2 years ago
package com.qs.serve.common.util;
import com.qs.serve.common.framework.mybatis.join.annotations.BindEntity;
import com.qs.serve.common.model.annotation.BusinessDifference;
import com.qs.serve.common.model.dto.DiffFieldVal;
import com.qs.serve.modules.bms.entity.BmsSupplierContacts;
import com.qs.serve.modules.sys.entity.SysBusinessLog;
import lombok.SneakyThrows;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* @author YenHex
* @since 2023/4/25
*/
public class BusinessDifferenceUtil {
@SneakyThrows
public static <T> List<DiffFieldVal> getDifferenceList(T orgVal, T newVal){
List<DiffFieldVal> diffFieldValList = new ArrayList<>();
if(orgVal==null||newVal==null){
return diffFieldValList;
}
Class<?> clazz = null;
try {
clazz = Class.forName(orgVal.getClass().getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if(clazz==null){return diffFieldValList;}
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
for (Annotation annotation : field.getAnnotations()) {
if(annotation instanceof BusinessDifference){
BusinessDifference busDiff = (BusinessDifference) annotation;
field.setAccessible(true);
Object orgValue = field.get(orgVal);
Object newValue = field.get(newVal);
if(newValue==null){
continue;
}
if(orgValue==null){
orgValue = "";
}
if(!orgValue.equals(newValue)){
DiffFieldVal fieldVal = new DiffFieldVal();
fieldVal.setField(field.getName());
fieldVal.setNewValue(newValue.toString());
fieldVal.setOrgValue(orgValue.toString());
fieldVal.setComment(busDiff.value());
diffFieldValList.add(fieldVal);
}
}
}
}
return diffFieldValList;
}
public static void main(String[] args) {
BmsSupplierContacts supplierContacts = new BmsSupplierContacts();
supplierContacts.setContactsName("名称1");
supplierContacts.setContactsPost("setContactsPost");
BmsSupplierContacts supplierContacts2 = new BmsSupplierContacts();
supplierContacts2.setContactsName("名称2");
supplierContacts2.setContactsNumber("123456");
supplierContacts2.setContactsPost("setContactsPost");
List<DiffFieldVal> diffFieldValList =BusinessDifferenceUtil.getDifferenceList(supplierContacts,supplierContacts2);
for (DiffFieldVal fieldVal : diffFieldValList) {
System.out.println(JsonUtil.objectToJson(fieldVal));
}
}
}