package com.post.excel;

import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

/**
 * 解析岗检表Excel表
 */
@Slf4j
public class ExcelParsePostCheckTest {

    /**
     * 解析到的Excel数据
     */
    private static List<ExcelParseBean> beanList = null;


    /**
     * 插入数据的数据：清单表
     */
    public static final Map<String, String> data = new TreeMap<>();
    /**
     * 插入数据的数据：清单明细表
     */
    public static final List<Map<String, String>> dataList = new ArrayList<>();
    /**
     * 记录表格所有的类型
     */
    public static final Map<String, Integer> typeMap = new HashMap<>();

    static {
        //类型（1-业务职责；2-党建及党风廉政建设职责；3-安全环保职责；4-合规保密职；5-任职资格与能力素质要求）
        typeMap.put("业务职责有效性", 1);
        typeMap.put("党建及党风廉政建设职责有效性", 2);
        typeMap.put("安全环保职责有效性", 3);
        typeMap.put("合规与保密职责有效性", 4);
        typeMap.put("任职资格与能力素质要求符合性", 5);
        typeMap.put("岗位责任制文件完整性与适宜性", 6);

    }

    /**
     * 最大的长度：[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 16, 17, 18, 19, 22, 23, 28, 30, 31, 39, 40, 41, 42, 43, 48, 51, 59, 60, 67, 81, 110, 114, 115, 124, 138, 139, 186, 203, 226, 297]
     */
    public static TreeSet<Integer> maxLengSet = new TreeSet<>();

    public static void main(String[] args) throws Exception {

        File file = new File("C:\\test\\postcheck\\2、岗位责任制检查表（模板）1225-方联殷V2.xlsx");
        InputStream inputStream = new FileInputStream(file);
        ExcelUtilsPostcheck excelUtil = new ExcelUtilsPostcheck(ExcelParseBean.class);
        beanList = excelUtil.importExcel(inputStream, -1);
//        List<ExcelBean> beanList = excelUtils.importExcel(null,inputStream,3);
        log.info("=====================================解析到 {} 行=====================================", beanList.size());
        System.out.println();

        for (int i = 0; i < beanList.size(); i++) {
            ExcelParseBean row = beanList.get(i);

            String cell_1 = row.getCell_1();
            String cell_2 = row.getCell_2();
            String cell_3 = row.getCell_3();
            String cell_4 = row.getCell_4();
            String cell_5 = row.getCell_5();
            String cell_6 = row.getCell_6();
            String cell_7 = row.getCell_7();
//            log.info("第 {} 行数据\t{}\t{}\t{}\t{}\t{}\t{}\t{}", i + 1, cell_1, cell_2, cell_3, cell_4, cell_5, cell_6, cell_7);

            if (!parseRowData(i, row)) {
                break;
            }
        }

        System.out.println();
        log.info("======================================解析到结束 {} 行=================================", beanList.size());
        log.info("======================================打印收集到的数据，清单明细行数 {} 行=================================", dataList.size());

        log.info("清单数据：" + data);
        for (int i = 0; i < dataList.size(); i++) {
            Map<String, String> dataInfo = dataList.get(i);
            log.info("明细 {}\t{}", i + 1, dataInfo);
        }
        log.info("======================================打印收集到的数据结束=================================");

        System.out.println("maxLengSet = " + maxLengSet);
    }

    /**
     * 按行解析数据
     *
     * @param rowNum 数组角标（从0开始，Excel表格视觉行号是：1、2、3、……）
     * @param row    该行数据
     * @return true：继续解析；false：终止解析；
     */
    public static boolean parseRowData(int rowNum, ExcelParseBean row) throws Exception {
        if (rowNum == 1 - 1) {
            parseRowDataOne(row);
        } else if (rowNum == 2 - 1) {
            parseRowDataTwo(row);
        } else if (rowNum == 3 - 1) {
//            parseRowDataThree(rowData);
        } else if (rowNum == 4 - 1) {
            parseRowDataFour(row);
        } else if (rowNum == 5 - 1) {
            parseRowData(row);
        }
        //大于14的行：已解析过
        else {
            return false;
        }
        return true;
    }


    /**
     * 当前解析的行号为：1
     * 标题
     *
     * @param row
     */
    public static void parseRowDataOne(ExcelParseBean row) throws Exception {
        String cell_1 = row.getCell_1();
        String cell_2 = row.getCell_2();
        String cell_3 = row.getCell_3();
        String cell_4 = row.getCell_4();
        String cell_5 = row.getCell_5();
        String cell_6 = row.getCell_6();
        String cell_7 = row.getCell_7();

        data.put("1岗检表", cell_1);
    }

