package cn.cslg.pas.controller;

import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.model.QueryPatentVO;
import cn.cslg.pas.common.model.dto.ProductPatentDTO;
import cn.cslg.pas.common.model.vo.TaskParams;
import cn.cslg.pas.common.model.vo.TaskVO;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.common.utils.StringUtils;
import cn.cslg.pas.common.utils.auth.checkAuth;
import cn.cslg.pas.service.ProjectService;
import cn.cslg.pas.service.TaskService;
import cn.cslg.pas.service.UploadPatentBatchService;
import cn.cslg.pas.service.asso.AssoProductPatentService;
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.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 任务 前端控制器
 * </p>
 *
 * @author 王岩
 * @since 2022-02-17
 */
@Tag(name = "产品相关专利管理")
@RestController
@RequestMapping(Constants.API_VERSION_V2 + "/proPatent")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProductPatentController {
    private final AssoProductPatentService assoProductPatentService;
    private final UploadPatentBatchService uploadPatentBatchService;
    private final ProjectService projectService;

    @PostMapping("/add")
    @Operation(summary = "添加产品相关专利")
    public String addPatents(@RequestBody ProductPatentDTO patentDTO) {

        List<String> notQuery = assoProductPatentService.addPatents(patentDTO);
        if (notQuery.size() > 0) {
            return Response.successBut(notQuery);
        }
        return Response.success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除产品相关专利")
    public String deletePatents(@RequestBody ProductPatentDTO patentDTO) {
        List<String> patentNos = assoProductPatentService.deletePatents(patentDTO);
        if (patentNos.size() == 0) {
            return Response.success("删除成功");
        }
        return Response.success(patentNos);
    }

    @PostMapping("/importPatents")
    @Operation(summary = "导入专利")
    public String importSysPatent(MultipartFile file, Integer productId) {
        TaskParams taskParams = projectService.getImportPatentTaskParamsBro(file, productId);
        uploadPatentBatchService.uploadPatentForProduct(taskParams, productId);
        return Response.success();
    }

    @PostMapping("/queryPatents")
    @Operation(summary = "查询产品相关专利")
    public String queryPatents(@RequestBody QueryPatentVO params) {
        return Response.success(assoProductPatentService.getPageList(params));
    }
}

