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

import org.apache.commons.lang3.time.DateUtils;
import org.springblade.common.constant.NhConstants;
import org.springblade.common.utils.CommonUtil;
import org.springblade.modules.nh.factory.common.EachRunner;
import org.springblade.modules.nh.factory.ginlong.GinlongApi;
import org.springblade.modules.nh.factory.ginlong.IRet;
import org.springblade.modules.nh.factory.ginlong.dto.stationyear.GetStationYearR;
import org.springblade.modules.nh.factory.ginlong.dto.stationyear.GetStationYearReq;
import org.springblade.modules.nh.pojo.dto.sync.MonthEnergyDTO;
import org.springblade.modules.nh.service.IMonitorEntryYearStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class GinlongStationYearEachRunner extends EachRunner<String> {

    @Autowired
    GinlongApi ginlongApi;
    @Autowired
	IMonitorEntryYearStatService monitorEntryYearStatService;

    /**
     * 每天凌晨一点统计当年的每个月的发电量
     */
    @Override
    public void handle(String stationId, Map<String, Object> ctx) throws IOException {
        final Date today = new Date();
        DateFormat df = new SimpleDateFormat("yyyy");
        String thisYear = df.format(today);
        handleData(thisYear, stationId,ctx);
        if (CommonUtil.yearBeginLessThanDays(3, today)) {
            sleepSilently();
            Date lastYear = DateUtils.addYears(today, -1);
            handleData(df.format(lastYear), stationId,ctx);
        }
    }

    private static void sleepSilently() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // ignore
        }
    }

    public void handleData(String year, String stationId,Map<String, Object> ctx) throws IOException {
        GetStationYearReq req = new GetStationYearReq();
        req.setId(stationId);
        req.setMoney("CNY");
        req.setYear(year);
        req.setTimeZone(8);
		req.setIndex(CommonUtil.getVendorIndex(ctx));
        IRet<List<GetStationYearR>> ret = ginlongApi.stationYear(req);
        if (Objects.equals(ret.getCode(), "0")) {
            List<GetStationYearR> dataList = ret.getData();
            if (CollectionUtils.isEmpty(dataList)) {
                return;
            }
            List<MonthEnergyDTO> monthEnergyDTOList = new ArrayList<>();
            DateFormat df = new SimpleDateFormat("yyyy-MM");
            for (GetStationYearR s : dataList) {
                Date date = new Date(s.getDate());
                MonthEnergyDTO monthEnergyDTO = new MonthEnergyDTO();
                monthEnergyDTO.setStationId(stationId);
                monthEnergyDTO.setUnit(s.getEnergyStr());
                monthEnergyDTO.setValue(s.getEnergy());
                monthEnergyDTO.setMonth(df.format(date));
                monthEnergyDTO.setSysType(NhConstants.SYS_TYPE_GINLONG);
                monthEnergyDTOList.add(monthEnergyDTO);
            }
            monitorEntryYearStatService.syncYearStatData(monthEnergyDTOList);
        }
    }

}
