-- ============================================================================
-- 医保影像云图像质控规范 - SQL数据导入脚本
--
-- 文档：医保影像云图像质控规范 (YXY-QC-2025-A1)
-- 创建时间：2026-01-13
--
-- 本文件包含：
-- - Part 1: 影像检查图像基础要求（4.1-4.7）
-- - Part 2: CT图像质量控制整体要求（5.3）
-- - Part 3: CT颅脑平扫质控标准（5.4.1.1）
-- - Part 4: CT颅脑增强质控标准（5.4.1.2）
--
-- 说明：根据现有表结构生成，适用于 qc_factor、qc_standard、qc_standard_factor 表
-- ============================================================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ===================================
-- Part 1: 影像检查图像基础要求（适用于所有影像模态）
-- ===================================

-- 4.1 有效图像（否决项）
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('BASIC_4_1', 'BASIC_4_1', '有效图像', 1, 'boolean',
'影像检查图像须确保清晰度与可辨识性，能够为诊断提供准确依据，严禁出现全黑、全白等无效影像',
'{
  "type": "opencv_check",
  "method": "checkValidImage",
  "params": {
    "minSharpness": 50.0,
    "minBrightness": 10.0,
    "maxBrightness": 245.0,
    "minStdDev": 20.0
  },
  "isVeto": true,
  "description": "使用Laplacian方差检测清晰度，检查亮度范围和标准差，识别全黑、全白图像"
}',
'opencv_analysis', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 4.2 真实人体图像（否决项）
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('BASIC_4_2', 'BASIC_4_2', '真实人体图像', 1, 'boolean',
'影像检查图像须真实反映人体解剖结构，严禁使用非真实人体图像，如：仿人体模型、质控水模、动植物等',
'{
  "type": "opencv_check",
  "method": "checkRealHumanBody",
  "params": {
    "minEdgeRatio": 0.015,
    "maxEdgeRatio": 0.50,
    "minTextureComplexity": 3.0
  },
  "isVeto": true,
  "description": "检查边缘密度和纹理复杂度，识别非真实人体图像（模型、水模、动植物等）"
}',
'opencv_analysis', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 4.3 原始完整图像
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('BASIC_4_3', 'BASIC_4_3', '原始完整图像', 1, 'boolean',
'影像检查图像须为原始采集内容，严禁篡改、添加、删减、伪造处理',
'{
  "type": "opencv_check",
  "method": "checkOriginalComplete",
  "params": {
    "maxAvgEdgeCount": 500.0,
    "checkCompressionArtifacts": true
  },
  "isVeto": false,
  "description": "检测图像是否经过篡改、添加、删减或伪造处理"
}',
'opencv_analysis', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 4.4 非拼接图像
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('BASIC_4_4', 'BASIC_4_4', '非拼接图像', 1, 'boolean',
'影像检查图像须完整包含患者同一次检查的全部图像，禁止将不同机构、不同患者、不同时间采集的检查图像拼接为同一次影像检查图像',
'{
  "type": "opencv_check",
  "method": "checkNonStitched",
  "params": {
    "detectSeams": true,
    "maxSeamEdges": 10,
    "checkInconsistentLighting": true
  },
  "isVeto": false,
  "description": "检测图像是否存在拼接痕迹、接缝或光照不一致"
}',
'opencv_analysis', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 4.5 非重复图像
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('BASIC_4_5', 'BASIC_4_5', '非重复图像', 1, 'boolean',
'同一患者的同次影像检查图像不得被重复使用于不同的检查记录中，严禁将部分影像检查图像跨患者、跨机构重复使用',
'{
  "type": "dicom_check",
  "method": "checkNonDuplicate",
  "params": {
    "checkAcrossStudies": true,
    "checkAcrossPatients": true,
    "checkAcrossInstitutions": true
  },
  "isVeto": false,
  "description": "通过SOP Instance UID和图像哈希值检查图像是否被重复使用"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 4.6 患者信息相符
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('BASIC_4_6', 'BASIC_4_6', '患者信息相符', 1, 'boolean',
'影像检查图像特征须与患者年龄、性别、病史等信息相符',
'{
  "type": "dicom_check",
  "method": "checkPatientInfoMatch",
  "params": {
    "checkAgeConsistency": true,
    "checkSexConsistency": true,
    "allowAgeDeviation": 5
  },
  "isVeto": false,
  "description": "验证DICOM中的患者年龄、性别与图像特征是否一致"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 4.7 检查部位一致
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('BASIC_4_7', 'BASIC_4_7', '检查部位一致', 1, 'boolean',
'影像检查图像显示的检查部位须与检查报告、医保结算对应信息保持一致',
'{
  "type": "dicom_check",
  "method": "checkBodyPartConsistent",
  "params": {
    "compareWithReport": true,
    "compareWithSettlement": true,
    "allowSynonyms": true
  },
  "isVeto": false,
  "description": "验证图像显示的检查部位与检查报告、医保结算信息是否一致"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);


-- ===================================
-- Part 2: CT图像质量控制整体要求（5.3）
-- ===================================

-- 5.3.1 图像信息（需包含DICOM元数据）（否决项）
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_5_3_1', 'CT_5_3_1', 'CT图像信息完整性', 2, 'boolean',
'需包含患者标识、扫描参数等DICOM元数据信息',
'{
  "type": "dicom_check",
  "method": "checkCTDicomMetadata",
  "params": {
    "requiredFields": [
      "PatientID",
      "PatientName",
      "StudyDate",
      "Modality",
      "SliceThickness",
      "KVP"
    ]
  },
  "isVeto": true,
  "description": "检查CT DICOM文件是否包含必需的元数据字段"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 5.3.2 图像质量（窗技术应用）
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_5_3_2', 'CT_5_3_2', 'CT窗技术应用', 2, 'boolean',
'图像对比度和灰度，应依检查部位的规范，合理应用窗技术，满足组织间的不同层次对比',
'{
  "type": "dicom_check",
  "method": "checkCTWindowTechnique",
  "params": {
    "requireWindowCenter": true,
    "requireWindowWidth": true,
    "checkAppropriateWindow": true
  },
  "isVeto": false,
  "description": "检查是否正确应用窗技术（窗宽窗位）"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);


-- ===================================
-- Part 3: CT颅脑质控标准（5.4.1）
-- ===================================

-- 5.4.1.1 头颅CT平扫
INSERT INTO `qc_standard` (`id`, `standard_code`, `standard_name`, `modality`, `body_part`, `scan_type`, `exam_item`, `factor_category`, `description`, `pass_score`, `status`, `create_time`, `update_time`) VALUES
('STD_CT_5_4_1_1', 'CT_5_4_1_1', '头颅CT平扫质控标准', 'CT', '颅脑', '平扫', '头颅CT平扫', 2,
'基于医保影像云规范的颅脑CT平扫质控标准，包含检查范围、重建参数、窗口技术、图像标识、伪影、清晰度等要求',
80, 1, NOW(), NOW());

-- 头颅CT平扫 - 检查范围
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_5_4_1_1_1', 'CT_5_4_1_1_1', '检查范围（颅脑）', 2, 'string',
'检查范围：从颅底至颅顶',
'{
  "type": "dicom_check",
  "method": "checkScanRangeSkullBaseToTop",
  "params": {
    "minSliceCount": 30,
    "expectedBodyPart": "BRAIN",
    "checkCoverage": true
  },
  "isVeto": false,
  "description": "验证扫描范围是否覆盖颅底至颅顶"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 头颅CT平扫 - 图像偏中心
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_5_4_1_1_2', 'CT_5_4_1_1_2', '图像偏中心', 2, 'string',
'图像偏中心：断层位于图像正中',
'{
  "type": "opencv_check",
  "method": "checkImageCentering",
  "params": {
    "maxOffsetRatio": 0.10
  },
  "isVeto": false,
  "description": "检测图像内容重心是否偏离中心"
}',
'opencv_analysis', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 头颅CT平扫 - 重建参数
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_5_4_1_1_3', 'CT_5_4_1_1_3', '重建参数', 2, 'object',
'重建参数：厚层层厚/层间隔≤5mm/5mm，薄层层厚/层间隔≤1.25mm/1.25mm，DFOV：23cm',
'{
  "type": "dicom_check",
  "method": "checkReconstructionParams",
  "params": {
    "thickSlice": {
      "maxSliceThickness": 5.0,
      "maxSliceInterval": 5.0
    },
    "thinSlice": {
      "maxSliceThickness": 1.25,
      "maxSliceInterval": 1.25
    },
    "dfov": {
      "nominal": 23.0,
      "tolerance": 3.0
    }
  },
  "isVeto": false,
  "description": "检查层厚、层间隔和DFOV参数"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 头颅CT平扫 - 窗口技术（脑窗+骨窗）（否决项）
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_5_4_1_1_4', 'CT_5_4_1_1_4', '窗口技术', 2, 'object',
'窗口技术：脑窗窗宽80~100HU，窗位35~45HU；骨窗窗宽3500~4000HU，窗位500~700HU',
'{
  "type": "dicom_check",
  "method": "checkWindowTechnique",
  "params": {
    "brainWindow": {
      "windowWidth": {"min": 80, "max": 100},
      "windowCenter": {"min": 35, "max": 45}
    },
    "boneWindow": {
      "windowWidth": {"min": 3500, "max": 4000},
      "windowCenter": {"min": 500, "max": 700}
    }
  },
  "isVeto": true,
  "description": "检查脑窗和骨窗的窗宽窗位是否符合标准"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 头颅CT平扫 - 扫描体位
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_5_4_1_1_5', 'CT_5_4_1_1_5', '扫描体位', 2, 'string',
'扫描体位：仰卧位',
'{
  "type": "dicom_check",
  "method": "checkScanPosition",
  "params": {
    "expectedPosition": "HFS",
    "allowedPositions": ["HFS", "FFS"]
  },
  "isVeto": false,
  "description": "验证患者体位是否符合标准（HFS-仰卧位头先进）"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 头颅CT平扫 - 图像标识（否决项）
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_5_4_1_1_6', 'CT_5_4_1_1_6', '图像标识', 2, 'boolean',
'图像标识：影像号、检查部位、姓名、性别、年龄、检查日期、检查时间、kV、mAs值、扫描方位',
'{
  "type": "dicom_check",
  "method": "checkImageAnnotation",
  "params": {
    "requiredFields": [
      "StudyInstanceUID",
      "BodyPartExamined",
      "PatientName",
      "PatientSex",
      "PatientAge",
      "StudyDate",
      "StudyTime",
      "KVP",
      "Exposure"
    ]
  },
  "isVeto": true,
  "description": "检查DICOM文件是否包含所有必需的图像标识信息"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 头颅CT平扫 - 图像伪影（否决项）
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_5_4_1_1_7', 'CT_5_4_1_1_7', '图像伪影', 2, 'boolean',
'图像伪影：没有运动伪影以及高衰减伪影',
'{
  "type": "opencv_check",
  "method": "checkImageArtifact",
  "params": {
    "maxMotionArtifactLevel": 2,
    "checkBeamHardening": true,
    "checkMetalArtifact": true,
    "maxArtifactRatio": 0.15
  },
  "isVeto": true,
  "description": "检测运动伪影、束硬化伪影、金属伪影等CT常见伪影"
}',
'opencv_analysis', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 头颅CT平扫 - 图像清晰度
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_5_4_1_1_8', 'CT_5_4_1_1_8', '图像清晰度', 2, 'number',
'图像清晰度：颅脑组织显示清晰',
'{
  "type": "opencv_check",
  "method": "checkImageClarity",
  "params": {
    "minSharpness": 100.0,
    "minContrast": 30.0
  },
  "isVeto": false,
  "description": "检测颅脑组织显示的清晰度"
}',
'opencv_analysis', 1, NOW(), NOW(), NULL, NULL, NULL);


