package com.yunyan.business.document.y23.budget.other;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.json.JSONUtil;
import com.yunyan.business.document.y23.budget.ReadBudT5T8ExcelY23Utils;
import com.yunyan.business.document.y23.budget.ReadBudgetExcelT6Y22Utils;
import com.yunyan.business.document.y23.budget.pojo.budgetY23.BudgetDataVo;
import com.yunyan.business.document.y23.budget.pojo.budgetY23.BudgetP3p5SubDeptVo;
import com.yunyan.business.document.y23.budget.pojo.budgetY23.BudgetP3p8DeptVo;
import com.yunyan.business.document.y23.budget.pojo.budgetY23.BudgetT6DeptVo;
import com.yunyan.common.utils.StringUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 废弃
 */
@Slf4j
public class ReadBudgetTable23Utils {

    /**
     * 读取 旧数据 更新
     * 读取 p2p5 excel 读取预算表  表5数据
     */
    @SneakyThrows
    public static void testReadExcelP2p5Vo2(String fileName, String tofileName, String byFileName) {
        BudgetDataVo finalExcelLastYearDataVo = ReadBudT5T8ExcelY23Utils.getSheetT5OrT8Infos(null, fileName, 0, 5);

        //BudgetP2p5Vo p2p5Vo = finalExcelLastYearDataVo.getP2p5Vo();
        List<BudgetP3p5SubDeptVo> subDeptList = finalExcelLastYearDataVo.getExplainVo().getP3p5Vo().getSubDeptList();


        List<BudgetP3p5SubDeptVo> newListUpdate = new ArrayList<>();

        //String fileName2 = "y2023/baise/y2023.baise.budget.t5.txt";
        String fileName2 = byFileName;
        InputStream templateInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("tempData/" + fileName2);
        if(null == templateInputStream) {
            log.error("读取 resouse的文件 异常: [{}]", fileName);
            throw new RuntimeException("读取 resouse的文件 异常: " + fileName);
        }
        String jsonTableContent2 = IoUtil.readUtf8(templateInputStream);
        if(StringUtils.isNotBlank(jsonTableContent2)) {
            List<BudgetP3p5SubDeptVo> listVo = JSONUtil.toList(jsonTableContent2, BudgetP3p5SubDeptVo.class);

            //if(!listVo.isEmpty()) {
            for (BudgetP3p5SubDeptVo oldDeptVo : listVo) {
                String deptCode = oldDeptVo.getDeptCode();
                BudgetP3p5SubDeptVo newChangeToDeptVo = subDeptList.stream().filter(i->i.getDeptCode().equals(deptCode)).findFirst().orElse(null);
                if(null != newChangeToDeptVo) {
                    //有就改变
                    newListUpdate.add(newChangeToDeptVo);
                } else {
                    //没有就保留原来的
                    newListUpdate.add(oldDeptVo);
                }
            }

            for (BudgetP3p5SubDeptVo newChangeToDeptVo : subDeptList) {
                String deptCode = newChangeToDeptVo.getDeptCode();
                BudgetP3p5SubDeptVo oldDeptVo = newListUpdate.stream().filter(i -> i.getDeptCode().equals(deptCode)).findFirst().orElse(null);
                if (null == oldDeptVo) {
                    log.debug("没有找到部门 [{}]", deptCode);
                    //没有就新增
                    newListUpdate.add(newChangeToDeptVo);
                    //throw new RuntimeException("没有找到部门 " + deptCode);
                }
            }

            String jsonTableContent = JSONUtil.toJsonStr(newListUpdate);

            try {
                File file = ResourceUtils.getFile(tofileName);
                FileUtil.writeString(jsonTableContent, file, CharsetUtil.UTF_8);
            } catch (FileNotFoundException e) {
                log.debug("文件异常 [{}]", e);
            }

            log.debug("json [{}]", jsonTableContent);
        }


    }

