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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springblade.common.constant.NhConstants;
import org.springblade.common.utils.CommonUtil;
import org.springblade.common.utils.TaskControl;
import org.springblade.core.tool.utils.DateUtil;
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.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.HashMap;
import java.util.List;
import java.util.Map;

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

	private final int indexCount = 3;
    private final GoodweUserStationListPageAction goodweUserStationListPageAction;
	private final GoodweInverterEachRunner goodweInverterEachRunner;
	private final IMonitorEntryStatService monitorEntryStatService;
	private final GoodweStationAlarmEachRunner goodweStationAlarmEachRunner;
	private final GoodweStationMonthEachRunner goodweStationMonthEachRunner;
	private final GoodweStationYearEachRunner goodweStationYearEachRunner;
	private final IMonitorInverterStatService monitorInverterStatService;
	private final GoodweInverterDayEachRunner goodweInverterDayEachRunner;
	private final GoodweInverterPacByDayEachRunner goodweInverterPacByDayEachRunner;
	private final GoodweAlarmPageAction goodweAlarmPageAction;

    /**
     * 每5分钟同步一次电站列表，之所以同步如此频繁，目的是记录电站每小时的发电量。
     */
    @Scheduled(cron = "11 */9 * * * ?")
    public void syncUserStationList() {
        TaskControl.runTask("goodwe:syncUserStationList", new Runnable() {
            @Override
            public void run() {
				for(int i=1;i<=indexCount;i++){
					goodweUserStationListPageAction.start("goodwe 获取用户电站列表", 100, CommonUtil.getCtx(i),1);
				}

            }
        });
    }

    /**
     * 每3小时同步一次电站设备列表
     */
    @Scheduled(cron = "0 47 */3 * * ?")
    public void syncInverterDetailList() {
        TaskControl.runTask("goodwe:syncInverterDetailList", new Runnable() {
            @Override
            public void run() {
				for(int i=1;i<=indexCount;i++) {
					List<String> stationIds = monitorEntryStatService.getStationIds(NhConstants.SYS_TYPE_GOODWE,i);
					goodweInverterEachRunner.start(stationIds, "电站设备列表（总）", 1L, CommonUtil.getCtx(i));
				}
            }
        });
    }

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

    /**
     * 每天凌晨1点统计一次日发电量，限制一分钟5次？
     */
    @Scheduled(cron = "0 38 8 * * ?")
    public void syncStationMonthData() {
        TaskControl.runTask("goodwe:syncStationMonthData", new Runnable() {
            @Override
            public void run() {
				for(int i=1;i<=indexCount;i++) {
					List<String> stationIds = monitorEntryStatService.getStationIds(NhConstants.SYS_TYPE_GOODWE,i);
					List<List<String>> partitions = ListUtils.partition(stationIds, 50);
					goodweStationMonthEachRunner.start(partitions, "获取单个电站某月的日数据", 15L, CommonUtil.getCtx(i));
				}
            }
        });
    }

	/**
	 * 每天凌晨2点统计一次月发电量，限制一分钟5次？
	 */
	@Scheduled(cron = "32 15 2 * * ?")
	public void syncStationYearData() {
		TaskControl.runTask("goodwe:syncStationYearData", new Runnable() {
			@Override
			public void run() {
				for(int i=1;i<=indexCount;i++) {
					List<String> stationIds = monitorEntryStatService.getStationIds(NhConstants.SYS_TYPE_GOODWE,i);
					List<List<String>> partitions = ListUtils.partition(stationIds, 50);
					Map<String,Object> ctx = CommonUtil.getCtx(i);
//					ctx.put("year",2023);
					goodweStationYearEachRunner.start(partitions, "获取单个电站某年的月数据", 12L, ctx);
				}
			}
		});
	}
    /**
     * 每两个小时获取一次今日的电量，按单条记录获取
     */
    @Scheduled(cron = "40 20 */2 * * ?")
    public void syncInverterDay() {
        TaskControl.runTask("goodwe:syncInverterDay", new Runnable() {
            @Override
            public void run() {
				for(int i=1;i<=indexCount;i++) {
					List<String> inverterSns = monitorInverterStatService.getInverterSns(NhConstants.SYS_TYPE_GOODWE,i);
					Map<String, Object> ctx = CommonUtil.getCtx(i);
					ctx.put("baseDate", DateUtil.format(new Date(), "yyyy-MM-dd"));
					goodweInverterDayEachRunner.start(inverterSns, "实时更新逆变器今日的数据", 1L, ctx);
				}
            }
        });
    }

    /**
     * 每天凌晨2点统计一次，逆变器昨日的功率
     */
    @Scheduled(cron = "0 4 2 * * ?")
    public void syncInverterPacDay() {
        TaskControl.runTask("goodwe:syncInverterPacDay", new Runnable() {
            @Override
            public void run() {
				for(int i=1;i<=indexCount;i++) {
					List<String> inverterSns = monitorInverterStatService.getInverterSns(NhConstants.SYS_TYPE_GOODWE);
					Map<String, Object> ctx = CommonUtil.getCtx(i);
					ctx.put("baseDate", DateUtil.format(DateUtil.plusDays(new Date(), -1), "yyyy-MM-dd"));
					goodweInverterPacByDayEachRunner.start(inverterSns, "同步逆变器昨日功率", 1L, ctx);
				}
            }
        });
    }


	private final GoodweStationFisGenDateEachRunner stationFisGenDateEachRunner;
	/**
	 * 获取电站的首日发电时间
	 */
	@Scheduled(cron = "20 30 13 * * ?")
	public void syncStationFisGenDate() {
		TaskControl.runTask("huawei:syncStationFisGenDate", new Runnable() {
			@Override
			public void run() {
				for(int i=1;i<=indexCount;i++){
					LambdaQueryWrapper<MonitorEntryStatEntity> queryWrapper = new LambdaQueryWrapper<>();
					queryWrapper.eq(MonitorEntryStatEntity::getType, NhConstants.SYS_TYPE_GOODWE)
						.isNull(MonitorEntryStatEntity::getFisGenerateTime)
						.gt(MonitorEntryStatEntity::getEtd,0)
						.select(MonitorEntryStatEntity::getOutStationId)
						.eq(MonitorEntryStatEntity::getVendorIndex,i);
					List<String> stationIds = monitorEntryStatService.listObjs(queryWrapper);
					stationFisGenDateEachRunner.start(stationIds, "获取电站的首日发电时间", 12L, CommonUtil.getCtx(i));
				}
			}
		});
	}

}
