package com.citygis.web.controller;

import com.citygis.common.core.domain.AjaxResult;
import com.citygis.web.domain.Kafka;
import com.citygis.web.service.KafkaConsumerServer;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * @Package com.shcd.web.controller.kafka
 * @Author zyl
 * @Date 2024/5/29 0029 15:29
 * @description: kafka消费信息页面交互
 */
@CrossOrigin
@RestController
@RequestMapping("/kafka")
public class KafkaConsumerController {

    @Resource
    KafkaConsumerServer kafkaCousumerServer;

    /**
     * 获取所有group
     *
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @RequestMapping("/getAllGroups")
    public AjaxResult listConsumerGroups() throws ExecutionException, InterruptedException {
        return kafkaCousumerServer.getAllGroupId();
    }

    /**
     * 获取group消费详情
     *
     * @param kafka
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @RequestMapping("/getConsumerGroupDetails")
    public AjaxResult getConsumerGroupDetails(@RequestBody Kafka kafka) throws ExecutionException, InterruptedException {
        return kafkaCousumerServer.getConsumerGroupDetails(kafka.getGroupId());
    }

    /**
     * 重置offset
     *
     * @param kafka
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @RequestMapping("/resetOffeSet")
    public AjaxResult resetOffeSet(@RequestBody Kafka kafka) throws ExecutionException, InterruptedException {
        return kafkaCousumerServer.resetOffeSet(kafka);
    }

    /**
     * 消费kafka消息
     *
     * @param map
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @RequestMapping("/kafkaListenerConsumer")
    public void kafkaListenerConsumer(@RequestBody Kafka kafka) {
        kafkaCousumerServer.kafkaListenerConsumer(kafka.getGroupId(), kafka.getTopic());
    }

    /**
     * 获取所有topic和partition
     *
     * @throws Exception
     */
    @RequestMapping("/getTopicAndPartition")
    public Map<String, Integer> getTopicAndPartition() throws Exception {
        return kafkaCousumerServer.getTopicAndPartition();
    }

    /**
     * 根据获取所有topic和partition
     *
     * @throws Exception
     */
    @RequestMapping("/getTopicAndPartitionByIp")
    public Map<String, Integer> getTopicAndPartitionByIp(@RequestBody Kafka kafka) throws Exception {
        return kafkaCousumerServer.getTopicAndPartitionByIp(kafka.getBootstrapServer());
    }

    /**
     * 删除group
     *
     * @param map
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @RequestMapping("/deleteConsumerGroups")
    public AjaxResult deleteConsumerGroups(@RequestBody Map<String, Object> map) throws ExecutionException, InterruptedException {
       return kafkaCousumerServer.deleteConsumerGroups(Arrays.asList(map.get("groupId").toString().replaceAll("\\[", "").replaceAll("]", "")));
    }

}
