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

import com.yunyan.business.constant.TemplateEpType;
import com.yunyan.business.document.y22.pojo.finalExcelY22.*;
import com.yunyan.business.document.y23.finalData.pojo.ChangeValueItemVo;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

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

    /**
     * 查看或者编辑时, 设置初始化一些数据
     * @param dataVo
     */
    public static void updateFillData(FinalExcelDataVo dataVo) {
        if(!"2023".equals(dataVo.getYear()) && !"2024".equals(dataVo.getYear()) && !"2025".equals(dataVo.getYear())) {
            return;
        }

        //更新 p3t4
        updateP3T4(dataVo.getP3T4Vo());
        //更新 p3t6 的数据
        updateP3T6(dataVo.getP3T6Vo());
        //更新 p3t7
        updateP3T7(dataVo.getP3T7Vo());
        //设置 p3p7p1 凭祥和博白
        updateP3T7P1InfoBsAndPx(dataVo);
    }


    /**
     * 更新 p3t4的 去掉单位开头字符串
     * @param oldData
     */
    public static void updateP3T4(FinalP3T4Vo oldData) {
        if(!Objects.isNull(oldData.getNoListFlag()) &&  StringUtils.isNotBlank(oldData.getNoListReason())) {
            //oldData.setNoListReason 查找 字符串的开头如果为单位, 去掉单位的, 只保留后面的字符串
            if(oldData.getNoListReason().startsWith("单位")) {
                oldData.setNoListReason(oldData.getNoListReason().substring(2));
            }
        }
    }

    private static void updateP3T6(FinalP3T6Vo oldData) {
        FinalP3T6ItemAbroadVo abroadVo = oldData.getAbroadVo();
        FinalP3T6ItemCarVo carVo = oldData.getCarVo();
        FinalP3T6ItemReceptiondVo receptiondVo = oldData.getReceptiondVo();

        abroadVo.setDeptGroupNum(abroadVo.getDeptGroupNum().replace(".0", ""));
        abroadVo.setPersonNum(abroadVo.getPersonNum().replace(".0", ""));

        carVo.setCarNum(carVo.getCarNum().replace(".0", ""));

        receptiondVo.setInBatch(receptiondVo.getInBatch().replace(".0", ""));
        receptiondVo.setInPersonNum(receptiondVo.getInPersonNum().replace(".0", ""));
        receptiondVo.setOutBatch(receptiondVo.getOutBatch().replace(".0", ""));
        receptiondVo.setOutPersonNum(receptiondVo.getOutPersonNum().replace(".0", ""));

    }

    /**
     * 更新 p3t7的 其他车辆的状态
     * @param oldData
     */
    private static void updateP3T7(FinalP3T7Vo oldData) {
        //国有资产占用情况说明
        //桂平提的, 现所有的都这样
        if(StringUtils.isNotBlank(oldData.getAssetVo().getDeviceStr())) {
            String newDeviceStr = oldData.getAssetVo().getDeviceStr().replace("（含）", "").replace(".0", "");
            oldData.getAssetVo().setDeviceStr(newDeviceStr);
        }


        String carStr = oldData.getAssetVo().getCarStr();
        carStr = carStr.replace(".0", "");
        String total = oldData.getAssetVo().getTotal().replace(".0", "");
        oldData.getAssetVo().setTotal(total);
        oldData.getAssetVo().setCarStr(carStr);
        if(StringUtils.isBlank(oldData.getAssetVo().getCarOtherNum())) {
            String carOtherNum = "0";
            if(carStr.contains("其他用车")) {
                carStr = carStr.substring(carStr.indexOf("其他用车"));
                carOtherNum = carStr.substring(4, carStr.indexOf("辆"));
            }
            oldData.getAssetVo().setCarOtherNum(carOtherNum);
        }

        //设置与年初预算的变化率
        FinalP3T7ItemDeptOperateVo deptOperateVo = oldData.getDeptOperateVo();
        ChangeValueItemVo changeItemVo = FinalY23Utils.getChangeItemVo( deptOperateVo.getTotal(), deptOperateVo.getBegin());
        deptOperateVo.setChangeItemVo(changeItemVo);

    }

    /**
     * 2023 凭祥与博白的 当经费运行为0时的设置
     * @param dataVo
     */
    private static void updateP3T7P1InfoBsAndPx(FinalExcelDataVo dataVo) {
        String templateExportType = dataVo.getTemplateExportType();
        if( StringUtils.isBlank(templateExportType) || !TemplateEpType.Y2023FT1PX.getCode().equals(templateExportType)
                && !TemplateEpType.Y2023FT4BB.getCode().equals(templateExportType)
                && !TemplateEpType.Y2023FT9LC.getCode().equals(templateExportType)
                && !TemplateEpType.Y2023FT7TDONG.getCode().equals(templateExportType)
                && !TemplateEpType.Y2023FT5GP.getCode().equals(templateExportType)
                && !TemplateEpType.Y2023FT8PG.getCode().equals(templateExportType)) {
            //只有 博白和凭祥, 陆川的设置
            return;
        }
        FinalP3T7Vo oldData = dataVo.getP3T7Vo();

        if("0".equals(oldData.getDeptOperateVo().getTotal())) {
            oldData.getDeptOperateVo().setBegin("0");
        }
        if("0".equals(oldData.getDeptOperateVo().getTotal()) && StringUtils.isBlank(oldData.getDeptOperateVo().getLastYearReason())) {
            oldData.getDeptOperateVo().setLastYearReason("本单位本年度未开展机关运行经费支出情况说明");
        }
    }

}
