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

import lombok.AllArgsConstructor;
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.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.MonthEnergyDTO;
import org.springblade.modules.nh.pojo.entity.MonitorEntryStatEntity;
import org.springblade.modules.nh.service.IMonitorEntryStatService;
import org.springblade.modules.nh.service.IMonitorEntryYearStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
@AllArgsConstructor
public class GoodweStationYearEachRunner extends EachRunner<List<String>> {


    private final GoodweApi goodweApi;

	private final IMonitorEntryYearStatService monitorEntryYearStatService;
	private final IMonitorEntryStatService entryStatService;

    @Override
    public void handle(List<String> stationIds, Map<String, Object> ctx) throws IOException {

		int year = cn.hutool.core.date.DateUtil.thisYear();
		if(ctx!=null && ctx.containsKey("year")){
			year = (int) ctx.get("year");
		}

        GetPlantsStaticDatasReqByTime req = new GetPlantsStaticDatasReqByTime();
        req.setIds(stationIds);
        req.setStartdt(DateUtil.parse(year+"-01","yyyy-MM").getTime());
        req.setEnddt(DateUtil.parse(year+"-12","yyyy-MM").getTime());
        req.setPeriod(2);

		req.setIndex(CommonUtil.getVendorIndex(ctx));
        GoodweRet<GetPlantsStaticDatasData> resp = goodweApi.getPlantsStaticDatas(req);
        if (resp.getCode() == 0) {
            GetPlantsStaticDatasData data = resp.getData();
            List<MonthEnergyDTO> monthEnergyDTOList = getMonthEnergyDTOS(data);
            monitorEntryYearStatService.syncYearStatData(monthEnergyDTOList);
        }
    }

    private List<MonthEnergyDTO> getMonthEnergyDTOS(GetPlantsStaticDatasData data) {
        List<GetPlantsStaticDatasSiteData> list = data.getList();
        List<MonthEnergyDTO> monthEnergyDTOList = new ArrayList<>();
        for (GetPlantsStaticDatasSiteData siteData : list) {
			MonitorEntryStatEntity entryStat = entryStatService.getByOutId(siteData.getSid());

            List<GetPlantsStaticDatasR> slist = siteData.getData();
            for (GetPlantsStaticDatasR s : slist) {
				Date tempDate = DateUtil.parse(s.getTime(),"yyyy-MM");
				Date monthDate = DateUtil.plusMonths(tempDate,1);
                MonthEnergyDTO monthEnergyDTO = new MonthEnergyDTO();
                monthEnergyDTO.setSysType(NhConstants.SYS_TYPE_GOODWE);
                monthEnergyDTO.setUnit(siteData.getDataunit());
				monthEnergyDTO.setValue(s.getValue());
//				if(entryStat.getCreateTime().before(monthDate)){
//					monthEnergyDTO.setValue(s.getValue());
//				}else{
//					monthEnergyDTO.setValue(0D);
//				}

                monthEnergyDTO.setMonth(s.getTime());
                monthEnergyDTO.setStationId(siteData.getSid());
                monthEnergyDTOList.add(monthEnergyDTO);
            }
        }
        return monthEnergyDTOList;
    }

}
