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 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;

/**
 * 合同项目, CM_Contract_Item_B
 */
@Getter
@Setter
@Entity
@Table(name = "CONTRACT_ITEM")
@ToString
public class ContractItem implements IdentityEntity, ContractBasedEntity, BasedEntity, Serializable {
    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;

    /**
     * 相关数据Id
     */
    @Column(name = "REF_ID")
    private Integer refId;
    /**
     * 项目编号
     */
    @Column(name = "ITEM_CODE")
    private String itemCode;
    /**
     * 项目
     */
    @Column(name = "TITLE")
    private String title;

    /**
     * 规格
     */
    @Column(name = "SPECIFICATION")
    private String specification;

    /**
     * 单位
     */
    @Column(name = "UNIT")
    private String unit;

    /**
     * 存货
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "INVENTORY_ID")
    @ToString.Exclude
    private Inventory inventory;

    /**
     * 不含税单价
     */
    @Column(name = "EX_TAX_PRICE")
    private double exclusiveTaxPrice;

    /**
     * 税率
     */
    @Column(name = "TAX_RATE")
    private double taxRate;

    /**
     * 含税单价
     */
    @Column(name = "TAX_PRICE")
    private double taxPrice;

    /**
     * 数量
     */
    @Column(name = "QUANTITY")
    private double quantity;

    /**
     * 创建日期
     */
    @Column(name = "CREATE_TIME")
    private LocalDateTime createDate;

    /**
     * 更新日期
     */
    @Column(name = "UPDATE_TIME")
    private LocalDateTime updateDate;

    @Column(name = "START_DATE")
    private LocalDate startDate;

    @Column(name = "END_DATE")
    private LocalDate endDate;

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

    /**
     * 更新人
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "UPDATE_BY")
    @ToString.Exclude
    private Employee updater;

    /**
     * 备注
     */
    @Column(name = "REMARK")
    private String remark;

    @Override
    public String toPrettyString() {
        return getTitle() + " " + getSpecification() + " x " + getQuantity();
    }

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

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