-- 统一巡检结果和三级告警增量脚本。
-- 执行前请先备份数据库；本脚本不删除现有巡检数据。

CREATE TABLE IF NOT EXISTS `inspection_result` (
  `id` varchar(32) NOT NULL COMMENT '主键ID',
  `result_code` varchar(64) NOT NULL COMMENT '结果编码',
  `dedupe_key` varchar(255) NOT NULL COMMENT '结果幂等键',
  `task_instance_id` varchar(64) DEFAULT NULL COMMENT '巡检任务实例数据库ID',
  `flow_instance_id` varchar(64) DEFAULT NULL COMMENT '工作流实例ID',
  `task_id` varchar(64) DEFAULT NULL COMMENT '巡检任务ID',
  `item_id` varchar(64) DEFAULT NULL COMMENT '检测项ID',
  `node_id` varchar(64) DEFAULT NULL COMMENT '工作流节点ID',
  `node_name` varchar(255) DEFAULT NULL COMMENT '工作流节点名称',
  `iteration_path` varchar(255) DEFAULT NULL COMMENT '循环迭代路径JSON',
  `result_type` varchar(20) NOT NULL COMMENT 'PPE/METER/MANUAL',
  `result_status` varchar(32) NOT NULL COMMENT 'PENDING/NORMAL/ABNORMAL/RECOGNIZE_FAILED',
  `result_name` varchar(255) NOT NULL COMMENT '检查名称',
  `value_number` decimal(20,8) DEFAULT NULL COMMENT '结构化数值',
  `value_text` varchar(1000) DEFAULT NULL COMMENT '文本结果',
  `unit` varchar(32) DEFAULT NULL COMMENT '单位',
  `alarm_level` tinyint DEFAULT NULL COMMENT '告警级别(1提示 2警告 3严重)',
  `alarm_message` varchar(1000) DEFAULT NULL COMMENT '告警描述',
  `confidence` decimal(10,8) DEFAULT NULL COMMENT '模型置信度',
  `source_event_id` varchar(128) DEFAULT NULL COMMENT '外部PPE事件ID',
  `evidence_url` varchar(1000) DEFAULT NULL COMMENT '主要MinIO证据地址',
  `evidence_type` varchar(20) DEFAULT NULL COMMENT '主要证据类型(IMAGE/VIDEO)',
  `raw_result` mediumtext COMMENT '模型或边缘端原始返回',
  `matched_rule_json` text COMMENT '最终命中的报警规则',
  `rule_snapshot_json` mediumtext COMMENT '完整报警规则快照',
  `reviewer` varchar(64) DEFAULT NULL COMMENT '人工复核人',
  `review_time` datetime DEFAULT NULL COMMENT '人工复核时间',
  `review_remark` varchar(1000) DEFAULT NULL COMMENT '人工复核说明',
  `review_version` int NOT NULL DEFAULT 0 COMMENT '人工复核版本',
  `alarm_id` varchar(64) DEFAULT NULL COMMENT '关联告警ID',
  `occurred_time` datetime NOT NULL COMMENT '结果发生时间',
  `create_by` varchar(64) DEFAULT '' COMMENT '创建者',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_by` varchar(64) DEFAULT '' COMMENT '更新者',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_inspection_result_code` (`result_code`),
  UNIQUE KEY `uk_inspection_result_dedupe` (`dedupe_key`),
  KEY `idx_result_task_instance` (`task_instance_id`),
  KEY `idx_result_flow_instance` (`flow_instance_id`),
  KEY `idx_result_status` (`result_status`),
  KEY `idx_result_type` (`result_type`),
  KEY `idx_result_occurred_time` (`occurred_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='统一巡检结果表';

CREATE TABLE IF NOT EXISTS `inspection_result_media` (
  `id` varchar(32) NOT NULL COMMENT '主键ID',
  `result_id` varchar(32) NOT NULL COMMENT '巡检结果ID',
  `media_type` varchar(20) NOT NULL COMMENT 'IMAGE或VIDEO',
  `media_url` varchar(1000) NOT NULL COMMENT 'MinIO媒体地址',
  `sort_order` int NOT NULL DEFAULT 0 COMMENT '排序号',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_result_media_result_id` (`result_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='巡检结果媒体证据表';

-- 已存在inspection_result表时补充主要证据类型字段。
SET @sql := IF((SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='inspection_result' AND column_name='evidence_type')=0,
  'ALTER TABLE inspection_result ADD COLUMN evidence_type varchar(20) DEFAULT NULL COMMENT ''主要证据类型(IMAGE/VIDEO)'' AFTER evidence_url', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 回填历史结果，使用与主表evidence_url完全一致的媒体记录，避免取错非主要证据。
UPDATE inspection_result r
JOIN inspection_result_media m ON m.result_id = r.id AND m.media_url = r.evidence_url
SET r.evidence_type = m.media_type
WHERE r.evidence_type IS NULL;

SET @sql := IF((SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='inspection_alarm' AND column_name='result_id')=0,
  'ALTER TABLE inspection_alarm ADD COLUMN result_id varchar(64) DEFAULT NULL COMMENT ''关联巡检结果ID'' AFTER task_instance_id', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @sql := IF((SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='inspection_alarm' AND column_name='alarm_source')=0,
  'ALTER TABLE inspection_alarm ADD COLUMN alarm_source varchar(20) DEFAULT NULL COMMENT ''告警来源'' AFTER result_id', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @sql := IF((SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='inspection_alarm' AND column_name='item_id')=0,
  'ALTER TABLE inspection_alarm ADD COLUMN item_id varchar(64) DEFAULT NULL COMMENT ''检测项ID'' AFTER alarm_source', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @sql := IF((SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=DATABASE() AND table_name='inspection_alarm' AND column_name='node_id')=0,
  'ALTER TABLE inspection_alarm ADD COLUMN node_id varchar(64) DEFAULT NULL COMMENT ''工作流节点ID'' AFTER item_id', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @sql := IF((SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema=DATABASE() AND table_name='inspection_alarm' AND index_name='uk_alarm_result_id')=0,
  'ALTER TABLE inspection_alarm ADD UNIQUE KEY uk_alarm_result_id (result_id)', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
