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 com.hdkj.lt.mq.service.voltageLimitViolation.FhzgVoltageLimitViolationPowerCutService;
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 RocketConsumerOutageLimitListener implements MessageListener {
    @Autowired
    MessageRetryLogService messageRetryLogService;

    @Autowired
    ObjectMapper objectMapper;

    private final FhzgGridOperationAbnormalityService service;

    private final FhzgOperationAbnormalityEventFeederService operationAbnormalityEventFeederService;
    private final FhzgOperationAbnormalityEventService operationAbnormalityEventService;
    private final FhzgVoltageLimitViolationPowerCutService limitViolationPowerCutService;

    //成片低电压场景
    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        try {
            message.getTopic();
            String type = message.getTag();
            String value = new String(message.getBody(), StandardCharsets.UTF_8);
            // 调用业务层操作
            this.executeFunc(value, type);
            return Action.CommitMessage;
        } catch (Exception e) {
            e.printStackTrace();
            return Action.ReconsumeLater;
        }
    }

    public void executeFunc(String value, String type) {
        limitViolationPowerCutService.acceptLimitPowercut(value);
    }
}
