-- 1. 品级信息表（存储产品各工序的品级定义信息）
CREATE TABLE qc_grade_info (
    grade_id BIGINT IDENTITY(1,1) PRIMARY KEY, -- 品级主键
    tenant_id VARCHAR(32) NOT NULL, -- 租户号
    grade_code VARCHAR(30) NOT NULL, -- 品级代号
    grade_name VARCHAR(255) NOT NULL, -- 品级名称
    operation_code VARCHAR(50), -- 工序编码
    operation_name VARCHAR(255), -- 工序名称（关联基础数据工序信息表）
    workshop VARCHAR(50), -- 车间/工程（关联车间信息表）
    factory_name VARCHAR(255), -- 工厂名称（关联工厂信息表）
    sort INT, -- 品级排序
    status CHAR(1), -- 状态（正常/停用）
    create_dept VARCHAR(255), -- 创建部门
    create_by VARCHAR(32), -- 创建人
    create_time DATETIME, -- 创建时间
    update_by VARCHAR(32), -- 修改人
    update_time DATETIME, -- 修改时间
    remark VARCHAR(255), -- 备注
    attr1 VARCHAR(255), -- 预留字段1
    attr2 VARCHAR(255), -- 预留字段2
    attr3 VARCHAR(255) -- 预留字段3
);
-- 添加表备注
EXEC sp_addextendedproperty 
    @name = N'MS_Description', 
    @value = N'存储产品各工序的品级定义信息，如合格、不良品、废品等', 
    @level0type = N'SCHEMA', @level0name = N'dbo', 
    @level1type = N'TABLE', @level1name = N'qc_grade_info';

-- 为品级信息表字段添加中文备注
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'品级主键' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'grade_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'租户号' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'tenant_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'品级代号' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'grade_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'品级名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'grade_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'工序编码' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'operation_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'工序名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'operation_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'车间/工程' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'workshop';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'工厂名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'factory_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'品级排序' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'sort';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'状态（正常/停用）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'status';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建部门' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'create_dept';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建人' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'create_by';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'create_time';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'修改人' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'update_by';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'修改时间' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'update_time';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'备注' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'remark';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'预留字段1' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'attr1';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'预留字段2' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'attr2';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'预留字段3' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_grade_info',@level2type=N'COLUMN',@level2name=N'attr3';


-- 2. 病疵信息表（存储各类病疵的详细信息，与质检项目绑定）
CREATE TABLE qc_defect_detail (
    defect_detail_id BIGINT IDENTITY(1,1) PRIMARY KEY, -- 病疵主键
    tenant_id VARCHAR(32), -- 租户号
    defect_code INT NOT NULL, -- 病疵代号
    defect_name VARCHAR(255) NOT NULL, -- 病疵名称
    defect_type VARCHAR(255), -- 病疵类型
    operation_code VARCHAR(255), -- 工序编码
    operation_name VARCHAR(255), -- 工序名称
    workshop VARCHAR(255), -- 车间/工程
    factory_name VARCHAR(255), -- 工厂名称
    sort VARCHAR(255), -- 病疵排序
    status VARCHAR(1) NOT NULL, -- 状态（0-正常，1-停用）
    is_flag VARCHAR(1) NOT NULL DEFAULT '0', -- 是否删除（1-是，0-否）
    create_dept VARCHAR(255), -- 创建部门
    create_by VARCHAR(32), -- 创建人
    create_time DATETIME, -- 创建时间
    update_by VARCHAR(32), -- 修改人
    update_time DATETIME, -- 修改时间
    remark VARCHAR(255) -- 备注
);
-- 添加表备注
EXEC sp_addextendedproperty 
    @name = N'MS_Description', 
    @value = N'存储产品质检中各类病疵的详细信息，包括病疵代号、名称、关联工序等，用于质检时记录问题明细', 
    @level0type = N'SCHEMA', @level0name = N'dbo', 
    @level1type = N'TABLE', @level1name = N'qc_defect_detail';

