package com.ruoyi.cai.mq.consumer;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.cai.mq.handle.HandleConfig;
import com.ruoyi.cai.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 WindowConsumer {

    public final static String WINDOW_QUEUE = "caiWindowQueue";
    public final static String WINDOW_EXCHANGE = "caiWindowExchange";
    public final static String WINDOW_KEY = "caiWindowKey";

    @Autowired
    private HandleConfig handleConfig;


    //  ,containerFactory = "customContainerFactory"
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = WINDOW_QUEUE, durable = "false", autoDelete = "false"),
            exchange = @Exchange(value = WINDOW_EXCHANGE),
            key = WINDOW_KEY))
    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);
    }
}
