-- 会议模块字段属性补齐：流程追溯、状态元数据、并发锁字段 + 字段注释统一

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'meeting' AND COLUMN_NAME = 'current_auditor_user_id');
SET @sql := IF(@c = 0, 'ALTER TABLE meeting ADD COLUMN current_auditor_user_id BIGINT UNSIGNED NULL AFTER freeze_reason', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'meeting' AND COLUMN_NAME = 'node_deadline_at');
SET @sql := IF(@c = 0, 'ALTER TABLE meeting ADD COLUMN node_deadline_at DATETIME NULL AFTER current_auditor_user_id', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'meeting' AND COLUMN_NAME = 'reject_count');
SET @sql := IF(@c = 0, 'ALTER TABLE meeting ADD COLUMN reject_count INT NOT NULL DEFAULT 0 AFTER node_deadline_at', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'meeting' AND COLUMN_NAME = 'last_action_at');
SET @sql := IF(@c = 0, 'ALTER TABLE meeting ADD COLUMN last_action_at DATETIME NULL AFTER reject_count', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'meeting' AND COLUMN_NAME = 'status_changed_at');
SET @sql := IF(@c = 0, 'ALTER TABLE meeting ADD COLUMN status_changed_at DATETIME NULL AFTER last_action_at', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'meeting' AND COLUMN_NAME = 'status_changed_by');
SET @sql := IF(@c = 0, 'ALTER TABLE meeting ADD COLUMN status_changed_by BIGINT UNSIGNED NULL AFTER status_changed_at', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'meeting' AND COLUMN_NAME = 'cancel_reason');
SET @sql := IF(@c = 0, 'ALTER TABLE meeting ADD COLUMN cancel_reason VARCHAR(500) NULL AFTER status_changed_by', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'meeting' AND COLUMN_NAME = 'postpone_reason');
SET @sql := IF(@c = 0, 'ALTER TABLE meeting ADD COLUMN postpone_reason VARCHAR(500) NULL AFTER cancel_reason', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'meeting' AND COLUMN_NAME = 'withdraw_reason');
SET @sql := IF(@c = 0, 'ALTER TABLE meeting ADD COLUMN withdraw_reason VARCHAR(500) NULL AFTER postpone_reason', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'meeting' AND COLUMN_NAME = 'lock_version');
SET @sql := IF(@c = 0, 'ALTER TABLE meeting ADD COLUMN lock_version INT NOT NULL DEFAULT 0 AFTER withdraw_reason', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'meeting' AND COLUMN_NAME = 'lock_at');
SET @sql := IF(@c = 0, 'ALTER TABLE meeting ADD COLUMN lock_at DATETIME NULL AFTER lock_version', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'meeting' AND COLUMN_NAME = 'locked_by');
SET @sql := IF(@c = 0, 'ALTER TABLE meeting ADD COLUMN locked_by BIGINT UNSIGNED NULL AFTER lock_at', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 查询优化：当前审核人和流程追溯字段索引
SET @idx := (SELECT COUNT(1) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'meeting' AND INDEX_NAME = 'idx_tenant_auditor_node');
SET @sql := IF(@idx = 0, 'ALTER TABLE meeting ADD KEY idx_tenant_auditor_node (tenant_id, current_auditor_user_id, current_audit_node)', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @idx := (SELECT COUNT(1) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'meeting' AND INDEX_NAME = 'idx_tenant_last_action');
SET @sql := IF(@idx = 0, 'ALTER TABLE meeting ADD KEY idx_tenant_last_action (tenant_id, last_action_at)', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 会议表字段注释统一补齐
ALTER TABLE meeting
    MODIFY COLUMN project_id BIGINT UNSIGNED NOT NULL COMMENT '所属项目ID',
    MODIFY COLUMN topic VARCHAR(256) NOT NULL COMMENT '会议主题',
    MODIFY COLUMN sub_project_name VARCHAR(128) NULL COMMENT '子项目名称',
    MODIFY COLUMN meeting_category VARCHAR(64) NULL COMMENT '会议类别',
    MODIFY COLUMN meeting_form VARCHAR(64) NULL COMMENT '会议形式',
    MODIFY COLUMN location VARCHAR(255) NULL COMMENT '会议地点',
    MODIFY COLUMN start_time DATETIME NULL COMMENT '会议开始时间',
    MODIFY COLUMN end_time DATETIME NULL COMMENT '会议结束时间',
    MODIFY COLUMN budget_cent BIGINT NOT NULL COMMENT '会议预算（分）',
    MODIFY COLUMN labor_ratio DECIMAL(8,6) NOT NULL DEFAULT 0.000000 COMMENT '劳务费用占比（0~1）',
    MODIFY COLUMN catering_ratio DECIMAL(8,6) NOT NULL DEFAULT 0.000000 COMMENT '餐费占比（0~1）',
    MODIFY COLUMN meeting_status VARCHAR(32) NOT NULL COMMENT '会议状态',
    MODIFY COLUMN audit_status VARCHAR(32) NOT NULL COMMENT '会议审核状态',
    MODIFY COLUMN current_audit_node VARCHAR(64) NULL COMMENT '当前审核节点',
    MODIFY COLUMN last_submit_at DATETIME NULL COMMENT '最后提交时间',
    MODIFY COLUMN last_reject_reason VARCHAR(500) NULL COMMENT '最后驳回原因摘要',
    MODIFY COLUMN overdue_days INT NOT NULL DEFAULT 0 COMMENT '逾期天数',
    MODIFY COLUMN risk_flags_json TEXT NULL COMMENT '风险标记JSON',
    MODIFY COLUMN is_frozen TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否冻结',
    MODIFY COLUMN freeze_reason VARCHAR(500) NULL COMMENT '冻结原因',
    MODIFY COLUMN current_auditor_user_id BIGINT UNSIGNED NULL COMMENT '当前审核人用户ID',
    MODIFY COLUMN node_deadline_at DATETIME NULL COMMENT '当前节点SLA截止时间',
    MODIFY COLUMN reject_count INT NOT NULL DEFAULT 0 COMMENT '累计驳回次数',
    MODIFY COLUMN last_action_at DATETIME NULL COMMENT '最后一次流程动作时间',
    MODIFY COLUMN status_changed_at DATETIME NULL COMMENT '状态最近变更时间',
    MODIFY COLUMN status_changed_by BIGINT UNSIGNED NULL COMMENT '状态最近变更人用户ID',
    MODIFY COLUMN cancel_reason VARCHAR(500) NULL COMMENT '取消原因',
    MODIFY COLUMN postpone_reason VARCHAR(500) NULL COMMENT '延期原因',
    MODIFY COLUMN withdraw_reason VARCHAR(500) NULL COMMENT '撤回原因',
    MODIFY COLUMN lock_version INT NOT NULL DEFAULT 0 COMMENT '字段锁版本号',
    MODIFY COLUMN lock_at DATETIME NULL COMMENT '字段锁定时间',
    MODIFY COLUMN locked_by BIGINT UNSIGNED NULL COMMENT '字段锁定操作人用户ID';
