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

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.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.yunyan.business.document.EasyExcelFinalListener;
import com.yunyan.business.document.utils.ReadExcelUtil;
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.business.document.y23.budget.pojo.budgetY23.BudgetT6ItemVo;
import com.yunyan.business.document.y23.budget.pojo.budgetY23.sheetData.BudgetP3p10p4NoDeptItemVo;
import com.yunyan.business.document.y23.budget.pojo.budgetY23.sheetData.BudgetP3p10p4NoDeptVo;
import com.yunyan.common.exception.ServiceException;
import com.yunyan.common.utils.StringUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * 预算表 表6 合成 的读取
 */
@Slf4j
public class ReadBudgetFileJsonDataUtils {


    private static void readNoInDeptP3p10BudgetDataTest() {
        readNoInDeptP3p10BudgetData("101", "桂平", "2024");
    }

    /**
     * 获取第10部分 是否开展单位的自评数据, 不开展返回 true, 开展返回 false
     * @param deptCode
     * @return
     */
    public static boolean readNoInDeptP3p10BudgetData( String year, String area, String deptCode) {

        if(StringUtils.isBlank(area)) {
            return false;
        }

        String fileName = "p3p10NoDept.budget.json";

        List<BudgetP3p10p4NoDeptVo> areaList = new ArrayList<>();
        InputStream templateInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("tempData/" + fileName);
        if(null == templateInputStream) {
            log.error("读取 resouse的文件 异常: [{}]", fileName);
            throw new RuntimeException("读取 resouse的文件 异常: " + fileName);
        }
        String jsonTableContent = IoUtil.readUtf8(templateInputStream);
        if(StringUtils.isNotBlank(jsonTableContent)) {
            areaList = JSONUtil.toList(jsonTableContent, BudgetP3p10p4NoDeptVo.class);
            if(areaList.isEmpty()) {
                return false;
            }

            BudgetP3p10p4NoDeptVo areaItem = areaList.stream().filter(i->i.getArea().equals(area) && i.getYear().equals(year)).findFirst().orElse(null);
            if(null == areaItem || areaItem.getList().isEmpty()) {
                return false;
            }

            BudgetP3p10p4NoDeptItemVo deptItem = areaItem.getList().stream().filter(i->i.getDeptCode().equals(deptCode)).findFirst().orElse(null);
            if(null != deptItem) {
                return true;
            }
        }

        return false;
    }

}
