1.具体方法
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;
public class CompareUtils {
private static final Logger logger = LoggerFactory.getLogger(CompareUtils.class);
@SneakyThrows
public static Object classOfSrc(Object source, Object target, List<String> excludeList, List<String> evaluationList, Boolean requiredType) {
Class<?> srcClass = source.getClass();
Field[] fields = srcClass.getDeclaredFields();
for (Field field : fields) {
String nameKey = field.getName();
if (excludeList.contains(nameKey) || nameKey.contains("Bhqs") || nameKey.contains("Bhbl")) {
continue;
}
if (requiredType) {
Integer srcValue = getClassValue(source, nameKey) == null ? null : (Integer) getClassValue(source, nameKey);
Integer tarValue = getClassValue(target, nameKey) == null ? null : (Integer) getClassValue(target, nameKey);
Field f1 = target.getClass().getDeclaredField(nameKey + "Bhqs");
f1.setAccessible(true);
if (srcValue != null && tarValue != null) {
if (tarValue > srcValue) {
f1.set(target, 1);
} else if (tarValue < srcValue) {
f1.set(target, 2);
} else {
f1.set(target, 3);
}
}
Field f2 = target.getClass().getDeclaredField(nameKey + "Bhbl");
f2.setAccessible(true);
if(srcValue != null && tarValue != null) {
if (srcValue != 0) {
int diff = tarValue - srcValue;
System.out.println(((diff < 0) ? -diff : diff) / srcValue);
double v = (double) ((diff < 0) ? -diff : diff) / srcValue * 100;
BigDecimal bg = new BigDecimal(v);
double d1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
f2.set(target, d1);
}
}
} else {
if (evaluationList.contains(nameKey + "Bhqs")) {
Integer srcValue = getClassValue(source, nameKey) == null ? 0 : (Integer) getClassValue(source, nameKey);
Integer tarValue = getClassValue(target, nameKey) == null ? 0 : (Integer) getClassValue(target, nameKey);
Field f1 = target.getClass().getDeclaredField(nameKey + "Bhqs");
f1.setAccessible(true);
if (srcValue > tarValue) {
f1.set(target, 1);
} else if (srcValue < tarValue) {
f1.set(target, 2);
} else {
f1.set(target, 3);
}
if (evaluationList.contains(nameKey + "Bhbl")) {
Field f2 = target.getClass().getDeclaredField(nameKey + "Bhbl");
f2.setAccessible(true);
if (null != srcValue) {
int diff = tarValue - srcValue;
System.out.println(((diff < 0) ? -diff : diff) / srcValue);
f2.set(target, (double) ((diff < 0) ? -diff : diff) / srcValue);
}
}
}
}
}
return target;
}
public static Object getClassValue(Object obj, String fieldName) {
if (obj == null) {
return null;
}
try {
Class beanClass = obj.getClass();
Method[] ms = beanClass.getMethods();
for (int i = 0; i < ms.length; i++) {
if (!ms[i].getName().startsWith("get")) {
continue;
}
Object objValue = null;
try {
objValue = ms[i].invoke(obj, new Object[]{});
} catch (Exception e) {
e.printStackTrace();
logger.info("反射取值出错:" + e.toString());
continue;
}
if (objValue == null) {
continue;
}
if (ms[i].getName().toUpperCase().equals(fieldName.toUpperCase())
|| ms[i].getName().substring(3).toUpperCase().equals(fieldName.toUpperCase())) {
return objValue;
} else if (fieldName.toUpperCase().equals("SID")
&& (ms[i].getName().toUpperCase().equals("ID") || ms[i].getName().substring(3).toUpperCase()
.equals("ID"))) {
return objValue;
}
}
} catch (Exception e) {
logger.info("取方法出错!" + e.toString());
}
return null;
}
}
测试
@GetMapping(value = "/compare")
@ApiOperation("比较测试")
public ApiResponse compare(){
TestDTO t1 = new TestDTO();
t1.setId(1);
t1.setFirst(5);
t1.setSecond(6);
TestDTO t2 = new TestDTO();
t2.setId(2);
t2.setFirst(5);
t2.setSecond(10);
List<String> excludeList = new ArrayList<>();
excludeList.add("id");
List<String> evaluationList = new ArrayList<>();
evaluationList.add("firstBhqs");
evaluationList.add("firstBhbl");
TestDTO a = (TestDTO) CompareUtils.classOfSrc(t1,t2,excludeList,null,true);
TestDTO b = (TestDTO) CompareUtils.classOfSrc(t1,t2,excludeList,evaluationList,false);
return ApiResponse.success(a);
}