package com.muyu.data.controller;

import com.alibaba.fastjson2.JSONObject;
import com.muyu.common.caffeine.enums.CacheNameEnums;
import com.muyu.common.core.utils.uuid.UUID;
import com.muyu.common.iotdb.config.IotDBSessionConfig;
import com.muyu.common.kafka.constant.KafkaConstants;
import com.muyu.common.rabbit.constants.RabbitConstants;
import com.muyu.data.domain.Information;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
* 测试
* @program: cloud-server
* @author: WangXin
* @create: 2024-09-28 14:55
**/
@RestController
@RequestMapping
@Log4j2
public class TestController {

    @Resource
    private KafkaProducer<String,JSONObject> kafkaProducer;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private IotDBSessionConfig iotDBConfig;
    @Resource
    private RedisTemplate<String,String> redisTemplate;
//    @Resource
//    private CaffeineCacheUtils cacheUtils;

    @Resource
    private CacheManager cacheManager;

    @GetMapping("/testKafka")
    public void sendMsg() {
        try {
            // 测试数据
            Information information = new Information();
            JSONObject from = JSONObject.from(information);
            ProducerRecord<String, JSONObject> producerRecord = new ProducerRecord<>(KafkaConstants.KafkaTopic, from);
            kafkaProducer.send(producerRecord);
            System.out.println("同步消息发送成功: " + from);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("同步消息发送失败");
        }
    }

    @GetMapping("/testRabbit/GoOnline")
    public void testRabbitGoOnline(@RequestParam("msg") String msg) {
        rabbitTemplate.convertAndSend(RabbitConstants.GO_ONLINE_QUEUE, msg, message -> {
            message.getMessageProperties().setMessageId(UUID.randomUUID().toString().replace("-",""));
            return message;
        });
    }

    @GetMapping("/testRabbit/Downline")
    public void testRabbitDownline(@RequestParam("msg") String msg) {
        rabbitTemplate.convertAndSend(RabbitConstants.DOWN_LINE_QUEUE, msg, message -> {
            message.getMessageProperties().setMessageId(UUID.randomUUID().toString().replace("-",""));
            return message;
        });
    }

    @GetMapping("/insertData")
    public void insertData(@RequestParam("deviceId") String deviceId, @RequestParam("time") long time, @RequestParam("value") double value) throws Exception {
        String sql = String.format("insert into root.one.%s(timestamp, temperature) values (%d, %f)", deviceId, time, value);
        iotDBConfig.getSessionPool().executeNonQueryStatement(sql);
    }

    @GetMapping("/testSetRedis")
    public void testSetRedis(@RequestParam("key") String key,@RequestParam("value") String value) {
        redisTemplate.opsForValue().set(key,value);
    }

    @GetMapping("/testGetCache")
    public void testGetCache(@RequestParam("cacheName") String cacheName,@RequestParam("key") String key) {
        Cache cache = cacheManager.getCache(cacheName);
        if (cache != null) {
            String v = cache.get(key,String.class);
            log.info("缓存值为: {}",v);
        }else {
            log.info("无缓存");
        }
    }

    @GetMapping("/textSetCache")
    public void textSetCache(
            @RequestParam("cacheName") String cacheName,
            @RequestParam("key") String key,
            @RequestParam("value") String value) {
        Cache cache = cacheManager.getCache(cacheName);
        if (cache != null){
            cache.put(key, value);
            log.info("设置缓存成功");
        }else {
            log.info("无缓存");
        }
    }

    @GetMapping("/testDelCache")
    public void testDelCache(@RequestParam("cacheName") String cacheName) {
        if (!CacheNameEnums.isCode(cacheName)){
            log.info("缓存分区不存在");
            return;
        }
        Cache cache = cacheManager.getCache(cacheName);
        if (cache != null) {
            cache.invalidate();
            log.info("删除缓存成功");
        }else{
            log.info("无缓存");
        }
    }




}
