package com.yida.data.system.configure;

import com.rabbitmq.client.Channel;
import com.yida.data.common.core.entity.WxPublicQr;
import com.yida.data.rabbit.constant.RabbitConstant;
import com.yida.data.system.service.EduAgentWxPublicReceiverService;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
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.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class SysRabbitReceiver {

    private final EduAgentWxPublicReceiverService eduAgentWxPublicReceiverService;

    /**
     * 区域后台消息接收者
     */
    @RabbitListener(bindings = @QueueBinding(
        value = @Queue(RabbitConstant.AGENT_RECEIVE_MSG_QUEUE),
        exchange = @Exchange(RabbitConstant.TELEPHONE_EXCHANGE)
    ))
    public void receiveAgentReceiver(WxPublicQr wxPublicQr, Channel channel, Message message) throws IOException {
        boolean success = true;
        try {
            eduAgentWxPublicReceiverService.wxPublicQrCall(wxPublicQr);
        } catch (Exception e) {
            success = false;
            log.error("消费消息失败", e);
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), success);
        }
    }
}