-- 为病疵信息表字段添加中文备注
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'病疵主键' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'defect_detail_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'租户号' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'tenant_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'病疵代号' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'defect_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'病疵名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'defect_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'病疵类型' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'defect_type';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'工序编码' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'operation_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'工序名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'operation_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'车间/工程' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'workshop';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'工厂名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'factory_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'病疵排序' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'sort';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'状态（0-正常，1-停用）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'status';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'是否删除（1-是，0-否）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'is_flag';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建部门' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'create_dept';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建人' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'create_by';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'create_time';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'修改人' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'update_by';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'修改时间' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'update_time';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'备注' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_defect_detail',@level2type=N'COLUMN',@level2name=N'remark';


-- 3. 检测类型表（定义质检的类型，如首检、过程检等）
CREATE TABLE qc_inspection_type (
    type_id BIGINT IDENTITY(1,1) PRIMARY KEY, -- 检测类型主键
    type_code VARCHAR(30) NOT NULL, -- 检测类型编码（自动生成）
    type_name VARCHAR(255) NOT NULL, -- 检测类型名称
    inspection_type VARCHAR(50) NOT NULL, -- 检测单类型（产品首检测/产品检测）
    create_method VARCHAR(20), -- 创建方式（自动创建/手动创建）
    create_time_point VARCHAR(20), -- 创建时间点（完工后/报工后）
    create_by VARCHAR(32), -- 创建人
    create_time DATETIME, -- 创建时间
    update_by VARCHAR(32), -- 修改人
    update_time DATETIME -- 修改时间
);
-- 添加表备注
EXEC sp_addextendedproperty 
    @name = N'MS_Description', 
    @value = N'定义质检的类型信息，包括检测类型名称、编码、检测单类型及任务创建方式，用于关联检测模板和触发质检任务', 
    @level0type = N'SCHEMA', @level0name = N'dbo', 
    @level1type = N'TABLE', @level1name = N'qc_inspection_type';

-- 为检测类型表字段添加中文备注
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测类型主键' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_type',@level2type=N'COLUMN',@level2name=N'type_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测类型编码（自动生成）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_type',@level2type=N'COLUMN',@level2name=N'type_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测类型名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_type',@level2type=N'COLUMN',@level2name=N'type_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测单类型（产品首检测/产品检测）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_type',@level2type=N'COLUMN',@level2name=N'inspection_type';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建方式（自动创建/手动创建）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_type',@level2type=N'COLUMN',@level2name=N'create_method';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间点（完工后/报工后）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_type',@level2type=N'COLUMN',@level2name=N'create_time_point';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建人' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_type',@level2type=N'COLUMN',@level2name=N'create_by';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_type',@level2type=N'COLUMN',@level2name=N'create_time';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'修改人' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_type',@level2type=N'COLUMN',@level2name=N'update_by';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'修改时间' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_type',@level2type=N'COLUMN',@level2name=N'update_time';


-- 4. 检测项类别表（定义检测项的分类，如外观质检、尺寸质检）
CREATE TABLE qc_inspection_item_category (
    category_id BIGINT IDENTITY(1,1) PRIMARY KEY, -- 检测项类别主键
    category_code VARCHAR(30) NOT NULL, -- 检测项类别编码（自动生成）
    category_name VARCHAR(255) NOT NULL, -- 检测项类别名称
    inspection_type VARCHAR(50) NOT NULL, -- 检测单类型（关联检测类型表）
    description VARCHAR(500), -- 描述
    create_by VARCHAR(32), -- 创建人
    create_time DATETIME, -- 创建时间
    update_by VARCHAR(32), -- 修改人
    update_time DATETIME, -- 修改时间
    FOREIGN KEY (inspection_type) REFERENCES qc_inspection_type(inspection_type)
);
-- 添加表备注
EXEC sp_addextendedproperty 
    @name = N'MS_Description', 
    @value = N'定义检测项的分类信息，如外观质检、尺寸质检等，关联检测类型表的检测单类型，用于组织检测项', 
    @level0type = N'SCHEMA', @level0name = N'dbo', 
    @level1type = N'TABLE', @level1name = N'qc_inspection_item_category';

