package cn.cslg.report.controller;


import cn.cslg.report.common.core.base.Constants;
import cn.cslg.report.common.model.dto.TaskTDO;
import cn.cslg.report.common.model.vo.*;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.common.utils.auth.checkAuth;
import cn.cslg.report.entity.*;
import cn.cslg.report.service.BaseService;
import cn.cslg.report.service.business.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
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;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;


@Tag(name = "任务管理")
@RestController
@RequestMapping(Constants.REPORT_API + "/task")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class TaskController {
    private final TaskService taskService;
    private final BaseService baseService;
    private final ImportTaskService importTaskService;

    @RequestMapping(value = "/addTask", method = RequestMethod.POST)
    @Operation(summary = "新增审核任务")
    public String addTask( String taskVO,List<MultipartFile> files) throws ParseException, IOException {
        return taskService.addTask(taskVO,files);
    }

    @RequestMapping(value = "/deleteTask", method = RequestMethod.POST)
    @Operation(summary = "删除任务")
    public String deleteTask(Integer id) {
        return taskService.deleteTask(id);
    }

    @RequestMapping(value = "/updateTask", method = RequestMethod.POST)
    @Operation(summary = "修改任务")
    public String updateTask(@RequestBody Task task) {
        return taskService.updateTask(task);
    }

    @RequestMapping(value = "/queryPageList", method = RequestMethod.POST)
    @Operation(summary = "分页查询任务列表")
    public String queryPageList(@RequestBody TaskVO taskVO) throws IOException {
        return Response.success(taskService.queryPageList(taskVO));
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @Operation(summary = "获取所有任务")
    public String getAllTask() {
        return taskService.getAllTask();
    }


    @RequestMapping(value = "/auditTask", method = RequestMethod.POST)
    @Operation(summary = "审核任务")
    public String auditTask(@RequestBody AuditTaskVO auditTaskVO) throws ParseException, IOException {
        return taskService.auditTask(auditTaskVO);
    }

    @RequestMapping(value = "/addAssTask", method = RequestMethod.POST)
    @Operation(summary = "添加分配任务")
    public String addAssTask(@RequestBody AssignTaskVO assignTaskVO) throws ParseException, IOException {
        return taskService.addAssTask(assignTaskVO);
    }

    @RequestMapping(value = "/reTaskPatents", method = RequestMethod.POST)
    @Operation(summary = "返回任务专利清单")
    public String reTaskPatents(@RequestBody PatentVO patentVO) throws ParseException, IOException {
         return taskService.reTaskPatents1(patentVO);
    }

    @RequestMapping(value = "/reTaskStateCount", method = RequestMethod.GET)
    @Operation(summary = "返回任务专利状态的数量")
    public String reTaskStateCount(Integer reportId,Integer taskId) throws ParseException, IOException {
        return Response.success(taskService.reTaskStateCount(reportId,taskId));
    }

    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @Operation(summary = "导入")
    public String importPatent(MultipartFile file, Integer reportId) throws ParseException, IOException {
        ImportTask importTask =baseService.getImportPatentTaskParamsBro(file, reportId);
            baseService.uploadToPCS(importTask);
        //解析file装载数据
        return Response.success(importTask );
    }

    @RequestMapping(value = "/import/delete", method = RequestMethod.GET)
    @Operation(summary = "删除导入记录")
    public String importPatent(int id) throws ParseException, IOException {
     Boolean flag =  importTaskService.delImportTask(id);
     if(flag) {
     return  Response.success();
     }
        return Response.error();
    }

    @RequestMapping(value = "/reImportTask", method = RequestMethod.POST)
    @Operation(summary = "根据报告Id和状态返回导入任务列表")
    public String reImportTask(@RequestBody ImportTask importTask) {

        return importTaskService.reImportTask(importTask);
    }

    @RequestMapping(value = "/finish", method = RequestMethod.GET)
    @Operation(summary = "完成任务")
    public String finish(Integer id) throws IOException {
        return taskService.finishTask(id);
    }

    @RequestMapping(value = "/reviewTheTasks", method = RequestMethod.POST)
    @Operation(summary = "查看任务")
    public String ReviewTheTasks(@RequestBody TaskTDO taskVO) throws IOException {
        return taskService.ReviewTheTasks(taskVO) ;
    }

    @RequestMapping(value = "/progress", method = RequestMethod.GET)
    @Operation(summary = "任务进度")
    public String progress(int id){
        return Response.success (taskService.reCount(id));
    }

    @RequestMapping(value = "/read", method = RequestMethod.GET)
    @Operation(summary = "未读变成已读")
    public String read(int taskID,String patentNo ,Integer reportId){
        return taskService.read(taskID,patentNo, reportId);
    }

    @RequestMapping(value = "/look", method = RequestMethod.GET)
    @Operation(summary = "查看某个专利的状态")
    public String look(int taskID,String patentNo,Integer reportId){
        return taskService.look(taskID,patentNo,reportId);
    }

    @RequestMapping(value = "/addSyneryTask", method = RequestMethod.POST)
    @Operation(summary = "添加协同任务")
    public String addSyneryTask(@RequestBody SyneryVO syneryVO) throws ParseException, IOException {
        return taskService.addSyneryTask(syneryVO);
    }

    @RequestMapping(value = "/getTaskSources", method = RequestMethod.GET)
    @Operation(summary = "获得任务过滤条件列表")
    public String getTaskSources(Integer reportId) throws  IOException {
        return Response.success(taskService.getTaskSources(reportId));
    }
}
