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.hdkj.lt.base.enums.EventTypeEnum;
import com.hdkj.lt.core.bizms.modle.po.MessageRetryLog;
import com.hdkj.lt.core.log.exception.ExceptionErrorLogCast;
import com.hdkj.lt.core.log.model.param.ErrorLogParam;
import com.hdkj.lt.mq.service.abnormal.FhzgGridOperationAbnormalityService;
import com.hdkj.lt.mq.service.base.MessageRetryLogService;
import com.hdkj.lt.mq.service.fault.IAnalysisFaultTransferResultService;
import com.hdkj.lt.mq.service.plan.IAnalysisTransferResultService;
import com.hdkj.lt.mq.service.voltageLimitViolation.FhzgVoltageLimitViolationPowerCutService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

import static com.hdkj.lt.base.constants.CommonConstant.TWO;

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

    private final IAnalysisTransferResultService analysisTransferResultService;
    private final IAnalysisFaultTransferResultService analysisFaultTransferResultService;
    private final FhzgGridOperationAbnormalityService fhzgGridOperationAbnormalityService;
    private final FhzgVoltageLimitViolationPowerCutService fhzgVoltageLimitViolationPowerCutService;

    private final MessageRetryLogService messageRetryLogService;

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        try {
            log.info("===== 解析转供范围计算计算推演平台结果-开始消费 =====");
            String type = message.getTag(); // 1-计划停电，2-故障停电，3运行调优，4全口径低电压
            String value = new String(message.getBody(), StandardCharsets.UTF_8);
            // 调用业务层操作
            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;
        }
    }

    /**
     * 执行mq消费方法
     */
    private void executeFunc(String value, String type) {
        Long linkId = Long.valueOf(value); // 处理参数信息
        try {
            if (StringUtils.equals("1", type)) {
                // 解析计划推演平台计划停电结果
                analysisTransferResultService.analysisTransferResult(linkId);
            } else if (StringUtils.equals("2", type)) {
                // 解析计划推演平台故障停电结果
                analysisFaultTransferResultService.analysisFaultTransferResult(linkId);
            } else if (StringUtils.equals("3", type)) {
                //解析运行调优场景数据
                fhzgGridOperationAbnormalityService.dealOperationAbnormalData(linkId);
            } else if (StringUtils.equals("4", type)) {
                //解析全口径低电压数据
                fhzgVoltageLimitViolationPowerCutService.analysisLimitTransferScheme(linkId);
            } else {
                log.info("===== 解析转供范围计算计算推演平台结果-暂无订阅对应操作 =====");
            }
        } catch (Exception e) {
            ErrorLogParam errorLogParam = new ErrorLogParam()
                    .setErrorTime(LocalDateTime.now())
                    .setEventId(value)
                    .setErrorCategory("计算推演平台结果解析失败")
                    .setErrorLevel("Warning")
                    .setSourceSystem("2")
                    .setOperationStep("解析计算推演平台返回")
                    .setRelatedId(value)
                    .setStage("9")
                    .setRelatedCategory("fhzg_plan_transfer_scheme")
                    .setDetails(" 计算推演平台返回解析失败，请求参数: value:" + value + ", type" + type);
            ExceptionErrorLogCast.cast(errorLogParam);
            e.printStackTrace();
        }

    }

}
