package com.microservices.cms.doc.domain;

import com.microservices.cms.utils.CmsConstants;
import com.microservices.common.core.annotation.Excel;
import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.StringUtils;
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 CmsDocDto {
    /**
     * 文章标识
     */
    @Excel(name = "文章标识")
    @ApiModelProperty(value = "文章标识", required = true)
    private Long id;
    /**
     * 栏目Id
     */
    @ApiModelProperty(value = "栏目Id")
    private Long dirId;
    /**
     * 文章名称
     */
    @Excel(name = "文章名称")
    @ApiModelProperty(value = "文章名称", required = true)
    private String name;
    /**
     * 文章内容
     */
    @Excel(name = "文章内容")
    @ApiModelProperty(value = "文章内容", required = true)
    private String content;
    /**
     * 文章头图
     */
    @Excel(name = "文章头图")
    @ApiModelProperty(value = "文章头图")
    private String headImg;
    /**
     * 排序
     */
    @ApiModelProperty(value = "顺序")
    private Integer order;

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

    /**
     * 是否在栏目下修改
     */

    @ApiModelProperty("是否在栏目下修改")
    private boolean isChangeOnDir;
    /**
     * 关键词
     */
    @ApiModelProperty("关键词")
    private String keywords;

    public String getName() {
        if (name != null) {
            name = name.replaceAll(CmsConstants.DIR_SEPARATOR,
                    CmsConstants.DIR_SEPARATOR_REPLACE);
        }
        return name;
    }

    public String getContent() {
        if (StringUtils.isEmpty(this.content)) {
            return this.content;
        }
        String base64DecoderString;
        try {
            byte[] decoderBytes = Base64.getDecoder().decode(this.content);
            base64DecoderString = new String(decoderBytes, StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new ServiceException("文章内容的Base64编码格式不正确");
        }
        return base64DecoderString;
    }
}
