package com.ecep.contract.cloud;

import java.time.Instant;
import java.util.Objects;

import org.hibernate.annotations.ColumnDefault;
import org.hibernate.proxy.HibernateProxy;

import com.ecep.contract.CloudType;
import com.ecep.contract.model.Company;

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 jakarta.persistence.Version;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 记录同步来源
 */
@Getter
@Setter
@Entity
//@org.springframework.data.relational.core.mapping.Table(name = "CLOUD_INFO")
@Table(name = "CLOUD_INFO", schema = "supplier_ms")
@ToString
public class CloudInfo {
    /**
     * 主键
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;

    /**
     * 记录源类型
     */
    @Column(name = "CLOUD_TYPE", length = 50)
    private CloudType type;

    /**
     *
     */
    @Column(name = "CLOUD_ID")
    private String cloudId;

    /**
     * 本地更新时间戳，控制更新频率和重复更新
     */
    @Column(name = "LATEST_UPDATE")
    private Instant latestUpdate;

    /**
     * 关联的公司
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COMPANY_ID")
    @ToString.Exclude
    private Company company;

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

    @Override
    public final boolean equals(Object o) {
        if (this == o) return true;
        if (o == null) return false;
        Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
        Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
        if (thisEffectiveClass != oEffectiveClass) return false;
        CloudInfo cloudInfo = (CloudInfo) o;
        return getId() != null && Objects.equals(getId(), cloudInfo.getId());
    }

    @Override
    public final int hashCode() {
        return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
    }
}