package com.zksy.data.controller;

import com.zksy.common.domain.Result;
import com.zksy.common.exception.BizIllegalException;
import com.zksy.data.domain.dto.TimeTaskDto;
import com.zksy.data.domain.po.XhScheduleType;
import com.zksy.data.domain.po.XhTimeTask;
import com.zksy.data.schedule.CollectDataSchedule;
import com.zksy.data.service.XhScheduleTypeService;
import com.zksy.data.service.XhTimeTaskService;
import com.zksy.data.utils.TaskManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/timeTask")
@Api(tags = "定时任务",description = "定时任务")
public class TimeTaskController {
    @Autowired
    private TaskManager taskManager;
    @Autowired
    private XhTimeTaskService xhTimeTaskService;
    @Autowired
    private XhScheduleTypeService xhScheduleTypeService;
    @PostMapping("/addTaskForXhSchedule")
    @ApiOperation("添加定时任务")
    public Result<String> addTaskForXhSchedule(@RequestBody TimeTaskDto timeTaskDto) {
        return xhTimeTaskService.saveTimeTask(timeTaskDto);
    }

    @PostMapping("/updateAllModifySchedule")
    @ApiOperation("修改所有定时任务")
    //前端会传一个数组来修改进行全量同步
    public Result<String> updateAllModifySchedule(@RequestBody List<XhTimeTask> xhTimeTaskList) {
        System.out.println("xhTimeTaskList:"+xhTimeTaskList);
        xhTimeTaskService.updateBatchById(xhTimeTaskList);
        return Result.ok("修改成功");
    }


    @DeleteMapping("/deleteTask")
    @ApiOperation("删除定时任务")
    public Result<String> deleteTask(@RequestParam String taskName) {
        return xhTimeTaskService.removeTimeTask(taskName);
    }

    @GetMapping("/getAllTask")
    @ApiOperation("获取所有的定时任务")
    public Result<List<XhTimeTask>> getAllTask() {
    	return xhTimeTaskService.getAllTask();
    }

    @GetMapping("/getAllTaskType")
    @ApiOperation("获取所有的定时任务类型")
    public Result<List<XhScheduleType>> getAllTaskType() {
    	return Result.ok(xhScheduleTypeService.list());
    }

    @GetMapping("/allRunningTask")
    @ApiOperation("获取所有正在执行的定时任务")
    public String allTask() {
    	return taskManager.getTaskMap().toString();
    }

    @GetMapping("/taskStatus")
    @ApiOperation("获取指定任务的状态")
    public String taskStatus(@RequestParam String taskId) {
    	return taskManager.getTaskStatus(taskId).toString();
    }

    @GetMapping("/stopAll")
    @ApiOperation("暂停所有定时任务")
    public Result<String> stopAll() {
        return xhTimeTaskService.stopAllTasks();
    }
}