package com.microservices.cms.doc.domain;

import com.microservices.common.core.exception.ServiceException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @author otto
 */
@Data
@ApiModel("文章详情")
public class CmsDocDetailDto extends CmsDocBaseDto {

    /**
     * 文章内容
     */
    @ApiModelProperty("文章内容")
    private String content;

    /**
     * 文章栏目
     */
    @ApiModelProperty("文章栏目")
    private CmsDir cmsDir;

    /**
     * 是否首次发布
     */
    @ApiModelProperty("是否首次发布")
    private Boolean isFirstPublish = true;

    public void setCmsDir(CmsDoc cmsDoc) {
        if (cmsDoc != null) {
            this.cmsDir = cmsDoc.copyToCmsDir();
        }
    }

    public String getContent() {
        String base64encodedString;
        try {
            base64encodedString = Base64.getEncoder().encodeToString(this.content.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            throw new ServiceException("文章内容的Base64编码格式不正确");
        }
        return base64encodedString;
    }
}
