package io.renren.modules.common;

import io.renren.modules.aspen.entity.TDashboarddayeleceightEntity;
import io.renren.modules.aspen.service.TDashboarddayeleceightService;
import io.renren.modules.sys.controller.AbstractController;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import com.opencsv.CSVReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
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 EveryEightGetController extends AbstractController {
    @Autowired
    private TDashboarddayeleceightService tDashboarddayeleceightService;
    //    @Scheduled(cron = "0 59 7 * * ?")
    //每日8：00触发
    @Scheduled(cron = "0 0 8 * * ?")
    public void getExcelData1(){
        logger.info("0:00获取数据");
//        String csvFile = "D://ssyFile/物料.csv";
        String csvFile = "C://dashboardAspen/wuliao.csv";
        TDashboarddayeleceightEntity tDashboarddayeleceightEntity = new TDashboarddayeleceightEntity();
        CSVReader reader = null;
        try {
            reader = new CSVReader(new FileReader(csvFile));
            String[] line;
            //定义行数
            int i =0;
            while ((line = reader.readNext()) != null) {
                if (i == 18) {
                    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]);
                    }
                    tDashboarddayeleceightEntity.setEvconsume(line[2]);
                    tDashboarddayeleceightEntity.setElecp(line[3]);
                    tDashboarddayeleceightEntity.setHechengqi(line[4]);
                    tDashboarddayeleceightEntity.setU2(line[5]);
                }
            }
            tDashboarddayeleceightEntity.setDataDate(new Date());
            Calendar now = Calendar.getInstance();
            tDashboarddayeleceightEntity.setDateDay(String.valueOf(now.get(Calendar.DAY_OF_MONTH)));
            logger.info("dashboardDay:" + tDashboarddayeleceightEntity);
            tDashboarddayeleceightService.save(tDashboarddayeleceightEntity);
        } 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))) {
                    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";
        }
    }
}
