package com.ecep.contract.model;

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

import org.hibernate.annotations.ColumnDefault;

import com.ecep.contract.VendorType;
import com.ecep.contract.util.HibernateProxyUtils;
import com.ecep.contract.vo.VendorVo;
import com.fasterxml.jackson.annotation.JsonIgnore;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.persistence.Version;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 供应商
 */
@Getter
@Setter
@Entity
@Table(name = "COMPANY_VENDOR", schema = "supplier_ms")
@ToString
public class Vendor implements IdentityEntity, CompanyBasedEntity, Serializable, Voable<VendorVo> {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "ID", nullable = false)
    private Integer id;

    /**
     * 类型
     */
    @Column(name = "TYPE")
    private VendorType type;

    @ColumnDefault("false")
    @Column(name = "PROTOCOL_PROVIDER", nullable = false)
    private boolean protocolProvider = false;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COMPANY_ID")
    @ToString.Exclude
    private Company company;

    @OneToMany(mappedBy = "vendor", fetch = FetchType.LAZY)
    @JsonIgnore
    @ToString.Exclude
    private List<VendorEntity> entities;

    @ManyToOne
    @JoinColumn(name = "VEN_CLS")
    private VendorCatalog catalog;
    /**
     * 发展日期
     */
    @Column(name = "DEV_DATE")
    private LocalDate developDate;

    /**
     * 文件夹路径
     */
    @Column(name = "PATH")
    private String path;

    /**
     * 对口联系人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COMPANY_CONTACT_ID")
    @ToString.Exclude
    private CompanyContact contact;

    /**
     * 采购的产品
     */
    @Column(name = "purchase")
    private String purchase;

    /**
     * 说明
     */
    @Column(name = "DESCRIPTION", columnDefinition = "TEXT")
    private String description;
    /**
     * 记录创建日期
     */
    @Column(name = "CREATED")
    private LocalDateTime created;

    @Version
    @ColumnDefault("0")
    @Column(name = "VERSION", nullable = false)
    private int version;

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

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

    @Override
    public VendorVo toVo() {
        VendorVo vo = new VendorVo();
        vo.setId(id);
        vo.setType(type);
        vo.setProtocolProvider(protocolProvider);
        if (company != null) {
            vo.setCompanyId(company.getId());
        }
        if (catalog != null) {
            vo.setCatalogId(catalog.getId());
        }
        vo.setDevelopDate(developDate);
        vo.setPath(path);
        if (contact != null) {
            vo.setContactId(contact.getId());
        }
        vo.setPurchase(purchase);
        vo.setDescription(description);
        vo.setCreated(created);
        vo.setVersion(version);
        return vo;
    }
}