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

import lombok.AllArgsConstructor;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.time.DateUtils;
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.Func;
import org.springblade.core.tool.utils.StringUtil;
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.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.springframework.stereotype.Service;

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

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

	private final GoodweApi goodweApi;
	private final IMonitorEntryStatService monitorEntryStatService;
	private final IMonitorEntryYearStatService yearStatService;
	private final IMonitorEntryMonthStatService monitorEntryMonthStatService;


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

	public void handleData(String stationId,int index) throws IOException {
		String month = yearStatService.getFirstMonth(stationId);
		if(month==null){
			return;
		}
		String start = month+"-01";
		Date startDate = DateUtil.parse(start,"yyyy-MM-dd");

//		if("2024-01".equals(month)){
//			startDate = new Date();
//		}


		GetPlantsStaticDatasReqByTime req = new GetPlantsStaticDatasReqByTime();
		req.setIds(Func.toStrList(stationId));
		req.setStartdt(startDate.getTime());
		req.setEnddt(DateUtils.truncate(DateUtils.addDays(startDate, 30), Calendar.DATE).getTime()); // 包含enddt指定的值
		req.setPeriod(1);
		req.setIndex(index);
		GoodweRet<GetPlantsStaticDatasData> resp = goodweApi.getPlantsStaticDatas(req);
		if (resp.getCode() == 0) {
			GetPlantsStaticDatasData data = resp.getData();
			List<GetPlantsStaticDatasSiteData> list = data.getList();
			Date firstDate = null;
			List<DayEnergyDTO> dayEnergList = new ArrayList<>();
			if(list!=null && list.size()>0){
				for (GetPlantsStaticDatasSiteData siteData : list) {
					List<GetPlantsStaticDatasR> slist = siteData.getData();
					for (GetPlantsStaticDatasR s : slist) {
						if(s.getValue()>0){
							Date date2 = DateUtil.parse(s.getTime(),"yyyy-MM-dd");
							if(firstDate!=null){
								if(date2.before(firstDate)){
									firstDate = date2;
								}
							}else{
								firstDate = date2;
							}
						}
						DayEnergyDTO dayEnergyDTO = new DayEnergyDTO();
						dayEnergyDTO.setStationId(siteData.getSid());
						dayEnergyDTO.setUnit(siteData.getDataunit());
						dayEnergyDTO.setValue(s.getValue());
						dayEnergyDTO.setDate(s.getTime());
						dayEnergList.add(dayEnergyDTO);
					}
				}
			}
			if(firstDate!=null){
				monitorEntryStatService.updateFisDate(stationId,DateUtil.formatDate(firstDate));
				monitorEntryMonthStatService.syncMonthStatData(dayEnergList);
			}

		}
	}

}
