package com.ecep.contract.model;

import java.util.Objects;

import com.ecep.contract.util.HibernateProxyUtils;
import com.ecep.contract.vo.PurchaseBillVoucherItemVo;

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 Item
 * 对应 U8 的 PurBillVouchs 表
 */
@Getter
@Setter
@Entity
@Table(name = "PURCHASE_BILL_VOUCHER_ITEM", schema = "supplier_ms")
@ToString
public class PurchaseBillVoucherItem
        implements IdentityEntity, BasedEntity, java.io.Serializable, Voable<PurchaseBillVoucherItemVo> {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;

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

    /**
     * 对应的采购账单凭证
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "VOUCHER_ID")
    @ToString.Exclude
    private PurchaseBillVoucher voucher;

    /**
     * 对应的采购订单, 对应 iPOsID 字段
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PURCHASE_ORDER_ITEM_ID")
    @ToString.Exclude
    private PurchaseOrderItem orderItem;

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "INVENTORY_ID")
    @ToString.Exclude
    private Inventory inventory;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CONTRACT_ID")
    @ToString.Exclude
    private Contract contract;

    @Column(name = "QUANTITY")
    private double quantity;

    /**
     * 结算单价
     */
    @Column(name = "PRICE")
    private double price;

    @Column(name = "DESCRIPTION", columnDefinition = "TEXT")
    private String description;

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

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

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

    @Override
    public PurchaseBillVoucherItemVo toVo() {
        PurchaseBillVoucherItemVo vo = new PurchaseBillVoucherItemVo();
        vo.setId(id);
        vo.setRefId(refId);
        if (voucher != null) {
            vo.setVoucherId(voucher.getId());
        }
        if (orderItem != null) {
            vo.setOrderItemId(orderItem.getId());
        }
        if (invoice != null) {
            vo.setInvoiceId(invoice.getId());
        }
        if (inventory != null) {
            vo.setInventoryId(inventory.getId());
        }
        if (contract != null) {
            vo.setContractId(contract.getId());
        }
        vo.setQuantity(quantity);
        vo.setPrice(price);
        vo.setDescription(description);
        return vo;
    }
}
