package io.renren.modules.common;

import com.alibaba.fastjson.JSON;
import com.opencsv.CSVReader;
import io.renren.common.utils.ExcelUtils;
import io.renren.modules.aspen.entity.TDashboarddataEntity;
import io.renren.modules.aspen.service.TDashboarddataService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.service.SysMenuService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@Component
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class AspenController extends AbstractController {

    @Autowired
    private TDashboarddataService tDashboarddataService;

    @Scheduled(cron = "0/15 * * * * ?")
//    @Scheduled(cron = "0 */15 * * * ?")
    public void getExcelData(){
        String csvFile = "D://ssyFile/物料.csv";
//        String csvFile = "C://dashboardAspen/wuliao.csv";
//        B:\GLOBAL\7430-BYC\NANJING\CB\CBP_C\15 CBP Cracker Share 乙烯装置共享文件夹\Dashboard data\物料.xlsm
        TDashboarddataEntity dashboarddataEntity = new TDashboarddataEntity();
        CSVReader reader = null;
        try {
            reader = new CSVReader(new FileReader(csvFile));
            String[] line;
            //定义行数
            int i = 0;
            while ((line = reader.readNext()) != null) {
                if (i == 4) {
                    logger.info("第" + i + "行");
                    for (int j = 0; j < 16; j++) {
                        logger.info("读数:" + line[j]);
                        if (line[j].equals("")) {
                            line[j] = "0.0";
                        }else {
                            if (!Character.isDigit(line[j].charAt(0))) {
                                line[j] = "0.0";
                            }else {
                                BigDecimal b = new BigDecimal(Double.parseDouble(line[j]));
                                double f = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                line[j] = String.valueOf(f);
                            }
                        }
                        logger.info(line[j]);
                    }
                    dashboarddataEntity.setEnergyYixi(line[0]);
                    dashboarddataEntity.setEnergyBingxi(line[1]);
                    dashboarddataEntity.setEnergyQingqi(line[2]);
                    dashboarddataEntity.setEnergyJiawan(line[3]);
                    dashboarddataEntity.setEnergyOffgas(line[4]);
                    dashboarddataEntity.setEnergyLpg(line[5]);
                    dashboarddataEntity.setEnergyCsi(line[6]);
                    dashboarddataEntity.setEnergyBenzene(line[7]);
                    dashboarddataEntity.setEnergyToluene(line[8]);
                    dashboarddataEntity.setEnergyXylene(line[9]);
                    dashboarddataEntity.setEnergyTanliu(line[10]);
                    dashboarddataEntity.setEnergyWashoil(line[11]);
                    dashboarddataEntity.setEnergyTanwu(line[12]);
                    dashboarddataEntity.setEnergyIma(line[13]);
                    dashboarddataEntity.setEnergyCjiu(line[14]);
                    dashboarddataEntity.setEnergyRpg(line[15]);
                }
                if (i == 11) {
                    logger.info("第" + i + "行");
                    for (int j = 0; j < 5; j++) {
                        logger.info("读数:" + line[j]);
                        if (line[j].equals("")) {
                            line[j] = "0.0";
                        }else {
                            if (!Character.isDigit(line[j].charAt(0))) {
                                line[j] = "0.0";
                            }else {
                                BigDecimal b = new BigDecimal(Double.parseDouble(line[j]));
                                double f = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                line[j] = String.valueOf(f);
                            }
                        }
                        logger.info(line[j]);
                    }
                    dashboarddataEntity.setEnergyfeedNap(line[0]);
                    dashboarddataEntity.setEnergyfeedYiwan(line[1]);
                    dashboarddataEntity.setEnergyfeedTanwu(line[2]);
                    dashboarddataEntity.setEnergyfeedTanliu(line[3]);
                    dashboarddataEntity.setEnergyfeedLpg(line[4]);
                }
                i++;
            }
            dashboarddataEntity.setCreatedate(new Date());
            logger.info("dashboardData:" + dashboarddataEntity);
            tDashboarddataService.save(dashboarddataEntity);
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*Workbook workbook = ExcelUtils.getWorkBook("B://GLOBAL/7430-BYC/NANJING/CB/CBP/CBP_C/15 CBP Cracker Share 乙烯装置共享文件夹/Dashboard data/物料.xlsx");
//        Workbook workbook = ExcelUtils.getWorkBook("D://ssyFile/ceshi.xlsx");
        Sheet sheet = workbook.getSheetAt(0);
        int rowNum = sheet.getLastRowNum();
        TDashboarddataEntity dashboarddataEntity = new TDashboarddataEntity();
        logger.info("rowNum:" + rowNum);
        for (int i = 0; i <= rowNum; i++) {
            if (i == 0) {
                logger.info("i:" + i);
                Row row = sheet.getRow(i);
                int cellNum = row.getLastCellNum();
                for (int j = 0; j <= cellNum; j++) {
                    if (j == 4) {
                        logger.info("==========");
                    }
                    Cell cell = row.getCell(j);
                    String cellValue = ExcelUtils.getCellValue(cell);

                }
            }
            if (i == 4) {
                logger.info("i:" + i);
                Row row = sheet.getRow(i);
                int cellNum = row.getPhysicalNumberOfCells();
                for (int j = 0; j < cellNum; j++) {
                    Cell cell = row.getCell(j);
                    String cellValue = ExcelUtils.getCellValue(cell);
                    if (j == 0) {
                        dashboarddataEntity.setEnergyYixi(cellValue);
                    }else if (j == 1) {
                        dashboarddataEntity.setEnergyBingxi(cellValue);
                    }else if (j == 2) {
                        dashboarddataEntity.setEnergyQingqi(cellValue);
                    }else if (j == 3) {
                        dashboarddataEntity.setEnergyJiawan(cellValue);
                    }else if (j == 4) {
                        dashboarddataEntity.setEnergyOffgas(cellValue);
                    }else if (j == 5) {
                        dashboarddataEntity.setEnergyLpg(cellValue);
                    }else if (j == 6) {
                        dashboarddataEntity.setEnergyCsi(cellValue);
                    }else if (j == 7) {
                        dashboarddataEntity.setEnergyBenzene(cellValue);
                    }else if (j == 8) {
                        dashboarddataEntity.setEnergyToluene(cellValue);
                    }else if (j == 9) {
                        dashboarddataEntity.setEnergyXylene(cellValue);
                    }else if (j == 10) {
                        dashboarddataEntity.setEnergyTanliu(cellValue);
                    }else if (j == 11) {
                        dashboarddataEntity.setEnergyWashoil(cellValue);
                    }else if (j == 12) {
                        dashboarddataEntity.setEnergyTanwu(cellValue);
                    }else if (j == 13) {
                        dashboarddataEntity.setEnergyIma(cellValue);
                    }else if (j == 14) {
                        dashboarddataEntity.setEnergyCjiu(cellValue);
                    }else if (j == 15) {
                        dashboarddataEntity.setEnergyRpg(cellValue);
                    }
                }
            }
            if (i == 6) {
                logger.info("i:" + i);
                Row row = sheet.getRow(i);
                int cellNum = row.getLastCellNum();
                for (int j = 0; j <= cellNum; j++) {
                    if (j == 0) {
                        logger.info("==========");
                    }
                    Cell cell = row.getCell(j);
                    String cellValue = ExcelUtils.getCellValue(cell);

                }
            }
            if (i == 11) {
                logger.info("i:" + i);
                Row row = sheet.getRow(i);
                int cellNum = row.getPhysicalNumberOfCells();
                for (int j = 0; j < cellNum; j++) {
                    Cell cell = row.getCell(j);
                    String cellValue = ExcelUtils.getCellValue(cell);
                    if (j == 0) {
                        dashboarddataEntity.setEnergyfeedNap(cellValue);
                    }else if (j ==1) {
                        dashboarddataEntity.setEnergyfeedYiwan(cellValue);
                    }else if (j ==2) {
                        dashboarddataEntity.setEnergyfeedTanwu(cellValue);
                    }else if (j ==3) {
                        dashboarddataEntity.setEnergyfeedTanliu(cellValue);
                    }else if (j ==4) {
                        dashboarddataEntity.setEnergyfeedLpg(cellValue);
                    }
                }
            }
        }
        dashboarddataEntity.setCreatedate(new Date());
        logger.info("dashboardData:" + dashboarddataEntity);
        tDashboarddataService.save(dashboarddataEntity);*/
    }
}
