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

import cn.hutool.core.util.NumberUtil;
import com.yunyan.business.constant.TemplateEpType;
import com.yunyan.business.document.y22.pojo.finalExcelY22.*;
import com.yunyan.business.document.y23.budget.pojo.budgetY23.*;
import com.yunyan.common.utils.NumberFormatUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 2023 设置预算 数据的处理 工具类
 */
@Slf4j
public class FinalY23BudgetCompareUtils {

    /**
     * 比较前一年的数据
     * @param excelDataYearVo
     * @param budgetDataVo
     * @return
     */
    public static void setBudgetData(FinalExcelDataVo excelDataYearVo, BudgetDataVo budgetDataVo ) {

        //setP3p2CategoryItem(excelDataYearVo, budgetDataVo);
        setP3p2CategorySubItemBudget(excelDataYearVo, budgetDataVo);
        setP3p3ListItem(excelDataYearVo, budgetDataVo);
        setP3T4ItemBudget(excelDataYearVo, budgetDataVo);
        setP3T5ItemBudget(excelDataYearVo, budgetDataVo);
        setP3T7Budget(excelDataYearVo, budgetDataVo);

        //更新 过滤掉 0
        FinalY23AreaUtils.setAreaCompareBudgetYear(excelDataYearVo);
    }

    @Deprecated
    /**
     * 使用项的 合计  预算
     * 设置 p3p2部分的 大类 ,1外层 直接比对
     * @param excelDataYearVo
     * @return
     */
    public static void setP3p2CategoryItem(FinalExcelDataVo excelDataYearVo, BudgetDataVo budgetDataVo) {
        /*List<FinalP3T2CategoryItemVo> list = excelDataYearVo.getP3T2Vo().getList();
        List<BudgetP3p4ItemVo> budgetExpenditureList = budgetDataVo.getExplainVo().getP3p4Vo().getExpenditureVo().getList();
        BudgetP3p4SubItemVo expenditureVo = budgetDataVo.getExplainVo().getP3p4Vo().getExpenditureVo();
        FinalP3T2Vo p3T2Vo = excelDataYearVo.getP3T2Vo();
        p3T2Vo.setExpenditureBudgetBegin(expenditureVo.getTotal());
        p3T2Vo.setExpenditureBudgetRatio(FinalY23Utils.getSuccessRatio(p3T2Vo.getExpenditureFinal(), p3T2Vo.getExpenditureBudgetBegin()));


        for (FinalP3T2CategoryItemVo p3T2CategoryItemVo : list) {
            BudgetP3p4ItemVo budgetP3p4ItemVo = budgetExpenditureList.stream().filter(it-> it.getName().equals(p3T2CategoryItemVo.getName())).findFirst().orElse(null);
            if(Objects.isNull(budgetP3p4ItemVo)) {
                log.info("更新预算 p3p2 p3T2CategoryItemVo 未找到对应的");
                continue;
            }

            p3T2CategoryItemVo.setBeginValue(budgetP3p4ItemVo.getTotal());
            p3T2CategoryItemVo.setInfo(String.format("%s（类）年初预算为%s万元，决算数为%s万元", p3T2CategoryItemVo.getName(), p3T2CategoryItemVo.getBeginValue(), p3T2CategoryItemVo.getFinalValue()));
        }*/
    }

