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.dto.ProjectFieldOrderDTO;
import cn.cslg.pas.common.model.vo.ProjectFieldOrderVO;
import cn.cslg.pas.common.utils.CacheUtils;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.common.utils.StringUtils;
import cn.cslg.pas.common.utils.auth.checkAuth;
import cn.cslg.pas.service.ProjectFieldService;
import cn.cslg.pas.domain.ProjectField;
import cn.cslg.pas.common.model.vo.ProjectFieldVO;
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.web.bind.annotation.*;

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

/**
 * <p>
 * 专题库字段 前端控制器
 * </p>
 *
 * @author 王岩
 * @since 2021-12-24
 */
@Tag(name = "专题库自定义字段")
@RestController
@RequestMapping(Constants.API_VERSION_V2 + "/project/field")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProjectFieldController {

    private final ProjectFieldService projectFieldService;
    private final CacheUtils cacheUtils;
    private final LoginUtils loginUtils;
    @Permission(roles = {1,2,3,4})
    @checkAuth(FunId = "/workspace/common/customField/check")
    @GetMapping("list")
    @Operation(summary = "自定义字段列表")
    public String getPageList(ProjectFieldVO params) throws IOException {
        return Response.success(projectFieldService.getPageList(params));
    }

    @Permission(roles = {1,2})
    @checkAuth(FunId = "/workspace/common/customField/add")
    @PostMapping("add")
    @Operation(summary = "新增自定义字段")
    public String add(@RequestBody ProjectField projectField) {
        return projectFieldService.add(projectField);
    }

    @Permission(roles = {1,2})
    @checkAuth(FunId = "/workspace/common/customField/modify")
    @PostMapping("edit")
    @Operation(summary = "编辑自定义字段")
    public String edit(@RequestBody ProjectField projectField) {
        return projectFieldService.edit(projectField);
    }

    @Permission(roles = {1,2})
    @checkAuth(FunId = "/workspace/common/customField/delete")
    @PostMapping("delete")
    @Operation(summary = "删除自定义字段")
    public String delete(Integer id) {
        return projectFieldService.delete(id);
    }

    @GetMapping("tree")
    @Operation(summary = "获取树类型的自定义字段")
    public String getPatentQueryTree(Integer projectId) {
        return Response.success(projectFieldService.getTreeOptionList(projectId));
    }

    @GetMapping("{id}")
    @Operation(summary = "根据ID获取自定义字段")
    public String getProjectCustomFieldById(@PathVariable Integer id) {
        return Response.success(projectFieldService.getProjectFieldById(id));
    }

    @GetMapping("custom")
    @Operation(summary = "获取自定义字段")
    public String getProjectCustomField(Integer projectId) {
        return Response.success(projectFieldService.getProjectCustomField(projectId));
    }

    @checkAuth(FunId = "/workspace/common/customField/import")
    @PostMapping("import")
    @Operation(summary = "从一个专题库导入一个自定义字段")
    public String importProjectField(String id, Integer projectId) {
        projectFieldService.importProjectField(StringUtils.changeStringToInteger(id, ","), projectId);
        return Response.success(true);
    }

    @Permission(roles = {1,2})
    @checkAuth(FunId = "/workspace/common/customField/copy")
    @PostMapping("copy")
    @Operation(summary = "将自定义字段复制到一个专题库中")
    public String copyProjectField(Integer id, String projectId) {
        projectFieldService.copyProjectField(id, StringUtils.changeStringToInteger(projectId, ","));
        return Response.success(true);
    }

    @Permission(roles = {1, 2})
    @PostMapping("order")
    @Operation(summary = "用户更新字段的顺序")
    public String setProjectFieldOrder(@RequestBody ProjectFieldOrderVO params) {
        cacheUtils.setProjectFieldOrder(params.getProjectId(), loginUtils.getId(), params.getData());
        return Response.success(true);
    }

    @GetMapping("order")
    @Operation(summary = "获取用户的自定义字段排序")
    public String getProjectFieldOrder(Integer projectId) {
        return Response.success(cacheUtils.getProjectFieldOrder(projectId, loginUtils.getId()));
    }
}
