-- =====================================================
-- 武术比赛管理系统 - 完整数据库设计
-- Database: martial_db
-- Author: Claude Code
-- Date: 2025-11-29
-- =====================================================

-- 设置字符集
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- =====================================================
-- 1. 赛事管理核心表
-- =====================================================

-- 赛事信息表
DROP TABLE IF EXISTS `martial_competition`;
CREATE TABLE `martial_competition` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `competition_name` varchar(200) NOT NULL COMMENT '赛事名称',
  `competition_code` varchar(50) NOT NULL COMMENT '赛事编码(用于裁判登录)',
  `organizer` varchar(200) DEFAULT NULL COMMENT '主办单位',
  `location` varchar(100) DEFAULT NULL COMMENT '地区',
  `venue` varchar(200) DEFAULT NULL COMMENT '详细地点',
  `registration_start_time` datetime DEFAULT NULL COMMENT '报名开始时间',
  `registration_end_time` datetime DEFAULT NULL COMMENT '报名结束时间',
  `competition_start_time` datetime DEFAULT NULL COMMENT '比赛开始时间',
  `competition_end_time` datetime DEFAULT NULL COMMENT '比赛结束时间',
  `introduction` text COMMENT '赛事简介',
  `poster_images` varchar(1000) DEFAULT NULL COMMENT '宣传图片(JSON数组)',
  `contact_person` varchar(50) DEFAULT NULL COMMENT '联系人',
  `contact_phone` varchar(20) DEFAULT NULL COMMENT '联系电话',
  `contact_email` varchar(100) DEFAULT NULL COMMENT '联系邮箱',
  `rules` text COMMENT '竞赛规则',
  `requirements` text COMMENT '参赛要求',
  `awards` text COMMENT '奖项设置',
  `regulation_files` varchar(1000) DEFAULT NULL COMMENT '规程文件(JSON数组)',
  `total_participants` int DEFAULT 0 COMMENT '报名总人数',
  `total_amount` decimal(10,2) DEFAULT 0.00 COMMENT '报名总金额',
  `status` int DEFAULT 0 COMMENT '状态(0-未开始,1-报名中,2-比赛中,3-已结束,4-已取消)',
  `create_user` bigint DEFAULT NULL COMMENT '创建人',
  `create_dept` bigint DEFAULT NULL COMMENT '创建部门',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_user` bigint DEFAULT NULL COMMENT '更新人',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `is_deleted` int DEFAULT 0 COMMENT '是否已删除',
  `tenant_id` varchar(12) DEFAULT '000000' COMMENT '租户ID',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_code` (`competition_code`),
  KEY `idx_tenant_status` (`tenant_id`,`status`),
  KEY `idx_time` (`competition_start_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='赛事信息表';

-- 比赛项目表
DROP TABLE IF EXISTS `martial_project`;
CREATE TABLE `martial_project` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `competition_id` bigint NOT NULL COMMENT '赛事ID',
  `project_name` varchar(100) NOT NULL COMMENT '项目名称',
  `project_code` varchar(50) DEFAULT NULL COMMENT '项目编码',
  `category` varchar(50) DEFAULT NULL COMMENT '组别(男子组/女子组)',
  `type` int DEFAULT 1 COMMENT '类型(1-个人,2-双人,3-集体)',
  `min_participants` int DEFAULT 1 COMMENT '最少参赛人数',
  `max_participants` int DEFAULT 1 COMMENT '最多参赛人数',
  `estimated_duration` int DEFAULT 5 COMMENT '预估时长(分钟)',
  `price` decimal(10,2) DEFAULT 0.00 COMMENT '报名费用',
  `description` varchar(500) DEFAULT NULL COMMENT '项目描述',
  `sort_order` int DEFAULT 0 COMMENT '排序',
  `status` int DEFAULT 1 COMMENT '状态(0-禁用,1-启用)',
  `create_user` bigint DEFAULT NULL,
  `create_dept` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  KEY `idx_competition` (`competition_id`),
  KEY `idx_tenant` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='比赛项目表';

-- 场地信息表
DROP TABLE IF EXISTS `martial_venue`;
CREATE TABLE `martial_venue` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `competition_id` bigint NOT NULL COMMENT '赛事ID',
  `venue_name` varchar(100) NOT NULL COMMENT '场地名称',
  `venue_code` varchar(50) DEFAULT NULL COMMENT '场地编码',
  `location` varchar(200) DEFAULT NULL COMMENT '场地位置',
  `capacity` int DEFAULT 0 COMMENT '容纳人数',
  `facilities` varchar(500) DEFAULT NULL COMMENT '设施说明',
  `status` int DEFAULT 1 COMMENT '状态(0-禁用,1-启用)',
  `create_user` bigint DEFAULT NULL,
  `create_dept` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  KEY `idx_competition` (`competition_id`),
  KEY `idx_tenant` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='场地信息表';

-- =====================================================
-- 2. 报名订单相关表
-- =====================================================

-- 报名订单表
DROP TABLE IF EXISTS `martial_registration_order`;
CREATE TABLE `martial_registration_order` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `order_no` varchar(50) NOT NULL COMMENT '订单号',
  `competition_id` bigint NOT NULL COMMENT '赛事ID',
  `user_id` bigint DEFAULT NULL COMMENT '用户ID',
  `user_name` varchar(50) DEFAULT NULL COMMENT '用户名',
  `contact_person` varchar(50) DEFAULT NULL COMMENT '联系人',
  `contact_phone` varchar(20) DEFAULT NULL COMMENT '联系电话',
  `organization` varchar(200) DEFAULT NULL COMMENT '所属单位',
  `total_participants` int DEFAULT 0 COMMENT '参赛总人数',
  `total_amount` decimal(10,2) DEFAULT 0.00 COMMENT '订单总金额',
  `paid_amount` decimal(10,2) DEFAULT 0.00 COMMENT '已支付金额',
  `payment_method` int DEFAULT NULL COMMENT '支付方式(1-微信,2-支付宝,3-线下)',
  `payment_time` datetime DEFAULT NULL COMMENT '支付时间',
  `status` int DEFAULT 0 COMMENT '状态(0-待支付,1-已支付,2-已取消,3-已退款)',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `create_user` bigint DEFAULT NULL,
  `create_dept` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_order_no` (`order_no`),
  KEY `idx_competition` (`competition_id`),
  KEY `idx_user` (`user_id`),
  KEY `idx_tenant_status` (`tenant_id`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='报名订单表';

-- 参赛选手表
DROP TABLE IF EXISTS `martial_athlete`;
CREATE TABLE `martial_athlete` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `order_id` bigint NOT NULL COMMENT '订单ID',
  `competition_id` bigint NOT NULL COMMENT '赛事ID',
  `project_id` bigint DEFAULT NULL COMMENT '项目ID',
  `player_name` varchar(50) NOT NULL COMMENT '选手姓名',
  `player_no` varchar(50) DEFAULT NULL COMMENT '参赛编号',
  `gender` int DEFAULT 1 COMMENT '性别(1-男,2-女)',
  `age` int DEFAULT NULL COMMENT '年龄',
  `id_card` varchar(18) DEFAULT NULL COMMENT '身份证号',
  `contact_phone` varchar(20) DEFAULT NULL COMMENT '联系电话',
  `organization` varchar(200) DEFAULT NULL COMMENT '所属单位',
  `team_name` varchar(100) DEFAULT NULL COMMENT '队伍名称',
  `category` varchar(50) DEFAULT NULL COMMENT '组别',
  `order_num` int DEFAULT 0 COMMENT '出场顺序',
  `introduction` varchar(500) DEFAULT NULL COMMENT '选手简介',
  `attachments` varchar(1000) DEFAULT NULL COMMENT '附件(JSON数组)',
  `photo_url` varchar(500) DEFAULT NULL COMMENT '照片URL',
  `registration_status` int DEFAULT 0 COMMENT '报名状态(0-待确认,1-已确认,2-已取消)',
  `competition_status` int DEFAULT 0 COMMENT '比赛状态(0-待出场,1-进行中,2-已完成)',
  `total_score` decimal(10,3) DEFAULT NULL COMMENT '总分',
  `ranking` int DEFAULT NULL COMMENT '排名',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `create_user` bigint DEFAULT NULL,
  `create_dept` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  KEY `idx_order` (`order_id`),
  KEY `idx_competition` (`competition_id`),
  KEY `idx_project` (`project_id`),
  KEY `idx_player_no` (`player_no`),
  KEY `idx_tenant` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='参赛选手表';

-- =====================================================
-- 3. 赛程安排相关表
-- =====================================================

-- 活动日程表
DROP TABLE IF EXISTS `martial_activity_schedule`;
CREATE TABLE `martial_activity_schedule` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `competition_id` bigint NOT NULL COMMENT '赛事ID',
  `schedule_date` date NOT NULL COMMENT '日程日期',
  `schedule_time` time DEFAULT NULL COMMENT '日程时间',
  `event_name` varchar(200) NOT NULL COMMENT '活动项目',
  `venue` varchar(200) DEFAULT NULL COMMENT '地点',
  `description` varchar(500) DEFAULT NULL COMMENT '描述',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `sort_order` int DEFAULT 0 COMMENT '排序',
  `status` int DEFAULT 1 COMMENT '状态(0-未开始,1-进行中,2-已完成)',
  `create_user` bigint DEFAULT NULL,
  `create_dept` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  KEY `idx_competition` (`competition_id`),
  KEY `idx_date` (`schedule_date`),
  KEY `idx_tenant` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='活动日程表';

-- 赛程编排表(竞赛分组)
DROP TABLE IF EXISTS `martial_schedule`;
CREATE TABLE `martial_schedule` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `competition_id` bigint NOT NULL COMMENT '赛事ID',
  `order_id` bigint DEFAULT NULL COMMENT '订单ID',
  `group_title` varchar(200) NOT NULL COMMENT '分组标题',
  `group_code` varchar(50) DEFAULT NULL COMMENT '分组编码',
  `group_type` int DEFAULT 1 COMMENT '分组类型(1-个人,2-双人,3-集体)',
  `venue_id` bigint DEFAULT NULL COMMENT '场地ID',
  `project_id` bigint DEFAULT NULL COMMENT '项目ID',
  `schedule_date` date DEFAULT NULL COMMENT '比赛日期',
  `time_slot` varchar(50) DEFAULT NULL COMMENT '时间段',
  `start_time` datetime DEFAULT NULL COMMENT '开始时间',
  `end_time` datetime DEFAULT NULL COMMENT '结束时间',
  `participant_count` int DEFAULT 0 COMMENT '参赛队伍数/人数',
  `estimated_duration` int DEFAULT 0 COMMENT '预估时长(分钟)',
  `is_confirmed` int DEFAULT 0 COMMENT '是否已确认(0-未确认,1-已确认)',
  `status` int DEFAULT 0 COMMENT '状态(0-待开始,1-进行中,2-已完成)',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `create_user` bigint DEFAULT NULL,
  `create_dept` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  KEY `idx_competition` (`competition_id`),
  KEY `idx_venue` (`venue_id`),
  KEY `idx_date_time` (`schedule_date`,`time_slot`),
  KEY `idx_tenant` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='赛程编排表';

-- 选手赛程关联表
DROP TABLE IF EXISTS `martial_schedule_athlete`;
CREATE TABLE `martial_schedule_athlete` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `schedule_id` bigint NOT NULL COMMENT '赛程ID',
  `athlete_id` bigint NOT NULL COMMENT '选手ID',
  `competition_id` bigint NOT NULL COMMENT '赛事ID',
  `order_num` int DEFAULT 0 COMMENT '出场顺序',
  `is_completed` int DEFAULT 0 COMMENT '是否已完赛(0-未完赛,1-已完赛)',
  `is_refereed` int DEFAULT 0 COMMENT '是否已裁判(0-未裁判,1-已裁判)',
  `create_user` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  KEY `idx_schedule` (`schedule_id`),
  KEY `idx_athlete` (`athlete_id`),
  KEY `idx_competition` (`competition_id`),
  KEY `idx_tenant` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='选手赛程关联表';

-- =====================================================
-- 4. 裁判评分相关表
-- =====================================================

-- 裁判信息表
DROP TABLE IF EXISTS `martial_judge`;
CREATE TABLE `martial_judge` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `name` varchar(50) NOT NULL COMMENT '裁判姓名',
  `gender` int DEFAULT 1 COMMENT '性别(1-男,2-女)',
  `phone` varchar(20) DEFAULT NULL COMMENT '手机号',
  `id_card` varchar(18) DEFAULT NULL COMMENT '身份证号',
  `referee_type` int DEFAULT 2 COMMENT '裁判类型(1-裁判长,2-普通裁判)',
  `level` varchar(50) DEFAULT NULL COMMENT '等级/职称',
  `specialty` varchar(200) DEFAULT NULL COMMENT '擅长项目',
  `photo_url` varchar(500) DEFAULT NULL COMMENT '照片URL',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `status` int DEFAULT 1 COMMENT '状态(0-禁用,1-启用)',
  `create_user` bigint DEFAULT NULL,
  `create_dept` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  KEY `idx_phone` (`phone`),
  KEY `idx_type` (`referee_type`),
  KEY `idx_tenant_status` (`tenant_id`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='裁判信息表';

-- 裁判邀请码表
DROP TABLE IF EXISTS `martial_judge_invite`;
CREATE TABLE `martial_judge_invite` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `competition_id` bigint NOT NULL COMMENT '赛事ID',
  `judge_id` bigint DEFAULT NULL COMMENT '裁判ID',
  `invite_code` varchar(50) NOT NULL COMMENT '邀请码',
  `role` varchar(20) NOT NULL COMMENT '角色(judge-普通裁判,chief_judge-裁判长)',
  `venue_id` bigint DEFAULT NULL COMMENT '分配场地ID',
  `projects` varchar(500) DEFAULT NULL COMMENT '分配项目(JSON数组)',
  `expire_time` datetime DEFAULT NULL COMMENT '过期时间',
  `is_used` int DEFAULT 0 COMMENT '是否已使用(0-未使用,1-已使用)',
  `use_time` datetime DEFAULT NULL COMMENT '使用时间',
  `status` int DEFAULT 1 COMMENT '状态(0-禁用,1-启用)',
  `create_user` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_competition_code` (`competition_id`,`invite_code`),
  KEY `idx_judge` (`judge_id`),
  KEY `idx_tenant` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='裁判邀请码表';

-- 扣分项配置表
DROP TABLE IF EXISTS `martial_deduction_item`;
CREATE TABLE `martial_deduction_item` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `item_name` varchar(200) NOT NULL COMMENT '扣分项名称',
  `item_code` varchar(50) DEFAULT NULL COMMENT '扣分项编码',
  `deduction_point` decimal(10,3) DEFAULT 0.000 COMMENT '扣分值',
  `category` varchar(50) DEFAULT NULL COMMENT '分类',
  `applicable_projects` varchar(500) DEFAULT NULL COMMENT '适用项目(JSON数组)',
  `description` varchar(500) DEFAULT NULL COMMENT '描述',
  `sort_order` int DEFAULT 0 COMMENT '排序',
  `status` int DEFAULT 1 COMMENT '状态(0-禁用,1-启用)',
  `create_user` bigint DEFAULT NULL,
  `create_dept` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  KEY `idx_tenant_status` (`tenant_id`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='扣分项配置表';

-- 评分记录表
DROP TABLE IF EXISTS `martial_score`;
CREATE TABLE `martial_score` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `competition_id` bigint NOT NULL COMMENT '赛事ID',
  `athlete_id` bigint NOT NULL COMMENT '选手ID',
  `project_id` bigint DEFAULT NULL COMMENT '项目ID',
  `schedule_id` bigint DEFAULT NULL COMMENT '赛程ID',
  `judge_id` bigint NOT NULL COMMENT '裁判ID',
  `judge_name` varchar(50) DEFAULT NULL COMMENT '裁判姓名',
  `score` decimal(10,3) NOT NULL COMMENT '评分(5.000-10.000)',
  `deduction_items` varchar(500) DEFAULT NULL COMMENT '选中的扣分项ID(JSON数组)',
  `note` varchar(500) DEFAULT NULL COMMENT '评分备注',
  `score_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '评分时间',
  `status` int DEFAULT 1 COMMENT '状态(1-正常,2-已修改,3-已作废)',
  `create_user` bigint DEFAULT NULL,
  `create_dept` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  KEY `idx_competition` (`competition_id`),
  KEY `idx_athlete` (`athlete_id`),
  KEY `idx_judge` (`judge_id`),
  KEY `idx_tenant` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='评分记录表';

-- 成绩表(总分汇总)
DROP TABLE IF EXISTS `martial_result`;
CREATE TABLE `martial_result` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `competition_id` bigint NOT NULL COMMENT '赛事ID',
  `athlete_id` bigint NOT NULL COMMENT '选手ID',
  `project_id` bigint DEFAULT NULL COMMENT '项目ID',
  `venue_id` bigint DEFAULT NULL COMMENT '场地ID',
  `player_name` varchar(50) DEFAULT NULL COMMENT '选手姓名',
  `team_name` varchar(100) DEFAULT NULL COMMENT '队伍名称',
  `total_score` decimal(10,3) DEFAULT NULL COMMENT '总分(所有裁判平均分)',
  `original_score` decimal(10,3) DEFAULT NULL COMMENT '原始总分',
  `adjusted_score` decimal(10,3) DEFAULT NULL COMMENT '调整后总分',
  `adjust_range` decimal(10,3) DEFAULT 0.005 COMMENT '允许调整范围',
  `adjust_note` varchar(500) DEFAULT NULL COMMENT '调整说明',
  `ranking` int DEFAULT NULL COMMENT '排名',
  `medal` int DEFAULT NULL COMMENT '奖牌(1-金牌,2-银牌,3-铜牌)',
  `is_final` int DEFAULT 0 COMMENT '是否最终成绩(0-否,1-是)',
  `publish_time` datetime DEFAULT NULL COMMENT '发布时间',
  `create_user` bigint DEFAULT NULL,
  `create_dept` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_competition_athlete` (`competition_id`,`athlete_id`,`project_id`),
  KEY `idx_athlete` (`athlete_id`),
  KEY `idx_project` (`project_id`),
  KEY `idx_ranking` (`ranking`),
  KEY `idx_tenant` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='成绩表';

-- =====================================================
-- 5. 信息发布相关表
-- =====================================================

-- 信息发布表
DROP TABLE IF EXISTS `martial_info_publish`;
CREATE TABLE `martial_info_publish` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `competition_id` bigint DEFAULT NULL COMMENT '赛事ID(NULL表示全局)',
  `title` varchar(200) NOT NULL COMMENT '标题',
  `info_type` int DEFAULT 1 COMMENT '信息类型(1-通知,2-公告,3-重要)',
  `content` text COMMENT '内容',
  `images` varchar(1000) DEFAULT NULL COMMENT '图片(JSON数组)',
  `publish_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '发布时间',
  `is_published` int DEFAULT 0 COMMENT '是否已发布(0-未发布,1-已发布)',
  `sort_order` int DEFAULT 0 COMMENT '排序',
  `status` int DEFAULT 1 COMMENT '状态(0-禁用,1-启用)',
  `create_user` bigint DEFAULT NULL,
  `create_dept` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  KEY `idx_competition` (`competition_id`),
  KEY `idx_type` (`info_type`),
  KEY `idx_tenant_status` (`tenant_id`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='信息发布表';

-- 比赛实况表
DROP TABLE IF EXISTS `martial_live_update`;
CREATE TABLE `martial_live_update` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `competition_id` bigint NOT NULL COMMENT '赛事ID',
  `schedule_id` bigint DEFAULT NULL COMMENT '赛程ID',
  `athlete_id` bigint DEFAULT NULL COMMENT '选手ID',
  `update_type` int DEFAULT 1 COMMENT '实况类型(1-赛况,2-比分,3-精彩瞬间)',
  `title` varchar(200) DEFAULT NULL COMMENT '标题',
  `content` varchar(1000) DEFAULT NULL COMMENT '内容',
  `images` varchar(1000) DEFAULT NULL COMMENT '图片(JSON数组)',
  `score_info` varchar(200) DEFAULT NULL COMMENT '比分信息',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
  `sort_order` int DEFAULT 0 COMMENT '排序',
  `create_user` bigint DEFAULT NULL,
  `create_dept` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  KEY `idx_competition` (`competition_id`),
  KEY `idx_schedule` (`schedule_id`),
  KEY `idx_update_time` (`update_time`),
  KEY `idx_tenant` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='比赛实况表';

-- 轮播图表
DROP TABLE IF EXISTS `martial_banner`;
CREATE TABLE `martial_banner` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `title` varchar(200) NOT NULL COMMENT '轮播图标题',
  `image_url` varchar(500) NOT NULL COMMENT '轮播图图片URL',
  `link_url` varchar(500) DEFAULT NULL COMMENT '跳转链接',
  `sort_order` int DEFAULT 0 COMMENT '排序顺序',
  `status` int DEFAULT 1 COMMENT '状态(0-禁用,1-启用)',
  `create_user` bigint DEFAULT NULL,
  `create_dept` bigint DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_user` bigint DEFAULT NULL,
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` int DEFAULT 0,
  `tenant_id` varchar(12) DEFAULT '000000',
  PRIMARY KEY (`id`),
  KEY `idx_sort` (`sort_order`),
  KEY `idx_tenant_status` (`tenant_id`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='轮播图表';

-- =====================================================
-- 6. 统计分析相关视图
-- =====================================================

-- 参赛人数统计视图
CREATE OR REPLACE VIEW v_martial_participant_stats AS
SELECT
  a.competition_id,
  a.organization AS school_unit,
  a.category,
  COUNT(CASE WHEN p.type = 1 THEN 1 END) AS individual_count,
  COUNT(CASE WHEN p.type = 2 THEN 1 END) AS dual_count,
  COUNT(CASE WHEN p.type = 3 THEN 1 END) AS team_count,
  COUNT(CASE WHEN a.gender = 2 THEN 1 END) AS female_count,
  COUNT(*) AS total_count
FROM martial_athlete a
LEFT JOIN martial_project p ON a.project_id = p.id
WHERE a.is_deleted = 0
GROUP BY a.competition_id, a.organization, a.category;

-- 项目时间统计视图
CREATE OR REPLACE VIEW v_martial_project_time_stats AS
SELECT
  p.competition_id,
  p.project_name,
  a.category AS participant_category,
  COUNT(DISTINCT a.team_name) AS team_count,
  COUNT(*) AS participant_count,
  p.estimated_duration,
  (COUNT(DISTINCT a.team_name) * p.estimated_duration) AS total_estimated_duration
FROM martial_project p
LEFT JOIN martial_athlete a ON p.id = a.project_id AND a.is_deleted = 0
WHERE p.is_deleted = 0
GROUP BY p.competition_id, p.id, p.project_name, a.category;

-- 金额统计视图
CREATE OR REPLACE VIEW v_martial_amount_stats AS
SELECT
  o.competition_id,
  a.organization AS school_unit,
  COUNT(DISTINCT a.project_id) AS project_count,
  SUM(p.price) AS total_amount
FROM martial_registration_order o
INNER JOIN martial_athlete a ON o.id = a.order_id AND a.is_deleted = 0
LEFT JOIN martial_project p ON a.project_id = p.id AND p.is_deleted = 0
WHERE o.is_deleted = 0 AND o.status = 1
GROUP BY o.competition_id, a.organization;

-- =====================================================
-- 7. 初始化基础数据
-- =====================================================

-- 插入默认扣分项
INSERT INTO `martial_deduction_item` (`id`, `item_name`, `deduction_point`, `category`, `sort_order`, `tenant_id`) VALUES
(1, '动作不规范', 0.100, '技术动作', 1, '000000'),
(2, '节奏失误', 0.050, '技术动作', 2, '000000'),
(3, '器械掉落', 0.200, '器械使用', 3, '000000'),
(4, '出界', 0.100, '场地规则', 4, '000000'),
(5, '超时', 0.100, '时间规则', 5, '000000'),
(6, '服装不符', 0.050, '着装要求', 6, '000000'),
(7, '礼仪不当', 0.050, '行为规范', 7, '000000'),
(8, '其他违规', 0.100, '其他', 8, '000000');

SET FOREIGN_KEY_CHECKS = 1;

-- =====================================================
-- 索引优化说明
-- =====================================================
-- 1. 所有表都添加了 tenant_id 索引,支持多租户查询
-- 2. 外键字段都添加了索引,提高关联查询性能
-- 3. 常用查询字段添加了组合索引
-- 4. 时间字段添加了索引,支持按时间范围查询
-- 5. 状态字段添加了组合索引,支持状态过滤

-- =====================================================
-- 数据完整性说明
-- =====================================================
-- 1. 所有主表都包含 BladeX 标准字段(create_user, create_dept, create_time, update_user, update_time, is_deleted, tenant_id)
-- 2. 支持逻辑删除(is_deleted)
-- 3. 支持多租户(tenant_id)
-- 4. 所有decimal字段使用3位小数精度,满足评分需求
-- 5. 使用datetime类型存储时间,支持精确到秒的时间记录
