package com.inspect.metadata.montdatamqhandle;

import com.alibaba.fastjson.JSONObject;
import com.inspect.metadata.montdatamqhandle.service.IBasedataMontDataHandleService;
import com.rabbitmq.client.Channel;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class BasedataMontDataMqAcceptHandle {
    private final Logger log = LoggerFactory.getLogger(BasedataMontDataMqAcceptHandle.class);
    @Resource
    RegisterMontDataHandelBean registerMontDataHandelBean;

    @RabbitListener(
            bindings = {@QueueBinding(
                    value = @Queue(
                            value = "${spring.rabbitmq.listener.montdata.queue.name}",
                            durable = "${spring.rabbitmq.listener.montdata.queue.durable}"
                    ),
                    exchange = @Exchange(
                            value = "${spring.rabbitmq.listener.montdata.exchange.name}",
                            durable = "${spring.rabbitmq.listener.montdata.exchange.durable}",
                            type = "${spring.rabbitmq.listener.montdata.exchange.type}",
                            ignoreDeclarationExceptions = "${spring.rabbitmq.listener.montdata.exchange.ignoreDeclarationExceptions}"
                    ),
                    key = {"${spring.rabbitmq.listener.montdata.key}"}
            )}
    )
    @RabbitHandler
    public void onOrderMessage(@Payload String order, Channel channel, @Headers Map<String, Object> headers) throws Exception {
        JSONObject jsonObject = JSONObject.parseObject(order);
        String type = jsonObject.getString("Type");
        String uuid = jsonObject.getString("uuid");
        log.info("[METADATA] RECV MSG FROM RABBITMQ 会话: {}, 类型: {}, 消息体: {}", uuid, type, order);
        if (!StringUtils.isEmpty(type)) {
            Long deliveryTag = (Long) headers.get("amqp_deliveryTag");
            channel.basicAck(deliveryTag, false);
            IBasedataMontDataHandleService basedataMontDataHandleService;
            if ("21".equals(type)) {
                basedataMontDataHandleService = this.registerMontDataHandelBean.getServiceImpl("basedataMont-21");
                basedataMontDataHandleService.handleMontData(jsonObject);
            }

            if ("10004".equals(type)) {
                basedataMontDataHandleService = this.registerMontDataHandelBean.getServiceImpl("basedataMont-10004");
                basedataMontDataHandleService.handleMontData(jsonObject);
            }

            if ("20001".equals(type)) {
                basedataMontDataHandleService = this.registerMontDataHandelBean.getServiceImpl("basedataMont-20001");
                basedataMontDataHandleService.handleMontData(jsonObject);
            }

            if ("5".equals(type)) {
                basedataMontDataHandleService = this.registerMontDataHandelBean.getServiceImpl("basedataMont-5");
                basedataMontDataHandleService.handleMontData(jsonObject);
            }

            if ("3".equals(type)) {
                basedataMontDataHandleService = this.registerMontDataHandelBean.getServiceImpl("basedataMont-3");
                basedataMontDataHandleService.handleMontData(jsonObject);
            }

            if ("4".equals(type)) {
                basedataMontDataHandleService = this.registerMontDataHandelBean.getServiceImpl("basedataMont-4");
                basedataMontDataHandleService.handleMontData(jsonObject);
            }

            if ("2".equals(type)) {
                basedataMontDataHandleService = this.registerMontDataHandelBean.getServiceImpl("basedataMont-2");
                basedataMontDataHandleService.handleMontData(jsonObject);
            }

            if ("1".equals(type)) {
                basedataMontDataHandleService = this.registerMontDataHandelBean.getServiceImpl("basedataMont-1");
                basedataMontDataHandleService.handleMontData(jsonObject);
            }

            if ("61".equals(type)) {
                basedataMontDataHandleService = this.registerMontDataHandelBean.getServiceImpl("basedataMont-61");
                basedataMontDataHandleService.handleMontData(jsonObject);
            }

            if ("41".equals(type)) {
                basedataMontDataHandleService = this.registerMontDataHandelBean.getServiceImpl("basedataMont-41");
                basedataMontDataHandleService.handleMontData(jsonObject);
            }

            if ("modelup-1024".equals(type)) {
                basedataMontDataHandleService = this.registerMontDataHandelBean.getServiceImpl("basedataMont-modelup-1024");
                basedataMontDataHandleService.handleMontData(jsonObject);
            }

            if ("heartbeat".equals(type)) {
                basedataMontDataHandleService = this.registerMontDataHandelBean.getServiceImpl("basedataMont-heartbeat");
                basedataMontDataHandleService.handleMontData(jsonObject);
            }

            if ("modelup-1024-test".equals(type)) {
                basedataMontDataHandleService = this.registerMontDataHandelBean.getServiceImpl("basedataMont-modelup-1024-test");
                basedataMontDataHandleService.handleMontData(jsonObject);
            }

        }
    }
}