-- ===================================
-- 关联质控因子到头颅CT平扫标准
-- ===================================

DELETE FROM qc_standard_factor WHERE standard_id = 'STD_CT_5_4_1_1';

-- 基础要求（4.1-4.7）
INSERT INTO `qc_standard_factor` (`id`, `standard_id`, `factor_id`, `weight`, `is_required`, `sort_order`, `create_time`, `update_time`) VALUES
('SF_CT_5_4_1_1_4_1', 'STD_CT_5_4_1_1', 'BASIC_4_1', 15.00, 1, 1, NOW(), NOW()),
('SF_CT_5_4_1_1_4_2', 'STD_CT_5_4_1_1', 'BASIC_4_2', 15.00, 1, 2, NOW(), NOW()),
('SF_CT_5_4_1_1_4_3', 'STD_CT_5_4_1_1', 'BASIC_4_3', 5.00, 0, 3, NOW(), NOW()),
('SF_CT_5_4_1_1_4_4', 'STD_CT_5_4_1_1', 'BASIC_4_4', 5.00, 0, 4, NOW(), NOW()),
('SF_CT_5_4_1_1_4_5', 'STD_CT_5_4_1_1', 'BASIC_4_5', 5.00, 0, 5, NOW(), NOW()),
('SF_CT_5_4_1_1_4_6', 'STD_CT_5_4_1_1', 'BASIC_4_6', 5.00, 0, 6, NOW(), NOW()),
('SF_CT_5_4_1_1_4_7', 'STD_CT_5_4_1_1', 'BASIC_4_7', 5.00, 0, 7, NOW(), NOW()),
-- CT整体要求（5.3）
('SF_CT_5_4_1_1_5_3_1', 'STD_CT_5_4_1_1', 'CT_5_3_1', 10.00, 1, 8, NOW(), NOW()),
('SF_CT_5_4_1_1_5_3_2', 'STD_CT_5_4_1_1', 'CT_5_3_2', 5.00, 0, 9, NOW(), NOW()),
-- 颅脑专项要求
('SF_CT_5_4_1_1_SPEC_1', 'STD_CT_5_4_1_1', 'CT_5_4_1_1_1', 5.00, 0, 10, NOW(), NOW()),
('SF_CT_5_4_1_1_SPEC_2', 'STD_CT_5_4_1_1', 'CT_5_4_1_1_2', 5.00, 0, 11, NOW(), NOW()),
('SF_CT_5_4_1_1_SPEC_3', 'STD_CT_5_4_1_1', 'CT_5_4_1_1_3', 5.00, 0, 12, NOW(), NOW()),
('SF_CT_5_4_1_1_SPEC_4', 'STD_CT_5_4_1_1', 'CT_5_4_1_1_4', 10.00, 1, 13, NOW(), NOW()),
('SF_CT_5_4_1_1_SPEC_5', 'STD_CT_5_4_1_1', 'CT_5_4_1_1_5', 5.00, 0, 14, NOW(), NOW()),
('SF_CT_5_4_1_1_SPEC_6', 'STD_CT_5_4_1_1', 'CT_5_4_1_1_6', 10.00, 1, 15, NOW(), NOW()),
('SF_CT_5_4_1_1_SPEC_7', 'STD_CT_5_4_1_1', 'CT_5_4_1_1_7', 10.00, 1, 16, NOW(), NOW()),
('SF_CT_5_4_1_1_SPEC_8', 'STD_CT_5_4_1_1', 'CT_5_4_1_1_8', 5.00, 0, 17, NOW(), NOW());



