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.mq.service.base.impl.FhzgPlanSchemeScoreServiceImpl2;
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;

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

    @Autowired
    private FhzgPlanSchemeScoreServiceImpl2 planSchemeScoreService;

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        try {
            log.info("===== 处理计算方案评分-开始消费 =====");
            String type = message.getTag();
            String value = new String(message.getBody(), StandardCharsets.UTF_8);
            execute(value,type);
            return Action.CommitMessage;
        } catch (Exception e) {
            e.printStackTrace();
            return Action.ReconsumeLater;
        }
    }


    private void execute(String value, String type) {
        if ("1".equals(type)) {
            planSchemeScoreService.planScore(Long.valueOf(value));
        } else if ("2".equals(type)) {
            planSchemeScoreService.repairScore(Long.valueOf(value));
        } else {
            log.info("===== 处理计算方案评分-暂无订阅对应操作 =====");
        }
    }
}
