Files
wms-saas/docs/database/04-inventory/wms_inventory.sql
T
2026-09-30 11:11:54 +08:00

33 lines
2.6 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 表:wms_inventory
-- 用途:库存余额。某个货主、仓库、库位、SKU、批次上的数量结果。
-- 约定:InnoDB / utf8mb4 / 无外键,表间按业务编码关联。详见 docs/database/00-conventions.md
-- 说明:余额是结果。任何数量变化必须与 wms_stock_log 处于同一事务。扣减使用带数量条件的原子 UPDATE(affected rows = 0 即库存不足),禁止先 SELECT 再在应用里计算后 UPDATE。多行更新按 id 升序加锁。库位上的待检、待上架库存放在收货或质检库位,靠库位策略排除出可分配范围,不另设数量桶。
CREATE TABLE `wms_inventory` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '内部主键',
`tenant_code` VARCHAR(32) NOT NULL COMMENT '租户编码',
`warehouse_code` VARCHAR(32) NOT NULL COMMENT '仓库编码',
`customer_code` VARCHAR(32) NOT NULL COMMENT '货主编码',
`location_code` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '库位编码;在途库存使用空字符串,表示仓库级',
`sku_code` VARCHAR(64) NOT NULL COMMENT 'SKU 编码',
`batch_no` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '批次号,不管批次时为空字符串',
`available_qty` DECIMAL(16,4) NOT NULL DEFAULT 0.0000 COMMENT '可用数量',
`reserved_qty` DECIMAL(16,4) NOT NULL DEFAULT 0.0000 COMMENT '预占数量,已分配给出库尚未拣货',
`picked_qty` DECIMAL(16,4) NOT NULL DEFAULT 0.0000 COMMENT '已拣数量,已拣下尚未出库',
`hold_qty` DECIMAL(16,4) NOT NULL DEFAULT 0.0000 COMMENT '冻结数量',
`damaged_qty` DECIMAL(16,4) NOT NULL DEFAULT 0.0000 COMMENT '残损数量',
`in_transit_qty` DECIMAL(16,4) NOT NULL DEFAULT 0.0000 COMMENT '在途数量,只出现在库位编码为空的行',
`version` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '乐观锁版本',
`created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间',
`updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_inventory_bucket` (`tenant_code`, `warehouse_code`, `customer_code`, `location_code`, `sku_code`, `batch_no`),
KEY `idx_tenant_wh_sku` (`tenant_code`, `warehouse_code`, `sku_code`),
KEY `idx_tenant_sku` (`tenant_code`, `sku_code`),
KEY `idx_tenant_wh_location` (`tenant_code`, `warehouse_code`, `location_code`),
CONSTRAINT `chk_inventory_qty_nonneg` CHECK (
`available_qty` >= 0 AND `reserved_qty` >= 0 AND `picked_qty` >= 0
AND `hold_qty` >= 0 AND `damaged_qty` >= 0 AND `in_transit_qty` >= 0
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='库存余额';