    @SneakyThrows
    public static void testReadExcelP2p5Vo(String fileName, String tofileName) {
        BudgetDataVo finalExcelLastYearDataVo = ReadBudT5T8ExcelY23Utils.getSheetT5OrT8Infos(null, fileName, 0, 5);

        //BudgetP2p5Vo p2p5Vo = finalExcelLastYearDataVo.getP2p5Vo();
        List<BudgetP3p5SubDeptVo> subDeptList = finalExcelLastYearDataVo.getExplainVo().getP3p5Vo().getSubDeptList();
        String jsonTableContent = JSONUtil.toJsonStr(subDeptList);

        try {
            File file = ResourceUtils.getFile(tofileName);
            FileUtil.writeString(jsonTableContent, file, CharsetUtil.UTF_8);
        } catch (FileNotFoundException e) {
            log.debug("文件异常 [{}]", e);
        }

        log.debug("json [{}]", jsonTableContent);
    }

    /**
     * 预算 表8读取 读取 p2p8 excel, 保存
     */
    @SneakyThrows
    public static void testReadExcelP2p8Vo(String fileName, String tofileName) {
        BudgetDataVo finalExcelLastYearDataVo = ReadBudT5T8ExcelY23Utils.getSheetT5OrT8Infos(null, fileName, 0, 8);

        //BudgetP2p5Vo p2p5Vo = finalExcelLastYearDataVo.getP2p5Vo();
        List<BudgetP3p8DeptVo> subDeptList = finalExcelLastYearDataVo.getExplainVo().getP3p8Vo().getDeptList();
        String jsonTableContent = JSONUtil.toJsonStr(subDeptList);

        try {
            File file = ResourceUtils.getFile(tofileName);
            FileUtil.writeString(jsonTableContent, file, CharsetUtil.UTF_8);
        } catch (FileNotFoundException e) {
            log.debug("文件异常 [{}]", e);
        }
    }

    /**
     * t6保存
     */
    @SneakyThrows
    public static void testBudgetT6SaveData(String fileName, String tofileName) {
        List<BudgetT6DeptVo> list = ReadBudgetExcelT6Y22Utils.getSheetInfos(null, fileName);
        ReadBudgetExcelT6Y22Utils.saveToFile(list, tofileName);
    }

    @SneakyThrows
    public static void testBudgetT6SaveData2(String fileName, String tofileName, String byFileName) {
        List<BudgetT6DeptVo> list = ReadBudgetExcelT6Y22Utils.getSheetInfos(null, fileName);

        List<BudgetT6DeptVo> newListUpdate = new ArrayList<>();

        //String fileName2 = "y2023/baise/y2023.baise.budget.t5.txt";
        InputStream templateInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("tempData/" + byFileName);
        if(null == templateInputStream) {
            log.error("读取 resouse的文件 异常: [{}]", fileName);
            throw new RuntimeException("读取 resouse的文件 异常: " + fileName);
        }
        String jsonTableContent2 = IoUtil.readUtf8(templateInputStream);

        if(StringUtils.isBlank(jsonTableContent2)) {
            log.error("读取 resouse的文件 为空: [{}]", fileName);
            return;
        }

        List<BudgetT6DeptVo> listVo = JSONUtil.toList(jsonTableContent2, BudgetT6DeptVo.class);

        //if(!listVo.isEmpty()) {
        for (BudgetT6DeptVo oldDeptVo : listVo) {
            String deptCode = oldDeptVo.getDeptCode();
            BudgetT6DeptVo newChangeToDeptVo = list.stream().filter(i->i.getDeptCode().equals(deptCode)).findFirst().orElse(null);
            if(null != newChangeToDeptVo) {
                //有就改变
                newListUpdate.add(newChangeToDeptVo);
            } else {
                //没有就保留原来的
                newListUpdate.add(oldDeptVo);
            }
        }

        for (BudgetT6DeptVo newChangeToDeptVo : list) {
            String deptCode = newChangeToDeptVo.getDeptCode();
            BudgetT6DeptVo oldDeptVo = newListUpdate.stream().filter(i -> i.getDeptCode().equals(deptCode)).findFirst().orElse(null);
            if (null == oldDeptVo) {
                log.debug("没有找到部门 [{}]", deptCode);
                //没有就新增
                newListUpdate.add(newChangeToDeptVo);
                throw new RuntimeException("没有找到部门 " + deptCode);
            }
        }

        ReadBudgetExcelT6Y22Utils.saveToFile(newListUpdate, tofileName);

    }

}
