package com.ecep.contract.model;

import java.time.LocalDateTime;
import java.util.Objects;

import com.ecep.contract.util.HibernateProxyUtils;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 采购账单凭证(专用发票)， Purchase Bill Voucher
 * 对应 U8 的 PurBillVouch 表
 */
@Getter
@Setter
@Entity
@Table(name = "PURCHASE_BILL_VOUCHER", schema = "supplier_ms")
@ToString
public class PurchaseBillVoucher implements IdentityEntity, BasedEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;

    /**
     * PurBillVouch 表的 PBVID 主键
     */
    @Column(name = "REF_ID")
    private int refId;

    @Column(name = "CODE")
    private String code;

    /**
     * 所属公司
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COMPANY_ID")
    @ToString.Exclude
    private Company company;

    /**
     * 发票，对应 cPBVCode 字段
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "INVOICE_ID")
    @ToString.Exclude
    private Invoice invoice;

    /**
     * 业务员
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "EMPLOYEE_ID")
    @ToString.Exclude
    private Employee employee;

    /**
     * 制单人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "MAKER_ID")
    @ToString.Exclude
    private Employee maker;
    /**
     * 制单日期
     */
    @Column(name = "MAKER_DATE")
    private LocalDateTime makerDate;
    /**
     * 修改日期
     */
    @Column(name = "MODIFY_DATE")
    private LocalDateTime modifyDate;
    /**
     * 审核人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "VERIFIER_ID")
    @ToString.Exclude
    private Employee verifier;
    /**
     * 审核日期
     */
    @Column(name = "VERIFIER_DATE")
    private LocalDateTime verifierDate;
    /**
     * 备注
     */
    @Column(name = "DESCRIPTION", columnDefinition = "TEXT")
    private String description;

    @Override
    public String toPrettyString() {
        return getId() + "#" + getCode();
    }

    @Override
    public final boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (object == null) {
            return false;
        }
        if (HibernateProxyUtils.isNotEffectiveClassEquals(object, this)) {
            return false;
        }
        PurchaseBillVoucher that = (PurchaseBillVoucher) object;
        return getId() != null && Objects.equals(getId(), that.getId());
    }

    @Override
    public final int hashCode() {
        return HibernateProxyUtils.hashCode(this);
    }
}
