package com.huatek.busi.task.transformer.part;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.huatek.busi.service.transformer.TransformerEpaDeviationService;
import com.huatek.busi.service.transformer.TransformerEpaDeviationStatisticsService;
import com.huatek.busi.task.base.AbstractBusiTaskExecutor;
import com.huatek.busi.vo.epa.EpaDeviationOneDataVo;

import lombok.extern.slf4j.Slf4j;

/**
 * 油色谱偏差率按天统计
 * 
 * @author anker_cheng
 * @date 2019/07/15
 */
@Slf4j
@Component("transfromEqmDeviationTask")
public class TransfromEqmDeviationTask extends AbstractBusiTaskExecutor {
    @Autowired
    private TransformerEpaDeviationService transformerEpaDeviationService;
    @Autowired
    private TransformerEpaDeviationStatisticsService transformerEpaDeviationStatisticsService;

    @Override
    public void execute() {
        LocalDate yesterday = LocalDate.now().minusDays(1);
        execute(yesterday);
    }

    @Override
    public void execute(String params) {
        LocalDate day = toLocalDate(params);
        execute(day);
    }

    @Override
    public void execute(LocalDate day) {
        LocalDateTime startTime = day.atStartOfDay();
        LocalDateTime endTime = day.plusDays(1).atStartOfDay();
        log.info("{} start...", day);
        // 查询数据
        List<EpaDeviationOneDataVo> list = transformerEpaDeviationService.getOneData(startTime, endTime);
        if (CollectionUtils.isEmpty(list)) {
            log.info("transfromEqmDeviationTask: {}-{}数据为0是!", startTime, endTime);
            return;
        }
        // 删除旧的数据
        transformerEpaDeviationStatisticsService.deleteTransformerEpaDeviationStatistics(day);
        // 重新保存
        for (EpaDeviationOneDataVo vo : list) {
            transformerEpaDeviationStatisticsService.saveTransformerEpaDeviationStatistics(vo, day);
        }
        log.info("{} end!", day);
    }
    
    public static void main(String[] args)
    {
    	LocalDate yesterday = LocalDate.now().minusDays(1);
    	System.out.println(yesterday);
    	
    }
}
