package com.huatek.busi.task.cwg;

import java.time.LocalDate;
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.common.CacMonitorTypeCode;
import com.huatek.busi.dto.CwgTemperatureStatisticsDto;
import com.huatek.busi.service.temperature.CwgTemperatureService;
import com.huatek.busi.service.temperature.TemperIncreaseRateService;
import com.huatek.busi.task.base.AbstractBusiTaskExecutor;
import com.huatek.busi.vo.CwgTemperatureStatisticsVo;

import lombok.extern.slf4j.Slf4j;

/**
 * 温度增长率告警 定时任务
 * 
 * @author buck_guo
 * @date 2019/11/06
 */
@Slf4j
@Component("temperIncreaseTask")
public class TemperIncreaseTask extends AbstractBusiTaskExecutor {
    @Autowired
    private CwgTemperatureService cwgTemperatureService;
    @Autowired
    private TemperIncreaseRateService temperIncreaseRateService;

    @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) {
        log.info("{}测温柜温度日增长率、月增长率定时任务start", day);
        CwgTemperatureStatisticsDto param = new CwgTemperatureStatisticsDto();
        param.setCacMonitorTypeCode(CacMonitorTypeCode.CWG.getCode());
        param.setStartTime(day.atStartOfDay());
        param.setEndTime(day.plusDays(1).atStartOfDay());
        // 统计列表
        List<CwgTemperatureStatisticsVo> list = cwgTemperatureService.getListEquInfo(param);
        if (!CollectionUtils.isEmpty(list)) {
            for (CwgTemperatureStatisticsVo item : list) {
                // 保存增长率
                temperIncreaseRateService.saveTemperIncreaseRate(item, day);
            }
        }
        log.info("{}测温柜温度日增长率、月增长率定时任务end", day);
    }
}
