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

import java.time.LocalDateTime;
import java.util.Objects;

import com.ecep.contract.ds.company.model.Company;
import com.ecep.contract.model.IdentityEntity;
import com.ecep.contract.model.Voable;
import org.hibernate.annotations.ColumnDefault;

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

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
@Table(name = "CLOUD_TYC", schema = "supplier_ms")
public class CloudTyc implements IdentityEntity, Voable<CloudTycVo> {
    /**
     * 主键
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;

    /**
     * 评分
     */
    @ColumnDefault("0")
    @Column(name = "SCORE", nullable = false)
    private Integer score;

    @Column(name = "CLOUD_LATEST")
    private LocalDateTime cloudLatest;

    /**
     * 平台编号
     */
    @Column(name = "CLOUD_ID")
    private String cloudId;

    /**
     * 本地更新时间戳，控制更新频率和重复更新
     */
    @Column(name = "LATEST_UPDATE")
    private LocalDateTime 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 object) {
        if (this == object)
            return true;
        if (object == null)
            return false;
        if (HibernateProxyUtils.isNotEffectiveClassEquals(object, this)) {
            return false;
        }
        CloudTyc cloudInfo = (CloudTyc) object;
        return getId() != null && Objects.equals(getId(), cloudInfo.getId());
    }

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

    @Override
    public CloudTycVo toVo() {
        CloudTycVo vo = new CloudTycVo();
        vo.setId(id);
        vo.setScore(score);
        vo.setCloudLatest(cloudLatest);
        vo.setCloudId(cloudId);
        vo.setLatestUpdate(latestUpdate);
        if (company != null) {
            vo.setCompanyId(company.getId());
        }
        vo.setVersion(version);
        // active字段默认为false，在CloudTycVo类中已经设置
        return vo;
    }

}