-- 为检测项类别表字段添加中文备注
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测项类别主键' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item_category',@level2type=N'COLUMN',@level2name=N'category_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测项类别编码（自动生成）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item_category',@level2type=N'COLUMN',@level2name=N'category_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测项类别名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item_category',@level2type=N'COLUMN',@level2name=N'category_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测单类型（关联检测类型表）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item_category',@level2type=N'COLUMN',@level2name=N'inspection_type';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'描述' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item_category',@level2type=N'COLUMN',@level2name=N'description';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建人' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item_category',@level2type=N'COLUMN',@level2name=N'create_by';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item_category',@level2type=N'COLUMN',@level2name=N'create_time';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'修改人' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item_category',@level2type=N'COLUMN',@level2name=N'update_by';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'修改时间' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item_category',@level2type=N'COLUMN',@level2name=N'update_time';


-- 5. 检测项定义表（定义具体的检测项目信息，如胎面外观、尺寸等）
CREATE TABLE qc_inspection_item (
    item_id BIGINT IDENTITY(1,1) PRIMARY KEY, -- 检测项主键
    item_code VARCHAR(30) NOT NULL, -- 检测项编码
    item_name VARCHAR(255) NOT NULL, -- 检测项名称
    inspection_position VARCHAR(255), -- 检测位置
    category_id BIGINT, -- 检测项类别（关联检测项类别表）
    inspection_type VARCHAR(50), -- 检测单类型
    method VARCHAR(50), -- 检测方法（目视/千分尺）
    detect_type VARCHAR(20) NOT NULL, -- 检测方式（定性/定量）
    control_type VARCHAR(20), -- 控制类型（手动输入）
    standard_value DECIMAL(18,2), -- 标准值
    upper_limit DECIMAL(18,2), -- 控制上限
    lower_limit DECIMAL(18,2), -- 控制下限
    spec_name VARCHAR(50), -- 规格名称
    spec_upper VARCHAR(50), -- 规格上线
    spec_lower VARCHAR(50), -- 规格下线
    description VARCHAR(500), -- 检测项说明
    option_code VARCHAR(30), -- 检测选项编码
    option_name VARCHAR(50), -- 检测选项名称
    is_default VARCHAR(1), -- 是否默认选项（是/否）
    FOREIGN KEY (category_id) REFERENCES qc_inspection_item_category(category_id)
);
-- 添加表备注
EXEC sp_addextendedproperty 
    @name = N'MS_Description', 
    @value = N'定义具体的检测项目信息，包括检测项名称、编码、方法、上下限等，关联检测项类别，用于检测模板和质检结果记录', 
    @level0type = N'SCHEMA', @level0name = N'dbo', 
    @level1type = N'TABLE', @level1name = N'qc_inspection_item';

-- 为检测项定义表字段添加中文备注
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测项主键' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'item_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测项编码' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'item_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测项名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'item_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测位置' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'inspection_position';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测项类别（关联检测项类别表）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'category_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测单类型' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'inspection_type';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测方法（目视/千分尺等）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'method';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测方式（定性/定量）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'detect_type';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'控制类型（手动输入等）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'control_type';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'标准值' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'standard_value';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'控制上限' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'upper_limit';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'控制下限' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'lower_limit';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'规格名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'spec_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'规格上线' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'spec_upper';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'规格下线' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'spec_lower';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测项说明' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'description';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测选项编码' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'option_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测选项名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'option_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'是否默认选项（是/否）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_item',@level2type=N'COLUMN',@level2name=N'is_default';


-- 6. 检测模板主表（定义质检模板的基本信息，如关联物料、检测类型等）
CREATE TABLE qc_inspection_template (
    template_id BIGINT IDENTITY(1,1) PRIMARY KEY, -- 模板主键
    template_code VARCHAR(30) NOT NULL, -- 模板编码
    template_name VARCHAR(255) NOT NULL, -- 模板名称
    material_code VARCHAR(50), -- 物料编码
    material_name VARCHAR(255), -- 物料名称
    type_id BIGINT, -- 关联检测类型表ID
    station_code VARCHAR(50), -- 工位编码
    station_name VARCHAR(255), -- 工位名称
    process_code VARCHAR(50), -- 工序编码
    process_name VARCHAR(255), -- 工序名称
    supplier_code VARCHAR(50), -- 供应商编码
    supplier_name VARCHAR(255), -- 供应商名称
    create_by VARCHAR(32), -- 创建人
    create_time DATETIME, -- 创建时间
    update_by VARCHAR(32), -- 修改人
    update_time DATETIME, -- 修改时间
    description VARCHAR(500), -- 模板说明
    FOREIGN KEY (type_id) REFERENCES qc_inspection_type(type_id)
);
-- 添加表备注
EXEC sp_addextendedproperty 
    @name = N'MS_Description', 
    @value = N'存储质检模板的基本信息，包括模板名称、关联物料、检测类型、工序等，用于生成质检任务的标准模板', 
    @level0type = N'SCHEMA', @level0name = N'dbo', 
    @level1type = N'TABLE', @level1name = N'qc_inspection_template';

