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

import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.time.DateUtils;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.nh.factory.common.EachRunner;
import org.springblade.modules.nh.factory.goodwe.GoodweApi;
import org.springblade.modules.nh.factory.goodwe.GoodweRet;
import org.springblade.modules.nh.factory.goodwe.dto.getplantsstaticdatas.GetPlantsStaticDatasData;
import org.springblade.modules.nh.factory.goodwe.dto.getplantsstaticdatas.GetPlantsStaticDatasR;
import org.springblade.modules.nh.factory.goodwe.dto.getplantsstaticdatas.GetPlantsStaticDatasReqByTime;
import org.springblade.modules.nh.factory.goodwe.dto.getplantsstaticdatas.GetPlantsStaticDatasSiteData;
import org.springblade.modules.nh.factory.saj.SajApi;
import org.springblade.modules.nh.factory.saj.req.QueryDeviceHistoryReq;
import org.springblade.modules.nh.factory.saj.resp.SajDeviceData;
import org.springblade.modules.nh.factory.saj.resp.SajDeviceDataItem;
import org.springblade.modules.nh.factory.saj.resp.SajRet;
import org.springblade.modules.nh.pojo.dto.sync.DayEnergyDTO;
import org.springblade.modules.nh.service.IMonitorEntryMonthStatService;
import org.springblade.modules.nh.service.IMonitorEntryStatService;
import org.springblade.modules.nh.service.IMonitorEntryYearStatService;
import org.springblade.modules.nh.service.IMonitorInverterStatService;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * 实时获取电站当前的发电量和功率
 */
@Service
@AllArgsConstructor
public class SajStationFisGenDateEachRunner extends EachRunner<String> {

	private final SajApi sajApi;
	private final IMonitorEntryStatService monitorEntryStatService;
	private final IMonitorEntryYearStatService yearStatService;
	private final IMonitorEntryMonthStatService monitorEntryMonthStatService;
	private final IMonitorInverterStatService inverterStatService;


	@Override
	public void handle(String stationId, Map<String, Object> ctx) throws IOException {
		handleData(stationId, 1);
	}

	public void handleData(String stationId,int index) throws IOException {
		String month = yearStatService.getFirstMonth(stationId);
		if(month==null){
			return;
		}
		List<String> sns = inverterStatService.getSnsByStation(stationId);
		if(CollectionUtil.isEmpty(sns)) return;

		String startStr = month+"-01";
		Date start = DateUtil.parse(startStr,"yyyy-MM-dd");
		Date end = DateUtil.plusMonths(start,1);
		QueryDeviceHistoryReq req = new QueryDeviceHistoryReq();
		req.setTimeUnit(1);
		req.setStartTime(DateUtil.formatDateTime(start));
		req.setEndTime(DateUtil.formatDateTime(end));
		req.setDeviceSn(sns.get(0));
		SajRet<SajDeviceData> resp = sajApi.getDeviceHistory(req);

		if(resp.getCode()==200){
			SajDeviceData data = resp.getData();
			List<SajDeviceDataItem> dataList = data.getData();
			Date firstDate = null;
			List<DayEnergyDTO> dayEnergList = new ArrayList<>();
			if(dataList!=null && dataList.size()>0){
				for (SajDeviceDataItem s : dataList) {
						if(s.getPVEnergy()>0){
							Date date2 = DateUtil.parse(s.getDataTime(),"yyyy-MM-dd hh:mm:ss");
							if(firstDate!=null){
								if(date2.before(firstDate)){
									firstDate = date2;
								}
							}else{
								firstDate = date2;
							}
						}
						DayEnergyDTO dayEnergyDTO = new DayEnergyDTO();
						dayEnergyDTO.setStationId(stationId);
						dayEnergyDTO.setUnit("kwh");
						dayEnergyDTO.setValue(s.getPVEnergy());
						dayEnergyDTO.setDate(s.getDataTime());
						dayEnergList.add(dayEnergyDTO);
				}
			}
			if(firstDate!=null){
				monitorEntryStatService.updateFisDate(stationId,DateUtil.formatDate(firstDate));
				monitorEntryMonthStatService.syncMonthStatData(dayEnergList);
			}

		}
	}

}
