package org.springblade.modules.nh.factory.ginlong.scheduler;

import lombok.extern.slf4j.Slf4j;
import org.springblade.common.utils.CommonUtil;
import org.springblade.common.utils.TaskControl;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.modules.nh.factory.ginlong.GinlongApi;
import org.springblade.modules.nh.service.IMonitorEntryStatService;
import org.springblade.modules.nh.service.IMonitorInverterStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@EnableScheduling
@Component
@Slf4j
public class GinlongScheduler {

	private final int indexCount = 2;
	@Autowired
	GinlongApi ginlongApi;
	@Autowired
	GinlongUserStationListPageAction ginlongUserStationListPageAction;
	@Autowired
	GinlongInverterDetailListPageAction ginlongInverterDetailListPageAction;
	@Autowired
	IMonitorEntryStatService monitorEntryStatService;
	@Autowired
	GinlongStationMonthEachRunner ginlongStationMonthEachRunner;
	@Autowired
	GinlongAlarmListPageAction ginlongAlarmListPageAction;
	@Autowired
	GinlongInverterDayEachRunner ginlongInverterDayEachRunner;
	@Autowired
	IMonitorInverterStatService monitorInverterStatService;
	@Autowired
	GinlongStationYearEachRunner ginlongStationYearEachRunner;

	/**
	 * 每五分钟统计一次
	 * checked
	 */
	@Scheduled(cron = "0 */13 * * * ?")
	public void syncUserStationList() {
		System.out.println("获取用户电站列表The current time is: " + new Date());
		TaskControl.runTask("ginlong:syncUserStationList", new Runnable() {
			@Override
			public void run() {
				for(int i=1;i<=indexCount;i++) {
					ginlongUserStationListPageAction.start("获取用户电站列表", 100, CommonUtil.getCtx(i),1);
				}
			}
		});
	}

	/**
	 * 每五分钟统计一次
	 * checked
	 */
	@Scheduled(cron = "10 */15 * * * ?")
	public void syncInverterDetailList() {
		TaskControl.runTask("ginlong:syncInverterDetailList", new Runnable() {
			@Override
			public void run() {
				for(int i=1;i<=indexCount;i++) {
					ginlongInverterDetailListPageAction.start("获取逆变器详情列表", 100, CommonUtil.getCtx(i),1);
				}
			}
		});
	}

	/**
	 * 每天凌晨统计一次
	 * checked
	 */
	@Scheduled(cron = "0 0 1 * * ?")
	public void syncStationMonthData() {
		TaskControl.runTask("ginlong:syncStationMonthData", new Runnable() {
			@Override
			public void run() {
				for(int i=1;i<=indexCount;i++) {
					List<String> stationIds = monitorEntryStatService.getStationIds("0",i);
					ginlongStationMonthEachRunner.start(stationIds, "获取单个电站某月的日数据", 2L, CommonUtil.getCtx(i));
				}
			}
		});
	}

	/**
	 * 每天凌晨统计一次
	 * checked
	 */
	@Scheduled(cron = "0 11 1 * * ?")
	public void syncStationYearData() {
		TaskControl.runTask("ginlong:syncStationYearData", new Runnable() {
			@Override
			public void run() {
				for (int i = 1; i <= indexCount; i++) {
					List<String> stationIds = monitorEntryStatService.getStationIds("0",i);
					ginlongStationYearEachRunner.start(stationIds, "获取单个电站某年的月数据", 1L, CommonUtil.getCtx(i));
				}
			}
		});
	}

	/**
	 * 每10分钟统计
	 * checked
	 */
//    @Scheduled(cron = "20 */10 * * * ?")
//    public void syncCollectorList() {
//        TaskControl.runTask("ginlong:syncCollectorList", new Runnable() {
//            @Override
//            public void run() {
//                collectorListPageAction.start("获取获取账号下采集器列表", 100, 1);
//            }
//        });
//    }

	/**
	 * 每隔 5分钟同步最近7天的告警信息
	 * checked
	 */
	@Scheduled(cron = "0 */10 * * * ?")
	public void syncAlarmList() {
		TaskControl.runTask("ginlong:syncAlarmList", new Runnable() {
			@Override
			public void run() {
				for (int i = 1; i <= indexCount; i++) {
					ginlongAlarmListPageAction.start("获取账号下设备报警列表", 100, CommonUtil.getCtx(i),1);
				}
			}
		});
	}

	/**
	 * 每天凌晨2点统计一次
	 * checked
	 */
	@Scheduled(cron = "0 3 2 * * ?")
	public void syncInverterDay() {
		TaskControl.runTask("ginlong:syncInverterDay", new Runnable() {
			@Override
			public void run() {
				for (int i = 1; i <= indexCount; i++) {
					List<String> inverterSns = monitorInverterStatService.getInverterSns("0",i);
					Map<String, Object> ctx = CommonUtil.getCtx(i);
					ctx.put("baseDate", DateUtil.format(new Date(), "yyyy-MM-dd"));
					ginlongInverterDayEachRunner.start(inverterSns, "获取单台逆变器某日的实时数据", 1L, ctx);
				}
			}
		});
	}

}
