package com.yunyan.business.document.y23.finalData;

import com.yunyan.business.document.y22.pojo.finalExcelY22.*;
import com.yunyan.business.document.y23.finalData.pojo.ChangeValueItemVo;
import com.yunyan.common.utils.NumberFormatUtils;
import com.yunyan.common.utils.StringUtils;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * 更新 决算的数据 初始化
 */
public class CalculateDataUtils {

    /**
     * 计算 核算 一些数据
     * @param dataVo
     */
    public static void calculateData(FinalExcelDataVo dataVo) {

        //更新 p3p2
        updateP3T2(dataVo.getP3T2Vo());
        updateP3T4(dataVo.getP3T4Vo());

    }

    private static void updateP3T2(FinalP3T2Vo oldData) {
        if(Objects.isNull(oldData)) {
            return;
        }
        if(Objects.isNull(oldData.getSubItemList())) {
            return;
        }
        BigDecimal beginValueTotal = BigDecimal.ZERO;
        for (FinalP3T2ItemVo itemVo : oldData.getSubItemList()) {
            BigDecimal beginValue = FinalY23Utils.strFormaTtoNumBigdecimal(itemVo.getBeginValue());
            if(beginValue.compareTo(BigDecimal.ZERO) == 0) {
                continue;
            }
            beginValueTotal = beginValueTotal.add(beginValue);
        }

        //更新预算
        oldData.setExpenditureBudgetBegin(NumberFormatUtils.addThousandSeparatorForInteger(beginValueTotal));
        oldData.setExpenditureBudgetRatio(FinalY23Utils.getSuccessRatio(oldData.getExpenditureFinal(), oldData.getExpenditureBudgetBegin()));
    }

    private static void updateP3T4(FinalP3T4Vo oldData) {
        if(Objects.isNull(oldData)) {
            return;
        }

        BigDecimal beginValueTotal = BigDecimal.ZERO;
        for (FinalP3T4CategoryItemVo itemVo : oldData.getList()) {
            BigDecimal beginValue = FinalY23Utils.strFormaTtoNumBigdecimal(itemVo.getBeginValue());
            if(beginValue.compareTo(BigDecimal.ZERO) == 0) {
                continue;
            }
            beginValueTotal = beginValueTotal.add(beginValue);
        }

        //更新预算
        oldData.setExpenditureBudgetBegin(NumberFormatUtils.addThousandSeparatorForInteger(beginValueTotal));
        oldData.setExpenditureBudgetRatio(FinalY23Utils.getSuccessRatio(oldData.getExpenditureFinal(), oldData.getExpenditureBudgetBegin()));
    }

}
