package cn.reghao.tnb.search.esapp.blog.model.po;

import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.search.esapp.blog.model.dto.ArticleDto;
import cn.reghao.tnb.search.esapp.util.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.search.engine.backend.types.Highlightable;
import org.hibernate.search.engine.backend.types.Projectable;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.KeywordField;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author reghao
 * @date 2023-04-10 17:57:36
 */
@NoArgsConstructor
@Setter
@Getter
@Indexed(index = "article")
@Entity
@Table(name = "blog_article")
public class Article extends BaseEntity {
    @KeywordField(projectable = Projectable.YES)
    @Column(nullable = false, unique = true)
    private String articleId;
    @FullTextField(analyzer = "ikAnalyzer", highlightable = Highlightable.ANY)
    @Column(nullable = false)
    private String title;
    @Column(nullable = false)
    private String excerpt;
    @FullTextField(analyzer = "ikAnalyzer", highlightable = Highlightable.ANY)
    @Column(columnDefinition="text")
    private String content;
    @Column(nullable = false)
    private String editor;
    @Column(nullable = false)
    private Integer categoryId;
    @Column(nullable = false)
    private Boolean published;
    @Column(nullable = false)
    private LocalDateTime publishAt;
    private transient List<String> tagIds;
    private Integer weight;
    private Integer postType;
    private Long owner;

    public Article(String articleId, ArticleDto articleDto, String excerpt) {
        this.articleId = articleId;
        this.title = articleDto.getTitle();
        this.excerpt = excerpt;
        this.content = articleDto.getContent();
        this.editor = articleDto.getEditor();
        this.categoryId = articleDto.getCategoryId();
        this.published = articleDto.getPublished();
        this.publishAt = LocalDateTime.now();
        this.weight = 0;
        this.postType = articleDto.getPostType();
        this.owner = UserContext.getUserId();
    }
}
