package com.ag.account.feeUtil;

import com.ag.entity.fee.ContractItem;
import com.ag.entity.fee.face.IBulkTon;
import com.ag.entity.fee.face.ICondition;
import com.alibaba.fastjson.JSONObject;

/**
 * Created by Administrator on 2018-03-17.
 */
public class BulkTonUtil {

    /**
     * 计算计费吨
     * @param bulkTon
     * @param itemVO
     */
    public static void calcTon(IBulkTon bulkTon, ContractItem itemVO){
        double weight = bulkTon.getWeight() == null ? 0 : bulkTon.getWeight() / 1000;
        double volume = bulkTon.getVolume() == null ? 0 : bulkTon.getVolume();
        //如果费率中的体积吨金额不为空，将在detail中替换体积吨换算率，重写rate，amount等。
        if (itemVO.getTonsStere() == null){
            itemVO.setTonsStere(1d);
        }
        volume = volume * itemVO.getTonsStere();
        //择大计费 0=计费吨1=重量吨2=体积吨
        //无选择按计费吨
        if(itemVO.getByton() == null || itemVO.getByton() == 0){
            itemVO.setByton(0);
            if (weight > volume){
                bulkTon.setTon(weight);
            }else {
                bulkTon.setTon(volume);
            }
        }
        if (itemVO.getByton() == 1){
            bulkTon.setTon(weight);
        }
        if (itemVO.getByton() == 2){
            bulkTon.setTon(volume);
        }
        //最小计费吨判断
        if (itemVO.getMinTon() != null){
            if (bulkTon.getTon() < itemVO.getMinTon()){
                bulkTon.setTon(itemVO.getMinTon());
            }
        }

        /*double volTon = volume * itemVO.getTonsStere();
        if (weight > volTon){
            bulkTon.setTon(weight);
        }else {
            bulkTon.setTon(volTon);
        }
        if (bulkTon.getTon() < 1){
            bulkTon.setTon(1d);
        }*/
    }

    public static Double calcTon(JSONObject json, ContractItem itemVO){
        double target = 0d;
        double weight = !json.containsKey(ICondition.weight) ? 0 : json.getDouble(ICondition.weight);
        double volume = !json.containsKey(ICondition.volume) ? 0 : json.getDouble(ICondition.volume);
        //如果费率中的体积吨金额不为空，将在detail中替换体积吨换算率，重写rate，amount等。
        if (itemVO.getTonsStere() == null){
            itemVO.setTonsStere(1d);
        }
        volume = volume * itemVO.getTonsStere();
        //择大计费 0=计费吨1=重量吨2=体积吨
        if (weight > volume){
            target = weight;
        }else {
            target = volume;
        }
        if (itemVO.getByton() == null){
            itemVO.setByton(1);
        }
        if (itemVO.getByton() == 1 && weight > 0d){
            target = weight;
        }
        if (itemVO.getByton() == 2 && volume > 0d){
            target = volume;
        }
        //最小计费吨判断
        if (itemVO.getMinTon() != null){
            if (target < itemVO.getMinTon()){
                target = itemVO.getMinTon();
            }
        }
        return target;
    }

    /**
     * 计费吨取整方式
     * @param contractItem
     */
    public static Double roundTon(Double ton, ContractItem contractItem) {
        if (contractItem.getIsFormat() == null){
            return ton;
        }
        //进整
        if (contractItem.getIsFormat() == 0){
            return Math.ceil(ton);
        }
        //四舍五入
        if (contractItem.getIsFormat() == 1){
            return (double)Math.round(ton);
        }
        //去小数
        if (contractItem.getIsFormat() == 2){
            return Math.floor(ton);
        }
        return null;
    }

    public static void roundTon(IBulkTon bulkTon, ContractItem contractItem) {
        if (contractItem.getIsFormat() == null){
            return;
        }
        //进整
        if (contractItem.getIsFormat() == 0){
            bulkTon.setTon(Math.ceil(bulkTon.getTon()));
        }
        //四舍五入
        if (contractItem.getIsFormat() == 1){
            bulkTon.setTon((double)Math.round(bulkTon.getTon()));
        }
        //去小数
        if (contractItem.getIsFormat() == 2){
            bulkTon.setTon(Math.floor(bulkTon.getTon()));
        }
    }

    /**
     * 吊箱超重取整方式
     * @param bulkTon
     * @param contractItem
     */
    public static void roundOverTon(IBulkTon bulkTon, ContractItem contractItem) {
        if (contractItem.getIsFormat() == null){
            return;
        }
        if (bulkTon.getOverWeight() > 0 && bulkTon.getOverWeight() <= 0.5 && contractItem.getMinTon() != null){
            bulkTon.setOverWeight(1.0);
        }
        //进整
        if (contractItem.getIsFormat() == 0){
            bulkTon.setOverWeight(Math.ceil(bulkTon.getOverWeight()));
        }
        //四舍五入
        if (contractItem.getIsFormat() == 1){
            bulkTon.setOverWeight((double)Math.round(bulkTon.getOverWeight()));
        }
        //去小数
        if (contractItem.getIsFormat() == 2){
            bulkTon.setOverWeight(Math.floor(bulkTon.getOverWeight()));
        }
    }


}
