package io.renren.modules.common;

import com.opencsv.CSVReader;

import io.renren.modules.aspen.entity.TDashboarddayelecEntity;
import io.renren.modules.aspen.service.TDashboarddayelecService;
import io.renren.modules.sys.controller.AbstractController;
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.Calendar;
import java.util.Date;

@Component
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class DayGetController extends AbstractController {

    @Autowired
    private TDashboarddayelecService tDashboarddayelecService;

//    @Scheduled(cron = "0/30 * * * * ?")
//    @Scheduled(cron = "0 */15 * * * ?")
    //每日0：00触发
    @Scheduled(cron = "0 0 0 * * ?" )
    public void getExcelData(){
        logger.info("实时获取数据");
//        String csvFile = "D://ssyFile/物料.csv";
        String csvFile = "D://elecdashboardaspen/elecDashboardData1.csv";
        TDashboarddayelecEntity tDashboarddayelecEntity = new TDashboarddayelecEntity();
        CSVReader reader = null;
        try {
            reader = new CSVReader(new FileReader(csvFile));
            String[] line;
            //定义行数
            int i = 0;
            while ((line = reader.readNext()) != null) {
                i++;
                if (i == 2) {
                    logger.info("第" + i + "行");
                    for (int j = 2; j < 7; 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]);
                    }
                    tDashboarddayelecEntity.setGtg1Lv(line[2]);
                    tDashboarddayelecEntity.setGtg2Lv(line[3]);
                    tDashboarddayelecEntity.setGtg3Lv(line[4]);
                    tDashboarddayelecEntity.setStg1Lv(line[5]);
                    tDashboarddayelecEntity.setMwhLv(line[6]);
                }
                if (i == 3) {
                    logger.info("第" + i + "行");
                    for (int j = 0; j < 6; 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]);
                    }
                    tDashboarddayelecEntity.setGtg1Wr(line[2]);
                    tDashboarddayelecEntity.setGtg2Wr(line[3]);
                    tDashboarddayelecEntity.setGtg3Wr(line[4]);
                    tDashboarddayelecEntity.setStg1Wr(line[5]);
                }
                if (i == 4) {
                    logger.info("第" + i + "行");
                    for (int j = 0; j < 6; 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]);
                    }
                    tDashboarddayelecEntity.setMwhWr(line[6]);
                }
                if (i == 6) {
                    logger.info("第" + i + "行");
                    for (int j = 2; j < 7; j++) {
                        logger.info("读数:" + line[j]);
                        if (line[j].equals("")) {
                            line[j] = "0";
                        }else {
                            if (!Character.isDigit(line[j].charAt(0))) {
                                line[j] = "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]);
                    }
                    tDashboarddayelecEntity.setHhp(line[2]);
                    tDashboarddayelecEntity.setHp(line[3]);
                    tDashboarddayelecEntity.setLp(line[4]);
                    tDashboarddayelecEntity.setMp(line[5]);
                    tDashboarddayelecEntity.setTotalConsumption(line[6]);
                }
                if (i == 9) {
                    logger.info("第" + i + "行");
                    for (int j = 2; j < 6; 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]);
                    }
//                    Integer HhpNum=new Integer(line[2]).intValue();
//                    Integer HpNum=new Integer(line[3]).intValue();
//                    Integer LpNum=new Integer(line[4]).intValue();
//                    Integer MpNum=new Integer(line[5]).intValue();
                    tDashboarddayelecEntity.setHhpNum(line[2]);
                    tDashboarddayelecEntity.setHpNum(line[3]);
                    tDashboarddayelecEntity.setLpNum(line[4]);
                    tDashboarddayelecEntity.setMpNum(line[5]);

                }
                if (i == 10) {
                    logger.info("第" + i + "行");
                    for (int j = 2; j < 6; j++) {
                        logger.info("读数:" + line[j]);
                        if (line[j].equals("")) {
                            line[j] = "0";
                        }else {
                            if (!Character.isDigit(line[j].charAt(0))) {
                                line[j] = "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]);
                    }
                    tDashboarddayelecEntity.setHhpTemp(line[2]);
                    tDashboarddayelecEntity.setHpTemp(line[3]);
                    tDashboarddayelecEntity.setLpTemp(line[4]);
                    tDashboarddayelecEntity.setMpTemp(line[5]);
                }
                /* SS */
                if (i == 13) {
                    logger.info("第" + i + "行");
                    for(int j=2;j<5;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]);
                    }
                    tDashboarddayelecEntity.setShihua(line[2]);
                    tDashboarddayelecEntity.setShiyou(line[3]);
                    tDashboarddayelecEntity.setShunshiliang(line[4]);
                }
                if (i == 18) {
                    logger.info("第" + i + "行");
                    for(int j=2;j<6;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]);
                    }
                    tDashboarddayelecEntity.setYpg(line[2]);
                    tDashboarddayelecEntity.setDaodianliang(line[3]);
                    tDashboarddayelecEntity.setEveLec(line[4]);
                    tDashboarddayelecEntity.setFenggucha(rounding(line[5]));
                }
                if (i == 20) {
                    logger.info("第" + i + "行");
                    for(int j=2;j<4;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]);
                    }
                    tDashboarddayelecEntity.setBycElec(line[2]);
                    tDashboarddayelecEntity.setBocElec(line[3]);
                }
            }
            tDashboarddayelecEntity.setCreatedate(new Date());
            tDashboarddayelecEntity.setDataDate(new Date());
            Calendar now = Calendar.getInstance();
            tDashboarddayelecEntity.setDateDay(String.valueOf(now.get(Calendar.DAY_OF_MONTH)));
            logger.info("dashboardData:" + tDashboarddayelecEntity);
            tDashboarddayelecService.save(tDashboarddayelecEntity);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public String rounding(String lineNum) {
        try {
            logger.info("读数:" + lineNum);
            if (lineNum.equals("")) {
                lineNum = "0.0";
            }else {
                if (!Character.isDigit(lineNum.charAt(0))) {
                    if ("-".equals(lineNum.substring(0,1))){
                        BigDecimal b = new BigDecimal(Double.parseDouble(lineNum));
                        double f = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                        lineNum = String.valueOf(f);
                    }else {
                        lineNum = "0.0";
                    }
                }else {
                    BigDecimal b = new BigDecimal(Double.parseDouble(lineNum));
                    double f = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    lineNum = String.valueOf(f);
                }
            }
            logger.info(lineNum);
            return lineNum;
        }catch (Exception e) {
            logger.error(String.valueOf(e));
            return "0.0";
        }
    }
}
