package io.renren.modules.common;

import com.opencsv.CSVReader;
import io.renren.modules.aspen.entity.TDashboarddayEntity;
import io.renren.modules.aspen.service.TDashboarddayService;
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 TDashboarddayService tDashboarddayService;

//    @Scheduled(cron = "0 35 9 * * ?")
    @Scheduled(cron = "0 59 7 * * ?")
    public void getExcelData(){
        logger.info("按天获取数据");
//        String csvFile = "D://ssyFile/物料.csv";
        String csvFile = "C://dashboardAspen/wuliao.csv";
        TDashboarddayEntity dashboarddayEntity = new TDashboarddayEntity();
        CSVReader reader = null;
        try {
            reader = new CSVReader(new FileReader(csvFile));
            String[] line;
            //定义行数
            int i = 0;
            while ((line = reader.readNext()) != null) {
                if (i == 15) {
                    logger.info("第" + i + "行");
                    dashboarddayEntity.setDailyYixi(rounding(line[0]));
                    dashboarddayEntity.setProductPutput(rounding(line[0]));
                    if (!Character.isDigit(line[1].charAt(0))) {
                        dashboarddayEntity.setLossRate("0.01");
                    }else {
                        double lossNum = Double.parseDouble(line[1]) * 100;
                        dashboarddayEntity.setLossRate(String.valueOf(lossNum));
                    }
                    dashboarddayEntity.setEnergyConsumption(rounding(line[2]));
                }
                if (i == 19) {
                    logger.info("第" + i + "行");
                    dashboarddayEntity.setDailyBingxi(rounding(line[0]));
                }
                i++;
            }
            dashboarddayEntity.setDataDate(new Date());
            Calendar now = Calendar.getInstance();
            dashboarddayEntity.setDateDay(String.valueOf(now.get(Calendar.DAY_OF_MONTH)));
            logger.info("dashboardDay:" + dashboarddayEntity);
            tDashboarddayService.save(dashboarddayEntity);
        } 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";
        }
    }
}
