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.getinverterdatabycolumn.GetInverterDataByColumnR;
import org.springblade.modules.nh.factory.goodwe.dto.getinverterdatabycolumn.GetInverterDataByColumnReq;
import org.springblade.modules.nh.factory.goodwe.dto.getinverterdatabycolumn.GetInverterDataByColumnResp;
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 GoodweInverterDayEachRunner extends EachRunner<String> {

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

    @Override
    public void handle(String sn, Map<String, Object> ctx) throws IOException {
        GetInverterDataByColumnReq req = new GetInverterDataByColumnReq();
        String date = (String) ctx.get("baseDate");
        req.setDate(date);
        req.setId(sn);
        req.setColumn(0);
		req.setIndex(CommonUtil.getVendorIndex(ctx));
        GoodweRet<GetInverterDataByColumnResp> resp = goodweApi.getInverterDataByColumn(req);
        if (resp.getCode() == 0) {
            GetInverterDataByColumnResp data = resp.getData();
            List<GetInverterDataByColumnR> energyRawList = data.getColumn1();
            List<InverterHourlyEnergyDTO> energyList = new ArrayList<>();
            for (GetInverterDataByColumnR getInverterDataByColumnR : energyRawList) {
                InverterHourlyEnergyDTO energyItem = new InverterHourlyEnergyDTO();
                energyItem.setEnergy(getInverterDataByColumnR.getColumn());
                energyItem.setSyncTime(getInverterDataByColumnR.getDate());
				energyItem.setSn(sn);
                energyList.add(energyItem);
            }
            monitorInverterDailyStatService.syncHourlyData(NhConstants.SYS_TYPE_GOODWE, energyList);
        }
    }

}
