package com.eshore.gringotts.web.domain.entity;

import com.eshore.gringotts.core.Constants;
import com.eshore.gringotts.web.domain.base.entity.CheckingNeededEntity;
import com.eshore.gringotts.web.domain.base.entity.LogicDeleteEntity;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.NamedAttributeNode;
import javax.persistence.NamedEntityGraph;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

/**
 * 确权
 *
 * @author lanyuanxiaoyao
 * @date 2024-11-26
 */
@Getter
@Setter
@ToString(callSuper = true)
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
@Table(name = Constants.TABLE_PREFIX + "confirmation")
@NamedEntityGraph(name = "confirmation.list", attributeNodes = {
        @NamedAttributeNode(value = "target"),
        @NamedAttributeNode(value = "createdUser"),
})
@NamedEntityGraph(name = "confirmation.detail", attributeNodes = {
        @NamedAttributeNode(value = "target"),
        @NamedAttributeNode(value = "evidences"),
        @NamedAttributeNode(value = "createdUser"),
        @NamedAttributeNode(value = "modifiedUser"),
})
@SQLDelete(sql = "update " + Constants.TABLE_PREFIX + "confirmation" + " set deleted = true where id = ?")
@Where(clause = LogicDeleteEntity.LOGIC_DELETE_CLAUSE)
public class Confirmation extends CheckingNeededEntity {
    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    private DataResource target;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinTable(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT), inverseForeignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    @ToString.Exclude
    private Set<DataFile> evidences;
}
