实体字段对比(记录修改日志)

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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章