当前位置:网站首页>[records of different objects required by QIPA]

[records of different objects required by QIPA]

2022-06-22 21:22:00 konmor

Dream and distance

Development is coolie , Recently, there is such a demand :
Log the modified data Those fields have been modified .

ok . I can't wring my arm ,** Of . Do it and you're done .
Demand analysis : No river .

Demand fulfillment : very (bu) difficult (gan).

To record Those have been modified You need to know the field values before and after modification Compare and record . namely The original data of the database Compare with the value passed by the front end .

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.*;    
    /**
     * @param clazz    Class types 
     * @param a        Previous object 
     * @param b        Object after value 
     * @param ignores  Ignore objects that do not need to be compared 
     * @param <T>
     * @return  Different values  key: attribute ,value  Compare values before and after 
     * @throws IllegalAccessException
     */    
    public <T> Map<String, String> findNotEqualFields(Class<T> clazz, T a, T b, Set<String> ignores) throws IllegalAccessException {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
        }
        Map map = null;
        for (Field field : fields) {
            if (ignores != null && ignores.contains(field.getName())) {
                continue;
            }
            if (!Objects.equals(field.get(a), field.get(b))) {
                if (map == null) {
                    map = new HashMap();
                    map.put(" Modification time ",new Date());
                }
                map.put(field.getName(), StringUtils.join(" Before the change :", field.get(a), ",", " After modification :", field.get(b), ";"));
            }
        }
        return map;
    }

Recently, there is another demand 【 Personnel and organization information synchronization 】【 Total 】

It is necessary to compare whether the objects are inconsistent , Inconsistent use update, Consistent data is not updated .

 

Demand analysis : No river .

Demand fulfillment :*******

Don't you know how to use The form of message sending notification , Modify those data Send a notice to the downstream . Then you can modify the data directionally .

Full data comparison Add and modify Such a wonderful flower needs It is entirely a mistake of upstream design .

Implementation scheme :

1. Compare those that are different Record different data And modify it

 /**
     * @param clazz
     * @param a
     * @param b
     * @param ignores
     * @param <T>
     * @return
     * @throws IllegalAccessException
     */
    public <T> Boolean isAllEquals(Class<T> clazz, T a, T b, Set<String> ignores) throws IllegalAccessException {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
        }

        for (Field field : fields) {
            if (ignores != null && ignores.contains(field.getName())) {
                continue;
            }
            if (!Objects.equals(field.get(a), field.get(b))) {
                return false;
            }
        }
        return true;
    }

原网站

版权声明
本文为[konmor]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221911159626.html