package com.yida.data.school.visitor.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.school.visitor.service.EduVisitorRecordService;
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 VisitorRabbitReceiver {

    private final EduVisitorRecordService eduVisitorRecordService;

    /**
     * 访客扫描邀请码
     */
    @RabbitListener(bindings = @QueueBinding(
        value = @Queue(RabbitConstant.VISITOR_INVITE_CODE_QUEUE),
        exchange = @Exchange(RabbitConstant.WXPUBLIC_QR_EXCHANGE)
    ))
    public void visitorScanInviteCode(WxPublicQr wxPublicQr, Channel channel, Message message) throws IOException {
        boolean success = true;
        try {
            eduVisitorRecordService.scanInviteCodeCall(wxPublicQr);
        } catch (Exception e) {
            success = false;
            log.error("消费消息失败", e);
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), success);
        }
    }
}
