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.utils.Response;
import cn.cslg.pas.common.utils.StringUtils;
import cn.cslg.pas.common.utils.auth.checkAuth;
import cn.cslg.pas.domain.ProjectFolder;
import cn.cslg.pas.common.model.vo.ProjectFolderPatentVO;
import cn.cslg.pas.common.model.vo.ProjectFolderVO;
import cn.cslg.pas.service.ProjectFolderPatentLinkService;
import cn.cslg.pas.service.ProjectFolderService;
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 java.util.List;

/**
 * <p>
 * 专题库文件夹 前端控制器
 * </p>
 *
 * @author 王岩
 * @since 2021-12-24
 */
@Tag(name = "专题库专利文件夹")
@RestController
@RequestMapping(Constants.API_VERSION_V2 + "/project/folder")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProjectFolderController {

    private final ProjectFolderService projectFolderService;
    private final ProjectFolderPatentLinkService projectFolderPatentLinkService;

    @GetMapping("list")
    @Operation(summary = "文件夹列表")
    public String getList(ProjectFolderVO params) {
        return Response.success(projectFolderService.getList(params));
    }

    @Permission(roles = {2})
    @checkAuth(FunId = "/workspace/common/folder_manage/add")
    @PostMapping("add")
    @Operation(summary = "新增文件夹")
    public String add(@RequestBody ProjectFolder projectFolder) {
        return projectFolderService.add(projectFolder);
    }

    @Permission(roles = {2})
    @PostMapping("edit")
    @Operation(summary = "编辑文件夹")
    public String edit(@RequestBody List<ProjectFolder> folderList, String projectId) {
        return projectFolderService.edit(folderList, Integer.parseInt(projectId));
    }

    @Permission(roles = {2})
    @checkAuth(FunId = "/workspace/common/folder_manage/delete")
    @PostMapping("delete")
    @Operation(summary = "删除文件夹")
    public String delete(Integer id) {
        return projectFolderService.delete(id);
    }

    @Permission(roles = {1, 2})
    @PostMapping("/patent/delete")
    @Operation(summary = "删除文件夹里的专利")
    public String deletePatent(Integer folderId, String ids) {
        List<Integer> patentIds = StringUtils.changeStringToInteger(ids, ",");
        projectFolderPatentLinkService.deleteByFolderIdAndPatentIds(folderId, patentIds);
        return Response.success(true);
    }

    @Permission(roles = {2})
    @checkAuth(FunId = "/workspace/folder/batchOperation/copy")
    @PostMapping("patent")
    @Operation(summary = "文件夹批量移动、复制专利")
    public String updateProjectFolderPatent(@RequestBody ProjectFolderPatentVO params) {
        projectFolderPatentLinkService.updateProjectFolderPatent(params);
        return Response.success(true);
    }
}
