-- =====================================================
-- 迁移脚本: 添加缺失的字段和表
-- 版本: V3
-- 描述: 补充 venue_type, result 字段, 以及新增表
-- 日期: 2026-01-13
-- =====================================================

-- 1. martial_venue 添加 venue_type 字段
SET @exist := (SELECT COUNT(*) FROM information_schema.columns 
               WHERE table_schema = DATABASE() 
               AND table_name = 'martial_venue' 
               AND column_name = 'venue_type');
SET @sql := IF(@exist = 0, 
    'ALTER TABLE martial_venue ADD COLUMN venue_type varchar(50) DEFAULT NULL COMMENT "场地类型(indoor-室内,outdoor-室外)" AFTER venue_code',
    'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 2. martial_result 添加评分相关字段
SET @exist := (SELECT COUNT(*) FROM information_schema.columns 
               WHERE table_schema = DATABASE() 
               AND table_name = 'martial_result' 
               AND column_name = 'chief_judge_score');
SET @sql := IF(@exist = 0, 
    'ALTER TABLE martial_result 
     ADD COLUMN chief_judge_score decimal(10,2) DEFAULT NULL COMMENT "主裁判评分" AFTER publish_time,
     ADD COLUMN chief_judge_id bigint DEFAULT NULL COMMENT "主裁判ID" AFTER chief_judge_score,
     ADD COLUMN chief_judge_time datetime DEFAULT NULL COMMENT "主裁判评分时间" AFTER chief_judge_id,
     ADD COLUMN chief_judge_note varchar(500) DEFAULT NULL COMMENT "主裁判备注" AFTER chief_judge_time,
     ADD COLUMN general_judge_score decimal(10,2) DEFAULT NULL COMMENT "副裁判评分" AFTER chief_judge_note,
     ADD COLUMN general_judge_id bigint DEFAULT NULL COMMENT "副裁判ID" AFTER general_judge_score,
     ADD COLUMN general_judge_time datetime DEFAULT NULL COMMENT "副裁判评分时间" AFTER general_judge_id,
     ADD COLUMN general_judge_note varchar(500) DEFAULT NULL COMMENT "副裁判备注" AFTER general_judge_time,
     ADD COLUMN score_status int DEFAULT 0 COMMENT "评分状态" AFTER general_judge_note',
    'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 3. martial_project 添加 venue_id 字段
SET @exist := (SELECT COUNT(*) FROM information_schema.columns 
               WHERE table_schema = DATABASE() 
               AND table_name = 'martial_project' 
               AND column_name = 'venue_id');
SET @sql := IF(@exist = 0, 
    'ALTER TABLE martial_project ADD COLUMN venue_id bigint DEFAULT NULL COMMENT "场地ID" AFTER competition_id',
    'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
