package com.zy.asrs.utils; import com.core.common.Cools; 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, fieldValue1, 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); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } field.setAccessible(true); // 设置为可访问 try { return field.get(object); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } }