package com.pj.rabbitmq;

import cn.hutool.core.util.RandomUtil;
import com.pj.bank_byte.BankByte;
import com.pj.bank_byte.BankByteMapper;
import com.pj.enummj.FinishStatus;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MessageReceiver {

    @Autowired
    private BankByteMapper bankByteMapper;

    private final RabbitTemplate levelOneConnectionFactory;
    private final RabbitTemplate levelTwoConnectionFactory;
    private final RabbitTemplate customsConnectionFactory;
    private final RabbitMQProperties rabbitMQProperties;

    @Autowired
    public MessageReceiver(@Qualifier("levelOneConnectionFactory") CachingConnectionFactory levelOneConnectionFactory,
                           @Qualifier("levelTwoConnectionFactory") CachingConnectionFactory levelTwoConnectionFactory,
                           @Qualifier("customsConnectionFactory") CachingConnectionFactory customsConnectionFactory,
                           RabbitMQProperties rabbitMQProperties) {
        this.levelOneConnectionFactory = new RabbitTemplate(levelOneConnectionFactory);
        this.levelTwoConnectionFactory = new RabbitTemplate(levelTwoConnectionFactory);
        this.customsConnectionFactory = new RabbitTemplate(customsConnectionFactory);
        this.rabbitMQProperties = rabbitMQProperties;
    }

    /**
     * 一级市场接收
     * @param message
     */
    @RabbitListener(queues = "${mq.level-one-receive-queue}",containerFactory = "levelOneRabbitListenerContainer")
    public void receiveLevelOneMessage(String message) {
        System.out.println("-------------------接收一级市场报文信息: \n" + message);
        bankByteMapper.insert(new BankByte(RandomUtil.randomString(64), message, rabbitMQProperties.getLevelOneReceiveQueue(), FinishStatus.FINISH_STATUS_ZERO.getCode(),new Date()));
    }
    /**
     * 二级市场接收
     * @param message
     */
    @RabbitListener(queues = "${mq.level-two-receive-queue}",containerFactory = "levelTwoRabbitListenerContainer")
    public void receiveLevelTwoMessage(String message) {
        System.out.println("-------------------接收二级市场报文信息: \n" + message);
        bankByteMapper.insert(new BankByte(RandomUtil.randomString(64),message, rabbitMQProperties.getLevelTwoReceiveQueue(), FinishStatus.FINISH_STATUS_ZERO.getCode(),new Date()));
    }
    /**
     * 海关结关指令相关报文接收
     * @param message
     */
    @RabbitListener(queues = "${mq.customs-receive-queue}", containerFactory = "customsRabbitListenerContainer")
    public void receiveCustomsMessage(String message) {
        System.out.println("-------------------接收海关结关指令报文信息: \n" + message);
        bankByteMapper.insert(new BankByte(RandomUtil.randomString(64), message, rabbitMQProperties.getCustomsReceiveQueue(), FinishStatus.FINISH_STATUS_ZERO.getCode(), new Date()));
    }

}
