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.vo.ProjectFieldOptionVO;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.common.utils.auth.checkAuth;
import cn.cslg.pas.domain.ProjectFieldOption;
import cn.cslg.pas.domain.ProjectFieldTree;
import cn.cslg.pas.service.ProjectFieldOptionService;
import cn.cslg.pas.service.ProjectFieldService;
import cn.cslg.pas.service.ProjectFieldTextService;
import cn.cslg.pas.service.ProjectFieldTreeService;
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.*;

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

    private final ProjectFieldTreeService projectFieldTreeService;
    private final ProjectFieldOptionService projectFieldOptionService;



    @GetMapping("list")
    @Operation(summary = "选项列表")
    public String getList(ProjectFieldOptionVO params) {
        if (params.getType() == 6) {
            return Response.success(projectFieldTreeService.getProjectFieldTreeNodeByFieldId(params.getFieldId()));
        } else {
            return Response.success(projectFieldOptionService.getListByFieldId(params.getFieldId()));
        }
    }

    @Permission(roles = {1, 2})
    @PostMapping("add")
    @Operation(summary = "添加")
    public String add(@RequestBody ProjectFieldOption projectFieldOption) {
        if (projectFieldOption.getType() == 6) {
            ProjectFieldTree tree = new ProjectFieldTree();
            tree.setName(projectFieldOption.getName());
            tree.setParentId(projectFieldOption.getParentId());
            tree.setFieldId(projectFieldOption.getFieldId());
            return Response.success(projectFieldTreeService.add(tree));
        } else {
            return Response.success(projectFieldOptionService.add(projectFieldOption));
        }
    }

    @Permission(roles = {1, 2})
    @PostMapping("edit")
    @Operation(summary = "编辑")
    public String edit(@RequestBody ProjectFieldOption projectFieldOption) {
        if (projectFieldOption.getType() == 6) {
            ProjectFieldTree tree = new ProjectFieldTree();
            tree.setId(projectFieldOption.getId());
            tree.setName(projectFieldOption.getName());
            tree.setParentId(projectFieldOption.getParentId());
            tree.setFieldId(projectFieldOption.getFieldId());
            projectFieldTreeService.edit(tree);
        } else {
            projectFieldOptionService.edit(projectFieldOption);
        }
        return Response.success(true);
    }

    @checkAuth(FunId = "/workspace/common/customField/managementOptions")
    @PostMapping("update")
    @Operation(summary = "更新选项")
    public String update(@RequestBody ProjectFieldOption projectFieldOption) {
        return projectFieldOptionService.updateProjectFieldOption(projectFieldOption);
    }

    @Permission(roles = {1, 2})
    @PostMapping("delete")
    @Operation(summary = "删除选项")
    public String delete(Integer type, Integer id) {
        if (type == 6) {
            projectFieldTreeService.delete(id);
        } else {
            projectFieldOptionService.delete(id);
        }
        return Response.success(true);
    }
}
