package com.sky.ioc.message;

import com.alibaba.fastjson.JSONObject;
import com.sky.ioc.entity.domain.notice.Notice;
import com.sky.ioc.mapper.notice.NoticeMapper;
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.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.AccessType;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Date;


@Component
@RabbitListener(queues = "message")
@Slf4j
public class MessageReceiver {

    @Autowired
    NoticeMapper noticeMapper;

    @RabbitHandler(isDefault = true)
    public void process(Message message){
        log.info("====接收到" + message.getMessageProperties().getConsumerQueue() + "队列的消息=====");
        log.info(message.getMessageProperties().toString());
       // System.out.println();
        JSONObject jsonObject =  JSONObject.parseObject(new String(message.getBody()));
        Notice notice = new Notice();
        notice.setTitle(jsonObject.getString("title"));
        notice.setContent(jsonObject.getString("content"));
        notice.setReceived(jsonObject.get("staff").toString());
        if(jsonObject.get("send_time")!=null){
            notice.setTime(jsonObject.getString("send_time"));
        }else{
            notice.setTime(new Date()+"");
        }
        notice.setCreateTime(new Date()+"");
        noticeMapper.insert(notice);

        }
}