    /**
     * 设置 p3p2 sublist的最后list下的项的数
     * @param excelDataYearVo
     * @param budgetDataVo
     */
    public static void setP3p2CategorySubItemBudget(FinalExcelDataVo excelDataYearVo, BudgetDataVo budgetDataVo) {
        List<FinalP3T2CategoryItemVo> list = excelDataYearVo.getP3T2Vo().getList();
        List<BudgetP3p5ItemVo> budgetList = budgetDataVo.getExplainVo().getP3p5Vo().getList();

        for (FinalP3T2CategoryItemVo p3T2CategoryItemVo : list) {
            BigDecimal budgetTotal = BigDecimal.ZERO;
            for (FinalP3T2ItemVo p3T2ItemVo : p3T2CategoryItemVo.getList()) {
                //取 本年的预算数据, 后对比
                Optional<BudgetP3p5ItemVo> subDeptItemVoOptional = budgetList.stream()
                        .filter(subdep -> {
                            String code = subdep.getCode1() + subdep.getCode2() + subdep.getCode3();
                            return code.equals(p3T2ItemVo.getCode());
                        } ).findFirst();
                if(!subDeptItemVoOptional.isPresent()) {
                    log.debug("决算p3p2的科目[{}]的年初预算没有值", p3T2ItemVo.getCode());
                    continue;
                }

                //设置 年初的预算值, 已经是万元
                //String beginValue = NumberFormatUtils.amountInThousands(subDeptItemVoOptional.get().getTotal().replace(",", ""));
                String beginValue = subDeptItemVoOptional.get().getTotal();
                FinalY23CompareLastUtils.setP3T2BudgetItemInfoChange(p3T2ItemVo,  beginValue);

                //记录总的
                BigDecimal beginValueBigdecimal = StringUtils.isNotBlank(beginValue) ? new BigDecimal(beginValue.replace(",","")) : BigDecimal.ZERO;
                budgetTotal = NumberUtil.add(budgetTotal, beginValueBigdecimal);

                //更新 比率 与 对比的增长率
                /*p3T2ItemVo.setBeginValue(beginValue);
                p3T2ItemVo.setRatio(FinalY23Utils.getSuccessRatio(p3T2ItemVo.getFinalValue(), p3T2ItemVo.getBeginValue()));
                p3T2ItemVo.setInfo(FinalY23Utils.getInfoStrP3T2(p3T2ItemVo.getName(), p3T2ItemVo.getFinalValue(), p3T2ItemVo.getBeginValue(), p3T2ItemVo.getRatio()));

                //更新 判断是否是增长 大于小于才有 原因的填写, 相等不用输入原因
                int compareInt = FinalY23Utils.compareValue(p3T2ItemVo.getFinalValue(), p3T2ItemVo.getBeginValue());
                Integer reasonFlag = 0 == compareInt ? 0 : 1;
                String compareValueStr = FinalY23Utils.compareValueStr(compareInt);
                p3T2ItemVo.setReasonFlag(reasonFlag);
                p3T2ItemVo.setCompareValueStr(compareValueStr);*/
            }

            p3T2CategoryItemVo.setBeginValue(NumberFormatUtils.addThousandSeparatorForInteger(budgetTotal));;
            p3T2CategoryItemVo.setInfo(String.format("%s（类）年初预算为%s万元，决算数为%s万元", p3T2CategoryItemVo.getName(), p3T2CategoryItemVo.getBeginValue(), p3T2CategoryItemVo.getFinalValue()));
        }

        //设置 p3t2的sub子项的预算
        List<FinalP3T2ItemVo> p3T2SubItemList = excelDataYearVo.getP3T2Vo().getSubItemList();
        if(!Objects.isNull(p3T2SubItemList) && !p3T2SubItemList.isEmpty()) {
            for (FinalP3T2ItemVo p3T2ItemVo : p3T2SubItemList ) {
                //取 本年的预算数据, 后对比
                Optional<BudgetP3p5ItemVo> subDeptItemVoOptional = budgetList.stream()
                        .filter(subdep -> {
                            String code = subdep.getCode1() + subdep.getCode2() + subdep.getCode3();
                            return code.equals(p3T2ItemVo.getCode());
                        } ).findFirst();
                if(!subDeptItemVoOptional.isPresent()) {
                    log.debug("决算p3p2的科目[{}]的年初预算没有值", p3T2ItemVo.getCode());
                    continue;
                }
                String beginValue = subDeptItemVoOptional.get().getTotal();
                FinalY23CompareLastUtils.setP3T2BudgetItemInfoChange(p3T2ItemVo,  beginValue);
            }
        } else {
            log.info("p3t2的 sub子项的为空");
        }

        //过滤掉 去 预算数和决算数为0的项
        setP3p2CategorySubItemBudgetFilterZero(excelDataYearVo);
    }

    /**
     * p3p2 大类 过滤掉 去 预算数和决算数为0的项
     * @param excelDataYearVo
     */
    public static void setP3p2CategorySubItemBudgetFilterZero(FinalExcelDataVo excelDataYearVo) {
        List<FinalP3T2CategoryItemVo> list = excelDataYearVo.getP3T2Vo().getList();

        for (FinalP3T2CategoryItemVo p3T2CategoryItemVo : list) {
            List<FinalP3T2ItemVo> newList = new ArrayList<>();
            for (FinalP3T2ItemVo p3T2ItemVo : p3T2CategoryItemVo.getList()) {
                if("0".equals(p3T2ItemVo.getFinalValue()) && "0".equals(p3T2ItemVo.getBeginValue()) ) {
                    continue;
                }
                newList.add(p3T2ItemVo);
            }
            p3T2CategoryItemVo.setList(newList);
        }
    }

