package io.renren.modules.test;

import io.renren.modules.test.entity.ElecDashboardData;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 电厂大屏抓取数据测试类（.xls/.xlsx文件）
 *
 * @author: Wang Zi Wen
 * @email: wangggziwen@163.com
 * @datetime: 2022/8/30 9:22
 */
public class TestPullDataExcel {

    public static void main(String[] args) {
        TestPullDataExcel testPullDataExcel = new TestPullDataExcel();
        testPullDataExcel.getExcelData();
    }

    /**
     * 抓取excel数据
     */
    private void getExcelData() {
        // excel对象
        Workbook workbook = null;
        try {
            // 文件读取路径
            String filePath = "C://elecdashboard/elecDashboardData.xlsx";
            // 文件输入流
            InputStream inputStream = new FileInputStream(filePath);
            // 根据文件后缀名，决定excel（workbook）对象的具体实现
            if (filePath.substring(filePath.lastIndexOf(".") + 1).equals("xls")){
                workbook = new HSSFWorkbook(inputStream);
            }else if (filePath.substring(filePath.lastIndexOf(".") + 1).equals("xlsx")){
                workbook = new XSSFWorkbook(inputStream);
            }
            // 根据sheet index，获取sheet对象
            Sheet sheet1 = workbook.getSheetAt(0);  // 数据监控
            Sheet sheet2 = workbook.getSheetAt(1);  // 蒸汽平衡
            Sheet sheet3 = workbook.getSheetAt(2);  // 环保监控
            // 电厂大屏数据对象
            ElecDashboardData elecDashboardData = new ElecDashboardData();
            //========================================电厂大屏数据第一页：数据监控========================================
            // 发电机
            String gtg1PowerGenLevel = this.getCellData(sheet1, 2, 2);  // 1#GTG发电水平
            String gtg1Efficiency = this.getCellData(sheet1, 3, 2);     // 1#GTG效率（百分比）
            String gtg2PowerGenLevel = this.getCellData(sheet1, 2, 3);  // 2#GTG发电水平
            String gtg2Efficiency = this.getCellData(sheet1, 3, 3);     // 2#GTG效率（百分比）
            String gtg3PowerGenLevel = this.getCellData(sheet1, 2, 4);  // 3#GTG发电水平
            String gtg3Efficiency = this.getCellData(sheet1, 3, 4);     // 3#GTG效率（百分比）
            String stgPowerGenLevel = this.getCellData(sheet1, 2, 5);   // STG发电水平
            String stgEfficiency = this.getCellData(sheet1, 3, 5);      // STG效率（百分比）
            String totalPowerGen = this.getCellData(sheet1, 2, 6);      // 总发电量
            elecDashboardData.setGtg1PowerGenLevel(gtg1PowerGenLevel);
            elecDashboardData.setGtg1Efficiency(gtg1Efficiency);
            elecDashboardData.setGtg2PowerGenLevel(gtg2PowerGenLevel);
            elecDashboardData.setGtg2Efficiency(gtg2Efficiency);
            elecDashboardData.setGtg3PowerGenLevel(gtg3PowerGenLevel);
            elecDashboardData.setGtg3Efficiency(gtg3Efficiency);
            elecDashboardData.setStgPowerGenLevel(stgPowerGenLevel);
            elecDashboardData.setStgEfficiency(stgEfficiency);
            elecDashboardData.setTotalPowerGen(totalPowerGen);
            // 蒸汽
            String steamHhp = this.getCellData(sheet1, 6, 2);    // 蒸汽HHP
            String steamHp = this.getCellData(sheet1, 6, 3);     // 蒸汽HP
            String steamMp = this.getCellData(sheet1, 6, 4);     // 蒸汽MP
            String steamLp = this.getCellData(sheet1, 6, 5);     // 蒸汽LP
            elecDashboardData.setSteamHhp(steamHhp);
            elecDashboardData.setSteamHp(steamHp);
            elecDashboardData.setSteamMp(steamMp);
            elecDashboardData.setSteamLp(steamLp);
            // 天然气
            String gasRealTime = this.getCellData(sheet1, 9, 2);    // 瞬时量
            String gasLeft = this.getCellData(sheet1, 9, 3);        // 日剩余量
            String gasUsed = this.getCellData(sheet1, 9, 4);        // 日消耗量（实际）
            String gasPlan = this.getCellData(sheet1, 9, 5);        // 日消耗量（计划）
            String gasElec = this.getCellData(sheet1, 11, 2);       // 电厂
            String gasSynGas = this.getCellData(sheet1, 11, 3);     // 合成气
            String gasU2 = this.getCellData(sheet1, 11, 4);         // U2
            String gasStyrene = this.getCellData(sheet1, 11, 5);    // 苯乙烯
            elecDashboardData.setGasRealTime(gasRealTime);
            elecDashboardData.setGasLeft(gasLeft);
            elecDashboardData.setGasUsed(gasUsed);
            elecDashboardData.setGasPlan(gasPlan);
            elecDashboardData.setGasElec(gasElec);
            elecDashboardData.setGasSynGas(gasSynGas);
            elecDashboardData.setGasU2(gasU2);
            elecDashboardData.setGasStyrene(gasStyrene);
            // 电量
            String elecPowerGen = this.getCellData(sheet1, 14, 2);    // 昨日发电量
            String elecPowerSwitch = this.getCellData(sheet1, 14, 3); // 倒电量
            String elecUsed = this.getCellData(sheet1, 14, 4);        // 每日用电量
            String elecDiff = this.getCellData(sheet1, 14, 5);        // 每日峰谷差
            String elecBycUsed = this.getCellData(sheet1, 16, 2);     // BYC用电量
            String elecBocUsed = this.getCellData(sheet1, 16, 3);     // BOC用电量
            elecDashboardData.setElecPowerGen(elecPowerGen);
            elecDashboardData.setElecPowerSwitch(elecPowerSwitch);
            elecDashboardData.setElecUsed(elecUsed);
            elecDashboardData.setElecDiff(elecDiff);
            elecDashboardData.setElecBycUsed(elecBycUsed);
            elecDashboardData.setElecBocUsed(elecBocUsed);
            // 煤耗
            String carbonUsed = this.getCellData(sheet1, 19, 2);        // 每日煤耗量
            String carbonRealTime = this.getCellData(sheet1, 19, 3);    // 瞬时煤耗量
            elecDashboardData.setCarbonUsed(carbonUsed);
            elecDashboardData.setCarbonRealTime(carbonRealTime);
            // SUB
            String sub = this.getCellData(sheet1, 22, 2);               // SUB
            String subFurnaceLoad = this.getCellData(sheet1, 22, 3);    // SUB开工锅炉负荷（百分比）
            elecDashboardData.setSub(sub);
            elecDashboardData.setSubFurnaceLoad(subFurnaceLoad);
            //========================================电厂大屏数据第二页：蒸汽平衡=======================================
            // HHP
            String hhpPphhp = this.getCellData(sheet2, 2, 1);   // HHP PPHHP
            String hhpAaae = this.getCellData(sheet2, 2, 2);    // HHP AA/AE
            String hhpGaa = this.getCellData(sheet2, 2, 3);     // HHP GAA
            String hhpSyn = this.getCellData(sheet2, 2, 4);     // HHP SYN
            String hhpP3802 = this.getCellData(sheet2, 2, 5);   // HHP P3802
            elecDashboardData.setHhpPphhp(hhpPphhp);
            elecDashboardData.setHhpAaae(hhpAaae);
            elecDashboardData.setHhpGaa(hhpGaa);
            elecDashboardData.setHhpSyn(hhpSyn);
            elecDashboardData.setHhpP3802(hhpP3802);
            // HP
            String hpLpg = this.getCellData(sheet2, 5, 1);    // HP LPG（百分比）
            String hpSub = this.getCellData(sheet2, 5, 2);    // HP SUB（百分比）
            String hpNg = this.getCellData(sheet2, 5, 3);    // HP NG
            String hpJ1501 = this.getCellData(sheet2, 5, 4);    // HP J1501（百分比）
            String hpJ1504In = this.getCellData(sheet2, 5, 5);    // HP J1504入
            String hpJ1504Out = this.getCellData(sheet2, 5, 6);    // HP J1504出
            String hpUser = this.getCellData(sheet2, 5, 7);    // HP HP用户
            elecDashboardData.setHpLpg(hpLpg);
            elecDashboardData.setHpSub(hpSub);
            elecDashboardData.setHpNg(hpNg);
            elecDashboardData.setHpJ1501(hpJ1501);
            elecDashboardData.setHpJ1504In(hpJ1504In);
            elecDashboardData.setHpJ1504Out(hpJ1504Out);
            elecDashboardData.setHpUser(hpUser);
            // MP
            String mpNcipmp = this.getCellData(sheet2, 8, 1);   // MP NCI MP
            String mp150511 = this.getCellData(sheet2, 8, 2);   // MP 15051_2（百分比）
            String mp150512 = this.getCellData(sheet2, 8, 3);   // MP 15051_2（百分比）
            String mpPpmp = this.getCellData(sheet2, 8, 4);     // MP PPMP
            String mpP9801In = this.getCellData(sheet2, 8, 5);  // MP P9801入
            String mpP9801Out = this.getCellData(sheet2, 8, 6); // MP P9801出
            String mpJ1502 = this.getCellData(sheet2, 8, 7);    // MP J1502（百分比）
            String mpUser = this.getCellData(sheet2, 8, 8);     // MP MP用户
            elecDashboardData.setMpNcipMp(mpNcipmp);
            elecDashboardData.setMp150511(mp150511);
            elecDashboardData.setMp150512(mp150512);
            elecDashboardData.setMpPpMp(mpPpmp);
            elecDashboardData.setMpP9801In(mpP9801In);
            elecDashboardData.setMpP9801Out(mpP9801Out);
            elecDashboardData.setMpJ1502(mpJ1502);
            elecDashboardData.setMpUser(mpUser);
            // LP
            String lpPpLp = this.getCellData(sheet2, 11, 1);        // LP PP LP
            String lpEmpty = this.getCellData(sheet2, 11, 2);       // LP防空
            String lpMpLetdown = this.getCellData(sheet2, 11, 3);   // LP MP LETDOWN
            String lpJ1509 = this.getCellData(sheet2, 11, 4);       // LP J1509（百分比）
            String lpJ1503 = this.getCellData(sheet2, 11, 5);       // LP J1503（百分比）
            String lpLdpeTs = this.getCellData(sheet2, 11, 6);      // LP LDPE TS
            String lpLdpeDm = this.getCellData(sheet2, 11, 7);      // LP LDPE DM
            String lpUser = this.getCellData(sheet2, 11, 8);        // LP用户
            elecDashboardData.setLpPpLp(lpPpLp);
            elecDashboardData.setLpEmpty(lpEmpty);
            elecDashboardData.setLpMpLetdown(lpMpLetdown);
            elecDashboardData.setLpJ1509(lpJ1509);
            elecDashboardData.setLpJ1503(lpJ1503);
            elecDashboardData.setLpLdpeTs(lpLdpeTs);
            elecDashboardData.setLpLdpeDm(lpLdpeDm);
            elecDashboardData.setLpUser(lpUser);
            // BCC
            String bccSyn = this.getCellData(sheet2, 14, 1);    // BCC SYN
            String bccYpc = this.getCellData(sheet2, 14, 2);    // BCC YPC
            elecDashboardData.setBccSyn(bccSyn);
            elecDashboardData.setBccYpc(bccYpc);
            //========================================电厂大屏数据第三页：环保监控=======================================
            System.out.println(elecDashboardData.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 抓取单元格数据
     * 如果单元格数据为空，返回空字符串
     *
     * @param sheet excel的sheet对象
     * @param rowIndex 行下标
     * @param cellIndex 列下标
     * @return 单元格数据
     */
    private String getCellData(Sheet sheet, int rowIndex, int cellIndex) {
        // 单元格数据
        String cellValue = "";
        try {
            // 根据行下标，获取行对象
            Row row = sheet.getRow(rowIndex);
            // 根据列下表，获取单元格对象，如果单元格的值为空，则返回null
            Cell cell = row.getCell(cellIndex, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
            if (cell != null) {
                // 设置单元格格式
                cell.setCellType(CellType.STRING);
                // 获取单元格数据
                cellValue = cell.getStringCellValue();
                // 将excel空单元格中取到的"#NAME?"转换为空字符串
                if ("#NAME?".equals(cellValue)) {
                    cellValue = "";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return cellValue;
        }
    }
}