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

import lombok.AllArgsConstructor;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.modules.nh.factory.aiswei.AisweiApi;
import org.springblade.modules.nh.factory.aiswei.AisweiRet;
import org.springblade.modules.nh.factory.aiswei.dto.getplantoutputpro.GetPlantOutputProR;
import org.springblade.modules.nh.factory.aiswei.dto.getplantoutputpro.GetPlantOutputProReq;
import org.springblade.modules.nh.factory.aiswei.dto.getplantoutputpro.GetPlantOutputProResp;
import org.springblade.modules.nh.factory.common.EachRunner;
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 org.springframework.util.CollectionUtils;

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

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

	private final IMonitorEntryStatService monitorEntryStatService;
	private final IMonitorEntryYearStatService yearStatService;
	private final IMonitorEntryMonthStatService monthStatService;
	private final AisweiApi aisweiApi;

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

	public void handleData(String outId) throws IOException {
		String month = yearStatService.getFirstMonth(outId);
//		String start = month+"-01";
//		Date startDate = DateUtil.parse(start,"yyyy-MM-dd");
//		Date endDate = DateUtil.plusMonths(startDate,1);

		GetPlantOutputProResp resp = syncOutput(outId, "bymonth", month);
		if(resp!=null){
			List<GetPlantOutputProR> list = resp.getResult();
			if (CollectionUtils.isEmpty(list)) {
				return;
			}

			String firstDate = null;
			for (GetPlantOutputProR item : list) {
				if(item.getValue()>0){
					if(firstDate==null){
						firstDate = item.getTime();
					}
//					else{
//						Date star = DateUtil.parse(firstDate, "yyyy-MM-dd");
//						Date end = DateUtil.parse(item.getTime(), "yyyy-MM-dd");
//						if(end.before(star)){
//							firstDate = item.getTime();
//						}
//					}
				}
			}
			if(firstDate!=null){
				monitorEntryStatService.updateFisDate(outId,firstDate);
			}
		}
	}

	private GetPlantOutputProResp syncOutput(String stationId, String period, String date) {
		try {
			GetPlantOutputProReq req = new GetPlantOutputProReq();
			req.setApikey(stationId);
			req.setDate(date);
			req.setPeriod(period); // "time": "2024-01",
			AisweiRet<GetPlantOutputProResp> pro = aisweiApi.getPlantOutputPro(req);
			if (Objects.equals(pro.getStatus(), 200)) {
				return pro.getData();
			} else {
//				log.error("aiswei 同步日月年数据失败, status:{}, info:{}, stationId:{}", pro.getStatus(), pro.getInfo(), stationId);
			}
			Thread.sleep(1000);
		} catch (Exception ex) {
//			log.error("aiswei 同步日月年数据失败", ex);
		}
		return null;
	}

}
