實體字段對比(記錄修改日誌)

1,全字段回顯到頁面進行修改的情況

用戶提交的數據與通過編碼查詢數據庫返回的數據實體進行對比

    /**
     * 對比兩個bean的差異屬性
     * @param oldBean
     * @param newBean
     * @param clazz
     * @param <T>
     * @return
     */
    public static  <T> String contrastObj(Object oldBean, Object newBean,Class<T> clazz) {
        String str = "";
        T pojo1 = (T) oldBean;
        T pojo2 = (T) newBean;
        try {
            Field[] fields = clazz.getDeclaredFields();
            int i = 1;
            for (Field field : fields) {
                if ("serialVersionUID".equals(field.getName())) {
                    continue;
                }
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                Method getMethod = pd.getReadMethod();
                Object o1 = getMethod.invoke(pojo1);
                Object o2 = getMethod.invoke(pojo2);
                //如果新值爲空,不修改舊值
                if (o2 == null) {
                    continue;
                }
                if (!o2.equals(o1)) {
                    if (i != 1) {
                        str += ";";
                    }
                    String customFieldValue = "";
                    // 要顯示的字段名
                    String fieldName = "";
                    if (customFieldValue != "") {
                        fieldName = customFieldValue;
                    } else {
                        fieldName = field.getName();
                    }
                    str += fieldName + ":舊值:" + o1 + ",新值:" + o2;
                    i++;
                }
            }
        } catch (Exception e) {
            log.error("字段對比失敗:{}",e.toString());
        }
        return str;
    }

2,部分不可修改字段回顯到頁面進行修改的情況

比如編碼,名稱不能修改,其他字段不回顯到頁面,要修改才填值,只需要篩選有值並且不是回顯字段的就是修改字段

在對象實體屬性上添加一個自定義註解,比如@SapField,有這個註解的就是需要記錄修改內容的

@Data
public class SapProductDTO implements Serializable {

    private static final long serialVersionUID = -3291179406702449261L;

    /**
     * 不可編輯的
     */
    @JSONField(name = "ITEM1")
    private String ITEM1;
    /**
     * 可編輯的字段
     */
    @SapFiled
    @JSONField(name = "ITEM2")
    private String ITEM2;
    
    。。。。
}
/**
     * 篩選sap修改字段,排除回顯在頁面的不可編輯字段
     * @param sapProductDTO 實體對象
     * @return
     */
    public static String sapDiffField(SapProductDTO sapProductDTO) {
        String diffStr = "";
        Field[] fields = sapProductDTO.getClass().getDeclaredFields();
        int i = 1;
        try {
            for (Field field : fields) {
                if ("serialVersionUID".equals(field.getName())) {
                    continue;
                }
                //只比較sap獨有字段--有sap標籤的字段
                SapFiled fieldAnnotation = field.getDeclaredAnnotation(SapFiled.class);
                if(fieldAnnotation == null){
                    continue;
                }
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), sapProductDTO.getClass());
                Method getMethod = pd.getReadMethod();
                String o1 = (String) getMethod.invoke(sapProductDTO);
                if(StringUtil.isNotEmpty(o1)){
                    if (i != 1) {
                        diffStr += ";";
                    }
                    diffStr += field.getName() + ":新值:" + o1;
                    i++;
                }
            }
        } catch (Exception e) {
            log.error("sap字段對比失敗:{}",e.toString());
        }
        return diffStr;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章