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

import org.apache.commons.lang3.time.DateUtils;
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.ginlong.GinlongApi;
import org.springblade.modules.nh.factory.ginlong.IRet;
import org.springblade.modules.nh.factory.ginlong.dto.stationmonth.GetStationMonthR;
import org.springblade.modules.nh.factory.ginlong.dto.stationmonth.GetStationMonthReq;
import org.springblade.modules.nh.pojo.dto.sync.DayEnergyDTO;
import org.springblade.modules.nh.service.IMonitorEntryMonthStatService;
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.*;

/**
 * 不同于goodwe, ginlong 日发电量统计是以月为维度，也就是说一次性同步一个月内每天的发电量。
 * goodwe 仅同步最近3天的发电量。
 * 日发电量规则：数据延迟一天，也就是说要看今天的发电量需要等到明天。
 */
@Service
public class GinlongStationMonthEachRunner extends EachRunner<String> {

    @Autowired
    GinlongApi ginlongApi;
    @Autowired
	IMonitorEntryMonthStatService monitorEntryMonthStatService;

    @Override
    public void handle(String stationId, Map<String, Object> ctx) throws IOException {
		if(ctx!=null && ctx.containsKey("month")){
			handleData((String) ctx.get("month"), stationId,ctx);
		}else{
			Date today = new Date();
			if (CommonUtil.monthBeginLessThanDays(1, today)) {
				Date lastMonth = DateUtil.minusMonths(today, 1);
				handleData(DateUtil.format(lastMonth,"yyyy-MM"), stationId,ctx);
			}
			handleData(DateUtil.format(today,"yyyy-MM"), stationId,ctx);
		}

    }

    public void handleData(String month, String stationId,Map<String, Object> ctx) throws IOException {
        GetStationMonthReq req = new GetStationMonthReq();
        req.setId(stationId);
        req.setMoney("CNY");
        req.setMonth(month);
        req.setTimeZone(8);
		req.setIndex(CommonUtil.getVendorIndex(ctx));
        IRet<List<GetStationMonthR>> ret = ginlongApi.stationMonth(req);
        if (Objects.equals(ret.getCode(), "0")) {
            List<GetStationMonthR> dataList = ret.getData();
            if (CollectionUtils.isEmpty(dataList)) {
                return;
            }
            List<DayEnergyDTO> dayEnergyDTOList = new ArrayList<>();
            for (GetStationMonthR getStationMonthR : dataList) {
                Date date = new Date(getStationMonthR.getDate());
                if (DateUtils.isSameDay(date, new Date())) {
                    // 不使用当天的发电量
                    continue;
                }
                DayEnergyDTO dayEnergyDTO = new DayEnergyDTO();
                dayEnergyDTO.setStationId(stationId);
                dayEnergyDTO.setUnit(getStationMonthR.getEnergyStr());
                dayEnergyDTO.setValue(getStationMonthR.getEnergy());
                dayEnergyDTO.setDate(DateUtil.format(date, "yyyy-MM-dd"));
                dayEnergyDTOList.add(dayEnergyDTO);
            }
            monitorEntryMonthStatService.syncMonthStatData(dayEnergyDTOList);
        }
    }

}
