package cn.cslg.pas.controller;

import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.model.dto.QueryQrtzTaskLogsDTO;
import cn.cslg.pas.common.model.dto.TaskAddNewDTO;
import cn.cslg.pas.common.model.dto.QueryTaskDTO;
import cn.cslg.pas.common.model.vo.TaskVO;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.domain.Task;
import cn.cslg.pas.domain.asso.TaskCondition;
import cn.cslg.pas.service.TaskService;
import cn.cslg.pas.service.upLoadPatent.JobService;
import cn.cslg.pas.service.upLoadPatent.UploadTaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 任务 前端控制器
 * </p>
 *
 * @author 王岩
 * @since 2022-02-17
 */
@Slf4j
@Tag(name = "系统任务")
@RestController
@RequestMapping(Constants.API_VERSION_V2 + "/task")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class TaskController {
    private final TaskService taskService;
    private final UploadTaskService uploadTaskService;
    private final JobService jobService;

    @PostMapping("/addTask")
    @Operation(summary = "新增网站导入任务")
    public String add(@RequestBody TaskAddNewDTO taskAddNewDTO) throws IOException, SchedulerException {
        log.info("开始处理【新增网站导入专利任务】的请求，请求参数为：{}", taskAddNewDTO);

        TaskCondition taskCondition = new TaskCondition();
        //新增欧专局网站导入任务
        if (taskAddNewDTO.getTaskType2().equals(3)) {
            taskCondition = uploadTaskService.addEpoTask(taskAddNewDTO);
            //新增专利之星网站导入任务
        } else if (taskAddNewDTO.getTaskType2().equals(4)) {
            taskCondition = uploadTaskService.addPatentStarTask(taskAddNewDTO);
        }

        //若是定时任务，则新增添加定时任务的job
        if (taskCondition.getTaskType() == 1) {
            jobService.addJob(taskCondition);
        }
        return Response.success("添加任务完成");
    }

//    @PostMapping("/addPatentNoTask")
//    @Operation(summary = "根据专利号导入专利任务")
//    public String addPatentNoTask(@RequestBody TaskAddNewDTO taskAddNewDTO) throws IOException {
//        uploadTaskService.addPatentNoTask(taskAddNewDTO);
//        return Response.success();
//    }

    @PostMapping("/queryTasks")
    @Operation(summary = "查询任务列表")
    public String queryTasks(@RequestBody QueryTaskDTO queryTaskDTO) throws IOException {
        return Response.success(taskService.queryTasks(queryTaskDTO));
    }

    @PostMapping("/queryQrtzTaskLogs")
    @Operation(summary = "查询定时任务执行日志")
    public String queryQrtzTaskLogs(@RequestBody QueryQrtzTaskLogsDTO queryQrtzTaskLogsDTO) {
        return Response.success(taskService.queryQrtzTaskLogs(queryQrtzTaskLogsDTO));
    }

    @GetMapping("/pause")
    @Operation(summary = "暂停任务")
    public Response pauseTask(Integer taskId) throws InterruptedException {
        log.info("开始处理【暂停任务】的请求，请求参数为：taskId={}", taskId);
        Integer taskStatus = taskService.pauseTask(taskId);
        return Response.ok(taskStatus);
    }

    @GetMapping("/continue")
    @Operation(summary = "继续任务")
    public Response continueTask(Integer taskId) {
        log.info("开始处理【继续任务】的请求，请求参数为：taskId={}", taskId);
        Integer taskStatus = taskService.continueTask(taskId);
        return Response.ok(taskStatus);
    }

    @GetMapping("/cancelTask")
    @Operation(summary = "取消任务")
    public String cancelTask(Integer taskId) throws SchedulerException, InterruptedException {
        log.info("开始处理【取消任务】的请求，请求参数为：taskId={}", taskId);
        taskService.cancelTask(taskId);
        return Response.success("任务取消成功");
    }

    @GetMapping("list")
    @Operation(summary = "任务列表")
    public String getPageList(TaskVO params) throws IOException {
        return Response.success(taskService.getPageList(params));
    }

    @PostMapping("delete")
    @Operation(summary = "删除任务")
    public String delete(Integer id) {
        return taskService.delete(id);
    }

    @GetMapping("queue")
    @Operation(summary = "获取进行中的任务")
    public String getQueueList(Integer type, Integer projectId, Integer productId) {
        return Response.success(taskService.getQueueList(type, projectId, productId));
    }

    @PostMapping("update")
    @Operation(summary = "更新任务")
    public String update(@RequestBody Task task) {
        taskService.updateStatus(task.getId(), task.getStatus(), task.getEndTime());
        return Response.success(true);
    }

}