-- 为检测模板主表字段添加中文备注
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'模板主键' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'template_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'模板编码' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'template_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'模板名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'template_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'物料编码' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'material_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'物料名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'material_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'关联检测类型表ID' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'type_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'工位编码' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'station_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'工位名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'station_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'工序编码' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'process_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'工序名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'process_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'供应商编码' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'supplier_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'供应商名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'supplier_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建人' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'create_by';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'create_time';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'修改人' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'update_by';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'修改时间' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'update_time';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'模板说明' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_template',@level2type=N'COLUMN',@level2name=N'description';


-- 7. 检测模板子表（存储模板关联的具体检测项信息）
CREATE TABLE qc_template_item (
    template_item_id BIGINT IDENTITY(1,1) PRIMARY KEY, -- 模板子表主键
    template_id BIGINT NOT NULL, -- 关联模板主表ID
    item_id BIGINT NOT NULL, -- 关联检测项定义ID
    item_code VARCHAR(30) NOT NULL, -- 检测项编码
    item_name VARCHAR(255) NOT NULL, -- 检测项名称
    inspection_position VARCHAR(255), -- 检测位置
    category_name VARCHAR(255), -- 检测项目类别
    inspection_type VARCHAR(50), -- 检测类型
    detect_type VARCHAR(20) NOT NULL, -- 检测方式（定性/定量）
    control_type VARCHAR(20), -- 控制类型
    standard_value DECIMAL(18,2), -- 标准值
    upper_limit DECIMAL(18,2), -- 控制上限
    lower_limit DECIMAL(18,2), -- 控制下限
    spec_name VARCHAR(50), -- 规格名称
    spec_upper VARCHAR(50), -- 规格上线
    spec_lower VARCHAR(50), -- 规格下线
    description VARCHAR(500), -- 检测项说明
    FOREIGN KEY (template_id) REFERENCES qc_inspection_template(template_id),
    FOREIGN KEY (item_id) REFERENCES qc_inspection_item(item_id)
);
-- 添加表备注
EXEC sp_addextendedproperty 
    @name = N'MS_Description', 
    @value = N'存储质检模板关联的具体检测项明细，包括检测项名称、上下限、检测方式等，与模板主表和检测项定义表关联', 
    @level0type = N'SCHEMA', @level0name = N'dbo', 
    @level1type = N'TABLE', @level1name = N'qc_template_item';

-- 为检测模板子表字段添加中文备注
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'模板子表主键' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'template_item_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'关联模板主表ID' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'template_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'关联检测项定义ID' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'item_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测项编码' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'item_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测项名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'item_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测位置' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'inspection_position';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测项目类别' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'category_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测类型' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'inspection_type';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测方式（定性/定量）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'detect_type';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'控制类型' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'control_type';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'标准值' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'standard_value';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'控制上限' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'upper_limit';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'控制下限' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'lower_limit';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'规格名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'spec_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'规格上线' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'spec_upper';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'规格下线' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'spec_lower';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测项说明' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_template_item',@level2type=N'COLUMN',@level2name=N'description';


