package cn.cslg.pas.controller;

import cn.cslg.pas.common.core.annotation.Permission;
import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.model.PersonnelVO;
import cn.cslg.pas.common.model.dto.TaskAddNewDTO;
import cn.cslg.pas.common.model.vo.ProjectImportPatentVO;
import cn.cslg.pas.common.model.vo.ProjectImportVO;
import cn.cslg.pas.common.model.vo.TaskParams;
import cn.cslg.pas.common.utils.*;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.common.utils.auth.checkAuth;
import cn.cslg.pas.exception.XiaoShiException;
import cn.cslg.pas.service.ProjectImportService;
import cn.cslg.pas.service.ProjectImportStatusService;
import cn.cslg.pas.service.ProjectService;
import cn.cslg.pas.service.UploadPatentBatchService;
import cn.cslg.pas.service.upLoadPatent.UploadTaskService;
import cn.dev33.satoken.stp.StpUtil;
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.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * <p>
 * 专题库导入 前端控制器
 * </p>
 *
 * @author 王岩
 * @since 2021-12-24
 */
@Tag(name = "专题库导入")
@RestController
@RequestMapping(Constants.API_VERSION_V2 + "/project/import")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProjectImportController {
    private final ProjectService projectService;
    private final CacheUtils cacheUtils;
    private final ProjectImportService projectImportService;
    private final ProjectImportStatusService projectImportStatusService;
    private final LoginUtils loginUtils;
    private final UploadPatentBatchService uploadPatentBatchService;
    private final UploadTaskService uploadTaskService;

    @Permission(roles = {2})
    @PostMapping("patent")
    @Operation(summary = "为专题库导入专利")
    public String importPatent(MultipartFile file, String json) throws IOException {
        ProjectImportPatentVO params = JsonUtils.jsonToPojo(json, ProjectImportPatentVO.class);
        if (params == null) {
            return Response.error("参数错误");
        }
//        TaskParams taskParams = projectService.getImportPatentTaskParams(file, params.getProjectId());
//        uploadPatentBatchService.uploadPatentBatch(taskParams, params);
        //Excel导入专利
        uploadTaskService.addExcelTask(file, json);
        return Response.success(true);
    }

    @PostMapping("sysPatent")
    @Operation(summary = "导入专利")
//    @Async("singleThreadAsyncTaskExecutor")
    public String importSysPatent(@RequestBody TaskParams taskParams) {
        return uploadPatentBatchService.uploadPatentBatchBro(taskParams);
    }

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

    @checkAuth(FunId = "/workspace/common/taskDelete")
    @PostMapping("delete")
    @Operation(summary = "删除导入任务")
    public String delete(Integer id) {
        return projectImportService.delete(id);
    }

    @GetMapping("status")
    @Operation(summary = "获取导入进度")
    public String getProjectImportStatusList(Integer importId) {
        return Response.success(projectImportStatusService.getProjectImportByImportId(importId));
    }

    @checkAuth(FunId = "/workspace/project/patent_import")
    @PostMapping
    @Operation(summary = "数据导入")
    public String importData(String url, ProjectImportVO params) {
        Integer userId = loginUtils.getId();
        String tempId = cacheUtils.getUserImportId(userId);
        PersonnelVO personnelVO = cacheUtils.getLoginUserPersonnel(userId);

        if (StringUtils.isNotEmpty(tempId)) {
            return Response.error("有导入任务在进行中");
        }
        Integer importId = projectImportService.add(userId, url, personnelVO.getTenantId());
        projectService.importData(url, params, userId, importId);
        cacheUtils.setUserImportId(userId, importId);
        return Response.success(importId);
    }

    @GetMapping("ongoing")
    @Operation(summary = "获取用户是否存在导入中的任务")
    public String getProjectImportOngoing() {
        Integer userId = loginUtils.getId();
        String tempId = cacheUtils.getUserImportId(userId);
        if (StringUtils.isNotEmpty(tempId)) {
            return Response.success(tempId);
        }
        return Response.success(false);
    }

    @PostMapping("/importByNos")
    @Operation(summary = "根据专利号导入专利到专题库或报告")
    public String importByNos(@RequestBody ProjectImportVO patentDTO) throws IOException {
//        List<String> notQuery = projectImportService.importByNos(patentDTO);
//        if (notQuery.size() > 0) {
//            return Response.successBut(notQuery);
//        }
//        return Response.success(true);
        TaskAddNewDTO taskAddNewDTO = new TaskAddNewDTO()
                .setProjectId(patentDTO.getProjectId())
                .setReportId(patentDTO.getReportId())
                .setConditions(patentDTO.getPatentNo());
        uploadTaskService.addPatentNoTask(taskAddNewDTO);
        return Response.success();
    }
}
