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

import com.ecep.contract.ds.company.model.Company;
import com.ecep.contract.model.BasedEntity;
import com.ecep.contract.model.IdentityEntity;
import com.ecep.contract.model.Voable;
import com.ecep.contract.util.HibernateProxyUtils;
import com.ecep.contract.vo.InvoiceVo;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.util.StringUtils;

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

/**
 * 收到的公司发票
 */
@Getter
@Setter
@Entity
@Table(name = "INVOICE", schema = "supplier_ms")
@ToString
public class Invoice implements IdentityEntity, BasedEntity, Voable<InvoiceVo> {

    @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);
    }

    @Override
    public InvoiceVo toVo() {
        InvoiceVo vo = new InvoiceVo();
        vo.setId(id);
        if (company != null) {
            vo.setCompanyId(company.getId());
        }
        vo.setCode(code);
        vo.setInvoiceDate(invoiceDate);
        vo.setDescription(description);
        return vo;
    }
}