-- 8. 质检主表（存储质检任务的整体信息，如检测单号、物料、合格数等）
CREATE TABLE qc_inspection_main (
    inspection_id BIGINT IDENTITY(1,1) PRIMARY KEY, -- 质检主键
    inspection_no VARCHAR(50) NOT NULL, -- 检测单号/卡号
    material_code VARCHAR(50), -- 物料编码
    material_type VARCHAR(50), -- 物料类型
    material_name VARCHAR(255), -- 物料名称
    process_name VARCHAR(255), -- 工序名称
    station_name VARCHAR(255), -- 工位名称
    inspection_qty INT NOT NULL, -- 质检数量
    qualified_qty INT DEFAULT 0, -- 合格数
    unqualified_qty INT DEFAULT 0, -- 不合格数
    result VARCHAR(10), -- 质检结果（合格/不合格）
    workshop VARCHAR(50), -- 车间
    inspection_category VARCHAR(20), -- 检验类别（首检/过程检）
    status VARCHAR(20) NOT NULL, -- 单据状态（未处理/完成）
    inspector VARCHAR(32), -- 检测人员
    shift VARCHAR(10), -- 班次
    team VARCHAR(50), -- 班组
    inspection_time DATETIME, -- 检验时间
    production_order VARCHAR(50), -- 生产订单号
    batch_no VARCHAR(50), -- 批次号
    barcode VARCHAR(100), -- 条码号
    supplier_name VARCHAR(255) -- 供应商名称
);
-- 添加表备注
EXEC sp_addextendedproperty 
    @name = N'MS_Description', 
    @value = N'存储质检任务的整体信息，包括检测单号、关联物料、质检数量、合格数、不合格数及单据状态等，作为质检结果的主表', 
    @level0type = N'SCHEMA', @level0name = N'dbo', 
    @level1type = N'TABLE', @level1name = N'qc_inspection_main';

-- 为质检主表字段添加中文备注
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'质检主键' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'inspection_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测单号/卡号' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'inspection_no';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'物料编码' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'material_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'物料类型' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'material_type';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'物料名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'material_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'工序名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'process_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'工位名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'station_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'质检数量' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'inspection_qty';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'合格数' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'qualified_qty';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'不合格数' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'unqualified_qty';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'质检结果（合格/不合格）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'result';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'车间' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'workshop';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检验类别（首检/过程检）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'inspection_category';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'单据状态（未处理/完成）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'status';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测人员' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'inspector';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'班次' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'shift';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'班组' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'team';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检验时间' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'inspection_time';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'生产订单号' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'production_order';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'批次号' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'batch_no';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'条码号' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'barcode';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'供应商名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_main',@level2type=N'COLUMN',@level2name=N'supplier_name';


-- 9. 质检结果子表（存储每个检测项的具体质检结果）
CREATE TABLE qc_inspection_result (
    result_id BIGINT IDENTITY(1,1) PRIMARY KEY, -- 结果主键
    inspection_id BIGINT NOT NULL, -- 关联质检主表ID
    item_id BIGINT NOT NULL, -- 关联检测项ID
    detect_result VARCHAR(10), -- 检测结果（合格/不合格）
    detect_value DECIMAL(18,2), -- 定量检测值
    spec_inspection VARCHAR(50), -- 规格质检值
    problem_detail VARCHAR(500), -- 定性不合格明细
    FOREIGN KEY (inspection_id) REFERENCES qc_inspection_main(inspection_id),
    FOREIGN KEY (item_id) REFERENCES qc_inspection_item(item_id)
);
-- 添加表备注
EXEC sp_addextendedproperty 
    @name = N'MS_Description', 
    @value = N'存储每个检测项的具体质检结果，包括检测结果、定量检测值、定性不合格明细等，关联质检主表和检测项定义表', 
    @level0type = N'SCHEMA', @level0name = N'dbo', 
    @level1type = N'TABLE', @level1name = N'qc_inspection_result';

-- 为质检结果子表字段添加中文备注
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'结果主键' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_result',@level2type=N'COLUMN',@level2name=N'result_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'关联质检主表ID' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_result',@level2type=N'COLUMN',@level2name=N'inspection_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'关联检测项ID' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_result',@level2type=N'COLUMN',@level2name=N'item_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测结果（合格/不合格）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_result',@level2type=N'COLUMN',@level2name=N'detect_result';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'定量检测值' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_result',@level2type=N'COLUMN',@level2name=N'detect_value';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'规格质检值' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_result',@level2type=N'COLUMN',@level2name=N'spec_inspection';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'定性不合格明细' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_inspection_result',@level2type=N'COLUMN',@level2name=N'problem_detail';


