package com.mortals.xhx.base.framework.listener;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mortals.xhx.common.key.QueueKey;
import com.mortals.xhx.common.pdu.access.AccessLogPdu;
import com.mortals.xhx.module.access.model.AccessLogEntity;
import com.mortals.xhx.module.access.service.AccessLogService;
import com.mortals.xhx.module.biz.model.BizLogEntity;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.impl.AMQImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

/**
 * rabbitmq 访问日志消息消费类
 *
 * @author: zxfei
 * @date: 2022/8/17 20:45
 * @description:
 **/
@Slf4j
@Component
@RabbitListener(queues = QueueKey.ACCESS_LOG_QUEUE,
        containerFactory = "consumerBatchContainerFactory")
public class AccessMessageConsumerListener {

    @Autowired
    private AccessLogService accessLogService;

   /* @RabbitHandler
    public void onMessage(List<String> messages) {
        log.info("[Access onMessage][线程编号:{} 消息数量：{}]", Thread.currentThread().getName() + Thread.currentThread().getId(), messages.size());
        List<AccessLogEntity> collect = messages.stream().map(message -> {
            try {
                AccessLogEntity entity =null;
                    entity = JSON.parseObject(message, AccessLogEntity.class);
                    entity.setId(IdUtil.getSnowflake(0, 1).nextId());
                    entity.setCreateUserId(1L);
                    entity.setCreateTime(new Date());
                return entity;
            } catch (Exception e) {
                log.info("反序列化异常", e);
                return null;
            }


        }).filter(f -> f != null).collect(Collectors.toList());
        // collect.forEach(accessLogEntity -> accessLogService.save(accessLogEntity));
        accessLogService.save(collect);
    }*/

    @RabbitHandler
    public void onMessage(List<AccessLogEntity> messages) {
        log.info("[Access onMessage][消息数量：{}]",  messages.size());
        List<AccessLogEntity> collect = messages.stream().map(entity -> {
            try {
               // AccessLogEntity entity =null;
                //entity = JSON.parseObject(message, AccessLogEntity.class);
                entity.setId(IdUtil.getSnowflake(0, 1).nextId());
                entity.setCreateUserId(1L);
                entity.setCreateTime(new Date());
                return entity;
            } catch (Exception e) {
                log.info("反序列化异常", e);
                return null;
            }

        }).filter(f -> f != null).collect(Collectors.toList());
        accessLogService.save(collect);
    }


    public static void main(String[] args) {

        ArrayList<?> messages = new ArrayList<>();

        // messages.add("111");

        if (messages.stream().noneMatch((o -> !(o instanceof String)))) {
            log.info("都匹配");

        } else {
            log.info("存在不匹配");
        }


/*
        String json = "{\n" +
                "    \"appName\":\"smart-gateway\",\n" +
                "    \"deleted\":0,\n" +
                "    \"duration\":12,\n" +
                "    \"hostName\":\"172.17.0.1\",\n" +
                "    \"logLevel\":\"INFO\",\n" +
                "    \"logTime\":1661239155832,\n" +
                "    \"method\":\"POST\",\n" +
                "    \"pspanId\":0,\n" +
                "    \"requestData\":\"\",\n" +
                "    \"requestIp\":\"192.168.0.217\",\n" +
                "    \"requestTime\":1661239155832,\n" +
                "    \"responseData\":\"\",\n" +
                "    \"responseTime\":1661239155844,\n" +
                "    \"schemaData\":\"http\",\n" +
                "    \"spanId\":1,\n" +
                "    \"targetServer\":\"base-manager\",\n" +
                "    \"traceID\":\"63047f73dacc9d863249b7c5\",\n" +
                "    \"ua\":\"\",\n" +
                "    \"uri\":\"/base/business/list\"\n" +
                "}";


        System.out.println(json);


        AccessLogEntity accessLogEntity = new AccessLogEntity();
        accessLogEntity.setRequestData(json);


        System.out.println(JSON.toJSONString(accessLogEntity));

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("111", json);

        System.out.println(JSON.toJSONString(jsonObject));

        System.out.println(JSONUtil.formatJsonStr(JSON.toJSONString(jsonObject)));*/


    }


}
