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.fasterxml.jackson.databind.ObjectMapper;
import com.hdkj.lt.core.bizms.modle.po.MessageRetryLog;
import com.hdkj.lt.core.bizms.modle.po.operationAbnormality.FhzgOperationAbnormalityEvent;
import com.hdkj.lt.core.bizms.modle.po.operationAbnormality.FhzgOperationAbnormalityEventFeeder;
import com.hdkj.lt.modle.dto.abnormal.AbnormalMessageDataDTO;
import com.hdkj.lt.mq.entity.convert.MQEntityToVOConvertor;
import com.hdkj.lt.mq.service.abnormal.FhzgGridOperationAbnormalityService;
import com.hdkj.lt.mq.service.abnormal.FhzgOperationAbnormalityEventFeederService;
import com.hdkj.lt.mq.service.abnormal.FhzgOperationAbnormalityEventService;
import com.hdkj.lt.mq.service.base.MessageRetryLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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


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

    @Autowired
    MessageRetryLogService messageRetryLogService;

    @Autowired
    ObjectMapper objectMapper;

    private final FhzgGridOperationAbnormalityService service;

    private final FhzgOperationAbnormalityEventFeederService operationAbnormalityEventFeederService;
    private final FhzgOperationAbnormalityEventService operationAbnormalityEventService;


    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        try {
            message.getTopic();
            String type = message.getTag();
            String value = new String(message.getBody(), StandardCharsets.UTF_8);
            if (value.startsWith("\"") && value.endsWith("\"")) {
                value = value.substring(1, value.length() - 1);
            }
            // 调用业务层操作
            this.executeFunc(value, type);
            return Action.CommitMessage;
        } catch (Exception e) {
            MessageRetryLog messageRetryLog = MessageRetryLog.builder().messageId(message.getTopic())
                    .errorLog(e.getMessage()).messageBody(new String(message.getBody(), StandardCharsets.UTF_8))
                    .tag(message.getTag()).step(1).build();
            messageRetryLogService.saveLog(messageRetryLog);
            e.printStackTrace();
            return Action.ReconsumeLater;
        }
    }

    public void executeFunc(String value, String type) {
        FhzgOperationAbnormalityEvent event = operationAbnormalityEventService.getById(value);
        List<FhzgOperationAbnormalityEventFeeder> eventFeederList = operationAbnormalityEventFeederService.list(new LambdaQueryWrapper<FhzgOperationAbnormalityEventFeeder>().eq(FhzgOperationAbnormalityEventFeeder::getEventId, value));
        AbnormalMessageDataDTO abnormalMessageDataDTO = MQEntityToVOConvertor.INSTANCE.toAbnormalMessageDataDTO(event, eventFeederList);
        abnormalMessageDataDTO.setLoadAlarmType(eventFeederList.get(0).getLoadAlarmType());
        service.AcceptOperationAbnormalMessage(abnormalMessageDataDTO);
    }
}