    /**
     * 天等的 p3p1 项 过滤掉 去 预算数和决算数为0的项
     * @param excelDataYearVo
     */
    public static void setP3p1ItemBudgetFilterZero(FinalExcelDataVo excelDataYearVo) {
        List<FinalP3T1TotalItemVo> incomeList = excelDataYearVo.getP3T1Vo().getIncome().getList();
        //支出
        List<FinalP3T1TotalItemVo> expenditureList = excelDataYearVo.getP3T1Vo().getExpenditure().getList();

        List<FinalP3T1TotalItemVo> newList = new ArrayList<>();
        for (FinalP3T1TotalItemVo itemVo : incomeList) {
            if("0".equals(itemVo.getValueYear()) && "0".equals(itemVo.getValueLastYear()) ) {
                continue;
            }
            newList.add(itemVo);
        }
        excelDataYearVo.getP3T1Vo().getIncome().setList(newList);

        List<FinalP3T1TotalItemVo> newList2 = new ArrayList<>();
        for (FinalP3T1TotalItemVo itemVo : expenditureList) {
            if("0".equals(itemVo.getValueYear()) && "0".equals(itemVo.getValueLastYear()) ) {
                continue;
            }
            newList2.add(itemVo);
        }
        excelDataYearVo.getP3T1Vo().getExpenditure().setList(newList2);
    }

    /**
     * 天等的  sublist item 过滤掉 去 预算数和决算数为0的项
     * @param excelDataYearVo
     */
    public static void setP3p2SubItemBudgetFilterZero(FinalExcelDataVo excelDataYearVo) {
        List<FinalP3T2ItemVo> list = excelDataYearVo.getP3T2Vo().getSubItemList();

        List<FinalP3T2ItemVo> newList = new ArrayList<>();
        for (FinalP3T2ItemVo itemVo : list) {
            if("0".equals(itemVo.getFinalValue()) && "0".equals(itemVo.getBeginValue()) ) {
                continue;
            }
            newList.add(itemVo);
        }
        excelDataYearVo.getP3T2Vo().setSubItemList(newList);
    }

    /**
     * 设置 p3p3 list的 预算数据的更新
     * @param excelDataYearVo
     * @param budgetDataVo
     */
    public static void setP3p3ListItem(FinalExcelDataVo excelDataYearVo, BudgetDataVo budgetDataVo) {
        List<FinalP3T3ItemVo> list = excelDataYearVo.getP3T3Vo().getList();

        List<BudgetT6ItemVo> budgetExpenditureList = budgetDataVo.getExplainVo().getP3p6Vo().getCategoryList();
        String templateExportType = excelDataYearVo.getTemplateExportType();

        if(Objects.isNull(budgetExpenditureList) || budgetExpenditureList.isEmpty()) {
            return;
        }

        List<FinalP3T3ItemVo> p3T3List = list;
        for (FinalP3T3ItemVo p3T3ItemVo : p3T3List ) {
            //取 本年的预算数据, 后对比
            Optional<BudgetT6ItemVo> itemVoOptional = budgetExpenditureList.stream().filter(i -> i.getName().equals(p3T3ItemVo.getName())).findFirst();
            if(!itemVoOptional.isPresent()) {
                log.debug("决算p3p3的科目[{}]的年初预算没有值", p3T3ItemVo.getName() );
                continue;
            }

            //设置 年初的预算值
            String beginValue = itemVoOptional.get().getTotal();


            //更新 比率 与 对比的增长率
            p3T3ItemVo.setBeginValue(beginValue);
            p3T3ItemVo.setRatio(FinalY23Utils.getSuccessRatio(p3T3ItemVo.getFinalValue(), p3T3ItemVo.getBeginValue()));
            p3T3ItemVo.setInfo(FinalY23AreaUtils.getInfoStrP3T3(p3T3ItemVo.getName(), p3T3ItemVo.getFinalValue(), p3T3ItemVo.getBeginValue(), p3T3ItemVo.getRatio(), templateExportType));

            //更新 判断是否是增长 大于小于才有 原因的填写, 相等不用输入原因
            int compareInt = FinalY23Utils.compareValue(p3T3ItemVo.getFinalValue(), p3T3ItemVo.getBeginValue());
            Integer reasonFlag = 0 == compareInt ? 0 : 1;
            String compareValueStr = FinalY23Utils.compareValueStr(compareInt);
            p3T3ItemVo.setReasonFlag(reasonFlag);
            p3T3ItemVo.setCompareValueStr(compareValueStr);
        }
    }

