package com.ecep.contract.model;

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

import com.ecep.contract.model.Voable;
import com.ecep.contract.vo.InventoryVo;
import com.ecep.contract.util.HibernateProxyUtils;

import com.ecep.contract.vo.VolumeSizeVo;
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, Voable<InventoryVo> {
    @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);
    }

    @Override
    public InventoryVo toVo() {
        InventoryVo vo = new InventoryVo();
        
        // 基本属性映射
        vo.setId(this.getId());
        vo.setName(this.getName());
        vo.setCode(this.getCode());
        vo.setSpecification(this.getSpecification());
        vo.setSpecificationLock(this.isSpecificationLock());
        vo.setNameLock(this.isNameLock());
        vo.setUnit(this.getUnit());
        vo.setDescription(this.getDescription());
        
        // 关联对象ID映射
        if (this.getCatalog() != null) {
            vo.setCatalogId(this.getCatalog().getId());
        }
        if (this.getCreator() != null) {
            vo.setCreatorId(this.getCreator().getId());
        }
        if (this.getUpdater() != null) {
            vo.setUpdaterId(this.getUpdater().getId());
        }
        
        // 时间属性映射
        vo.setCreateTime(this.getCreateTime());
        vo.setUpdateDate(this.getUpdateDate());
        
        // 重量属性映射
        vo.setWeight(this.getWeight());
        vo.setPackagedWeight(this.getPackagedWeight());
        vo.setWeightUnit(this.getWeightUnit());
        vo.setVolumeUnit(this.getVolumeUnit());
        vo.setSizeUnit(this.getSizeUnit());
        
        // 价格属性映射
        Price purchasePrice = new Price();
        purchasePrice.setTaxRate(this.getPurchasePrice().getTaxRate());
        purchasePrice.setPreTaxPrice(this.getPurchasePrice().getPreTaxPrice());
        purchasePrice.setPostTaxPrice(this.getPurchasePrice().getPostTaxPrice());
        vo.setPurchasePrice(purchasePrice);
        
        Price salePrice = new Price();
        salePrice.setTaxRate(this.getSalePrice().getTaxRate());
        salePrice.setPreTaxPrice(this.getSalePrice().getPreTaxPrice());
        salePrice.setPostTaxPrice(this.getSalePrice().getPostTaxPrice());
        vo.setSalePrice(salePrice);
        
        // 体积尺寸属性映射
        VolumeSizeVo volumeSize = new VolumeSizeVo();
        volumeSize.setVolume(this.getVolumeSize().getVolume());
        volumeSize.setLength(this.getVolumeSize().getLength());
        volumeSize.setWidth(this.getVolumeSize().getWidth());
        volumeSize.setHeight(this.getVolumeSize().getHeight());
        vo.setVolumeSize(volumeSize);

        VolumeSizeVo packagedVolumeSize = new VolumeSizeVo();
        packagedVolumeSize.setVolume(this.getPackagedVolumeSize().getVolume());
        packagedVolumeSize.setLength(this.getPackagedVolumeSize().getLength());
        packagedVolumeSize.setWidth(this.getPackagedVolumeSize().getWidth());
        packagedVolumeSize.setHeight(this.getPackagedVolumeSize().getHeight());
        vo.setPackagedVolumeSize(packagedVolumeSize);
        
        // 设置默认状态
        vo.setActive(false);
        
        return vo;
    }
}