-- ============================================================================
-- Part 4: CT颅脑增强质控标准（5.4.1.2）
-- ============================================================================

-- ===================================
-- 5.4.1.2 头颅CT增强 - 质控因子（qc_factor）
-- ===================================
-- 因子1：检查范围 - 从颅底至颅顶
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_HEAD_ENH_5_4_1_2_1', 'CT_HEAD_ENH_5_4_1_2_1', '检查范围', 3, 'boolean', '检查范围从颅底至颅顶，确保覆盖完整颅脑区域',
'{
  "type": "dicom_check",
  "method": "checkScanRangeSkullBaseToTop",
  "params": {
    "minSliceCount": 30,
    "expectedBodyPart": "BRAIN",
    "checkCoverage": true
  },
  "isVeto": false,
  "description": "通过DICOM序列图像数量和BodyPart标签验证扫描范围是否覆盖颅底至颅顶"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 因子2：图像偏中心 - 断层位于图像正中
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_HEAD_ENH_5_4_1_2_2', 'CT_HEAD_ENH_5_4_1_2_2', '图像偏中心', 3, 'string', '断层位于图像正中，允许适度偏移',
'{
  "type": "opencv_check",
  "method": "checkImageCentering",
  "params": {
    "maxOffsetRatio": 0.15,
    "checkCenterOfMass": true
  },
  "isVeto": false,
  "description": "检测图像内容重心是否偏离中心超过15%"
}',
'opencv_analysis', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 因子3：重建参数 - 层厚/层间隔、DFOV
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_HEAD_ENH_5_4_1_2_3', 'CT_HEAD_ENH_5_4_1_2_3', '重建参数', 3, 'object', '厚层层厚/层间隔≤5mm/5mm，薄层层厚/层间隔≤1.25mm/1.25mm，DFOV：23cm',
'{
  "type": "dicom_check",
  "method": "checkReconstructionParams",
  "params": {
    "thickSlice": {
      "maxSliceThickness": 5.0,
      "maxSliceInterval": 5.0
    },
    "thinSlice": {
      "maxSliceThickness": 1.25,
      "maxSliceInterval": 1.25
    },
    "dfov": {
      "nominal": 23.0,
      "tolerance": 5.0
    }
  },
  "isVeto": false,
  "description": "检查DICOM标签中的层厚、层间隔和DFOV是否符合规范"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 因子4：窗口技术 - 脑窗静脉期（否决项）
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_HEAD_ENH_5_4_1_2_4', 'CT_HEAD_ENH_5_4_1_2_4', '窗口技术', 3, 'object', '常规包含脑窗静脉期：窗宽80~100HU，窗位35~45HU',
'{
  "type": "dicom_check",
  "method": "checkWindowTechnique",
  "params": {
    "brainWindowVenous": {
      "windowWidth": {"min": 80, "max": 100},
      "windowCenter": {"min": 35, "max": 45}
    }
  },
  "isVeto": true,
  "description": "检查窗宽窗位是否符合脑窗静脉期标准（增强CT关键参数）"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 因子5：扫描体位 - 仰卧位
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_HEAD_ENH_5_4_1_2_5', 'CT_HEAD_ENH_5_4_1_2_5', '扫描体位', 3, 'string', '扫描体位：仰卧位',
'{
  "type": "dicom_check",
  "method": "checkScanPosition",
  "params": {
    "expectedPosition": "HFS",
    "allowedPositions": ["HFS", "FFS", "HFP", "FFP"],
    "checkPatientOrientation": true
  },
  "isVeto": false,
  "description": "验证患者体位是否符合标准（Head First Supine - HFS表示仰卧位头先进）"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 因子6：图像标识（否决项）
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_HEAD_ENH_5_4_1_2_6', 'CT_HEAD_ENH_5_4_1_2_6', '图像标识', 3, 'boolean', '图像标识须包含：影像号、检查部位、姓名、性别、年龄、检查日期、检查时间、kV、mAs值、扫描方位',
'{
  "type": "dicom_check",
  "method": "checkImageAnnotation",
  "params": {
    "requiredFields": [
      "StudyInstanceUID",        -- 影像号
      "BodyPartExamined",        -- 检查部位
      "PatientName",             -- 姓名
      "PatientSex",              -- 性别
      "PatientAge",              -- 年龄
      "StudyDate",               -- 检查日期
      "StudyTime",               -- 检查时间
      "KVP",                     -- kV值
      "Exposure",                -- mAs值
      "ScanOrientation"          -- 扫描方位
    ]
  },
  "isVeto": true,
  "description": "检查DICOM文件是否包含所有必需的图像标识信息"
}',
'dicom_metadata', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 因子7：图像伪影（否决项）
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_HEAD_ENH_5_4_1_2_7', 'CT_HEAD_ENH_5_4_1_2_7', '图像伪影', 3, 'boolean', '没有运动伪影以及高衰减伪影',
'{
  "type": "opencv_check",
  "method": "checkImageArtifactEnhanced",
  "params": {
    "maxMotionArtifactLevel": 2,
    "checkBeamHardening": true,
    "checkMetalArtifact": true,
    "maxArtifactRatio": 0.15
  },
  "isVeto": true,
  "description": "检测运动伪影、束硬化伪影、金属伪影等增强CT常见伪影"
}',
'opencv_analysis', 1, NOW(), NOW(), NULL, NULL, NULL);

