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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.NhConstants;
import org.springblade.common.utils.CommonUtil;
import org.springblade.common.utils.TaskControl;
import org.springblade.modules.nh.pojo.entity.MonitorEntryStatEntity;
import org.springblade.modules.nh.service.IMonitorEntryStatService;
import org.springblade.modules.nh.service.IMonitorInverterStatService;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

@EnableScheduling
@Component
@Slf4j
@AllArgsConstructor
public class SajScheduler {

    private final IMonitorEntryStatService entryStatService;
    private final IMonitorInverterStatService inverterStatService;
    private final SajStationListPageAction stationListPageAction;
    private final SajDeviceListPageAction deviceListPageAction;
    private final SajStationDetailEachRunner stationDetailEachRunner;
    private final SajStationDataEachRunner stationDataEachRunner;
    private final SajDeviceHistoryEachRunner deviceHistoryEachRunner;
    private final SajDeviceRealtimeEachRunner deviceRealtimeEachRunner;
    private final SajStationFisGenDateEachRunner fisGenDateEachRunner;
    private final SajStationStatusEachRunner stationStatusEachRunner;


    /**
     * 每5分钟同步一次电站列表，之所以同步如此频繁，目的是记录电站每小时的发电量。
     */
    @Scheduled(cron = "40 33 * * * ?")
    public void syncUserStationList() {
        TaskControl.runTask("saj:syncUserStationList", new Runnable() {
            @Override
            public void run() {
				stationListPageAction.start("saj 获取用户电站列表", 100, null,1);
            }
        });
    }

	/**
	 * 每5分钟同步一次电站列表，之所以同步如此频繁，目的是记录电站每小时的发电量。
	 */
	@Scheduled(cron = "50 */13 * * * ?")
	public void syncDeviceList() {
		TaskControl.runTask("saj:syncUserStationList", new Runnable() {
			@Override
			public void run() {
				deviceListPageAction.start("saj 获取用户电站列表", 100, null,1);
			}
		});
	}

	@Scheduled(cron = "10 11 */3 * * ?")
	public void syncUserStationDetail() {
		TaskControl.runTask("saj:syncUserStationDetail", new Runnable() {
			@Override
			public void run() {
				LambdaQueryWrapper<MonitorEntryStatEntity> queryWrapper = new LambdaQueryWrapper<>();
					queryWrapper.eq(MonitorEntryStatEntity::getType, NhConstants.SYS_TYPE_SAJ)
						.eq(MonitorEntryStatEntity::getState,0)
						.select(MonitorEntryStatEntity::getOutStationId)
						.eq(MonitorEntryStatEntity::getVendorIndex,1);
					List<String> stationIds = entryStatService.listObjs(queryWrapper);
				stationDetailEachRunner.start(stationIds, "获取单个电站详情", 0L, null);
			}
		});
	}

//	@Scheduled(cron = "10 38 * * * ?")
//	public void syncStationCurrent() {
//		TaskControl.runTask("saj:syncUserStationData", new Runnable() {
//			@Override
//			public void run() {
//				List<String> stationIds = entryStatService.getStationIds(NhConstants.SYS_TYPE_SAJ);
//				stationDataEachRunner.start(stationIds, "获取电站发电量", 0L, null);
//			}
//		});
//	}

	@Scheduled(cron = "50 30 4 * * ?")
	public void syncUserStationMonthYearData() {
		TaskControl.runTask("saj:syncUserStationMonthYearData", new Runnable() {
			@Override
			public void run() {
				List<String> stationIds = entryStatService.getStationIds(NhConstants.SYS_TYPE_SAJ);
				deviceHistoryEachRunner.start(stationIds, "获取电站发电量", 0L, null);
			}
		});
	}


	@Scheduled(cron = "55 15 * * * ?")
	public void syncDeviceRealtime() {
		TaskControl.runTask("saj:syncDeviceRealtime", new Runnable() {
			@Override
			public void run() {
				List<String> sns = inverterStatService.getInverterSns(NhConstants.SYS_TYPE_SAJ);
				deviceRealtimeEachRunner.start(sns, "获取设备实时发电量", 0L, null);
			}
		});
	}

	@Scheduled(cron = "0 40 * * * ?")
	public void syncStationFisGenDate() {
		TaskControl.runTask("huawei:syncStationFisGenDate", new Runnable() {
			@Override
			public void run() {
				LambdaQueryWrapper<MonitorEntryStatEntity> queryWrapper = new LambdaQueryWrapper<>();
				queryWrapper.eq(MonitorEntryStatEntity::getType, NhConstants.SYS_TYPE_SAJ)
					.isNull(MonitorEntryStatEntity::getFisGenerateTime)
					.gt(MonitorEntryStatEntity::getEta,0)
					.select(MonitorEntryStatEntity::getOutStationId);
				List<String> stationIds = entryStatService.listObjs(queryWrapper);
				fisGenDateEachRunner.start(stationIds, "获取电站的首日发电时间", 0L, null);
			}
		});
	}

	@Scheduled(cron = "25 */13 * * * ?")
	public void syncStationStatus() {
		TaskControl.runTask("huawei:syncStationFisGenDate", new Runnable() {
			@Override
			public void run() {
				List<String> stationIds = entryStatService.getStationIds(NhConstants.SYS_TYPE_SAJ);
				stationStatusEachRunner.start(stationIds, "获取电站发电量", 0L, null);
			}
		});
	}

}