-- 10. 不合格品待评审表（存储不合格品的待评审信息，用于评审处置）
CREATE TABLE qc_unqualified_review (
    review_id BIGINT IDENTITY(1,1) PRIMARY KEY, -- 评审主键
    unqualified_no VARCHAR(50) NOT NULL, -- 不合格检测单号
    inspection_id BIGINT NOT NULL, -- 关联质检主表ID
    inspection_type VARCHAR(20), -- 检测类型（过程检/来料检）
    work_order VARCHAR(50), -- 派工单号
    process_code VARCHAR(50), -- 工序编码
    process_name VARCHAR(255), -- 工序名称
    batch_no VARCHAR(50), -- 批次号
    material_name VARCHAR(255), -- 物料名称
    material_code VARCHAR(50), -- 物料编码
    inspector_result VARCHAR(10), -- 质检员检测结果
    review_result VARCHAR(20), -- 评审结果（报废/返工/退货等）
    reviewer VARCHAR(32), -- 评审人
    review_time DATETIME, -- 评审时间
    FOREIGN KEY (inspection_id) REFERENCES qc_inspection_main(inspection_id)
);
-- 添加表备注
EXEC sp_addextendedproperty 
    @name = N'MS_Description', 
    @value = N'存储不合格品的待评审信息，包括不合格检测单号、关联质检任务、检测类型及评审结果，用于不合格品的处置流程', 
    @level0type = N'SCHEMA', @level0name = N'dbo', 
    @level1type = N'TABLE', @level1name = N'qc_unqualified_review';

-- 为不合格品待评审表字段添加中文备注
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'评审主键' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_unqualified_review',@level2type=N'COLUMN',@level2name=N'review_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'不合格检测单号' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_unqualified_review',@level2type=N'COLUMN',@level2name=N'unqualified_no';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'关联质检主表ID' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_unqualified_review',@level2type=N'COLUMN',@level2name=N'inspection_id';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'检测类型（过程检/来料检）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_unqualified_review',@level2type=N'COLUMN',@level2name=N'inspection_type';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'派工单号' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_unqualified_review',@level2type=N'COLUMN',@level2name=N'work_order';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'工序编码' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_unqualified_review',@level2type=N'COLUMN',@level2name=N'process_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'工序名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_unqualified_review',@level2type=N'COLUMN',@level2name=N'process_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'批次号' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_unqualified_review',@level2type=N'COLUMN',@level2name=N'batch_no';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'物料名称' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_unqualified_review',@level2type=N'COLUMN',@level2name=N'material_name';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'物料编码' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_unqualified_review',@level2type=N'COLUMN',@level2name=N'material_code';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'质检员检测结果' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_unqualified_review',@level2type=N'COLUMN',@level2name=N'inspector_result';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'评审结果（报废/返工/退货等）' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_unqualified_review',@level2type=N'COLUMN',@level2name=N'review_result';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'评审人' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_unqualified_review',@level2type=N'COLUMN',@level2name=N'reviewer';
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'评审时间' , @level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'qc_unqualified_review',@level2type=N'COLUMN',@level2name=N'review_time';


-- 11. 不合格品评审记录表（存储不合格品的评审历史记录）
CREATE TABLE qc_unqualified_record (
    record_id BIGINT IDENTITY(1,1) PRIMARY KEY, -- 记录主键
    review_id BIGINT NOT NULL, -- 关联待评审表ID
    create_time DATETIME, -- 创建时间
    remark VARCHAR(500), -- 补充说明
    FOREIGN KEY (review_id) REFERENCES qc_unqualified_review(review_id)
);
-- 添加表备注
EXEC sp_addextendedproperty 
    @name = N'MS_Description', 
    @value = N'存储不合格品的评审历史记录，包括评审时间、补充说明等，关联不合格品待评审表，用于追溯评审过程', 
    @level0type = N'SCHEMA', @level0name = N'dbo', 
    @level1type = N'TABLE', @level1name = N'qc_unqualified_record';

-- 为不合格品评审记录表字段添加中文备注
EXEC sp_addextendedproperty @name=N'MS_Description', @value=N'记录主键' , @level0type=N
