package com.izouma.nineth.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import lombok.*;
import org.hibernate.Hibernate;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;

/**
 * @author ZouYuHong
 */
@Getter
@Setter
@ToString
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ApiModel("藏品分类")
@Table(name = "collection_category", indexes = {
        @Index(columnList = "name")

})
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class CollectionCategory extends BaseEntity {

    /**
     * 分类名称
     */
    @NotBlank(message = "分类名不能为空")
    private String name;

    /**
     * 父级分类ID
     */
    private Long parentId;
    /**
     * 项目Id
     */
    private int projectId;
    /**
     * 顺序
     */
    private Integer sort;

    @OneToMany
    @JoinColumn(name = "parentId", insertable = false, updatable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    @Where(clause = "del = 0")
    private List<CollectionCategory> children;


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
        CollectionCategory that = (CollectionCategory) o;
        return getId() != null && Objects.equals(getId(), that.getId());
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }

    public CollectionCategory(Long id,Long parentId) {
        super.setId(id);
        this.parentId = parentId;
    }
}
