-- =====================================================
-- 武术比赛管理系统 - 数据库字段补充脚本
-- 基于前端需求分析,补充缺失字段
-- Date: 2025-11-29
-- =====================================================

USE martial_db;

-- =====================================================
-- 1. martial_athlete (参赛选手表) - 添加4个字段
-- =====================================================
ALTER TABLE martial_athlete
ADD COLUMN id_card_type int DEFAULT 1 COMMENT '证件类型(1-身份证,2-护照,3-其他)' AFTER id_card,
ADD COLUMN birth_date date DEFAULT NULL COMMENT '出生日期' AFTER age,
ADD COLUMN nation varchar(50) DEFAULT NULL COMMENT '民族' AFTER birth_date,
ADD COLUMN organization_type int DEFAULT 1 COMMENT '单位类别(1-学校,2-协会,3-俱乐部,4-其他)' AFTER organization;

-- =====================================================
-- 2. martial_registration_order (报名订单表) - 添加7个字段
-- =====================================================
ALTER TABLE martial_registration_order
ADD COLUMN transaction_no varchar(100) DEFAULT NULL COMMENT '支付交易号' AFTER payment_time,
ADD COLUMN refund_amount decimal(10,2) DEFAULT 0.00 COMMENT '退款金额' AFTER paid_amount,
ADD COLUMN refund_time datetime DEFAULT NULL COMMENT '退款时间' AFTER transaction_no,
ADD COLUMN refund_reason varchar(500) DEFAULT NULL COMMENT '退款原因' AFTER refund_time,
ADD COLUMN invoice_type int DEFAULT 0 COMMENT '发票类型(0-不需要,1-普通,2-增值税)' AFTER organization,
ADD COLUMN invoice_title varchar(200) DEFAULT NULL COMMENT '发票抬头' AFTER invoice_type,
ADD COLUMN invoice_tax_no varchar(50) DEFAULT NULL COMMENT '税号' AFTER invoice_title;

-- =====================================================
-- 3. martial_score (评分表) - 添加5个字段
-- =====================================================
ALTER TABLE martial_score
ADD COLUMN venue_id bigint DEFAULT NULL COMMENT '场地ID' AFTER schedule_id,
ADD COLUMN original_score decimal(10,3) DEFAULT NULL COMMENT '原始评分(修改前)' AFTER score,
ADD COLUMN modify_reason varchar(500) DEFAULT NULL COMMENT '修改原因' AFTER note,
ADD COLUMN modify_time datetime DEFAULT NULL COMMENT '修改时间' AFTER score_time,
ADD COLUMN ip_address varchar(50) DEFAULT NULL COMMENT '评分IP地址' AFTER modify_time,
ADD INDEX idx_venue (venue_id);

-- =====================================================
-- 4. martial_result (成绩表) - 添加5个字段
-- =====================================================
ALTER TABLE martial_result
ADD COLUMN max_score decimal(10,3) DEFAULT NULL COMMENT '最高分(去掉用)' AFTER total_score,
ADD COLUMN min_score decimal(10,3) DEFAULT NULL COMMENT '最低分(去掉用)' AFTER max_score,
ADD COLUMN valid_score_count int DEFAULT 0 COMMENT '有效评分数' AFTER min_score,
ADD COLUMN difficulty_coefficient decimal(5,2) DEFAULT 1.00 COMMENT '难度系数' AFTER adjusted_score,
ADD COLUMN final_score decimal(10,3) DEFAULT NULL COMMENT '最终得分(总分*系数)' AFTER difficulty_coefficient;

-- =====================================================
-- 5. martial_project (比赛项目表) - 添加4个字段
-- =====================================================
ALTER TABLE martial_project
ADD COLUMN min_age int DEFAULT NULL COMMENT '最小年龄' AFTER max_participants,
ADD COLUMN max_age int DEFAULT NULL COMMENT '最大年龄' AFTER min_age,
ADD COLUMN gender_limit int DEFAULT 0 COMMENT '性别限制(0-不限,1-仅男,2-仅女)' AFTER max_age,
ADD COLUMN registration_deadline datetime DEFAULT NULL COMMENT '报名截止时间' AFTER price;

-- =====================================================
-- 6. martial_banner (轮播图表) - 添加4个字段
-- =====================================================
ALTER TABLE martial_banner
ADD COLUMN position int DEFAULT 1 COMMENT '显示位置(1-首页,2-赛事详情,3-其他)' AFTER title,
ADD COLUMN start_time datetime DEFAULT NULL COMMENT '开始显示时间' AFTER sort_order,
ADD COLUMN end_time datetime DEFAULT NULL COMMENT '结束显示时间' AFTER start_time,
ADD COLUMN click_count int DEFAULT 0 COMMENT '点击次数' AFTER end_time;

-- =====================================================
-- 7. martial_info_publish (信息发布表) - 添加3个字段
-- =====================================================
ALTER TABLE martial_info_publish
ADD COLUMN view_count int DEFAULT 0 COMMENT '阅读次数' AFTER sort_order,
ADD COLUMN attachments varchar(1000) DEFAULT NULL COMMENT '附件(JSON)' AFTER images,
ADD COLUMN publisher_name varchar(50) DEFAULT NULL COMMENT '发布人姓名' AFTER publish_time;

-- =====================================================
-- 8. martial_judge_invite (裁判邀请码表) - 添加4个字段
-- =====================================================
ALTER TABLE martial_judge_invite
ADD COLUMN device_info varchar(500) DEFAULT NULL COMMENT '设备信息' AFTER use_time,
ADD COLUMN login_ip varchar(50) DEFAULT NULL COMMENT '登录IP' AFTER device_info,
ADD COLUMN access_token varchar(500) DEFAULT NULL COMMENT '访问令牌' AFTER login_ip,
ADD COLUMN token_expire_time datetime DEFAULT NULL COMMENT 'token过期时间' AFTER access_token;

-- =====================================================
-- 验证修改
-- =====================================================
SELECT '=== 字段补充完成 ===' AS status;
SELECT 'martial_athlete 字段数:', COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='martial_db' AND TABLE_NAME='martial_athlete';
SELECT 'martial_registration_order 字段数:', COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='martial_db' AND TABLE_NAME='martial_registration_order';
SELECT 'martial_score 字段数:', COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='martial_db' AND TABLE_NAME='martial_score';
SELECT 'martial_result 字段数:', COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='martial_db' AND TABLE_NAME='martial_result';
SELECT 'martial_project 字段数:', COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='martial_db' AND TABLE_NAME='martial_project';
SELECT 'martial_banner 字段数:', COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='martial_db' AND TABLE_NAME='martial_banner';
SELECT 'martial_info_publish 字段数:', COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='martial_db' AND TABLE_NAME='martial_info_publish';
SELECT 'martial_judge_invite 字段数:', COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='martial_db' AND TABLE_NAME='martial_judge_invite';
