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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springblade.common.constant.NhConstants;
import org.springblade.common.utils.AsyncTaskRunner;
import org.springblade.common.utils.ListUtils;
import org.springblade.common.utils.TaskControl;
import org.springblade.modules.nh.factory.aiswei.AisweiApi;
import org.springblade.modules.nh.pojo.entity.MonitorEntryStatEntity;
import org.springblade.modules.nh.service.IAlarmInfoService;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@EnableScheduling
@Component
@Slf4j
public class AisweiScheduler {

	@Autowired
	AisweiApi aisweiApi;
	@Autowired
	AisweiUserStationListPageAction aisweiUserStationListPageAction;
	@Autowired
	AisweiUserStationEachRunner aisweiUserStationEachRunner;
	@Autowired
	IMonitorEntryStatService monitorEntryStatService;
	@Autowired
	AisStationDMYEachRunner aisStationDMYEachRunner;
	@Autowired
	IMonitorInverterStatService monitorInverterStatService;
	@Autowired
	AisweiInverterEachRunner aisweiInverterEachRunner;
	@Autowired
	AisweiInverterDetailEachRunner aisweiInverterDetailEachRunner;
	@Autowired
	AisweiHistoryAlarmEachRunner aisweiHistoryAlarmEachRunner;
	@Autowired
	AisweiAlarmEachRunner aisweiAlarmEachRunner;
	@Autowired
	AisweiAlarmRecoverEachRunner aisweiAlarmRecoverEachRunner;
	@Autowired
	IAlarmInfoService alarmInfoService;
	@Autowired
	AisweiInverterLatestDetailEachRunner aisweiInverterLatestDetailEachRunner;

	@Autowired
	AisweiStationFisGenDateEachRunner fisGenDateEachRunner;

	/**
	 * 每5分钟同步一次电站信息
	 */
	@Scheduled(cron = "0 */10 * * * ?")
	public void syncUserStationList() {
		TaskControl.newSingleInstanceTask("aiswei:syncUserStationList", new Runnable() {
			@Override
			public void run() {
				aisweiUserStationListPageAction.start("获取用户电站列表", 500, 0);
			}
		});
	}

	/**
	 * 每5分钟同步一次电站的当日、当月和当年发电量
	 */
	@Scheduled(cron = "0 */10 * * * ?")
	public void syncUserStationEnergy() {
		AsyncTaskRunner asyncTaskRunner = TaskControl.newSingleInstanceTask("aiswei:syncUserStationEnergy");
		List<String> stationIds = monitorEntryStatService.getStationIds(NhConstants.SYS_TYPE_AISWEI);
		List<List<String>> parts = ListUtils.maxParts(stationIds, 10);
		List<Runnable> tasks = new ArrayList<>();
		for (List<String> part : parts) {
			tasks.add(aisweiUserStationEachRunner.asyncStart(part, "aiswei 实时同步日、月、年发电量", 1L, new HashMap<>()));
		}
		asyncTaskRunner.run(tasks);
	}

	/**
	 * 每天凌晨1点统计一次电站的月发电量、年发电量数据
	 */
	@Scheduled(cron = "20 58 7 * * ?")
	public void syncStationDMYData() {
		List<String> stationIds = monitorEntryStatService.getStationIds(NhConstants.SYS_TYPE_AISWEI);
		TaskControl.runTask("aiswei:syncStationDMYData", aisStationDMYEachRunner.asyncStart(stationIds, "aiswei 凌晨同步日、月、年发电量", 0L, null));
//		AsyncTaskRunner asyncTaskRunner = TaskControl.newSingleInstanceTask("aiswei:syncStationDMYData");
//      List<List<String>> parts = ListUtils.maxParts(stationIds, 5);
//      List<Runnable> tasks = new ArrayList<>();
//      for (List<String> part : parts) {
//          tasks.add(aisStationDMYEachRunner.asyncStart(part, "aiswei 凌晨同步日、月、年发电量", 0L, null));
//      }
//      asyncTaskRunner.run(tasks);
	}

	/**
	 * 每30分钟同步电站的日功率数据
	 */
	@Scheduled(cron = "* */30 * * * ?")
	public void syncStationDailyPacData() {
		AsyncTaskRunner asyncTaskRunner = TaskControl.newSingleInstanceTask("aiswei:syncStationDailyPacData");
		List<String> stationIds = monitorEntryStatService.getStationIds(NhConstants.SYS_TYPE_AISWEI);
		List<List<String>> parts = ListUtils.maxParts(stationIds, 5);
		List<Runnable> tasks = new ArrayList<>();
		for (List<String> part : parts) {
			Map<String, Object> ctx2 = new HashMap<>();
			ctx2.put("stat_pac_only", "1");
			tasks.add(aisStationDMYEachRunner.asyncStart(part, "aiswei 每半小时同步pac", 0L, ctx2));
		}
		asyncTaskRunner.run(tasks);
	}

