-- MySQL 8.0+ 或 MariaDB 10.3+
-- 需求单主表
CREATE TABLE pm_demand_order (
                                 id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '自增主键ID',
                                 order_uuid VARCHAR(64) NOT NULL COMMENT '单据唯一ID（业务主键）',
                                 creator VARCHAR(50) NOT NULL COMMENT '操作人',
                                 create_time DATETIME NOT NULL COMMENT '需求日期',
                                 priority INT NOT NULL COMMENT '优先级序号',
                                 status INT DEFAULT 0 COMMENT '0：生效；1：失效',
                                 target_house_code VARCHAR(32) NOT NULL COMMENT '目标仓别',
                                 remark VARCHAR(500) COMMENT '备注',
                                 create_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                                 UNIQUE KEY uk_order_uuid (order_uuid)
) AUTO_INCREMENT=10000 COMMENT='需求单主表';

-- 需求单明细表
CREATE TABLE pmd_demand_dtl (
                                id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '自增主键ID',
                                detail_uuid VARCHAR(64) NOT NULL COMMENT '明细唯一ID（业务主键）',
                                order_id BIGINT NOT NULL COMMENT '关联主表自增ID',
                                order_uuid VARCHAR(64) NOT NULL COMMENT '关联主表业务ID',
                                work_order VARCHAR(64) NOT NULL COMMENT '工单编号',
                                sku_code VARCHAR(64) NOT NULL COMMENT '物料编码',
                                sku_name VARCHAR(200) NOT NULL COMMENT '物料名称',
                                qty DECIMAL(18,4) NOT NULL COMMENT '数量',
                                unit VARCHAR(20) NOT NULL COMMENT '单位',
                                target_area VARCHAR(64) NOT NULL COMMENT '目标库存地点',
                                production_line VARCHAR(64) NOT NULL COMMENT '产线',
                                sn VARCHAR(64) COMMENT '车辆序列号',
                                max_load VARCHAR(50) COMMENT '最大装载量',
                                batch_no VARCHAR(64) COMMENT '批次号',
                                remark VARCHAR(500) COMMENT '备注',
                                create_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                                UNIQUE KEY uk_detail_uuid (detail_uuid),
                                INDEX idx_order_id (order_id),
                                INDEX idx_order_uuid (order_uuid),
                                FOREIGN KEY (order_id) REFERENCES pm_demand_order(id) ON DELETE CASCADE
) AUTO_INCREMENT=10000 COMMENT='需求单明细表';
