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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.NhConstants;
import org.springblade.modules.nh.factory.aiswei.AisweiApi;
import org.springblade.modules.nh.factory.aiswei.AisweiRet;
import org.springblade.modules.nh.factory.aiswei.dto.getinvertercurrenterrorpro.GetInverterCurrentErrorProR;
import org.springblade.modules.nh.factory.aiswei.dto.getinvertercurrenterrorpro.GetInverterCurrentErrorProReq;
import org.springblade.modules.nh.factory.common.EachRunner;
import org.springblade.modules.nh.factory.ginlong.dto.alarmlist.GetAlarmListR;
import org.springblade.modules.nh.pojo.entity.MonitorEntryStatEntity;
import org.springblade.modules.nh.service.IAlarmCodeService;
import org.springblade.modules.nh.service.IAlarmInfoService;
import org.springblade.modules.nh.service.IMonitorEntryStatService;
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;
import java.util.Objects;

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

    @Autowired
    AisweiApi aisweiApi;
    @Autowired
	IAlarmInfoService monitorAlarmInfoService;
    @Autowired
	IAlarmCodeService monitorFaultCodeService;
	@Autowired
	IMonitorEntryStatService entryStatService;


    @Override
    public void handle(String stationId, Map<String, Object> ctx) throws IOException {
        GetInverterCurrentErrorProReq req = new GetInverterCurrentErrorProReq();
        req.setApikey(stationId);
        AisweiRet<List<GetInverterCurrentErrorProR>> resp = aisweiApi.getInverterCurrentErrorPro(req);
        if (!Objects.equals(resp.getStatus(), 200)) {
            log.info("aiswei 同步当前告警信息失败,info:{},stationId:{}", resp.getInfo(), stationId);
			if(resp.getInfo().contains("The station does not belong to this user")){
				LambdaQueryWrapper<MonitorEntryStatEntity> queryWrapper = new LambdaQueryWrapper<>();
				queryWrapper.eq(MonitorEntryStatEntity::getOutStationId, stationId);
				entryStatService.remove(queryWrapper);
			}
            return;
        }
        List<GetAlarmListR> alarmList = convList(resp.getData(), stationId);
        try {
            monitorAlarmInfoService.syncAlarmInfo(alarmList, NhConstants.SYS_TYPE_AISWEI);
        } catch (Exception ex) {
            log.error("aiswei sync alarm list error", ex);
        }
        try {
            monitorFaultCodeService.syncFaultCode(NhConstants.SYS_TYPE_AISWEI, alarmList);
        } catch (Exception ex) {
            log.error("aiswei sync fault code error", ex);
        }
    }

    private List<GetAlarmListR> convList(List<GetInverterCurrentErrorProR> list, String stationId) {
        List<GetAlarmListR> ret = new ArrayList<>();
        for (GetInverterCurrentErrorProR r : list) {
            ret.add(convR(r, stationId));
        }
        return ret;
    }

    private GetAlarmListR convR(GetInverterCurrentErrorProR r, String stationId) {
        GetAlarmListR ret = new GetAlarmListR();
        ret.setStationId(stationId);
        ret.setAlarmDeviceSn(r.getIsno());
        ret.setStationName(null);
        ret.setAlarmCode(String.valueOf(r.getErrorCode()));
        ret.setAlarmType(null); // 用不到
        ret.setAlarmLevel("2"); // aiswei全部当成报警好了
        ret.setAlarmBeginTime(r.getErrorDate());
        ret.setAlarmEndTime(null);
        ret.setAlarmMsg(r.getContext());
        ret.setState(null);
        ret.setWarningInfoData(r.getFaultCode()); // aiswei这里用来存
        ret.setAdvice(null);
        ret.setMachine(null);
        ret.setModel(null);
        return ret;
    }

}
