package com.yida.data.school.news.config;

import com.rabbitmq.client.Channel;
import com.yida.data.rabbit.constant.RabbitConstant;
import com.yida.data.school.dto.news.SysNoticeSaveDTO;
import com.yida.data.school.news.service.EduNoticeSystemService;
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.context.annotation.Configuration;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class NewsRabbitReceiver {

    private final EduNoticeSystemService eduNoticeSystemService;

    @RabbitListener(bindings = @QueueBinding(
        value = @Queue(RabbitConstant.MSG_NOTICE_SYS_SAVE_QUEUE),
        exchange = @Exchange(RabbitConstant.MSG_NOTICE_SYS_SAVE_KEY)
    ))
    public void receivePosition(SysNoticeSaveDTO notice, Channel channel, Message message) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("接收消息:[{}]", notice);
            eduNoticeSystemService.saveNotice(notice);
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            log.error("保存系统通知失败", e);
        }
    }
}
