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

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

import com.ecep.contract.ds.other.model.Invoice;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.IdentityEntity;
import com.ecep.contract.model.NamedEntity;
import com.ecep.contract.model.Voable;
import com.ecep.contract.vo.ContractInvoiceVo;

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 org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 合同发票实体类
 */
@Getter
@Setter
@Entity
@Table(name = "CONTRACT_INVOICE", schema = "supplier_ms")
@ToString
public class ContractInvoice implements IdentityEntity, NamedEntity, Voable<ContractInvoiceVo> {

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

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

    @Column(name = "NAME", length = 200)
    private String name;

    /**
     * 关联的合同对象
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CONTRACT_ID", foreignKey = @jakarta.persistence.ForeignKey(name = "FK_CONTRACT_INVOICE_CONTRACT"))
    @OnDelete(action = OnDeleteAction.CASCADE)
    @ToString.Exclude
    private Contract contract;

    /**
     * 关联的发票对象
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "INVOICE_ID", foreignKey = @jakarta.persistence.ForeignKey(name = "FK_CONTRACT_INVOICE_INVOICE"))
    @OnDelete(action = OnDeleteAction.SET_NULL)
    @ToString.Exclude
    private Invoice invoice;

    /**
     * 发票金额
     */
    @Column(name = "AMOUNT")
    private Double amount;

    /**
     * 创建人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SETUP_PERSON_ID", foreignKey = @jakarta.persistence.ForeignKey(name = "FK_CONTRACT_INVOICE_SETUP_PERSON"))
    @OnDelete(action = OnDeleteAction.SET_NULL)
    @ToString.Exclude
    private Employee setupPerson;
    /**
     * 提交日期
     */
    @Column(name = "SETUP_DATE")
    private LocalDate setupDate;

    /**
     * 更新人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "UPDATE_PERSON_ID", foreignKey = @jakarta.persistence.ForeignKey(name = "FK_CONTRACT_INVOICE_UPDATE_PERSON"))
    @OnDelete(action = OnDeleteAction.SET_NULL)
    @ToString.Exclude
    private Employee updatePerson;
    /**
     * 更新日期
     */
    @Column(name = "UPDATE_DATE")
    private LocalDate updateDate;

    /**
     * 备注
     */
    @Column(name = "REMARK", length = 500)
    private String remark;

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        ContractInvoice that = (ContractInvoice) o;
        return Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public ContractInvoiceVo toVo() {
        ContractInvoiceVo vo = new ContractInvoiceVo();
        vo.setId(id);
        vo.setCode(code);
        vo.setName(name);
        vo.setContractId(contract != null ? contract.getId() : null);
        vo.setInvoiceId(invoice != null ? invoice.getId() : null);
        vo.setAmount(amount);
        vo.setRemark(remark);
        vo.setSetupPersonId(setupPerson != null ? setupPerson.getId() : null);
        vo.setUpdatePersonId(updatePerson != null ? updatePerson.getId() : null);
        vo.setSetupDate(setupDate);
        vo.setUpdateDate(updateDate);

        return vo;
    }
}