-- 因子8：图像清晰度 - 增强CT脑部血管明显强化
INSERT INTO `qc_factor` (`id`, `factor_code`, `factor_name`, `factor_category`, `factor_type`, `description`, `check_rule`, `data_source`, `status`, `create_time`, `update_time`, `modality`, `body_part`, `scan_type`) VALUES
('CT_HEAD_ENH_5_4_1_2_8', 'CT_HEAD_ENH_5_4_1_2_8', '图像清晰度', 3, 'number', '增强CT脑部血管明显强化，血管对比度清晰',
'{
  "type": "opencv_check",
  "method": "checkImageClarityEnhanced",
  "params": {
    "minVesselContrast": 50.0,
    "minSharpness": 100.0,
    "checkEnhancement": true,
    "expectedEnhancementRatio": 1.5
  },
  "isVeto": false,
  "description": "检测增强CT血管强化程度和清晰度，评估血管显示质量"
}',
'opencv_analysis', 1, NOW(), NOW(), NULL, NULL, NULL);


-- ===================================
-- 2. 创建质控标准（qc_standard）
-- ===================================

INSERT INTO `qc_standard` (`id`, `standard_code`, `standard_name`, `standard_category`, `description`, `pass_score`, `status`, `create_time`, `update_time`) VALUES
('STD_CT_HEAD_ENHANCED', 'STD_CT_HEAD_ENHANCED', '头颅CT增强质控标准', 3, '基于国家医保局要求的头颅CT增强影像质量控制标准（GB 5.4.1.2），包含检查范围、重建参数、窗口技术、增强效果等专项要求',
80, 1, NOW(), NOW());


