自动化立体仓库 - WMS系统
#
zc
2024-10-28 d69e5421302ace9180c68f9ca968c1ba0d2115df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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));
    }
 
}