package cn.lihongjie.coal.deviceCategory.entity;

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

import io.hypersistence.utils.hibernate.type.array.ListArrayType;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;

import lombok.Data;

import org.hibernate.annotations.Formula;
import org.hibernate.annotations.Type;

import java.util.List;

@Data
@Entity
public class DeviceCategoryEntity extends OrgCommonEntity {

    @Type(ListArrayType.class)
    @Formula("(select array_agg(e.id) from t_device e where e.category_id = any(self_and_children_ids('t_device_category', id, true)))")
    private List<String> allDeviceIds;

    @Type(ListArrayType.class)
    @Formula("(select array_agg(e.id) from t_device e where e.category_id = any(self_and_children_ids('t_device_category', id, false)))")
    private List<String> childrenDeviceIds;

    @Type(ListArrayType.class)
    @Formula("(select array_agg(e.id) from t_device e where e.category_id = id)")
    private List<String> selfDeviceIds;


    @ManyToOne private DeviceCategoryEntity parent;

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    private List<DeviceCategoryEntity> children;
}
