package com.huatek.busi.task.monitor;

import java.text.ParseException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Lists;
import com.huatek.busi.service.monitor.BaseMonitorDataTaskService;
import com.huatek.busi.task.base.AbstractBusiTaskExecutor;

import lombok.extern.slf4j.Slf4j;

/**
 * 在线监测——监测数据定时任务
 * 
 * @author anker_cheng
 * @date 2019/09/16
 */
@Slf4j
@Component("busiOnlineMonitorDataTask")
public class BusiOnlineMonitorDataTask extends AbstractBusiTaskExecutor implements ApplicationContextAware {

    /** 实现类List缓存 */
    private List<BaseMonitorDataTaskService> serviceImplList;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, BaseMonitorDataTaskService> beansOfTypeMap =
            applicationContext.getBeansOfType(BaseMonitorDataTaskService.class);
        if (!CollectionUtils.isEmpty(beansOfTypeMap)) {
            serviceImplList = Lists.newArrayListWithCapacity(beansOfTypeMap.size());
            for (String key : beansOfTypeMap.keySet()) {
                log.debug("在线监测实现类：{}", key);
                serviceImplList.add(beansOfTypeMap.get(key));
            }
        }
    }

    @Override
    public void execute()  throws Exception{
        super.execute();
    }

    @Override
    public void execute(LocalDate day) {
        if (CollectionUtils.isEmpty(serviceImplList)) {
            return;
        }
        for (BaseMonitorDataTaskService task : serviceImplList) {
            log.info("在线监测数据{}统计开始 - {}", day, task.persistentClass().getSimpleName());
            task.doDailyMonitorDataStatistics(day);
            log.info("在线监测数据{}统计结束 - {}", day, task.persistentClass().getSimpleName());
        }
    }
}
