package cn.lihongjie.coal.base.entity;

import cn.lihongjie.coal.common.Ctx;

import jakarta.persistence.MappedSuperclass;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;

@MappedSuperclass
@Getter
@Setter
@FilterDef(name = "orgFilter", parameters = @ParamDef(name = "organizationId", type = String.class))
@Filter(name = "orgFilter", condition = "organization_id = :organizationId")
public class OrgCommonEntity extends CommonEntity {

    private String organizationId;

    @Override
    public void prePersist() {
        if (StringUtils.isEmpty(organizationId)) {

            this.organizationId =
                    Ctx.isLoggedIn() ? Ctx.currentUser().getOrganizationId() : organizationId;
        }
        super.prePersist();
    }

    @Override
    public void preUpdate() {
        if (StringUtils.isEmpty(organizationId)) {

            this.organizationId =
                    Ctx.isLoggedIn() ? Ctx.currentUser().getOrganizationId() : organizationId;
        }
        super.preUpdate();
    }
}
