package com.microservices.cms.doc.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.microservices.cms.utils.CmsConstants;
import com.microservices.common.core.annotation.Excel;
import com.microservices.common.core.web.domain.BaseSortEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;

/**
 * 文章信息对象 cms_doc
 *
 * @author otto
 * @date 2023-03-28
 */
@Data
public class CmsDoc extends BaseSortEntity {
    private static final long serialVersionUID = 1L;

    /**
     * ID
     */
    private Long id;

    /**
     * 文章名称
     */
    @Excel(name = "文章名称")
    private String name;

    /**
     * 文件名称
     */
    @Excel(name = "文件名称")
    private String fileName;

    /** 文件路径 */
    @Excel(name = "文件路径")
    private String filePath;

    /** 文件SHA */
    @Excel(name = "文件SHA")
    private String fileSha;

    /** 文件大小 */
    @Excel(name = "文件大小")
    private Long fileSize;

    /** 文件URL */
    @Excel(name = "文件URL")
    private String fileUrl;

    /**
     * 文件HTML
     */
    @Excel(name = "文件HTML")
    private String htmlUrl;

    /**
     * 访问数
     */
    @Excel(name = "访问数")
    private Long visits;

    @ApiModelProperty("发布人")
    private String publishUser;

    @ApiModelProperty("发布人昵称")
    private String publishUserNickname;
    /**
     * 发布人头像
     */
    @ApiModelProperty("发布人头像")
    private String publishUserImage;
    /**
     * 发布人访问地址
     */
    @ApiModelProperty("发布人访问地址")
    private String publishUserUrl;

    /**
     * 发布时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "发布时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date publishTime;

    /**
     * 文章概要
     */
    @Excel(name = "文章概要")
    private String summary;
    /**
     * 文章头图
     */
    @Excel(name = "文章头图")
    private String headImg;

    /**
     * 是否为栏目
     */
    @Excel(name = "是否为栏目")
    private Boolean isDir;
    /**
     * 栏目名称
     */
    @Excel(name = "栏目名称")
    private String dirName;
    /**
     * 组织Id
     */
    @Excel(name = "组织Id")
    private Long deptId;
    /**
     * 关联的仓库IssueId（用于文章评论）
     */
    @Excel(name = "关联的仓库IssueId（用于文章评论）")
    private Long issueId;
    /**
     * 是否首页展示（0代表不展示 1代表展示）
     */
    private String isHomepage;
    /**
     * 文本编辑器类型
     */
    private String editorType;

    /**
     * 审核状态（0：审核不通过，1：审核通过 2:待审核）
     */
    private String auditStatus;

    /**
     * 文章审核临时分支名
     */
    private String auditTmpBranchName;

    /**
     * 文章审核合并请求编号
     */
    private String auditPrNumber;

    /**
     * 文章审核类型：create || update || delete
     */
    private String auditType;

    /**
     * 文章创建者
     */
    private String createBy;

    /**
     * 审核驳回后是否可重新编辑 0不可重新编辑 1可重新编辑
     */
    private String auditEditableStatus;

    /**
     * 关键词
     */
    private String keywords;

    public CmsDir copyToCmsDir() {
        CmsDir cmsDir = new CmsDir();
        cmsDir.setId(id);
        cmsDir.setName(name);
        cmsDir.setSort(this.getSort());
        cmsDir.setCreatedAt(getCreateTime());
        return cmsDir;
    }

    public CmsDoc copyToRenameCmsDir(String newName) {
        CmsDoc cmsDoc = new CmsDoc();
        cmsDoc.setId(id);
        cmsDoc.setName(newName);
        cmsDoc.setFilePath(getDirPath(newName));
        cmsDoc.setDeptId(deptId);
        cmsDoc.setSort(this.getSort());
        cmsDoc.setCreateTime(getCreateTime());
        return cmsDoc;
    }

    public HotCmsDocDto copyToHotCmsDocDto() {
        HotCmsDocDto hotCmsDocDto = new HotCmsDocDto();
        hotCmsDocDto.setId(id);
        hotCmsDocDto.setName(name);
        return hotCmsDocDto;
    }

    public CmsDocDetailDto copyToCmsDocDetailDto() {
        CmsDocDetailDto cmsDocDto = new CmsDocDetailDto();
        return (CmsDocDetailDto) getCmsDocBaseDto(cmsDocDto);
    }

    public CmsDocBaseDto copyToCmsDocBaseDto() {
        CmsDocBaseDto cmsDocDto = new CmsDocBaseDto();
        return getCmsDocBaseDto(cmsDocDto);
    }

    public CmsDocAllDto copyToCmsDocAllDto() {
        CmsDocAllDto cmsDocDto = new CmsDocAllDto();
        CmsDocAllDto cmsDocAllDto = (CmsDocAllDto) getCmsDocBaseDto(cmsDocDto);
        cmsDocAllDto.setDirName(dirName);
        return cmsDocAllDto;
    }

    private CmsDocBaseDto getCmsDocBaseDto(CmsDocBaseDto cmsDocDto) {
        cmsDocDto.setId(id);
        cmsDocDto.setName(name);
        cmsDocDto.setHeadImg(headImg);
        cmsDocDto.setIsHomepage(isHomepage);
        cmsDocDto.setSummary(summary);
        cmsDocDto.setEditorType(editorType);
        cmsDocDto.setDeptId(deptId);

        if (visits == null) {
            cmsDocDto.setVisits(0L);
        } else {
            cmsDocDto.setVisits(visits);
        }

        cmsDocDto.setPublishTime(publishTime);
        cmsDocDto.setPublishUser(publishUser);
        cmsDocDto.setPublishUserUrl(publishUserUrl);
        cmsDocDto.setPublishUserImage(publishUserImage);
        cmsDocDto.setPublishUserNickname(publishUserNickname);
        cmsDocDto.setAuditStatus(auditStatus);
        cmsDocDto.setAuditType(auditType);
        cmsDocDto.setCreateBy(createBy);
        cmsDocDto.setKeywords(keywords);

        return cmsDocDto;
    }

    public String getDirPath() {
        return this.name + "/" + CmsConstants.KEEP_FILE_NAME;
    }

    public String getDirPath(String name) {
        return name + "/" + CmsConstants.KEEP_FILE_NAME;
    }
}