-- ===================================
-- 3. 关联质控因子到标准（qc_standard_factor）
-- ===================================

-- 删除旧的关联（如果存在）
DELETE FROM qc_standard_factor WHERE standard_id = 'STD_CT_HEAD_ENHANCED';

-- 插入新的关联（权重分配）
-- 头颅CT增强专项因子（权重分配：总计100）
INSERT INTO `qc_standard_factor` (`id`, `standard_id`, `factor_id`, `check_rule`, `weight`, `is_required`, `threshold_value`, `sort_order`, `create_time`, `update_time`, `pass_value`) VALUES
('SF_CT_ENH_1', 'STD_CT_HEAD_ENHANCED', 'CT_HEAD_ENH_5_4_1_2_1', NULL, 10.00, 1, NULL, 1, NOW(), NOW(), '{"passed": true}'),   -- 检查范围
('SF_CT_ENH_2', 'STD_CT_HEAD_ENHANCED', 'CT_HEAD_ENH_5_4_1_2_2', NULL, 8.00, 1, NULL, 2, NOW(), NOW(), '{"result": "normal"}'),  -- 图像偏中心
('SF_CT_ENH_3', 'STD_CT_HEAD_ENHANCED', 'CT_HEAD_ENH_5_4_1_2_3', NULL, 12.00, 1, NULL, 3, NOW(), NOW(), '{"passed": true}'),  -- 重建参数
('SF_CT_ENH_4', 'STD_CT_HEAD_ENHANCED', 'CT_HEAD_ENH_5_4_1_2_4', NULL, 20.00, 1, NULL, 4, NOW(), NOW(), '{"passed": true}'),  -- 窗口技术（否决项）
('SF_CT_ENH_5', 'STD_CT_HEAD_ENHANCED', 'CT_HEAD_ENH_5_4_1_2_5', NULL, 8.00, 1, NULL, 5, NOW(), NOW(), '{"position": "HFS"}'),  -- 扫描体位
('SF_CT_ENH_6', 'STD_CT_HEAD_ENHANCED', 'CT_HEAD_ENH_5_4_1_2_6', NULL, 17.00, 1, NULL, 6, NOW(), NOW(), '{"passed": true}'),  -- 图像标识（否决项）
('SF_CT_ENH_7', 'STD_CT_HEAD_ENHANCED', 'CT_HEAD_ENH_5_4_1_2_7', NULL, 15.00, 1, NULL, 7, NOW(), NOW(), '{"passed": true}'),  -- 图像伪影（否决项）
('SF_CT_ENH_8', 'STD_CT_HEAD_ENHANCED', 'CT_HEAD_ENH_5_4_1_2_8', NULL, 10.00, 1, NULL, 8, NOW(), NOW(), '{"value": {"$gte": 50}}')  -- 图像清晰度


