package com.ecep.contract.model;

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

import com.ecep.contract.util.HibernateProxyUtils;
import com.ecep.contract.vo.VendorEntityVo;

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;

/**
 * 供应商的关联详细项，关联信息用于绑定U8系统中的供应商编号
 */
@Getter
@Setter
@Entity
@Table(name = "COMPANY_VENDOR_ENTITY", schema = "supplier_ms", indexes = {

})
@ToString
public class VendorEntity implements IdentityEntity, Serializable, Voable<VendorEntityVo> {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COMPANY_VENDOR_ID")
    @ToString.Exclude
    private Vendor vendor;

    @Column(name = "NAME")
    private String name;

    @Column(name = "ABB_NAME")
    private String abbName;
    /**
     * 用友U8系统中的客户编号,1对多的原因使用编码 |code1|code2|
     */
    @Column(name = "VEN_CODE")
    private String code;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "VEN_CLS")
    @ToString.Exclude
    private VendorCatalog catalog;
    /**
     * 创建人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CREATOR_ID")
    @ToString.Exclude
    private Employee creator;

    /**
     * 修改人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "MODIFIER_ID")
    @ToString.Exclude
    private Employee modifier;

    /**
     * 修改日期
     */
    @Column(name = "MODIFY_DATE")
    private LocalDate modifyDate;
    /**
     * 发展日期
     */
    @Column(name = "DEV_DATE")
    private LocalDate developDate;

    /**
     * 更新日期，有数据更新时的日期
     */
    @Column(name = "UP_DATE")
    private LocalDate updatedDate;
    /**
     * 抓取时间戳，用于控制抓取频率
     */
    @Column(name = "FETCHED_TIME")
    private LocalDateTime fetchedTime;

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

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

    @Override
    public VendorEntityVo toVo() {
        VendorEntityVo vo = new VendorEntityVo();
        vo.setId(id);
        if (vendor != null) {
            vo.setVendorId(vendor.getId());
        }
        vo.setName(name);
        vo.setAbbName(abbName);
        vo.setCode(code);
        if (catalog != null) {
            vo.setCatalogId(catalog.getId());
        }
        if (creator != null) {
            vo.setCreatorId(creator.getId());
        }
        if (modifier != null) {
            vo.setModifierId(modifier.getId());
        }
        vo.setModifyDate(modifyDate);
        vo.setDevelopDate(developDate);
        vo.setUpdatedDate(updatedDate);
        vo.setFetchedTime(fetchedTime);
        return vo;
    }
}
