package com.gyee.frame.service.export;

import com.gyee.frame.model.config.FilePathConfig;
import com.gyee.frame.model.excel.LoadrateHourly;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoadrateHourlyService {

    private Map<Integer, String> ptMap;
    private Map<String, Integer> tpMap;

    public Map<Integer, String> getPtMap() {
        if (ptMap == null) {
            ptMap = new HashMap<>(5);
            ptMap.put(0, "PL_GDC");
            ptMap.put(1, "DWK_GDC");
            ptMap.put(2, "XH_GDC");
            ptMap.put(3, "MCH_GDC");
            ptMap.put(4, "HZJ_GDC");
            ptMap.put(5, "AK_GDC");
        }
        return ptMap;
    }

    public Map<String, Integer> getTpMap() {
        if (tpMap == null) {
            tpMap = new HashMap<>(5);
            tpMap.put("PL_GDC", 0);
            tpMap.put("DWK_GDC", 1);
            tpMap.put("XH_GDC", 2);
            tpMap.put("MCH_GDC", 3);
            tpMap.put("HZJ_GDC", 4);
            tpMap.put("AK_GDC", 5);
        }
        return tpMap;
    }

    public File getPath(String date, String time) {
        String stime = date.replaceAll("-", "");
        String path = FilePathConfig.getDataPath() + "\\光伏逆变器负荷率利用小时计算\\" + stime + time + ".xlsx";
        return new File(path);
    }

    public Map<String, File> getAllPath(String time) {
        Map<String, File> fileMap = new HashMap<>();
        File file1 = getPath(time, "0930");
        if (file1.exists()) fileMap.put("0930", file1);
        File file2 = getPath(time, "1230");
        if (file2.exists()) fileMap.put("1230", file2);
        File file3 = getPath(time, "1830");
        if (file3.exists()) fileMap.put("1830", file3);
        File file4 = getPath(time, "2200");
        if (file4.exists()) fileMap.put("2200", file4);
        return fileMap;
    }

    private Map<String, List<LoadrateHourly>> sheets2LoadrateHourly(File file) {
        XSSFWorkbook sheets = null;
        try {
            sheets = new XSSFWorkbook(file);
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        }

        getPtMap();
        Map<String, List<LoadrateHourly>> loadrateHourlysMap = new HashMap<>();
        //表
        for (int i = 0; i < 5; i++) {
            XSSFSheet sheetAt = sheets.getSheetAt(i);
            List<LoadrateHourly> loadrateHourlys = new ArrayList<>();
            //行
            for (int i1 = 0; i1 < sheetAt.getLastRowNum() - 1; i1++) {
                XSSFRow row = sheetAt.getRow(i1 + 2);
                LoadrateHourly lrh = new LoadrateHourly();
                lrh.setId(getCellValue(row.getCell(0)));
                lrh.setBranchcount(getCellValue(row.getCell(1)));
                lrh.setPlatecount(getCellValue(row.getCell(2)));
                lrh.setPlatecapacity(getCellValue(row.getCell(3)));
                lrh.setTime(getCellValue(row.getCell(4)));
                lrh.setPower(getCellValue(row.getCell(5)));
                lrh.setLoadfactor(getCellValue(row.getCell(6)));
                lrh.setGeneratedenergy(getCellValue(row.getCell(7)));
                lrh.setUtilizationhour(getCellValue(row.getCell(8)));
                loadrateHourlys.add(lrh);
            }
            loadrateHourlysMap.put(ptMap.get(i), loadrateHourlys);
        }
        return loadrateHourlysMap;
    }

    public String getCellValue(Cell cell) {
        if (cell == null) return "";
        String cellValue = "";
        //判断数据的类型
        switch (cell.getCellType()) {
            case NUMERIC: //数字
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            case STRING: //字符串
                cellValue = String.valueOf(cell.getStringCellValue());
                break;
            case BOOLEAN: //Boolean
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case FORMULA: //公式
                cellValue = String.format("%.2f", cell.getNumericCellValue());
                break;
            default:
                break;
        }
        return cellValue;
    }

    public Map<String, Map<String, List<LoadrateHourly>>> readExcel(String time) {
        Map<String, Map<String, List<LoadrateHourly>>> lrhs = new HashMap<>();
        Map<String, File> allPath = getAllPath(time);
        for (Map.Entry<String, File> stringFileEntry : allPath.entrySet()) {
            Map<String, List<LoadrateHourly>> stringListMap = sheets2LoadrateHourly(stringFileEntry.getValue());
            lrhs.put(stringFileEntry.getKey(), stringListMap);
        }
        return lrhs;
    }

    public List<LoadrateHourly> readExcel(String stationid, String date, String time) {
        File file = getPath(date, time);
        List<LoadrateHourly> stringListMap = null;
        if (file.exists()) {
            stringListMap = sheets2LoadrateHourly(stationid, file, time);
        }
        return stringListMap;
    }

    private List<LoadrateHourly> sheets2LoadrateHourly(String stationid, File file, String time) {
        XSSFWorkbook sheets = null;
        try {
            sheets = new XSSFWorkbook(file);
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        }
        getPtMap();
        getTpMap();
        XSSFSheet sheetAt = sheets.getSheetAt(tpMap.get(stationid));
        List<LoadrateHourly> loadrateHourlys = new ArrayList<>();
        if (time.equals("2200")) {
            for (int i1 = 0; i1 < sheetAt.getLastRowNum() - 1; i1++) {
                XSSFRow row = sheetAt.getRow(i1 + 2);
                LoadrateHourly lrh = new LoadrateHourly();
                lrh.setId(getCellValue(row.getCell(0)));
                lrh.setBranchcount(getCellValue(row.getCell(1)));
                lrh.setPlatecount(getCellValue(row.getCell(2)));
                lrh.setPlatecapacity(getCellValue(row.getCell(3)));
                lrh.setTime(getCellValue(row.getCell(4)));
                lrh.setGeneratedenergy(getCellValue(row.getCell(5)));
                lrh.setUtilizationhour(getCellValue(row.getCell(6)));
                loadrateHourlys.add(lrh);
            }
        } else {
            for (int i1 = 0; i1 < sheetAt.getLastRowNum() - 1; i1++) {
                XSSFRow row = sheetAt.getRow(i1 + 2);
                LoadrateHourly lrh = new LoadrateHourly();
                lrh.setId(getCellValue(row.getCell(0)));
                lrh.setBranchcount(getCellValue(row.getCell(1)));
                lrh.setPlatecount(getCellValue(row.getCell(2)));
                lrh.setPlatecapacity(getCellValue(row.getCell(3)));
                lrh.setTime(getCellValue(row.getCell(4)));
                lrh.setPower(getCellValue(row.getCell(5)));
                lrh.setLoadfactor(getCellValue(row.getCell(6)));
                loadrateHourlys.add(lrh);
            }
        }
        return loadrateHourlys;
    }
}
