package com.huatek.busi.task.curve;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
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.Maps;
import com.huatek.busi.common.CurveSubType;
import com.huatek.busi.common.CurveType;
import com.huatek.busi.common.util.DateUtil;
import com.huatek.busi.service.curve.BaseCurveSensorService;
import com.huatek.busi.service.impl.curve.helper.CurveMapping;
import com.huatek.busi.task.base.BusiTaskExecutor;
import com.huatek.busi.task.curve.helper.AbstractCurveTask;
import com.huatek.busi.task.curve.impl.TransformerRunStateCurveTask;

import lombok.extern.slf4j.Slf4j;

/**
 * 曲线图定时任务统一调度类
 *
 * @author anker_cheng
 * @date 2019/09/24
 */
@Slf4j
@Component("curveExecutorTask")
public class CurveExecutorTask extends AbstractCurveTask implements BusiTaskExecutor, ApplicationContextAware {

    /** 实现类Map缓存 */
    private final Map<String, BaseCurveSensorService> serviceImplMap = Maps.newConcurrentMap();

    @Autowired
    private TransformerRunStateCurveTask transformerRunStateCurveTask;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, BaseCurveSensorService> beansOfTypeMap =
            applicationContext.getBeansOfType(BaseCurveSensorService.class);
        if (CollectionUtils.isEmpty(beansOfTypeMap)) {
            log.debug("implement class is empty!");
            return;
        }
        for (BaseCurveSensorService service : beansOfTypeMap.values()) {
            if (service.persistentClass() == null) {
                continue;
            }
            log.debug("趋势图实现类：{}", service.persistentClass());
            serviceImplMap.put(service.persistentClass().getName(), service);
        }
    }

    @Override
    public void execute() {
//        LocalDate beginDateTime = LocalDate.parse("2019-06-25", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
//        LocalDate endDateTime = LocalDate.parse("2022-06-25", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
//        int days = Math.toIntExact(beginDateTime.until(endDateTime, ChronoUnit.DAYS));
//        for (int i = 0; i < days; i++) {
//            LocalDate yesterday = startTime.plusDays(i);
//            execute(yesterday);
//        }
        LocalDate yesterday = LocalDate.now().minusDays(1);
        execute(yesterday);
    }

    @Override
    public void execute(String params) {
        LocalDate day = LocalDate.parse(params, DateTimeFormatter.ofPattern(DateUtil.PATTERN_DATE));
        execute(day);
    }

    @Override
    public void execute(LocalDate date) {
        //工作状态，单独调用，只有一条曲线
        transformerRunStateCurveTask.execute(date);
        //历史曲线和趋势图调用，可能有多条曲线
        for (String key : serviceImplMap.keySet()) {
            BaseCurveSensorService service = serviceImplMap.get(key);
            if (service == null) {
                log.debug("{}找不到实现类", key);
                continue;
            }
            CurveType curveType = CurveMapping.getCurveMapping(key);
            if (curveType == null || CollectionUtils.isEmpty(curveType.getSeries())) {
                log.debug("{}找不到折线图配置", key);
                continue;
            }
            for (CurveSubType subType : curveType.getSeries()) {
                execute(service, date, subType.getColumn());
            }
        }
    }
}
