package com.zcxk.core.utils.rabbit;

import javax.annotation.Resource;

import com.zcxk.core.utils.MqService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.stereotype.Service;

/**
 * @author Andy
 * @date 2021-07-22 11:03
 */
@Service
@Slf4j
public class RabbitServiceImpl implements MqService {

    @Resource
    private AmqpTemplate amqpTemplate;

    public RabbitServiceImpl() {
    }


    @Override
    public void sendStr(String queueName, String message) {
        this.amqpTemplate.convertAndSend(queueName, message);
    }

    @Override
    public void send(String queueName, Object objMsg) {
        this.amqpTemplate.convertAndSend(queueName, objMsg);
    }

    @Override
    public void sendStr(String queueName, String message, long times) {
        this.amqpTemplate.convertAndSend(queueName, message, messageObj -> {
            MessageProperties messageProperties = messageObj.getMessageProperties();
            messageProperties.setExpiration(String.valueOf(times * 1000L));
            messageProperties.setMessageId(System.currentTimeMillis() + "");
            return messageObj;
        });
    }

    @Override
    public void send(String queueName, Object objMsg, long times) {
        this.amqpTemplate.convertAndSend(queueName, objMsg, messageObj -> {
            MessageProperties messageProperties = messageObj.getMessageProperties();
            messageProperties.setExpiration(String.valueOf(times * 1000L));
            messageProperties.setMessageId(System.currentTimeMillis() + "");
            return messageObj;
        });
    }

    @Override
    public void sendStr(String exchange, String routingKey, String message) {
        this.amqpTemplate.convertAndSend(exchange, routingKey, message);
    }

    @Override
    public void send(String exchange, String routingKey, Object objMsg) {
        this.amqpTemplate.convertAndSend(exchange, routingKey, objMsg);
    }

    @Override
    public void sendStr(String exchange, String routingKey, String message, long times) {
        this.amqpTemplate
            .convertAndSend(exchange, routingKey, message, messageObj -> {
                MessageProperties messageProperties = messageObj.getMessageProperties();
                messageProperties.setExpiration(String.valueOf(times * 1000L));
                messageProperties.setMessageId(System.currentTimeMillis() + "");
                return messageObj;
            });
    }

    @Override
    public void send(String exchange, String routingKey, Object objMsg, long times) {
        this.amqpTemplate
            .convertAndSend(exchange, routingKey, objMsg, messageObj -> {
                MessageProperties messageProperties = messageObj.getMessageProperties();
                messageProperties.setExpiration(String.valueOf(times * 1000L));
                messageProperties.setMessageId(System.currentTimeMillis() + "");
                return messageObj;
            });
    }
}
