package com.erp.tenant.entity.stock;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import com.erp.tenant.entity.common.BaseEntity;

import java.time.LocalDateTime;

/**
 * 库存实体
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("stocks")
public class Stock extends BaseEntity {

    @TableId(type = IdType.AUTO)
    private Long id;

    // ==================== 仓库和SKU ====================
    @TableField("warehouse_id")
    private Long warehouseId;

    @TableField("warehouse_name")
    private String warehouseName;

    @TableField("sku_code")
    private String skuCode;

    @TableField("sku_name")
    private String skuName;

    // ==================== 库存数量 ====================
    /** 库存数量 */
    private Integer quantity;

    /** 已锁定数量 */
    @TableField("locked_quantity")
    private Integer lockedQuantity;

    /** 预留数量 */
    @TableField("reserved_quantity")
    private Integer reservedQuantity;

    /** 残次品数量 */
    @TableField("defective_quantity")
    private Integer defectiveQuantity;

    // ==================== 可用库存计算属性 ====================
    @TableField(exist = false)
    private Integer availableQuantity;

    // ==================== 预警 ====================
    /** 预警阈值 */
    @TableField("warning_threshold")
    private Integer warningThreshold;

    // ==================== 时间 ====================
    @TableField("last_inbound_at")
    private LocalDateTime lastInboundAt;

    @TableField("last_outbound_at")
    private LocalDateTime lastOutboundAt;

    @TableField("updated_at")
    private LocalDateTime updatedAt;

    // ==================== 辅助方法 ====================
    public Integer calcAvailable() {
        int qty = quantity != null ? quantity : 0;
        int locked = lockedQuantity != null ? lockedQuantity : 0;
        int reserved = reservedQuantity != null ? reservedQuantity : 0;
        int defective = defectiveQuantity != null ? defectiveQuantity : 0;
        return qty - locked - reserved - defective;
    }

    public boolean isLowStock() {
        return calcAvailable() <= (warningThreshold != null ? warningThreshold : 10);
    }

    public boolean isOutOfStock() {
        return calcAvailable() <= 0;
    }
}