-- ===================================

-- ===================================
-- 验证查询
-- ===================================

-- 查看所有头颅CT平扫质控因子
SELECT
    f.factor_code,
    f.factor_name,
    f.description,
    JSON_EXTRACT(f.check_rule, '$.isVeto') as is_veto,
    JSON_EXTRACT(f.check_rule, '$.method') as check_method
FROM qc_factor f
WHERE f.id LIKE 'CT_5_4_1_1_%' OR f.id LIKE 'BASIC_%'
ORDER BY f.factor_code;

-- 查看头颅CT平扫标准及其关联的因子
SELECT
    s.standard_code,
    s.standard_name,
    s.modality,
    s.body_part,
    s.scan_type,
    f.factor_code,
    f.factor_name,
    sf.weight,
    sf.is_required,
    sf.sort_order
FROM qc_standard s
JOIN qc_standard_factor sf ON s.id = sf.standard_id
JOIN qc_factor f ON sf.factor_id = f.id
WHERE s.id = 'STD_CT_5_4_1_1'
ORDER BY sf.sort_order;

-- 统计信息
SELECT '基础质控因子' as type, COUNT(*) as count FROM qc_factor WHERE id LIKE 'BASIC_%'
UNION ALL
SELECT '头颅CT平扫质控因子', COUNT(*) FROM qc_factor WHERE id LIKE 'CT_5_4_1_1_%'
UNION ALL
SELECT '头颅CT平扫标准-因子关联', COUNT(*) FROM qc_standard_factor WHERE standard_id = 'STD_CT_5_4_1_1';

SET FOREIGN_KEY_CHECKS = 1;

-- ===================================
-- 文件说明
-- ===================================
-- 本文件包含了基础的影像质控因子和头颅CT平扫的完整质控标准
-- 下一步将创建其他CT部位的质控标准（口腔颌面、眼部、鼻咽部、颈部、胸部、腹部、盆腔）
-- 以及MR相关的质控标准
