package com.ecep.contract.model;

import com.ecep.contract.util.HibernateProxyUtils;
import com.ecep.contract.vo.ExtendVendorInfoVo;
import com.ecep.contract.model.Voable;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

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

@Getter
@Setter
@Entity
@Table(name = "CONTRACT_EXTEND_VENDOR_INFO", schema = "supplier_ms")
@ToString
public class ExtendVendorInfo implements IdentityEntity, Serializable, Voable<ExtendVendorInfoVo> {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CONTRACT_ID")
    @ToString.Exclude
    private Contract contract;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "VENDOR_GROUP_ID")
    @ToString.Exclude
    private VendorGroup group;

    /**
     * 合同序号
     */
    @Column(name = "CODE_SN")
    private int codeSequenceNumber;

    /**
     * 是否已分配指定供应商
     */
    @Column(name = "ASSIGNED_PROVIDER")
    private boolean assignedProvider;

    /**
     * 是否是预采购
     */
    @Column(name = "PRE_PURCHASE")
    private boolean prePurchase;

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

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

    @Override
    public ExtendVendorInfoVo toVo() {
        ExtendVendorInfoVo vo = new ExtendVendorInfoVo();
        vo.setId(id);
        if (contract != null) {
            vo.setContractId(contract.getId());
        }
        if (group != null) {
            vo.setGroupId(group.getId());
        }
        vo.setCodeSequenceNumber(codeSequenceNumber);
        vo.setAssignedProvider(assignedProvider);
        vo.setPrePurchase(prePurchase);
        return vo;
    }
}
