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

import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.nh.factory.aiswei.AisweiApi;
import org.springblade.modules.nh.factory.aiswei.AisweiRet;
import org.springblade.modules.nh.factory.aiswei.dto.getinverterrecoverstatuspro.GetInverterRecoverStatusProR;
import org.springblade.modules.nh.factory.aiswei.dto.getinverterrecoverstatuspro.GetInverterRecoverStatusProReq;
import org.springblade.modules.nh.factory.common.EachRunner;
import org.springblade.modules.nh.pojo.dto.sync.InverterRecoverDTO;
import org.springblade.modules.nh.service.IAlarmInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

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

    @Autowired
    AisweiApi aisweiApi;
    @Autowired
	IAlarmInfoService monitorAlarmInfoService;

    @Override
    public void handle(String faultCodes, Map<String, Object> ctx) throws IOException {
        GetInverterRecoverStatusProReq req = new GetInverterRecoverStatusProReq();
        req.setFaultCodes(faultCodes);
        AisweiRet<List<GetInverterRecoverStatusProR>> resp = aisweiApi.getInverterRecoverStatusPro(req);
        if (!Objects.equals(resp.getStatus(), 200)) {
            log.error("sync recover error,info:{}", resp.getInfo());
            return;
        }

        List<GetInverterRecoverStatusProR> data = resp.getData();
        if (CollectionUtils.isEmpty(data)) {
            return;
        }
        List<InverterRecoverDTO> recoverList = new ArrayList<>();
        for (GetInverterRecoverStatusProR datum : data) {
            if (Objects.equals(datum.getRecover(), 1)) { // 0-未恢复Y 1- 已恢复
                InverterRecoverDTO inverterRecoverDTO = new InverterRecoverDTO();
                inverterRecoverDTO.setRecoverTime(datum.getRecoverTime());
                inverterRecoverDTO.setFaultCode(datum.getFaultCode());
                recoverList.add(inverterRecoverDTO);
            }
        }
        monitorAlarmInfoService.syncRecoverTime(recoverList);
    }

}