    /**
     * 当前解析的行号为：
     *
     * @param row
     */
    public static void parseRowDataTwo(ExcelParseBean row) throws Exception {
        String cell_1 = row.getCell_1();
        String cell_2 = row.getCell_2();
        String cell_3 = row.getCell_3();
        String cell_4 = row.getCell_4();
        String cell_5 = row.getCell_5();
        String cell_6 = row.getCell_6();
        String cell_7 = row.getCell_7();
        log.info("检查时间：{} 检查部门及岗位：{}", cell_3, cell_5);

    }


    /**
     * 当前解析的行号为：
     *
     * @param row
     */
    public static void parseRowDataFour(ExcelParseBean row) throws Exception {
        String cell_1 = row.getCell_1();
        String cell_2 = row.getCell_2();
        String cell_3 = row.getCell_3();
        String cell_4 = row.getCell_4();
//        String cell_5 = row.getCell_5();
        String cell_6 = row.getCell_6();
        String cell_7 = row.getCell_7();
        if (StringUtils.isBlank(cell_1)
                || StringUtils.isBlank(cell_2) || StringUtils.isBlank(cell_3)
                || StringUtils.isBlank(cell_4) || StringUtils.isBlank(cell_6)
                || StringUtils.isBlank(cell_7)) {
            throw new BaseException("文件第四行错误，请检查！（序号、检查项目、检查内容、检查标准（D列E列应为合并单元格）、检查方法、检查结果）");
        }
        if (!"序号".equalsIgnoreCase(cell_1)
                || !"检查项目".equalsIgnoreCase(cell_2)
                || !"检查内容".equalsIgnoreCase(cell_3)
                || !"检查标准".equalsIgnoreCase(cell_4)
                || !"检查方法".equalsIgnoreCase(cell_6)
                || !"检查结果".equalsIgnoreCase(cell_7)
        ) {

            throw new BaseException("文件第四行错误，请检查！（序号、检查项目、检查标准、检查方法、检查结果）");
        }

    }


    /**
     * 当前解析的行号为：>= 5
     * 第5行以下都是数据，一直解析到底
     *
     * @param row
     */
    public static void parseRowData(ExcelParseBean row) throws Exception {
        int index = beanList.indexOf(row);
        if (index == beanList.size() - 1) {
            log.info("文件最后一行，解析结束！index = {} row = {}", index, row);
            return;
        }
        //开始解析数据
        String last_cell_2 = null;
        for (int i = index; i < beanList.size(); i++) {
            ExcelParseBean thisRow = beanList.get(i);

            String cell_1 = thisRow.getCell_1();//序号
            String cell_2 = thisRow.getCell_2();//检查项目
            String cell_3 = thisRow.getCell_3();//检查内容
            String cell_4 = thisRow.getCell_4();//检查标准

            String cell_6 = thisRow.getCell_6();//检查方法
            String cell_7 = thisRow.getCell_7();//检查结果


            Map<String, String> rowData = new TreeMap<>();
            //如果为空使用上一行的值
            if (StringUtils.isBlank(cell_2)) {
                cell_2 = last_cell_2;
            }
            //记录当前行的值
            else {
                last_cell_2 = cell_2;

            }
            Integer type = typeMap.get(cell_2);
            if (type == null) {
                throw new BaseException("文件第" + (i + 1) + "行B列检查项目解析失败，请检查！【" + cell_2 + "】");
            }

            //前四项不能出现空值，第二项有合并单元格，取上一次的值后也不会有空值
            if (StringUtils.isBlank(cell_1) || StringUtils.isBlank(cell_2) || StringUtils.isBlank(cell_3) || StringUtils.isBlank(cell_4)) {
                throw new BaseException("文件第" + (i + 1) + "行ABCD列解析失败，请检查！【" + cell_2 + "】");
            }

            rowData.put("0序号", String.valueOf(type));
            rowData.put("1序号", cell_1);
            rowData.put("2检查项目", cell_2);
            rowData.put("3检查内容", cell_3);
            rowData.put("4检查标准", cell_4);
            rowData.put("5检查方法", cell_6);
            rowData.put("6检查结果", cell_7);
            dataList.add(rowData);

            maxLengSet.add(cell_1.length());
            maxLengSet.add(cell_2.length());
            maxLengSet.add(cell_3.length());
            maxLengSet.add(cell_4.length());
            maxLengSet.add(cell_6.length());
            maxLengSet.add(cell_7.length());
        }
    }

}
