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

import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springblade.common.constant.NhConstants;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.nh.factory.common.EachRunner;
import org.springblade.modules.nh.factory.ginlong.dto.userstationlist.GetStationR;
import org.springblade.modules.nh.factory.saj.SajApi;
import org.springblade.modules.nh.factory.saj.req.QueryDeviceHistoryReq;
import org.springblade.modules.nh.factory.saj.req.QueryPlantEnergyReq;
import org.springblade.modules.nh.factory.saj.resp.*;
import org.springblade.modules.nh.pojo.dto.sync.DayEnergyDTO;
import org.springblade.modules.nh.pojo.dto.sync.MonthEnergyDTO;
import org.springblade.modules.nh.service.*;
import org.springframework.stereotype.Service;

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

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

	private final SajApi sajApi;
	private final IMonitorInverterStatService inverterStatService;
	private final IMonitorEntryStatService monitorEntryStatService;
	private final IMonitorEntryDailyStatService dailyStatService;
	private final IMonitorEntryMonthStatService monthStatService;
	private final IMonitorEntryYearStatService yearStatService;

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

	public void handleData(String stationId,Map<String, Object> ctx) throws IOException {
		List<String> sns = inverterStatService.getSnsByStation(stationId);
		syncLast30Days(stationId,sns);
		syncLast12Month(stationId,sns);
	}

	private void syncLast30Days(String outStationId,List<String> sns){
		if(CollectionUtil.isEmpty(sns)) return;

		Date end = DateUtil.now();
		Date start = DateUtil.minusMonths(end,1);
		List<DayEnergyDTO> dayEnergList = Lists.newArrayList();
		QueryDeviceHistoryReq req = new QueryDeviceHistoryReq();
		req.setTimeUnit(1);
		req.setStartTime(DateUtil.formatDateTime(start));
		req.setEndTime(DateUtil.formatDateTime(end));

		for(String sn : sns){
			req.setDeviceSn(sn);
			SajRet<SajDeviceData> resp = sajApi.getDeviceHistory(req);
			if(resp.getCode()==200){
				SajDeviceData data = resp.getData();
				List<SajDeviceDataItem> dataList = data.getData();
				for(SajDeviceDataItem dataItem : dataList){
					if(dataItem.getPVEnergy()!=null){
						DayEnergyDTO dayEnergyDTO = new DayEnergyDTO();
						dayEnergyDTO.setStationId(outStationId);
						dayEnergyDTO.setUnit("kwh");
						dayEnergyDTO.setValue(dataItem.getPVEnergy());
						Date time = DateUtil.parse(dataItem.getDataTime(),"yyyy-MM-dd hh:mm:ss");
						dayEnergyDTO.setDate(DateUtil.format(time,"yyyy-MM-dd"));
						dayEnergList.add(dayEnergyDTO);
					}
				}
			}
		}

		if(dayEnergList.size()>0){
			monthStatService.syncMonthStatData(dayEnergList);
		}
	}

	private void syncLast12Month(String outStationId,List<String> sns){
		if(CollectionUtil.isEmpty(sns)) return;

		Date end = DateUtil.now();
		Date start = DateUtil.minusMonths(end,24);
		List<MonthEnergyDTO> monthEnergyDTOList = Lists.newArrayList();
		QueryDeviceHistoryReq req = new QueryDeviceHistoryReq();
		req.setTimeUnit(2);
		req.setStartTime(DateUtil.formatDateTime(start));
		req.setEndTime(DateUtil.formatDateTime(end));

		for(String sn : sns){
			req.setDeviceSn(sn);
			SajRet<SajDeviceData> resp = sajApi.getDeviceHistory(req);
			if(resp.getCode()==200){
				SajDeviceData data = resp.getData();
				List<SajDeviceDataItem> dataList = data.getData();
				for(SajDeviceDataItem dataItem : dataList){
					if(dataItem.getPVEnergy()!=null) {
						MonthEnergyDTO monthEnergyDTO = new MonthEnergyDTO();
						monthEnergyDTO.setSysType(NhConstants.SYS_TYPE_SAJ);
						monthEnergyDTO.setUnit("kwh");
						monthEnergyDTO.setValue(dataItem.getPVEnergy());
						Date time = DateUtil.parse(dataItem.getDataTime(), "yyyy-MM-dd hh:mm:ss");
						monthEnergyDTO.setMonth(DateUtil.format(time, "yyyy-MM"));
						monthEnergyDTO.setStationId(outStationId);
						monthEnergyDTOList.add(monthEnergyDTO);
					}
				}
			}
		}

		if(monthEnergyDTOList.size()>0){
			yearStatService.syncYearStatData(monthEnergyDTOList);
		}
	}

	private GetStationR newGetStationR(){
		GetStationR r = new GetStationR();
		r.setState(1);
		r.setDayEnergy(0D);
		r.setMonthEnergy(0D);
		r.setYearEnergy(0D);
		r.setAllEnergy(0D);
		r.setDayEnergyStr("kwh");
		r.setMonthEnergyStr("kwh");
		r.setYearEnergyStr("kwh");
		r.setAllEnergyStr("kwh");
		r.setVendorIndex(1);
		r.setPower(0D);
		r.setPowerStr("kw");
		r.setDataTimestamp(System.currentTimeMillis());
		return r;
	}

}
