package com.tzy.listener.mq;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.tzy.common.constant.MqConstans;
import com.tzy.system.domain.TzySysNoticeRecord;
import com.tzy.system.service.ITzySysNoticeRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;


@Slf4j
@Component
public class CommunityNotifyConsumer {

	@Resource
	private ITzySysNoticeRecordService noticeRecordService;

	@RabbitHandler
	@RabbitListener(queues = {MqConstans.QUEUE_COMMUNITY_NOTIFY})
	public void notifyConsumer(String param, Channel channel, Message message) throws IOException {
		log.debug("社区通知消息:{}", param);
		try {
			List<TzySysNoticeRecord> noticeRecords = JSON.parseArray(param, TzySysNoticeRecord.class);
			noticeRecords.forEach(noticeRecord -> noticeRecordService.insertTzySysNoticeRecord(noticeRecord));
		} catch (Exception e) {
			log.error("社区通知消息异常,参数:{}", param, e);
		}
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}

}