    /**
     * p3p3 大类 过滤掉 去 预算数和决算数为0的项
     * @param excelDataYearVo
     */
    public static void setP3p3ItemBudgetFilterZero(FinalExcelDataVo excelDataYearVo) {
        List<FinalP3T3ItemVo> list = excelDataYearVo.getP3T3Vo().getList();

        List<FinalP3T3ItemVo> newList = new ArrayList<>();
        for (FinalP3T3ItemVo itemVo : list) {
            if("0".equals(itemVo.getFinalValue()) && "0".equals(itemVo.getBeginValue()) ) {
                continue;
            }
            newList.add(itemVo);
        }

        excelDataYearVo.getP3T3Vo().setList(newList);
    }


    /**
     * 决算 设置 比较 p3p4的项的预算数
     * @param excelDataYearVo
     */
    public static void setP3T4ItemBudget(FinalExcelDataVo excelDataYearVo, BudgetDataVo budgetDataVo) {
        List<FinalP3T4CategoryItemVo> list = excelDataYearVo.getP3T4Vo().getList();
        List<BudgetP3p8ItemVo> budgetList = budgetDataVo.getExplainVo().getP3p8Vo().getList();

        for (FinalP3T4CategoryItemVo categoryItemVo : list) {
            if(categoryItemVo.getList().isEmpty()) {
                continue;
            }
            BigDecimal budgetTotal = BigDecimal.ZERO;
            for (FinalP3T4ItemVo p3T4ItemVo : categoryItemVo.getList() ) {
                //取 本年的预算数据, 后对比
                Optional<BudgetP3p8ItemVo> subDeptItemVoOptional = budgetList.stream()
                        .filter(subdep -> {
                            String code = subdep.getCode1() + subdep.getCode2() + subdep.getCode3();
                            return code.equals(p3T4ItemVo.getCode());
                        } )
                        .findFirst();
                if(!subDeptItemVoOptional.isPresent()) {
                    log.debug("决算p3p4的科目[{}]的年初预算没有值", p3T4ItemVo.getCode());
                    continue;
                }

                //设置 年初的预算值, 已经是万元
                //String beginValue = NumberFormatUtils.amountInThousands(subDeptItemVoOptional.get().getTotal().replace(",", ""));
                String beginValue = subDeptItemVoOptional.get().getTotal();

                //更新 比率 与 对比的增长率
                p3T4ItemVo.setBeginValue(beginValue);
                p3T4ItemVo.setRatio(FinalY23Utils.getSuccessRatio(p3T4ItemVo.getFinalValue(), p3T4ItemVo.getBeginValue()));
                p3T4ItemVo.setInfo(FinalY23Utils.getInfoStrP3T2(p3T4ItemVo.getName(), p3T4ItemVo.getFinalValue(), p3T4ItemVo.getBeginValue(), p3T4ItemVo.getRatio()));

                //判断是否是增长 大于小于才有 原因的填写, 相等不用输入原因
                int compareInt = FinalY23Utils.compareValue(p3T4ItemVo.getFinalValue(), p3T4ItemVo.getBeginValue());
                Integer reasonFlag = 0 == compareInt ? 0 : 1;
                String compareValueStr = FinalY23Utils.compareValueStr(compareInt);
                p3T4ItemVo.setReasonFlag(reasonFlag);
                p3T4ItemVo.setCompareValueStr(compareValueStr);

                //记录总的
                BigDecimal beginValueBigdecimal = StringUtils.isNotBlank(beginValue) ? new BigDecimal(beginValue.replace(",","")) : BigDecimal.ZERO;
                budgetTotal = NumberUtil.add(budgetTotal, beginValueBigdecimal);
            }

            categoryItemVo.setBeginValue(NumberFormatUtils.addThousandSeparatorForInteger(budgetTotal));
            categoryItemVo.setRatio(FinalY23Utils.getSuccessRatio(categoryItemVo.getFinalValue(), categoryItemVo.getBeginValue()));
            categoryItemVo.setInfo(FinalY23Utils.getInfoStr(categoryItemVo.getName(), categoryItemVo.getFinalValue(), categoryItemVo.getBeginValue(), categoryItemVo.getRatio()));
        }
    }

