package com.gyee.frame.util.excel;

import com.gyee.frame.common.exception.QiNiuException;
import com.gyee.frame.common.exception.enums.QiNiuErrorEnum;
import com.gyee.frame.model.auto.Misdailydata;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.StringUtils;
import com.sun.el.stream.Stream;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * mis日报数据导入
 */
@Slf4j
public class MisDailyUtil {

    static String name = "C:\\Users\\HP\\Desktop\\2022年数据\\2022年8月国电电力MIS每日数据.xlsx";


    /**
     * @return
     * @throws Exception
     */
    public List<MisDailyData> importStream(MultipartFile file) {
        InputStream is = null;
        Workbook wb = null;
        List<MisDailyData> list = new ArrayList<>();
        try {
            String fileName = file.getOriginalFilename(); //读取文件名
            is = file.getInputStream();  //读取文件流

            String year = fileName.substring(0, 4);
            String month = fileName.substring(5, fileName.indexOf("月"));
            Calendar cal = Calendar.getInstance();
            cal.setTime(DateUtils.parseStrtoDate(year + "-" + month + "-01", DateUtils.YYYY_MM_DD));
            int dayMaximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //获取当月最大的一天

            //校验文件类型
            if (checkFile(fileName) == 1) {
                wb = new XSSFWorkbook(is);
            } else if (checkFile(fileName) == 2) {
                wb = new HSSFWorkbook(is);
            } else {
                throw new QiNiuException(QiNiuErrorEnum.FILE_NAME_ERROR);
            }

            Iterator<Sheet> sheets = wb.sheetIterator();
            while (sheets.hasNext()) {
                Sheet sheet = sheets.next(); //读取sheet

                //如果sheet不是日报内容则过滤
                String name = sheet.getSheetName(); //读取sheet名称
                if (StringUtils.isEmpty(name) || !name.contains("日"))
                    continue;
                String day = name.substring(0, name.indexOf("日"));

                for (int i = 8; i <= sheet.getLastRowNum(); i++) {
                    Row row = sheet.getRow(i);  //读取行
                    if (row == null)
                        continue;
                    if (i > 25)
                        break;
                    //sheet有表格但里边没数据
                    if (sheet.getRow(20).getCell(0).getNumericCellValue() <= 0)
                        break;

                    MisDailyData data;
                    //单元格格式不统一，特殊处理
                    if (i == 9 || i == 12 || i == 14) {
                        data = new MisDailyData(row, sheet.getRow(i + 1));
                    } else {
                        data = new MisDailyData(row, null);
                    }

                    if (i == 10 || i == 13 || i == 15)
                        continue;

                    //比如6/31没有这天，系统会转成7/1，需要过滤掉
                    if (Integer.valueOf(day) > dayMaximum)
                        continue;

                    data.setimportdate(DateUtils.parseStrtoDate(year + "-" + month + "-" + day, DateUtils.YYYY_MM_DD));
                    Map<Integer, String> map = projectMap();
                    data.setProjectId(map.containsKey(data.getunitNo()) ? map.get(data.getunitNo()) : "");
                    list.add(data);
                }
            }
        } catch (IndexOutOfBoundsException e) {
            throw new QiNiuException(QiNiuErrorEnum.FILE_NAME_ERROR);
        } catch (Exception e) {
            throw new QiNiuException(QiNiuErrorEnum.FILE_LONG_ERROR);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (wb != null) {
                try {
                    wb.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return list;
    }


    /**
     * 判断File文件的类型
     *
     * @param fileName 传入的文件
     * @return 1-XLS文件，2-XLSX文件，3-文件类型错误
     */
    private int checkFile(String fileName) {
        if (fileName.endsWith(".xlsx")) {
            return 1;
        }
        if (fileName.endsWith(".xls")) {
            return 2;
        }

        return 3;
    }

    private Map<Integer, String> projectMap() {
        Map<Integer, String> map = new HashMap<>();
        map.put(206, "MHS01_GC");
        map.put(310, "XS02_GC");
        map.put(311, "MHS02_GC");
        map.put(312, "SBQ04_GC");
        map.put(313, "SBQ03_GC");
        map.put(434, "QS03_GC");
        map.put(207, "NSS01_GC");
        map.put(208, "NSS02_GC");
        map.put(238, "NSS03_GC");
        map.put(239, "QS01_GC");
        map.put(245, "QS02_GC");
        map.put(263, "SBQ01_GC");
        map.put(286, "SBQ02_GC");
        map.put(291, "XS01_GC");
        map.put(0, "HJ");

        return map;
    }

    public static void main(String[] args) throws Exception {
//        File file = new File(name);
//        String fileName = file.getName();
//
//        InputStream is = new FileInputStream(file);
//
//        List<MisDailyData> list = importStream(fileName, is);
//
//        for (MisDailyData d : list)
//            System.out.println(d.toString());


    }


    /*
     * miss故障
     * */

    public  List<Misdailydata> Stream1(MultipartFile file) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook("C:\\Users\\Administrator\\Desktop\\2022年7月国电电力MIS故障日报.xlsx");


        InputStream is = null;
        Workbook wb = null;

        //获取工作表
        XSSFSheet sheet = workbook.getSheetAt(0);


        String fileName = file.getOriginalFilename(); //读取文件名
        is = file.getInputStream();  //读取文件流

        String year = fileName.substring(0, 4);
        String month = fileName.substring(5, fileName.indexOf("月"));
        Calendar cal = Calendar.getInstance();
        cal.setTime(DateUtils.parseStrtoDate(year + "-" + month + "-01", DateUtils.YYYY_MM_DD));

        //校验文件类型
        if (checkFile(fileName) == 1) {
            wb = new XSSFWorkbook(is);
        } else if (checkFile(fileName) == 2) {
            wb = new HSSFWorkbook(is);
        } else {
            throw new QiNiuException(QiNiuErrorEnum.FILE_NAME_ERROR);
        }


        Row row1 = sheet.getRow(0);
//读取场站名
        String m1 = String.valueOf(row1.getCell(1).getStringCellValue()).trim();
        String m2 = String.valueOf(row1.getCell(4).getStringCellValue()).trim();
        String q1 = String.valueOf(row1.getCell(7).getStringCellValue()).trim();
        String q2 = String.valueOf(row1.getCell(10).getStringCellValue()).trim();
        String n1 = String.valueOf(row1.getCell(13).getStringCellValue()).trim();
        String n2 = String.valueOf(row1.getCell(16).getStringCellValue()).trim();
        String n3 = String.valueOf(row1.getCell(19).getStringCellValue()).trim();
        String s1 = String.valueOf(row1.getCell(22).getStringCellValue()).trim();
        String s2 = String.valueOf(row1.getCell(25).getStringCellValue()).trim();
        String s3 = String.valueOf(row1.getCell(28).getStringCellValue()).trim();
        String s4 = String.valueOf(row1.getCell(31).getStringCellValue()).trim();
        String x1 = String.valueOf(row1.getCell(34).getStringCellValue()).trim();
        String x2 = String.valueOf(row1.getCell(37).getStringCellValue()).trim();
        String q3 = String.valueOf(row1.getCell(40).getStringCellValue()).trim();
        String zhj = String.valueOf(row1.getCell(43).getStringCellValue()).trim();

        List<Misdailydata> jh =new ArrayList<>();;
        for (int i = 2; i < sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            if (i > 32)
                break;
            //读取数据
            int day1 = Double.valueOf(row.getCell(0).getNumericCellValue()).intValue();
            int mhs1 = Double.valueOf(row.getCell(1).getNumericCellValue()).intValue();
            int mhs2 = Double.valueOf(row.getCell(4).getNumericCellValue()).intValue();
            int qs1 = Double.valueOf(row.getCell(7).getNumericCellValue()).intValue();
            int qs2 = Double.valueOf(row.getCell(10).getNumericCellValue()).intValue();
            int nss1 = Double.valueOf(row.getCell(13).getNumericCellValue()).intValue();
            int nss2 = Double.valueOf(row.getCell(16).getNumericCellValue()).intValue();
            int nss3 = Double.valueOf(row.getCell(19).getNumericCellValue()).intValue();
            int sbq1 = Double.valueOf(row.getCell(22).getNumericCellValue()).intValue();
            int sbq2 = Double.valueOf(row.getCell(25).getNumericCellValue()).intValue();
            int sbq3 = Double.valueOf(row.getCell(28).getNumericCellValue()).intValue();
            int sbq4 = Double.valueOf(row.getCell(31).getNumericCellValue()).intValue();
            int xs1 = Double.valueOf(row.getCell(34).getNumericCellValue()).intValue();
            int xs2 = Double.valueOf(row.getCell(37).getNumericCellValue()).intValue();
            int qs3 = Double.valueOf(row.getCell(40).getNumericCellValue()).intValue();
            int zhj1 = Double.valueOf(row.getCell(43).getNumericCellValue()).intValue();

            Date date = DateUtils.parseStrtoDate(year + "-" + month + "-" + day1, DateUtils.YYYY_MM_DD);
            Misdailydata data1 = new Misdailydata();
            data1.setName(m1);
            data1.setProjectid(projectMap1().get(m1));
            data1.setGzcs(mhs1);
            data1.setImportdate(date);

            Misdailydata data2 = new Misdailydata();
            data2.setName(m2);
            data2.setProjectid(projectMap1().get(m2));
            data2.setGzcs(mhs2);
            data2.setImportdate(date);

            Misdailydata data3 = new Misdailydata();
            data3.setName(n1);
            data3.setProjectid(projectMap1().get(n1));
            data3.setGzcs(nss1);
            data3.setImportdate(date);

            Misdailydata data4 = new Misdailydata();
            data4.setName(n2);
            data4.setProjectid(projectMap1().get(n2));
            data4.setGzcs(nss2);
            data4.setImportdate(date);

            Misdailydata data5 = new Misdailydata();
            data5.setName(n3);
            data5.setProjectid(projectMap1().get(n3));
            data5.setGzcs(nss3);
            data5.setImportdate(date);

            Misdailydata data6 = new Misdailydata();
            data6.setName(q1);
            data6.setProjectid(projectMap1().get(q1));
            data6.setGzcs(qs1);
            data6.setImportdate(date);

            Misdailydata data7 = new Misdailydata();
            data7.setName(q2);
            data7.setProjectid(projectMap1().get(q2));
            data7.setGzcs(qs2);
            data7.setImportdate(date);

            Misdailydata data8 = new Misdailydata();
            data8.setName(q3);
            data8.setProjectid(projectMap1().get(q3));
            data8.setGzcs(qs3);
            data8.setImportdate(date);

            Misdailydata data9 = new Misdailydata();
            data9.setName(s1);
            data9.setProjectid(projectMap1().get(s1));
            data9.setGzcs(sbq1);
            data9.setImportdate(date);

            Misdailydata data10 = new Misdailydata();
            data10.setName(s2);
            data10.setProjectid(projectMap1().get(s2));
            data10.setGzcs(sbq2);
            data10.setImportdate(date);

            Misdailydata data11 = new Misdailydata();
            data11.setName(s3);
            data11.setProjectid(projectMap1().get(s3));
            data11.setGzcs(sbq3);
            data11.setImportdate(date);

            Misdailydata data12 = new Misdailydata();
            data12.setName(s4);
            data12.setProjectid(projectMap1().get(s4));
            data12.setGzcs(sbq4);
            data12.setImportdate(date);

            Misdailydata data13 = new Misdailydata();
            data13.setName(x1);
            data13.setProjectid(projectMap1().get(x1));
            data13.setGzcs(xs1);
            data13.setImportdate(date);

            Misdailydata data14 = new Misdailydata();
            data14.setName(x2);
            data14.setProjectid(projectMap1().get(x2));
            data14.setGzcs(xs2);
            data14.setImportdate(date);

            Misdailydata data15 = new Misdailydata();
            data15.setName(zhj);
            data15.setProjectid(projectMap1().get(zhj));
            data15.setGzcs(zhj1);
            data15.setImportdate(date);

//            jh.add(data);
            jh.add(data1);
            jh.add(data2);
            jh.add(data3);
            jh.add(data4);
            jh.add(data5);
            jh.add(data6);
            jh.add(data7);
            jh.add(data8);
            jh.add(data9);
            jh.add(data10);
            jh.add(data11);
            jh.add(data12);
            jh.add(data13);
            jh.add(data14);
            jh.add(data15);
        }

//        workbook.close();
        jh.forEach(ss->
                ss.setName(projectMap2().get(ss.getProjectid()))
                );
        return jh;
    }

    private Map<String, String> projectMap1() {
        Map<String, String> map = new HashMap<>();
        map.put("麻一", "MHS01_GC");
        map.put("麻分散", "MHS02_GC");
        map.put("青一", "QS01_GC");
        map.put("青二", "QS02_GC");
        map.put("牛一", "NSS01_GC");
        map.put("牛二", "NSS02_GC");
        map.put("牛三", "NSS03_GC");
        map.put("石一", "SBQ01_GC");
        map.put("石二", "SBQ02_GC");
        map.put("石三", "SBQ03_GC");
        map.put("石四", "SBQ04_GC");
        map.put("香一", "XS01_GC");
        map.put("香二", "XS02_GC");
        map.put("青三", "QS03_GC");
        map.put("合计", "HJ");
        return map;


    }
    private Map<String, String> projectMap2() {
        Map<String, String> map = new HashMap<>();
        map.put("MHS01_GC", "麻黄山风电场");
        map.put("MHS02_GC", "麻黄山分散式项目");
        map.put("QS01_GC", "青山一期");
        map.put("QS02_GC", "青山二期");
        map.put("NSS01_GC", "牛首山一期");
        map.put("NSS02_GC", "牛首山二期");
        map.put("NSS03_GC", "牛首山三期");
        map.put("SBQ01_GC", "石板泉风电");
        map.put("SBQ02_GC", "石板泉风电二期");
        map.put("SBQ03_GC", "太阳山项目(石板泉三期)");
        map.put("SBQ04_GC", "太阳山风光储(石板泉四期)");
        map.put("XS01_GC", "宁夏香山砖瓦子淌");
        map.put("XS02_GC", "宁夏韩家圈(香山二期)");
        map.put("QS03_GC", "青山三期");
        map.put("HJ", "合计");
        return map;
    }
}
