两实体对象比较哪些字段有更改

首先添加注解
/**

  • 数据结构属性注解。

  • @author carver.gu

  • @since 1.0, Apr 11, 2010
    */
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    public @interface PropertyMsg {

    String value();
    }

在实体类标上注解属性
@PropertyMsg(“车牌号”)
private String plateNum;
//车型
@PropertyMsg(“车型”)
private CarTypeEnum carType;
//运营证号
@PropertyMsg(“运营证号”)
private String operationNum;

对象比较工具
package com.xxzyt.wlpt.transport.utils;

import com.xxzyt.open.common.sdk.enums.CarTypeEnum;
import com.xxzyt.open.member.sdk.enums.BizTypeEnum;
import com.xxzyt.wlpt.transport.config.PropertyMsg;
import com.xxzyt.wlpt.transport.model.Record;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**

  • @Description //数组字符转互相转换

  • @Author Jack

  • @Date 2018/11/22
    **/
    @Component
    public class BeanChangeUtils {
    public List contrastObj(Object oldBean, Object newBean) {
    // 转换为传入的泛型T
    T pojo1 = (T) oldBean;
    T pojo2 = (T) newBean;
    // 通过反射获取类的Class对象
    Class clazz = pojo1.getClass();
    // 获取类型及字段属性
    Field[] fields = clazz.getDeclaredFields();
    return jdk8OrAfter(fields, pojo1, pojo2, clazz);

    }
    /**

    • lambda表达式,表达式内部的变量都是final修饰,需要传入需要传入final类型的数组

    • @param fields

    • @param pojo1

    • @param pojo2

    • @param clazz

    • @return
      */
      public List jdk8OrAfter(Field[] fields, T pojo1, T pojo2, Class clazz) {
      List modifyRecords =new ArrayList<>();
      Arrays.asList(fields).forEach(f -> {
      if (f.isAnnotationPresent(PropertyMsg.class)) {
      try {

               PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(clazz, f.getName());
               // 获取对应属性值
               Method getMethod = pd.getReadMethod();
               Object o1 = getMethod.invoke(pojo1);
               Object o2 = getMethod.invoke(pojo2);
               String typeName = f.getType().getName();
               if (o1 == null || o2 == null) {
                   return;
               }
               bigdecimal 类型的数据要去掉小数点后尾部的0不一致造成数据比对差异
               if (typeName.equals("java.math.BigDecimal") && o1 != null && o2 != null) {
                   BigDecimal val_old_big = new BigDecimal(String.valueOf(o1)).multiply(new BigDecimal(100));
                   BigDecimal val_new_big = new BigDecimal(String.valueOf(o2)).multiply(new BigDecimal(100));
                   if (String.valueOf(val_old_big).indexOf(".") != -1 || String.valueOf(val_new_big).indexOf(".") != -1) {
                       DecimalFormat formatter1 = new DecimalFormat();
                       o1 = formatter1.format(val_old_big);
                       o2 = formatter1.format(val_new_big);
                   }
               }
               if (!o1.toString().equals(o2.toString())) {
      
                   Record record = new Record();
                   record.setBeforeContent(o1.toString()).setTitle( f.getAnnotation(PropertyMsg.class).value()).setAfterContent(o2.toString());
                   if(StringUtils.isEmpty(record.getAfterContent())){
                       record.setAfterContent("空");
                   }
                   if(StringUtils.isEmpty(record.getBeforeContent())){
                       record.setBeforeContent("空");
                   }
                   //格式化时间格式
                   if(o1 instanceof  Date && o2 instanceof Date){
                       record.setBeforeContent(DateUtils.format((Date) o1,"yyyy-MM-dd"));
                       record.setAfterContent(DateUtils.format((Date) o2,"yyyy-MM-dd"));
                   }
                   if(o1 instanceof CarTypeEnum && o2 instanceof CarTypeEnum) {
                       record.setBeforeContent(((CarTypeEnum) o1).getMessage());
                       record.setAfterContent(((CarTypeEnum) o2).getMessage());
                   }
                   modifyRecords.add(record);
               }
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
      

      });
      return modifyRecords;
      }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值