package com.ag.account.feeUtil;

import com.ag.entity.account.FeeDetail;
import com.ag.entity.fee.TariffMultistep;
import com.ag.entity.fee.face.ICondition;
import com.ag.entity.fee.face.SourceProperty;
import com.ag.entity.fee.vo.TariffVO;
import com.ag.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * Created by yangxh on 2019/8/21.
 */
public class ReductionUtil {


    /**
     * 金额打折
     */
    public static void calcByDiscount(JSONObject json, FeeDetail detail){
        //减免金额
        if (!StrUtil.isEmpty(json.getString(ICondition.DERATE_MONEY)) && json.getDouble(ICondition.DERATE_MONEY) > 0){
            detail.setDerateMoney(json.getDouble(ICondition.DERATE_MONEY));
            detail.setFinalMoney(detail.getCalcMoney() - json.getDouble(ICondition.DERATE_MONEY));
            detail.setDiscount(detail.getCalcMoney() / detail.getStMoney() * 100);//最终折扣
        }
        //折扣
        if (!StrUtil.isEmpty(json.getString(ICondition.DISCOUNT)) && json.getDouble(ICondition.DISCOUNT) > 0){
            detail.setDiscount(json.getDouble(ICondition.DISCOUNT));
            detail.setFinalMoney(detail.getCalcMoney() * json.getDouble(ICondition.DISCOUNT) / 100);
            detail.setDiscount(detail.getFinalMoney() / detail.getStMoney() * 100);//最终折扣
        }
        if (detail.getActualRate() >= 0 && detail.getFinalMoney() < 0){
            detail.setFinalMoney(0d);
        }
        /*if (detail.getActualMoney() < 0){
            detail.setActualMoney(0d);
        }*/

    }

    /**
     * 重置减免天数
     */
    public static void calcByDerateDay(SourceProperty source, FeeDetail detail){
        JSONObject json = source.getProperty();

        if (StrUtil.isEmpty(json.getString(ICondition.DERATE_TYPE))){
            return;
        }
        if (StrUtil.isEmpty(json.getString(ICondition.DERATE_DAY))){
            return;
        }
        if (json.getInteger(ICondition.DERATE_DAY) < 0){
            return;
        }
        //获取标准阶梯费率
        TariffVO tariffVO = source.getTariffVO();
        List<TariffMultistep> stepList = tariffVO.getStepList();

        //减免类型(前免)
        if (json.getInteger(ICondition.DERATE_TYPE) == 0){
            //创建新费率
            TariffVO derateTariff = new TariffVO();
            source.setDerateTariff(derateTariff);
            BeanUtils.copyProperties(tariffVO, derateTariff);

            //构建前免天后的阶梯
            List<TariffMultistep> stepListNew = new ArrayList<>();
            derateTariff.setStepList(stepListNew);
            TariffMultistep stepNewFirst = new TariffMultistep();
            stepNewFirst.setStartVal(1);
            stepNewFirst.setEndVal(json.getInteger(ICondition.DERATE_DAY));
            stepNewFirst.setStepRate(0d);
            stepListNew.add(stepNewFirst);
            //无阶梯费率
            if (CollectionUtils.isEmpty(stepList)){
                TariffMultistep stepNewSecond = new TariffMultistep();
                stepNewSecond.setStartVal(json.getInteger(ICondition.DERATE_DAY) + 1);
                stepNewSecond.setEndVal(9999);
                stepNewFirst.setStepRate(tariffVO.getRate());
                stepListNew.add(stepNewSecond);
            }
            stepList.sort(Comparator.comparingInt(TariffMultistep::getStartVal));
            double minPrice = 0d;//最小阶梯金额
            for (TariffMultistep step : stepList){
                if (step.getStepRate() > 0){
                    minPrice = step.getStepRate();
                    break;
                }
            }
            //循环标准阶梯
            for (TariffMultistep step : stepList){
                //开始天大于减免天, 加入新阶梯
                if (step.getStartVal() > json.getInteger(ICondition.DERATE_DAY)){
                    TariffMultistep stepNew = new TariffMultistep();
                    BeanUtils.copyProperties(step, stepNew);
                    stepListNew.add(stepNew);
                    continue;
                }
                //结束天小于等于减免天, 跳过
                if (step.getEndVal() <= json.getInteger(ICondition.DERATE_DAY)){
                    continue;
                }
                //剩下的是免费天在开始天与结束天之间的
                TariffMultistep stepNew = new TariffMultistep();
                BeanUtils.copyProperties(step, stepNew);
                stepNew.setStartVal(json.getInteger(ICondition.DERATE_DAY) + 1);
                if (stepNew.getStepRate() == 0d){
                    stepNew.setStepRate(minPrice);
                }
                stepListNew.add(stepNew);
            }
            MultiStepUtil.calcAmount(derateTariff, detail);
            detail.setDerateDays(json.getInteger(ICondition.DERATE_DAY));//重置减免天
        }
        //减免类型(后免)
        if (json.getInteger(ICondition.DERATE_TYPE) == 2){
            Integer settleDays = detail.getSettleDays();
            detail.setSettleDays(detail.getSettleDays() - json.getInteger(ICondition.DERATE_DAY));//后免要计算的天数
            MultiStepUtil.calcAmount(tariffVO, detail);
            detail.setDerateDays(detail.getDerateDays() + json.getInteger(ICondition.DERATE_DAY));//重置减免天
            detail.setSettleDays(settleDays);//还原堆存天
        }
        detail.setCalcDays(detail.getSettleDays() - detail.getDerateDays());
        detail.setDiscount(detail.getFinalMoney() / detail.getStMoney() * 100);//最终折扣

    }
}
