package com.hdkj.lt.mq.listener;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hdkj.lt.core.bizms.modle.po.GridPdOutageEventBase;
import com.hdkj.lt.mq.entity.dto.AcceptFaultPowerCutDTO;
import com.hdkj.lt.mq.entity.dto.BreakerInfo;
import com.hdkj.lt.mq.entity.dto.FeederInfo;
import com.hdkj.lt.mq.service.base.GridPdOutageEventBaseService;
import com.hdkj.lt.mq.service.fault.AcceptFaultPowerCutService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class RocketConsumerOutageEventListener implements MessageListener {

    private final AcceptFaultPowerCutService acceptFaultPowerCutService;
    private final GridPdOutageEventBaseService gridPdOutageEventBaseService;

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        try {
            log.info("===== 收到供指故障停电事件，启动故障链路 =====");
            String type = message.getTag();
            String value = new String(message.getBody(), StandardCharsets.UTF_8);
            log.info("type:{},value:{}", type, value);
            if(value.startsWith("\"") && value.endsWith("\"")){
                value = value.substring(1, value.length() - 1);
            }
            // 调用业务层操作
            this.executeFunc(value, type);
            return Action.CommitMessage;
        } catch (Exception e) {
            e.printStackTrace();
            return Action.ReconsumeLater;
        }
    }

    /**
     * 执行mq消费方法
     */
    public void executeFunc(String value, String type) {
        //根据OutageNumber查询对应的停电事件
        GridPdOutageEventBase event = gridPdOutageEventBaseService.getOne(new LambdaQueryWrapper<GridPdOutageEventBase>()
                .eq(GridPdOutageEventBase::getOutageNumber, value)
//                todo:暂时屏蔽停复电标识
//                .eq(GridPdOutageEventBase::getOutageFlag, '0')
        );
        AcceptFaultPowerCutDTO acceptFaultPowerCutDTO = new AcceptFaultPowerCutDTO();
        acceptFaultPowerCutDTO.setFaultNo(event.getId().toString());
        acceptFaultPowerCutDTO.setSourceSystem("供指");
        acceptFaultPowerCutDTO.setFaultDesc(event.getOutageDesc());
        acceptFaultPowerCutDTO.setFaultStatus(event.getId().toString());
        acceptFaultPowerCutDTO.setStartTime(event.getBeginTime());
        acceptFaultPowerCutDTO.setEndTime(event.getEndTime());
        acceptFaultPowerCutDTO.setFaultType(Integer.parseInt(event.getOutageType()) - 1);
        List<FeederInfo> feederInfos = getFeederInfos(event);
        acceptFaultPowerCutDTO.setFeederList(feederInfos);
        acceptFaultPowerCutService.acceptFaultPowerCut(acceptFaultPowerCutDTO);
    }

    private static List<FeederInfo> getFeederInfos(GridPdOutageEventBase event) {
        List<FeederInfo> feederInfos = new ArrayList<>();
        FeederInfo feederInfo = new FeederInfo();
        feederInfo.setFeederPsrId(event.getRdtFeederId());
        feederInfo.setFeederName(event.getRdtFeederName());
        List<BreakerInfo> breakerInfos = new ArrayList<>();
        BreakerInfo breakerInfo = new BreakerInfo();
        breakerInfo.setBreakerPsrId(event.getFaultEquipId());
        breakerInfo.setBreakerName(event.getFaultEquipName());
        breakerInfo.setBreakerPsrType(event.getFaultEquipType());
        breakerInfos.add(breakerInfo);
        feederInfo.setBreakerList(breakerInfos);
        feederInfo.setFeederPsrId(event.getRdtFeederId());
        feederInfos.add(feederInfo);
        return feederInfos;
    }

}
