package com.microservices.pms.document.domain.vo;

import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.bean.BeanUtils;
import com.microservices.pms.document.domain.PmsDocument;
import com.microservices.pms.enums.PmsDocumentType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * 项目管理-文档库对象 pms_document
 * 
 * @author wanjia
 * @date 2024-01-08
 */
@Data
@ApiModel("文档输入对象")
public class PmsDocumentInputVo
{

    /** 文档名称 */
    @ApiModelProperty(value = "文档名称")
    @Size(min = 1, max = 50)
    @NotNull
    private String name;

    /** 文档内容 */
    @ApiModelProperty(value = "文档内容")
    private String content;

    /** 文件类型 1-文档目录 2-文档 3-附件 4-wiki库 */
    @ApiModelProperty(value = "文件类型 1-文档目录 2-文档 3-附件 4-wiki库", required = true)
    @NotNull
    private Integer docType;

    /** 父级id */
    @ApiModelProperty(value = "父级id", required = true)
    @NotNull
    private Long parentId;

    /** 项目ID */
    @ApiModelProperty(value = "项目ID")
    private Long pmsProjectId;

    /** 企业ID */
    @ApiModelProperty(value = "企业ID", required = true)
    @NotNull
    private Long pmsEnterpriseId;

    /** wiki库归属的gitlink仓库ID */
    @ApiModelProperty(value = "wiki库归属的gitlink仓库ID")
    private Long wikiGitlinkRepoId;

    /** 是否置顶（0否，1是） */
    @ApiModelProperty(value = "是否置顶")
    private Integer isTop;

    /** 权限 1-私有 2-全员读 3-全员读写 */
    @ApiModelProperty(value = "权限 1-私有 2-全员读 3-全员读写", required = true)
    @NotNull
    private Integer permission;

    public PmsDocument toPmsDocument() {
        PmsDocument pmsDocument = new PmsDocument();
        validateDocumentAttachment();
        BeanUtils.copyProperties(this, pmsDocument);
        return pmsDocument;
    }

    private void validateDocumentAttachment() {
        //文档类型是否为1-文档目录 2-文档 4-wiki库
        if (docType != PmsDocumentType.DOCUMENT_DIRECTORY.getKey()
                && docType != PmsDocumentType.DOCUMENT.getKey()
                && docType != PmsDocumentType.WIKI.getKey()) {
            throw new ServiceException("文档类型参数(docType)错误，1-文档目录 2-文档 4-wiki库");
        }
        if (docType == PmsDocumentType.WIKI.getKey() && wikiGitlinkRepoId == null) {
            throw new ServiceException("文档类型为wiki库类型，wiki库归属的仓库ID不能为空");
        }
        if (docType == PmsDocumentType.WIKI.getKey()) {
            permission = 2; //wiki类型，默认设置权限为全员可读
        }
    }
}
