package io.renren.modules.common;

import com.alibaba.fastjson.JSON;
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.util.Date;
import java.util.List;
@Component
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class AspenController extends AbstractController {

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private TDashboarddataService tDashboarddataService;

    @Scheduled(cron = "0/15 * * * * ?")
//    @Scheduled(cron = "0 */15 * * * ?")
    public void getExcelData(){
        Workbook workbook = ExcelUtils.getWorkBook("D://ssyFile/bcc.xlsx");
        Sheet sheet = workbook.getSheetAt(0);
        int rowNum = sheet.getPhysicalNumberOfRows();
        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 == 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);
    }
}
