package com.mortals.xhx.base.system.message.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.model.AccessLogPdu;
import com.mortals.framework.model.BizLogPdu;
import com.mortals.framework.model.ErrorLogPdu;
import com.mortals.framework.model.OperateLogPdu;
import com.mortals.framework.service.IMessageProduceService;
import com.mortals.xhx.base.system.message.RabbitMessageService;
import com.mortals.xhx.common.key.QueueKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class MessageProducer implements IMessageProduceService, RabbitMessageService, InitializingBean {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RabbitAdmin rabbitAdmin;

    public void syncAccessSend(AccessLogPdu accessLogPdu) {
        rabbitTemplate.convertAndSend(QueueKey.EXCHANGE, QueueKey.ACCESS_LOG_QUEUE, JSON.toJSONString(accessLogPdu));
    }

    @Override
    public void syncBizSend(BizLogPdu bizLogPdu) {
        rabbitTemplate.convertAndSend(QueueKey.EXCHANGE, QueueKey.BIZ_LOG_QUEUE, JSON.toJSONString(bizLogPdu));
    }

    @Override
    public void syncErrorSend(ErrorLogPdu errorLogPdu) {
        rabbitTemplate.convertAndSend(QueueKey.EXCHANGE, QueueKey.ERROR_LOG_QUEUE, JSON.toJSONString(errorLogPdu));
    }

    @Override
    public void syncOperSend(OperateLogPdu operLogPdu) {
        log.info("send operate log ==>{}", JSON.toJSONString(operLogPdu));
        String send = JSON.toJSONString(operLogPdu);
        rabbitTemplate.convertAndSend(QueueKey.EXCHANGE, QueueKey.OPERATION_LOG_QUEUE, send);
    }

    @Override
    public void sendMsg(String exchange, String routingKey, String message) {
        CorrelationData correlationData = new CorrelationData(IdUtil.fastSimpleUUID());
        rabbitTemplate.convertAndSend(exchange, routingKey, message, correlationData);
    }

    @Override
    public void queueAddAndBinds(String exchange, String routingKey, String queue) {
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 86400000);
        Queue rabbitQueue = new Queue(queue, true, false, false, args);
        DirectExchange directExchange = new DirectExchange(exchange);
        rabbitAdmin.declareQueue(rabbitQueue);
        rabbitAdmin.declareBinding(
                BindingBuilder.bind(rabbitQueue)
                        .to(directExchange)
                        .with(routingKey)
        );
        log.debug("队列创建绑定成功,queue:{}", queue);

    }

    @Override
    public Boolean queueDelete(String queue) {
        return rabbitAdmin.deleteQueue(queue);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println(111);
    }
}
