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

import java.time.LocalDate;
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 jakarta.persistence.*;
import org.hibernate.annotations.ColumnDefault;

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

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@Entity
@Table(name = "CLOUD_YU", schema = "supplier_ms")
@ToString
public class CloudYu implements IdentityEntity, Voable<CloudYuVo> {
    /**
     * 主键
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", nullable = false)
    private Integer id;

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

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

    @ColumnDefault("''")
    @Column(name = "EX_MSG")
    private String exceptionMessage;

    /**
     * 供应商信息更新日期
     */
    @Column(name = "VEN_UP_DATE")
    private LocalDate vendorUpdateDate;

    /**
     * 客户信息更新日期
     */
    @Column(name = "CUS_UP_DATE")
    private LocalDate customerUpdateDate;

    /**
     * 数据更新日期
     */
    @Column(name = "CLOUD_LATEST")
    private LocalDateTime cloudLatest;

    /**
     * 版本号
     */
    @Version
    @Column(name = "VERSION", nullable = false)
    private int version = 0;

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

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

    @Override
    public CloudYuVo toVo() {
        CloudYuVo vo = new CloudYuVo();
        vo.setId(id);
        vo.setLatestUpdate(latestUpdate);
        if (company != null) {
            vo.setCompanyId(company.getId());
        }
        vo.setExceptionMessage(exceptionMessage);
        vo.setVendorUpdateDate(vendorUpdateDate);
        vo.setCustomerUpdateDate(customerUpdateDate);
        vo.setCloudLatest(cloudLatest);
        vo.setVersion(version);
        // active字段默认为false，在CloudYuVo类中已经设置
        return vo;
    }
}
