package com.dxhy.extend.rabbitmq;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.extend.entity.ExtSysMessagecontrol;
import com.dxhy.extend.service.messagecontrol.ExtMessageControlService;

import cn.hutool.core.codec.Base64;
import lombok.extern.slf4j.Slf4j;

/**
 * Receiver.class 推送erp消息队列， 消费者 用于处理消息
 *
 * @author dxhy
 */
@Component
@Slf4j
public class ExtReceiver {

    @Resource
    private ExtMessageControlService messageControlService;

    /**
     * 导出消费者
     *
     * @param message
     *            入参
     */
    @RabbitHandler
    @RabbitListener(queues = "${TO_MSG_EXCEL_APPLY:TO_MSG_EXCEL_APPLY}")
    public void handleMessageOne(String message) {
        log.debug("接收的消息为:{}", message);
        try {
            if (message != null) {
                JSONObject jsStr = JSONObject.parseObject(Base64.decodeStr(message));
                ExtSysMessagecontrol vo = new ExtSysMessagecontrol();
                vo.setUserAccount((String)jsStr.get("useraccount"));
                vo.setTitle((String)jsStr.get("title"));
                vo.setContent((String)jsStr.get("content"));
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = sdf.format(new Date());
                Date time2 = sdf.parse(time);
                vo.setCreateTime(time2);
                vo.setUpdateTime(time2);
                vo.setUrl((String)jsStr.get("url"));
                vo.setOperationStatus("0");
                messageControlService.insertMessage(vo);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error("接收消息失败:{}", ex.getMessage());
        }
    }

}
