package com.inspect.metadata.mqutil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.inspect.metadata.eqpbook.domain.BasedataEqpBookNvr;
import com.inspect.metadata.eqpbook.service.IBasedataEqpBookNvrService;
import com.inspect.metadata.eqpbook.service.IBasedataEqpBookService;
import com.inspect.base.websocket.configure.WsPool;
import com.inspect.base.websocket.domain.WebSocketMessage;
import com.rabbitmq.client.Channel;

import java.util.List;
import java.util.Map;

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.beans.factory.annotation.Autowired;
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 BasedataNvr {
    @Resource
    private IBasedataEqpBookNvrService basedataEqpBookNvrService;
    @Resource
    private IBasedataEqpBookService basedataEqpBookService;
    public static final String WEBSOCKET_NVR_RESULT = "KEY_NVR";

    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue(
                                    value = "queue-9000",
                                    durable = "true"
                            ),
                            exchange = @Exchange(
                                    value = "exchange-9000",
                                    durable = "true",
                                    type = "direct",
                                    ignoreDeclarationExceptions = "true"
                            ),
                            key = {"springboot.9000"})})
    @RabbitHandler
    public void onOrderMessage(@Payload String order, Channel channel, @Headers Map<String, Object> headers) throws Exception {
        this.alarmMessageFromEqpBook(order);
        Long deliveryTag = (Long) headers.get("amqp_deliveryTag");
        channel.basicAck(deliveryTag, false);
    }

    public void alarmMessageFromEqpBook(String order) throws Exception {
        String str = "{\n    \"SendCode\": \"机器人及无人机巡视系统唯一标识\",\n    \"ReceiveCode\": \"巡视主机唯一标识\",\n    \"Type\": \"1025\",\n    \"Command\": \"\",\n    \"Code\": \"\",\n    \"Items\": \n[\n    {\n        \"patroldeviceName\":\"3\",\n        \"duration\":\"4\",\n        \"cycle\":\"5\",\n        \"integrity\":\"6\",\n        \"allcapacity\":\"7\",\n        \"usecapacity\":\"8\",\n        \"usepercentage\":\"9\"\n    },\n    {\n        \"patroldeviceName\":\"1\",\n        \"duration\":\"1\",\n        \"cycle\":\"1\",\n        \"integrity\":\"1\",\n        \"allcapacity\":\"1\",\n        \"usecapacity\":\"1\",\n        \"usepercentage\":\"1\"\n    }\n]}";
        JSONObject jsonObject = JSONObject.parseObject(order);
        if (!"".equals(jsonObject.get("Type")) && Integer.parseInt(String.valueOf(jsonObject.get("Type"))) == 1025) {
            List<BasedataEqpBookNvr> list = JSON.parseArray(String.valueOf(jsonObject.get("Items")), BasedataEqpBookNvr.class);
            WsPool.sendMessageToAll(WebSocketMessage.getInstance("KEY_NVR", list));
        }
    }
}
