package com.friday.distributor.controller;

import com.alibaba.fastjson.JSON;
import com.friday.distributor.queue.TaskQueueManager;
import com.friday.operation.api.distributor.dto.ClearQueueDto;
import com.friday.operation.api.distributor.dto.TaskQueueDto;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/task/distributor/queue")
@Slf4j
public class TaskQueueController {

    @Autowired
    private TaskQueueManager taskQueueManager;


    @ApiOperation(value = "新增队列")
    @RequestMapping(value = "/addQueue", method = RequestMethod.POST)
    public void addQueue(@RequestBody TaskQueueDto taskQueueDto) {
        taskQueueManager.addQueue(taskQueueDto);
    }

    @ApiOperation(value = "清除队列")
    @RequestMapping(value = "/clearQueue", method = RequestMethod.POST)
    public void clearQueue(@RequestBody ClearQueueDto clearQueueDto) {
        taskQueueManager.clearQueue(clearQueueDto, clearQueueDto.getKey());
    }

    @ApiOperation(value = "移除队列")
    @RequestMapping(value = "/removeQueue", method = RequestMethod.POST)
    public void removeQueue(@ApiParam(name = "queueKey") String queueKey) {
        taskQueueManager.removeQueue(queueKey);
    }

    @ApiOperation(value = "获取队列任务")
    @RequestMapping(value = "/taskList", method = RequestMethod.GET)
    public Object getTaskInQueue(@ApiParam(name = "queueKey") String queueKey) {
        return JSON.toJSONString(taskQueueManager.getAbbrTaskList(queueKey));
    }

    @ApiOperation(value = "获取指定的队列任务")
    @RequestMapping(value = "/getTaskById", method = RequestMethod.GET)
    public Object getTaskByIdInQueue(@ApiParam(name = "taskId") String taskId) {
        return taskQueueManager.getTaskById(taskId);
    }

    @ApiOperation(value = "获取指定的队列子任务")
    @RequestMapping(value = "/getJobById", method = RequestMethod.GET)
    public Object getJobByIdInQueue(@ApiParam(name = "invokeId") Integer invokeId) {
        return taskQueueManager.getJobById(invokeId);
    }

    @ApiOperation(value = "获取队列任务数量统计")
    @RequestMapping(value = "/getTaskStatistic", method = RequestMethod.GET)
    public Object getTaskStatistic() {
        return taskQueueManager.getTaskStatistic();
    }

}