	/**
	 * 每小时同步一次电站设备列表
	 * 同步逆变器sn，station_id,state,collector_sn
	 */
	@Scheduled(cron = "10 3 * * * ?")
	public void syncInverterList() {
		AsyncTaskRunner asyncTaskRunner = TaskControl.newSingleInstanceTask("aiswei:syncInverterList");
		List<String> stationIds = monitorEntryStatService.getStationIds(NhConstants.SYS_TYPE_AISWEI);
		List<List<String>> parts = ListUtils.maxParts(stationIds, 5);
		List<Runnable> tasks = new ArrayList<>();
		for (List<String> part : parts) {
			tasks.add(aisweiInverterEachRunner.asyncStart(part, "同步逆变器", 0L, null));
		}
		asyncTaskRunner.run(tasks);
	}

	/**
	 * 每小时同步逆变器按小时统计数据
	 */
	@Scheduled(cron = "39 11 * * * ?")
	public void syncInverterDetailList() {
		AsyncTaskRunner asyncTaskRunner = TaskControl.newSingleInstanceTask("aiswei:syncInverterDetailList");
		List<String> stationIds = monitorEntryStatService.getStationIds(NhConstants.SYS_TYPE_AISWEI);
		List<List<String>> parts = ListUtils.maxParts(stationIds, 5);
		List<Runnable> tasks = new ArrayList<>();
		for (List<String> part : parts) {
			tasks.add(aisweiInverterDetailEachRunner.asyncStart(part, "同步逆变器按小时统计数据", 0L, null));
		}
		asyncTaskRunner.run(tasks);
	}

	/**
	 * 每10分钟同步逆变器的最新数据，包含日发电量和总发电量，各种功率：视在功率、无功功率和功率等。
	 */
	@Scheduled(cron = "27 */11 * * * ?")
	public void syncInverterLatestDetails() {
		AsyncTaskRunner asyncTaskRunner = TaskControl.newSingleInstanceTask("aiswei:syncInverterLatestDetails");
		List<String> sns = monitorInverterStatService.getInverterSns(NhConstants.SYS_TYPE_AISWEI);
		List<List<String>> parts = ListUtils.split(sns, 10);
		List<String> snsList = new ArrayList<>();
		for (List<String> part : parts) {
			snsList.add(StringUtils.join(part, ","));
		}
		List<List<String>> parts1 = ListUtils.maxParts(snsList, 5);
		List<Runnable> tasks = new ArrayList<>();
		for (List<String> part1 : parts1) {
			tasks.add(aisweiInverterLatestDetailEachRunner.asyncStart(part1, "同步逆变器最新数据", 0L, null));
		}
		asyncTaskRunner.run(tasks);
	}

//    /**
//     * 每隔10分钟同步一次告警信息
//     */
//    @Scheduled(cron = "0 */10 * * * ?")
//    public void syncAlarmHistoryList() {
//        TaskControl.newSingleInstanceTask("aiswei:syncAlarmHistoryList", new Runnable() {
//            @Override
//            public void run() {
//                List<String> stationIds = monitorEntryStatService.getStationIds(NhConstants.SYS_TYPE_AISWEI);
//                aisweiHistoryAlarmEachRunner.start(stationIds, "同步历史报警信息", 0L, null);
//            }
//        });
//    }

	@Scheduled(cron = "0 */10 * * * ?")
	public void syncCurrentAlarmList() {
		TaskControl.newSingleInstanceTask("aiswei:syncCurrentAlarmList", new Runnable() {
			@Override
			public void run() {
				List<String> stationIds = monitorEntryStatService.getStationIds(NhConstants.SYS_TYPE_AISWEI);
				aisweiAlarmEachRunner.start(stationIds, "同步当前告警信息", 0L, null);
			}
		});
	}

	/**
	 * 获取电站的首日发电时间
	 */
	@Scheduled(cron = "44 16 * * * ?")
	public void syncStationFisGenDate() {
		TaskControl.runTask("atesi:syncStationFisGenDate", new Runnable() {
			@Override
			public void run() {
				LambdaQueryWrapper<MonitorEntryStatEntity> queryWrapper = new LambdaQueryWrapper<>();
				queryWrapper.eq(MonitorEntryStatEntity::getType, NhConstants.SYS_TYPE_AISWEI)
					.isNull(MonitorEntryStatEntity::getFisGenerateTime)
//					.eq(MonitorEntryStatEntity::getState,1)
					.select(MonitorEntryStatEntity::getOutStationId);
				List<String> stationIds = monitorEntryStatService.listObjs(queryWrapper);
				fisGenDateEachRunner.start(stationIds, "获取电站的首日发电时间", 0L, null);
			}
		});
	}

//    @Scheduled(cron = "0 */10 * * * ?")
//    public void syncAlarmRecoverList() {
//        TaskControl.newSingleInstanceTask("aiswei:syncAlarmRecoverList", new Runnable() {
//            @Override
//            public void run() {
//                List<String> faultCodes = alarmInfoService.getIdCodes(NhConstants.SYS_TYPE_AISWEI);
//                List<List<String>> split = ListUtils.split(faultCodes, 10);
//                List<String> batchList = new ArrayList<>();
//                for (List<String> strings : split) {
//                    batchList.add(StringUtils.join(strings, ","));
//                }
//                aisweiAlarmRecoverEachRunner.start(batchList, "同步告警恢复信息", 0L, null);
//            }
//        });
//    }

}
