package com.inspect.metadata.mqutil;

import com.inspect.metadata.patrolpointmnt.service.IBasedataPatrolPointService;
import com.rabbitmq.client.Channel;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

import org.apache.poi.util.IOUtils;
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;

@Component
public class BasedataPatrolPointConsumer {
    private final IBasedataPatrolPointService basedataPatrolPointService;

    public BasedataPatrolPointConsumer(IBasedataPatrolPointService basedataPatrolPointService) {
        this.basedataPatrolPointService = basedataPatrolPointService;
    }

    @RabbitListener(
            bindings = {@QueueBinding(
                    value = @Queue(
                            value = "queue-900",
                            durable = "true"
                    ),
                    exchange = @Exchange(
                            value = "exchange-900",
                            durable = "true",
                            type = "direct",
                            ignoreDeclarationExceptions = "true"
                    ),
                    key = {"springboot.900"}
            )}
    )
    @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) {
        String str = "{\n    \"SendCode\": \"机器人及无人机巡视系统唯一标识\",\n    \"ReceiveCode\": \"巡视主机唯一标识\",\n    \"Type\": \"251\",\n    \"Command\": \"\",\n    \"Code\": \"\",\n    \"Items\": [\n        {\n            \"item\": {\n                \"device_file_path\": \"设备模型文件路径\",\n                \"time\": \"时间\"\n            }\n        }\n    ]\n}";
    }

    public static InputStream readFile(String url) throws IOException {

        URLConnection urlConnection;
        try {
            if (!url.startsWith("http")) {
                return null;
            }

            URL urlObj = new URL(url);
            urlConnection = urlObj.openConnection();
            urlConnection.setConnectTimeout(30000);
            urlConnection.setReadTimeout('\uea60');
            urlConnection.setDoInput(true);
            return urlConnection.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("获取文件流异常！");
            urlConnection = null;
        } finally {
            IOUtils.closeQuietly(null);
        }

        return urlConnection.getInputStream();
    }
}
