package com.ecep.contract.ds.vendor.model;

import com.ecep.contract.ds.other.model.Invoice;
import com.ecep.contract.model.*;
import com.ecep.contract.util.HibernateProxyUtils;
import com.ecep.contract.vo.PurchaseSettlementVoucherItemVo;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Objects;

/**
 * 采购结算凭证详情
 * 对应 U8 的 PurSettleVouchs 表
 */
@Getter
@Setter
@Entity
@Table(name = "PURCHASE_SETTLE_VOUCHER_ITEM", schema = "supplier_ms")
@ToString
public class PurchaseSettlementVoucherItem implements IdentityEntity, BasedEntity, Voable<PurchaseSettlementVoucherItemVo> {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;
    /**
     * PurSettleVouchs 表的 ID 主键
     */
    @Column(name = "REF_ID")
    private int refId;
    /**
     * 采购结算单
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "VOUCHER_ID")
    @ToString.Exclude
    private PurchaseSettlementVoucher voucher;

    /**
     * 会计师
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ACCOUNTANT_ID")
    @ToString.Exclude
    private Employee accountant;

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

    /**
     * 对应的采购入库单
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PURCHASE_RECEIPT_ID")
    @ToString.Exclude
    private PurchaseReceipt receipt;

    /**
     * cInvCode 字段
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "INVENTORY_ID")
    @ToString.Exclude
    private Inventory inventory;

    @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;
        }
        PurchaseSettlementVoucherItem that = (PurchaseSettlementVoucherItem) 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 PurchaseSettlementVoucherItemVo toVo() {
        PurchaseSettlementVoucherItemVo vo = new PurchaseSettlementVoucherItemVo();
        vo.setId(id);
        vo.setRefId(refId);
        if (voucher != null) {
            vo.setVoucherId(voucher.getId());
            vo.setVoucherCode(voucher.getCode());
        }
        if (accountant != null) {
            vo.setAccountantId(accountant.getId());
            vo.setAccountantName(accountant.getName());
        }
        if (invoice != null) {
            vo.setInvoiceId(invoice.getId());
            vo.setInvoiceCode(invoice.getCode());
        }
        if (receipt != null) {
            vo.setReceiptId(receipt.getId());
            vo.setReceiptCode(receipt.getCode());
        }
        if (inventory != null) {
            vo.setInventoryId(inventory.getId());
            vo.setInventoryName(inventory.getName());
        }
        vo.setQuantity(quantity);
        vo.setPrice(price);
        vo.setDescription(description);
        return vo;
    }
}
