重构实战-1.原则:有boolean的if else 可将if 和 else 封装为方法。2. 查看传入的Bean用了哪些数据,是否可减少数据。

 最终效果:   // 计算保险费并生成保险费域的FeeShareBenefitBO
        FeeShareBenefitBO fee = null;
        if (shareBenefitHistory == null) {
            shareBenefitLogger.debug(LogUtil.f("shareBenefitHistory:{},", JsonUtil.toJson(shareBenefitHistory)));
            //部分历史数据没有shareBenefitHistory
            fee = calcFeeShareBenefitBO(refundFeeBo, shareBenefitHistory);
        } else {
            fee = calcFeeShareBenefitBO(originalPay, shareBenefitResultOld, refundFeeBo);
        }




最初效果:

 


long calcRefundInsure = calcRefundInsure(refundFeeBo, getPayTotoalRemoveRisk(prRefOriginalPay),
                shareBenefitRule);


        FeeShareBenefitBO fee = new FeeShareBenefitBO();


        long shared = 0;
        long tipShared = 0;
        long insureFromCShared = 0;
        long insureFromTipShared = 0;
        long refundInsure = 0;
        if (shareBenefitHistory != null) {
            //1.计算普通退款中保险费要退多少
            if (refundFeeBo.getShared() > shareBenefitHistory.getTotalCommonShare()) {
                //退款比之前分润的大,保险费要退
                insureFromCShared = refundFeeBo.getShared() - shareBenefitHistory.getTotalCommonShare();
                shared = shareBenefitHistory.getTotalCommonShare();
            } else {
                shared = refundFeeBo.getShared();
            }
            //2.计算小费退款中保险费要退多少
            if (refundFeeBo.getTipShared() > shareBenefitHistory.getTotalTipShare()) {
                //退款比之前分润的大,保险费要退
                insureFromTipShared = refundFeeBo.getTipShared() - shareBenefitHistory.getTotalTipShare();
                tipShared = shareBenefitHistory.getTotalTipShare();
            } else {
                tipShared = refundFeeBo.getTipShared();
            }
            refundInsure = insureFromCShared + insureFromTipShared;
            // refundInsure+tipShared+shared=最初订单域的小费+普通分润总额


        } else {

long calcRefundInsure = calcRefundInsure(refundFeeBo, getPayTotoalRemoveRisk(prRefOriginalPay),
                shareBenefitRule);


            long refundInsureLeft = 0;
            if (refundFeeBo.getShared() > calcRefundInsure) {
                refundInsureLeft = 0;
                shared = refundFeeBo.getShared() - calcRefundInsure;
            } else {
                shared = 0;
                refundInsureLeft = calcRefundInsure - refundFeeBo.getShared();
            }
            tipShared = refundFeeBo.getTipShared() - refundInsureLeft;
            refundInsure = calcRefundInsure;
            // refundInsure+tipShared+shared=最初订单域的小费+普通分润总额
        }


        fee.setCommonShared(shared);
        fee.setInsure(refundInsure);
        fee.setTipShared(tipShared);
        fee.setUnshared(refundFeeBo.getUnshared());
        return fee;
    }


ShareBenefitHistoryDO shareBenefitHistory = shareBenefitHistoryService.getShareBenefitHistory(prRefOriginalPay
            .getPayId());

 private FeeShareBenefitBO calacFeeShareBenefitBO(FeeBO refundFeeBo, ShareBenefitHistoryDO shareBenefitHistory,

                                                     long calcRefundInsure) {
        FeeShareBenefitBO fee = new FeeShareBenefitBO();


        long shared = 0;
        long tipShared = 0;
        long insureFromCShared = 0;
        long insureFromTipShared = 0;
        long refundInsure = 0;
        if (shareBenefitHistory != null) {
            //1.计算普通退款中保险费要退多少
            if (refundFeeBo.getShared() > shareBenefitHistory.getTotalCommonShare()) {
                //退款比之前分润的大,保险费要退
                insureFromCShared = refundFeeBo.getShared() - shareBenefitHistory.getTotalCommonShare();
                shared = shareBenefitHistory.getTotalCommonShare();
            } else {
                shared = refundFeeBo.getShared();
            }
            //2.计算小费退款中保险费要退多少
            if (refundFeeBo.getTipShared() > shareBenefitHistory.getTotalTipShare()) {
                //退款比之前分润的大,保险费要退
                insureFromTipShared = refundFeeBo.getTipShared() - shareBenefitHistory.getTotalTipShare();
                tipShared = shareBenefitHistory.getTotalTipShare();
            } else {
                tipShared = refundFeeBo.getTipShared();
            }
            refundInsure = insureFromCShared + insureFromTipShared;
            // refundInsure+tipShared+shared=最初订单域的小费+普通分润总额


        } else {
            long refundInsureLeft = 0;
            if (refundFeeBo.getShared() > calcRefundInsure) {
                refundInsureLeft = 0;
                shared = refundFeeBo.getShared() - calcRefundInsure;
            } else {
                shared = 0;
                refundInsureLeft = calcRefundInsure - refundFeeBo.getShared();
            }
            tipShared = refundFeeBo.getTipShared() - refundInsureLeft;
            refundInsure = calcRefundInsure;
            // refundInsure+tipShared+shared=最初订单域的小费+普通分润总额
        }


        fee.setCommonShared(shared);
        fee.setInsure(refundInsure);
        fee.setTipShared(tipShared);
        fee.setUnshared(refundFeeBo.getUnshared());
        return fee;
    }

你可能感兴趣的:(重构实战-1.原则:有boolean的if else 可将if 和 else 封装为方法。2. 查看传入的Bean用了哪些数据,是否可减少数据。)