package cn.lihongjie.coal.dictionary.entity;

import cn.lihongjie.coal.base.entity.CommonEntity;

import jakarta.persistence.*;

import lombok.Getter;
import lombok.Setter;

import org.hibernate.annotations.Comment;

import java.util.List;

@Entity
@Getter
@Setter
@Comment("数据字典数据项")
public class DictionaryItemEntity extends CommonEntity {

    @ManyToOne() private DictionaryEntity dictionary;

    @OneToMany(mappedBy = "parent")
    private List<DictionaryItemEntity> children;

    @ManyToOne
    @JoinColumn(name = "parent_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private DictionaryItemEntity parent;

    public void initParent(DictionaryItemEntity o) {

        this.parent = o;

        if (children != null) {
            children.forEach(x -> x.initParent(this));
        }
    }

    public void initDict(DictionaryEntity dictionaryEntity) {
        this.dictionary = dictionaryEntity;
        if (children != null) {
            children.forEach(x -> x.initDict(dictionaryEntity));
        }
    }
}
