同对象比较工具类(计算变化趋势、变化百分比)

1.具体方法

import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author Muan
 * @Date 2022/6/1 10:34
 * @Version 1.0
 */

public class CompareUtils {

    private static final Logger logger = LoggerFactory.getLogger(CompareUtils.class);

    /**
     * 源目标为非MAP类型时
     *
     * @param source
     * @param target
     * @return
     */
    @SneakyThrows
    public static Object classOfSrc(Object source, Object target, List<String> excludeList, List<String> evaluationList, Boolean requiredType) {
        Class<?> srcClass = source.getClass();
        Field[] fields = srcClass.getDeclaredFields();

        for (Field field : fields) {
            String nameKey = field.getName();
            if (excludeList.contains(nameKey) || nameKey.contains("Bhqs") || nameKey.contains("Bhbl")) {
                continue;
            }
            if (requiredType) {  //是否计算全部趋势及百分比
                Integer srcValue = getClassValue(source, nameKey) == null ? null : (Integer) getClassValue(source, nameKey);
                Integer tarValue = getClassValue(target, nameKey) == null ? null : (Integer) getClassValue(target, nameKey);
                //变化趋势设置属性
                Field f1 = target.getClass().getDeclaredField(nameKey + "Bhqs");
                f1.setAccessible(true);
                if (srcValue != null && tarValue != null) {
                    if (tarValue > srcValue) {
                        f1.set(target, 1);
                    } else if (tarValue < srcValue) {
                        f1.set(target, 2);
                    } else {
                        f1.set(target, 3);
                    }
                }

                //变化百分比设置属性
                Field f2 = target.getClass().getDeclaredField(nameKey + "Bhbl");
                f2.setAccessible(true);
                if(srcValue != null && tarValue != null) {
                    if (srcValue != 0) {
                        int diff = tarValue - srcValue;
                        System.out.println(((diff < 0) ? -diff : diff) / srcValue);
                        double v = (double) ((diff < 0) ? -diff : diff) / srcValue * 100;
                        BigDecimal bg = new BigDecimal(v);
                        double d1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                        f2.set(target, d1); //差取绝对值除以初始值
                    }
                }
            } else { //计算部门趋势及百分比
                if (evaluationList.contains(nameKey + "Bhqs")) { //自定义取部分趋势结果
                    Integer srcValue = getClassValue(source, nameKey) == null ? 0 : (Integer) getClassValue(source, nameKey);
                    Integer tarValue = getClassValue(target, nameKey) == null ? 0 : (Integer) getClassValue(target, nameKey);
                    //变化趋势设置属性
                    Field f1 = target.getClass().getDeclaredField(nameKey + "Bhqs");
                    f1.setAccessible(true);
                    if (srcValue > tarValue) {
                        f1.set(target, 1);
                    } else if (srcValue < tarValue) {
                        f1.set(target, 2);
                    } else {
                        f1.set(target, 3);
                    }
                    if (evaluationList.contains(nameKey + "Bhbl")) { //自定义取部分百分比
                        //变化百分比设置属性
                        Field f2 = target.getClass().getDeclaredField(nameKey + "Bhbl");
                        f2.setAccessible(true);
                        if (null != srcValue) {
                            int diff = tarValue - srcValue;
                            System.out.println(((diff < 0) ? -diff : diff) / srcValue);
                            f2.set(target, (double) ((diff < 0) ? -diff : diff) / srcValue); //差取绝对值除以初始值
                        }
                    }
                }
            }
        }
        return target;
    }

    /**
     * 根据字段名称取值
     *
     * @param obj
     * @param fieldName
     * @return
     */
    public static Object getClassValue(Object obj, String fieldName) {
        if (obj == null) {
            return null;
        }
        try {
            Class beanClass = obj.getClass();
            Method[] ms = beanClass.getMethods();
            for (int i = 0; i < ms.length; i++) {
                // 非get方法不取
                if (!ms[i].getName().startsWith("get")) {
                    continue;
                }
                Object objValue = null;
                try {
                    objValue = ms[i].invoke(obj, new Object[]{});
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.info("反射取值出错:" + e.toString());
                    continue;
                }
                if (objValue == null) {
                    continue;
                }
                if (ms[i].getName().toUpperCase().equals(fieldName.toUpperCase())
                        || ms[i].getName().substring(3).toUpperCase().equals(fieldName.toUpperCase())) {
                    return objValue;
                } else if (fieldName.toUpperCase().equals("SID")
                        && (ms[i].getName().toUpperCase().equals("ID") || ms[i].getName().substring(3).toUpperCase()
                        .equals("ID"))) {
                    return objValue;
                }
            }
        } catch (Exception e) {
             logger.info("取方法出错!" + e.toString());
        }
        return null;
    }

}

测试

@GetMapping(value = "/compare")
    @ApiOperation("比较测试")
    public ApiResponse compare(){
        TestDTO t1 = new TestDTO();
        t1.setId(1);
        t1.setFirst(5);
        t1.setSecond(6);
        TestDTO t2 = new TestDTO();
        t2.setId(2);
        t2.setFirst(5);
        t2.setSecond(10);
        List<String> excludeList = new ArrayList<>();
        excludeList.add("id");
        List<String> evaluationList = new ArrayList<>();
        //趋势
        evaluationList.add("firstBhqs");
        //比例
        evaluationList.add("firstBhbl");

        //计算全部趋势及百分比
        TestDTO a = (TestDTO) CompareUtils.classOfSrc(t1,t2,excludeList,null,true);
        //自定义计算趋势及百分比
        TestDTO b = (TestDTO) CompareUtils.classOfSrc(t1,t2,excludeList,evaluationList,false);
        return ApiResponse.success(a);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值