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

import lombok.extern.slf4j.Slf4j;
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.goodwe.GoodweApi;
import org.springblade.modules.nh.factory.goodwe.GoodweRet;
import org.springblade.modules.nh.factory.goodwe.dto.getInverterpacbyday.GetInverterPacByDayR;
import org.springblade.modules.nh.factory.goodwe.dto.getInverterpacbyday.GetInverterPacByDayReq;
import org.springblade.modules.nh.pojo.dto.sync.InverterHourlyEnergyDTO;
import org.springblade.modules.nh.service.IMonitorInverterDailyStatService;
import org.springblade.modules.nh.service.IMonitorInverterStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class GoodweInverterPacByDayEachRunner extends EachRunner<String> {

    @Autowired
    GoodweApi goodweApi;
    @Autowired
	IMonitorInverterStatService monitorInverterStatService;
    @Autowired
	IMonitorInverterDailyStatService monitorInverterDailyStatService;

    @Override
    public void handle(String sn, Map<String, Object> ctx) throws IOException {
        GetInverterPacByDayReq req = new GetInverterPacByDayReq();
        String date = (String) ctx.get("baseDate");
        req.setDate(date);
        req.setId(sn);
		req.setIndex(CommonUtil.getVendorIndex(ctx));
        GoodweRet<List<GetInverterPacByDayR>> resp = goodweApi.getInverterPacByDay(req);
        if (resp.getCode() == 0) {
            List<GetInverterPacByDayR> pacList = resp.getData();
            List<InverterHourlyEnergyDTO> list = new ArrayList<>();
            for (GetInverterPacByDayR datum : pacList) {
                InverterHourlyEnergyDTO dto = new InverterHourlyEnergyDTO();
                dto.setEnergy(null);
                dto.setJlgl(datum.getPac());
                dto.setSyncTime(datum.getDate());
                dto.setSn(sn);
                list.add(dto);
            }
            monitorInverterDailyStatService.syncHourlyData(NhConstants.SYS_TYPE_GOODWE, list);
        }
    }

}
