package com.ruoyi.xq.mq.consumer;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.xq.mq.config.HandleConfig;
import com.ruoyi.xq.mq.handle.IHandle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class CommonConsumer {
    public final static String COMMON_QUEUE = "xqCommonQueue";
    public final static String COMMON_EXCHANGE = "xqCommonExchange";
    public final static String COMMON_KEY = "xqCommonKey";

    @Autowired
    private HandleConfig handleConfig;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = COMMON_QUEUE, durable = "false", autoDelete = "false"),
            exchange = @Exchange(value = COMMON_EXCHANGE),
            key = COMMON_KEY)
            ,containerFactory = "customContainerFactory")
    public void calculateSalesQueue(String message) {
        log.info("队列消息处理-开始: message=" + message);
        try {
            JSONObject object = JSON.parseObject(message);
            String type = object.getString("type");
            IHandle handle = handleConfig.getHandle(type);
            handle.run(message);
        }catch (Exception e){
            log.error("队列消息处理-失败: message=" + message,e);
        }
        log.info("队列消息处理-结束: message=" + message);
    }
}
