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.common.utils.CommonUtil;
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.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.pojo.entity.MonitorEntryStatEntity;
import org.springblade.modules.nh.service.*;
import org.springframework.stereotype.Service;

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

/**
 * 实时获取电站当前的发电量和功率
 */
@Service
@AllArgsConstructor
public class SajStationDataEachRunner 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 {
		QueryPlantEnergyReq req = new QueryPlantEnergyReq();
		req.setPlantId(stationId);
		List<String> sns = inverterStatService.getSnsByStation(stationId);
		req.setDeviceSns(StringUtil.join(sns,","));
		syncNow(req);
	}

	private void syncLast30Days(QueryPlantEnergyReq req){
		Date now = DateUtil.parse(DateUtil.format(new Date(),"yyyyMMdd"),"yyyyMMdd");
		Date startDate = DateUtil.minusHours(now,1);
		List<DayEnergyDTO> dayEnergList = Lists.newArrayList();
		for(int i=1;i<=30;i++){
			Date localDate = DateUtil.minusDays(startDate,i);
			req.setClientDate(DateUtil.formatDateTime(localDate));
			SajRet<SajPlantData> resp =  sajApi.getPlantStatisticsData(req);
			if(resp.getCode()==200){
				SajPlantData plant = resp.getData();
				DayEnergyDTO dayEnergyDTO = new DayEnergyDTO();
				dayEnergyDTO.setStationId(req.getPlantId());
				dayEnergyDTO.setUnit("kwh");
				dayEnergyDTO.setValue(plant.getTodayPvEnergy());
				dayEnergyDTO.setDate(DateUtil.formatDate(localDate));
				dayEnergList.add(dayEnergyDTO);
			}
		}
		if(dayEnergList.size()>0){
			monthStatService.syncMonthStatData(dayEnergList);
		}
	}

	private void syncLast12Month(QueryPlantEnergyReq req){
		Date now = DateUtil.parse(DateUtil.format(new Date(),"yyyyMM"),"yyyyMM");
		Date startDate = DateUtil.minusHours(now,1);
		List<MonthEnergyDTO> monthEnergyDTOList = Lists.newArrayList();
		for(int i=0;i<12;i++){
			Date localDate = DateUtil.minusMonths(startDate,i);
			req.setClientDate(DateUtil.formatDateTime(localDate));
			SajRet<SajPlantData> resp =  sajApi.getPlantStatisticsData(req);
			if(resp.getCode()==200){
				SajPlantData plant = resp.getData();
				MonthEnergyDTO monthEnergyDTO = new MonthEnergyDTO();
				monthEnergyDTO.setSysType(NhConstants.SYS_TYPE_SAJ);
				monthEnergyDTO.setUnit("kwh");
				monthEnergyDTO.setValue(plant.getMonthPvEnergy());
				monthEnergyDTO.setMonth(DateUtil.format(localDate,"yyyy-MM"));
				monthEnergyDTO.setStationId(req.getPlantId());
				monthEnergyDTOList.add(monthEnergyDTO);
			}
		}
		if(monthEnergyDTOList.size()>0){
			yearStatService.syncYearStatData(monthEnergyDTOList);
		}
	}

	private void syncNow(QueryPlantEnergyReq req){
		req.setClientDate(DateUtil.formatDateTime(new Date()));
		SajRet<SajPlantCurrent> resp =  sajApi.currentPlantData(req);
		if(resp.getCode()==200){
			GetStationR r = newGetStationR();
			r.setId(req.getPlantId());
			SajPlantCurrent plant = resp.getData();
			r.setDayEnergy(plant.getTodayPvEnergy());
			r.setMonthEnergy(plant.getMonthPvEnergy());
			r.setYearEnergy(plant.getYearPvEnergy());
			r.setAllEnergy(plant.getTotalPvEnergy());
			r.setPower(plant.getPowerNow());
			List<GetStationR> syncList = Lists.newArrayList();
			syncList.add(r);
			monitorEntryStatService.syncEntryStat(syncList, NhConstants.SYS_TYPE_SAJ);
			dailyStatService.syncEntryDailyStat(syncList, NhConstants.SYS_TYPE_SAJ);
		}
	}

	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;
	}

}
