package com.ecep.contract.model;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.Objects;

import org.springframework.util.StringUtils;

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;

/**
 * 发票
 */
@Getter
@Setter
@Entity
@Table(name = "INVOICE", schema = "supplier_ms")
@ToString
public class Invoice implements IdentityEntity, BasedEntity, Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;

    /**
     * 发票号
     */
    @Column(name = "CODE")
    private String code;

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

    /**
     * 发票开票日期
     */
    @Column(name = "INVOICE_DATE")
    private LocalDate invoiceDate;

    /**
     * 备注
     */
    @Column(name = "DESCRIPTION", columnDefinition = "TEXT")
    private String description;

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

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

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