package com.zy.asrs.utils;
|
|
import com.core.common.Cools;
|
import com.zy.asrs.entity.OrderDetl;
|
import com.zy.common.model.LocDto;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.lang.reflect.Field;
|
|
/**
|
* 用户物料信息比对,判断是否为同一物料
|
*/
|
public class MatCompareUtils {
|
|
private static final Logger log = LoggerFactory.getLogger(MatCompareUtils.class);
|
|
private static final String[] sameFields = {"matnr", "batch", "outOrderNo", "luHao", "packing", "sPgNO", "proType"};
|
|
|
public static boolean compare(Object object1, Object object2) {
|
if (object1 == null || object2 == null) {
|
return false;
|
}
|
for (String fieldName : sameFields) {
|
Object fieldValue1 = getFieldValue(object1, fieldName);
|
Object fieldValue2 = getFieldValue(object2, fieldName);
|
if (!isSame(fieldValue1, fieldValue2)) {
|
log.info("字段{}信息不一致,{}_{}-{}_{}", fieldName, object1.getClass(), fieldValue1, object2.getClass(), fieldValue2);
|
return false;
|
}
|
}
|
return true;
|
}
|
|
private static boolean isSame(Object fieldValue1, Object fieldValue2) {
|
if (Cools.isEmpty(fieldValue1)) {
|
fieldValue1 = "";
|
}
|
if (Cools.isEmpty(fieldValue2)) {
|
fieldValue2 = "";
|
}
|
return fieldValue1.equals(fieldValue2);
|
}
|
|
private static Object getFieldValue(Object object, String fieldName) {
|
Class<?> aClass = object.getClass();
|
Field field = null;
|
try {
|
field = aClass.getDeclaredField(fieldName);
|
// 设置为可访问
|
field.setAccessible(true);
|
return field.get(object);
|
} catch (NoSuchFieldException | IllegalAccessException e) {
|
log.error("字段{}缺失,对象{}", fieldName, object);
|
return null;
|
}
|
}
|
|
public static void main(String[] args) {
|
LocDto locDto = new LocDto();
|
locDto.setMatnr("123");
|
OrderDetl orderDetl = new OrderDetl();
|
orderDetl.setMatnr("123");
|
System.out.println(compare(locDto, orderDetl));
|
}
|
|
}
|