kongdeqiang
2025-02-21 15e5365f1a59fa0538fe4fcc71ef28a934f93f06
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
package com.boying.util;
 
import org.springframework.beans.BeanUtils;
import com.boying.entity.EnterPark;
 
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author kdq
 * @version 1.0.0
 * @ClassName PlateComparator.java
 * @Description TODO
 * @createTime 2024年04月19日 15:57:00
 */
public class PlateComparator {
 
    public static List<Map<String,Object>> getList(String carNo, List<EnterPark> enterParkList) {
        List<Map<String,Object>> list = new ArrayList<>();
        for (EnterPark enterPark : enterParkList) {
            int i = compareLicensePlates(carNo, enterPark.getCarNo());
            if (i<2){
                Map<String, Object> returnMap = new HashMap<String, Object>();
                BeanInfo beanInfo = null;
                try {
                    beanInfo = Introspector.getBeanInfo(enterPark.getClass());
                    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
                    for (PropertyDescriptor pd : propertyDescriptors) {
                        String propertyName = pd.getName();
                        if (!"class".equals(propertyName)) {
                            Method readMethod = pd.getReadMethod();
                            Object result = readMethod.invoke(enterPark);
                            if (result != null) {
                                returnMap.put(propertyName, result);
                            } else {
                                returnMap.put(propertyName, "");
                            }
                        }
                    }
                } catch (IntrospectionException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                returnMap.put("count",i); // 新增次数
                list.add(returnMap);
            }
        }
        return list;
 
    }
 
    public static int compareLicensePlates(String oldLicensePlate, String newLicensePlate) {
        int length = Math.min(oldLicensePlate.length(), newLicensePlate.length());
        int count = 0;
        for (int i = 0; i < length; i++) {
            if (oldLicensePlate.charAt(i) != newLicensePlate.charAt(i)) {
               count ++;
            }
        }
        // 如果新车牌比旧车牌长,则追加新车牌多出的字符
        if (newLicensePlate.length() > oldLicensePlate.length()) {
            count++;
        }
        return count;
    }
}