    /**
     * 决算 设置 比较 p3p5的项的预算数
     * @param excelDataYearVo
     */
    public static void setP3T5ItemBudget(FinalExcelDataVo excelDataYearVo, BudgetDataVo budgetDataVo) {
        List<FinalP3T5CategoryItemVo> list = excelDataYearVo.getP3T5Vo().getList();
        List<BudgetP3p8ItemVo> budgetList = budgetDataVo.getExplainVo().getP3p9Vo().getList();

        for (FinalP3T5CategoryItemVo categoryItemVo : list) {
            if(categoryItemVo.getList().isEmpty()) {
                continue;
            }
            BigDecimal budgetTotal = BigDecimal.ZERO;
            for (FinalP3T5ItemVo p3T5ItemVo : categoryItemVo.getList() ) {
                //取 本年的预算数据, 后对比
                Optional<BudgetP3p8ItemVo> subDeptItemVoOptional = budgetList.stream()
                        .filter(subdep -> {
                            String code = subdep.getCode1() + subdep.getCode2() + subdep.getCode3();
                            return code.equals(p3T5ItemVo.getCode());
                        } )
                        .findFirst();
                if(!subDeptItemVoOptional.isPresent()) {
                    log.debug("决算p3p5的科目[{}]的年初预算没有值", p3T5ItemVo.getCode());
                    continue;
                }

                //设置 年初的预算值, 已经是万元
                //String beginValue = NumberFormatUtils.amountInThousands(subDeptItemVoOptional.get().getTotal().replace(",", ""));
                String beginValue = subDeptItemVoOptional.get().getTotal();

                //更新 比率 与 对比的增长率
                p3T5ItemVo.setBeginValue(beginValue);
                p3T5ItemVo.setRatio(FinalY23Utils.getSuccessRatio(p3T5ItemVo.getFinalValue(), p3T5ItemVo.getBeginValue()));
                p3T5ItemVo.setInfo(FinalY23Utils.getInfoStrP3T2(p3T5ItemVo.getName(), p3T5ItemVo.getFinalValue(), p3T5ItemVo.getBeginValue(), p3T5ItemVo.getRatio()));

                //判断是否是增长 大于小于才有 原因的填写, 相等不用输入原因
                int compareInt = FinalY23Utils.compareValue(p3T5ItemVo.getFinalValue(), p3T5ItemVo.getBeginValue());
                Integer reasonFlag = 0 == compareInt ? 0 : 1;
                String compareValueStr = FinalY23Utils.compareValueStr(compareInt);
                p3T5ItemVo.setReasonFlag(reasonFlag);
                p3T5ItemVo.setCompareValueStr(compareValueStr);

                //记录总的
                BigDecimal beginValueBigdecimal = StringUtils.isNotBlank(beginValue) ? new BigDecimal(beginValue.replace(",","")) : BigDecimal.ZERO;
                budgetTotal = NumberUtil.add(budgetTotal, beginValueBigdecimal);
            }

            categoryItemVo.setBeginValue(NumberFormatUtils.addThousandSeparatorForInteger(budgetTotal));
            categoryItemVo.setRatio(FinalY23Utils.getSuccessRatio(categoryItemVo.getFinalValue(), categoryItemVo.getBeginValue()));
            categoryItemVo.setInfo(FinalY23Utils.getInfoStr(categoryItemVo.getName(), categoryItemVo.getFinalValue(), categoryItemVo.getBeginValue(), categoryItemVo.getRatio()));
        }
    }

    /**
     * 决算 设置 比较 p3p7的预算数
     * @param excelDataYearVo
     */
    public static void setP3T7Budget(FinalExcelDataVo excelDataYearVo, BudgetDataVo budgetDataVo) {
        //机关运行经费支出 年初预算
        FinalP3T7ItemDeptOperateVo deptOperateVo = excelDataYearVo.getP3T7Vo().getDeptOperateVo();
        List<BudgetP3p6ItemVo> publicBudgetList = budgetDataVo.getExplainVo().getP3p6Vo().getPublicVo().getList1();

        //查找 预算公开表6的 302 商品和服务支出  对应过来的公用经费
        BudgetP3p6ItemVo publicItemVo = publicBudgetList.stream().filter(it -> "商品和服务支出".equals(it.getName1()) && "302".equals(it.getCode1())).findFirst().orElse(null);
        if(!Objects.isNull(publicItemVo)) {
            deptOperateVo.setBegin(publicItemVo.getTotal());
        }
        //桂平  田东 7、机关运行经费的年初预算数,取预算表6一般公共预算基本支出情况表的公用经费合计数
        if(TemplateEpType.Y2023FT5GP.getCode().equals(excelDataYearVo.getTemplateExportType()) || TemplateEpType.Y2023FT7TDONG.getCode().equals(excelDataYearVo.getTemplateExportType())) {
            String publicTotal = budgetDataVo.getExplainVo().getP3p6Vo().getPublicTotal();
            if(!Objects.isNull(publicTotal)) {
                deptOperateVo.setBegin(publicTotal);
            }
        }

    }


}
