package com.ag.account.feeUtil;

import cn.hutool.core.convert.Convert;
import com.ag.entity.account.FeeDetail;
import com.ag.entity.fee.TariffMultistep;
import com.ag.entity.fee.vo.TariffVO;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by Administrator on 2017-09-07.
 */
public class CarStepUtil {
    /*public static void calcAmount(IFeeItemPrice cntrProperty, AccountsDetails account) {
        TariffVO tariff = null;
        if (cntrProperty.getCustomTariff() != null) {
            tariff = cntrProperty.getCustomTariff();
        } else if(cntrProperty.getBaseTariff() != null) {
            tariff = cntrProperty.getBaseTariff();
        }
        if (tariff == null) {
            return;
        }
        getAmount(account, tariff);
    }*/

    /**
     * 阶梯费率算法
     * @param feeDetail
     * @param tariff
     * @return
     */
    public static void calcAmount(TariffVO tariff, FeeDetail feeDetail) {
        feeDetail.setCalcMoney(null);//清空计费金额
        List<TariffMultistep> stepList = tariff.getStepList();
        Integer quantity = Convert.toInt(feeDetail.getQuantity());
        //没有阶梯费率
        if (CollectionUtils.isEmpty(stepList)){
            feeDetail.setCalcMoney(quantity * tariff.getRate());
            return;
        }
        //最大包干阶梯费率
        TariffMultistep maxStep = null;
        for (TariffMultistep step : stepList){
            //赋值拖运包干公里数最大值（4公里或20公里）
            if (maxStep == null || step.getStepRate() > maxStep.getStepRate()){
                maxStep = step;
            }
            //公里数在阶梯范围内
            if (quantity >= step.getStartVal() && quantity <= step.getEndVal()){
                feeDetail.setCalcMoney(step.getStepRate());
                return;
            }
        }
        //超出阶梯费率范围（>20公里）
        if (feeDetail.getCalcMoney() == null){
            //超出最大公里数部分
            Assert.notNull(maxStep, "阶梯费率数据维护有误，请检查！");
            double amount = (feeDetail.getQuantity() - maxStep.getEndVal()) * tariff.getRate();
            feeDetail.setCalcMoney(amount + maxStep.getStepRate());
        }
    }

}
