| package com.zy.asrs.utils; | 
|   | 
| import com.core.common.Cools; | 
| import com.zy.asrs.entity.LocDetl; | 
| 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; | 
| import java.util.ArrayList; | 
| import java.util.List; | 
| import java.util.Objects; | 
|   | 
| /** | 
|  * 用户物料信息比对,判断是否为同一物料 | 
|  */ | 
| public class MatCompareUtils { | 
|   | 
|     private static final Logger log = LoggerFactory.getLogger(MatCompareUtils.class); | 
|   | 
|     private static final String[] sameFields = {"matnr", "batch", "outOrderNo", "luHao", "packing", "sPgNO", "proType","color"}; | 
|   | 
|   | 
|     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; | 
|     } | 
|   | 
|     /** | 
|      * 比较两个集合的类的属性,相同则抵消,最后判断两个集合是否完全抵消 | 
|      */ | 
|     public static boolean compareAndCheckCancellation(List<LocDetl> list1, List<OrderDetl> list2) { | 
|         if (list1.size() != list2.size()) { | 
|             return false; // If lists are not of the same size, cancellation is not possible | 
|         } | 
|   | 
|         List<OrderDetl> remainingList2 = new ArrayList<>(list2); | 
|   | 
|         // Compare and cancel out elements | 
|         for (LocDetl p1 : list1) { | 
|             boolean cancelled = false; | 
|             for (OrderDetl p2 : remainingList2) { | 
|                 if (Objects.equals(p1.getMatnr(),p2.getMatnr()) && Objects.equals(p1.getBatch(),p2.getBatch()) && Objects.equals(p1.getOutOrderNo(),p2.getOutOrderNo()) && | 
|                         Objects.equals(p1.getLuHao(),p2.getLuHao()) && Objects.equals(p1.getPacking(),p2.getPacking()) && Objects.equals(p1.getSPgNO(),p2.getSPgNO()) && | 
|                         Objects.equals(p1.getProType(),p2.getProType()) && Objects.equals(p1.getColor(),p2.getColor())) { | 
|                     remainingList2.remove(p2); | 
|                     cancelled = true; | 
|                     break; | 
|                 } | 
|             } | 
|             if (!cancelled) { | 
|                 return false; // If any element in list1 cannot be cancelled, return false | 
|             } | 
|         } | 
|   | 
|         return remainingList2.isEmpty(); // Return true if all elements in list1 were cancelled out | 
|     } | 
|   | 
|     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)); | 
|     } | 
|   | 
| } |