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.AttributeOverride;
import jakarta.persistence.AttributeOverrides;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
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;

/**
 * 存货物品清单
 */
@Getter
@Setter
@Entity
@Table(name = "INVENTORY", schema = "supplier_ms")
public class Inventory implements IdentityEntity, 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, optional = true)
    @JoinColumn(name = "CATALOG_ID")
    @ToString.Exclude
    private InventoryCatalog catalog;

    @Column(name = "CODE")
    private String code;

    @Column(name = "SPEC")
    private String specification;

    @Column(name = "SPEC_LOCK", nullable = false)
    private boolean specificationLock;

    @Column(name = "NAME")
    private String name;

    @Column(name = "NAME_LOCK")
    private boolean nameLock;

    /**
     * 采购价格
     */
    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "taxRate", column = @Column(name = "PURCHASE_TAX")),
            @AttributeOverride(name = "preTaxPrice", column = @Column(name = "PURCHASE_PRICE")),
            @AttributeOverride(name = "postTaxPrice", column = @Column(name = "PURCHASE_TAX_PRICE"))
    })
    private Price purchasePrice = new Price();

    /**
     * 销售价格
     */
    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "taxRate", column = @Column(name = "SALE_TAX")),
            @AttributeOverride(name = "preTaxPrice", column = @Column(name = "SALE_PRICE")),
            @AttributeOverride(name = "postTaxPrice", column = @Column(name = "SALE_TAX_PRICE"))
    })
    private Price salePrice = new Price();

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

    /**
     * 产品重量
     */
    @Column(name = "WEIGHT")
    private double weight;
    /**
     * 包装重量
     */
    @Column(name = "PACK_WEIGHT")
    private double packagedWeight;

    /**
     * 重量单位
     */
    @Column(name = "WEIGHT_UNIT")
    private String weightUnit = "kg";
    /**
     * 体积单位
     */
    @Column(name = "VOLUME_UNIT")
    private String volumeUnit = "m³";
    /**
     * 尺寸单位
     */
    @Column(name = "VOLUME_SIZE_UNIT")
    private String sizeUnit = "mm";

    /**
     * 体积尺寸(不含包装)
     */
    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "volume", column = @Column(name = "VOLUME")),
            @AttributeOverride(name = "length", column = @Column(name = "VOLUME_SIZE_LENGTH")),
            @AttributeOverride(name = "width", column = @Column(name = "VOLUME_SIZE_WIDTH")),
            @AttributeOverride(name = "height", column = @Column(name = "VOLUME_SIZE_HEIGHT"))
    })
    private VolumeSize volumeSize = new VolumeSize();
    /**
     * 包装体积尺寸
     */
    @Embedded
    @AttributeOverrides({
            @AttributeOverride(name = "volume", column = @Column(name = "PACK_VOLUME")),
            @AttributeOverride(name = "length", column = @Column(name = "PACK_VOLUME_SIZE_LENGTH")),
            @AttributeOverride(name = "width", column = @Column(name = "PACK_VOLUME_SIZE_WIDTH")),
            @AttributeOverride(name = "height", column = @Column(name = "PACK_VOLUME_SIZE_HEIGHT"))
    })
    private VolumeSize packagedVolumeSize = new VolumeSize();

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

    @Column(name = "CREATE_DATE")
    private LocalDate createTime;

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

    @Column(name = "UPDATE_DATE")
    private LocalDateTime updateDate;

    @Column(name = "DESCRIPTION")
    private String description;

    @Override
    public String toPrettyString() {
        return getName() + " " + getSpecification();
    }

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

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