-- 项目模块字段补齐（对齐产品文档）并补充字段注释

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'project' AND COLUMN_NAME = 'sub_project_name');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN sub_project_name VARCHAR(128) NULL AFTER project_name', '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 = 'project' AND COLUMN_NAME = 'start_date');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN start_date DATE NULL AFTER sub_project_name', '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 = 'project' AND COLUMN_NAME = 'end_date');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN end_date DATE NULL AFTER start_date', '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 = 'project' AND COLUMN_NAME = 'payment_status');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN payment_status VARCHAR(32) NOT NULL DEFAULT ''WAIT_SUBMIT'' AFTER risk_flags_json', '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 = 'project' AND COLUMN_NAME = 'labor_fee_ratio');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN labor_fee_ratio DECIMAL(8,6) NOT NULL DEFAULT 0.000000 AFTER payment_status', '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 = 'project' AND COLUMN_NAME = 'key_change_log_json');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN key_change_log_json TEXT NULL AFTER labor_fee_ratio', '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 = 'project' AND INDEX_NAME = 'idx_tenant_payment_status');
SET @sql := IF(@idx = 0, 'ALTER TABLE project ADD KEY idx_tenant_payment_status (tenant_id, payment_status)', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 字段注释（统一补齐）
ALTER TABLE project
    MODIFY COLUMN project_name VARCHAR(128) NOT NULL COMMENT '项目名称',
    MODIFY COLUMN sub_project_name VARCHAR(128) NULL COMMENT '子项目名称',
    MODIFY COLUMN enterprise_id BIGINT UNSIGNED NULL COMMENT '合作企业ID（历史兼容）',
    MODIFY COLUMN host_enterprise_id BIGINT UNSIGNED NULL COMMENT '主办单位ID',
    MODIFY COLUMN partner_enterprise_id BIGINT UNSIGNED NULL COMMENT '合作企业ID',
    MODIFY COLUMN host_owner_user_id BIGINT UNSIGNED NULL COMMENT '主办单位负责人用户ID',
    MODIFY COLUMN host_executor_user_id BIGINT UNSIGNED NULL COMMENT '主办单位项目执行人用户ID',
    MODIFY COLUMN partner_owner_user_id BIGINT UNSIGNED NULL COMMENT '合作企业负责人用户ID',
    MODIFY COLUMN partner_executor_user_id BIGINT UNSIGNED NULL COMMENT '合作企业项目执行人用户ID',
    MODIFY COLUMN start_date DATE NULL COMMENT '项目开始日期',
    MODIFY COLUMN end_date DATE NULL COMMENT '项目结束日期',
    MODIFY COLUMN budget_cent BIGINT NOT NULL COMMENT '项目总预算（分）',
    MODIFY COLUMN meeting_total INT NOT NULL COMMENT '会议总期数',
    MODIFY COLUMN meeting_completed_count INT NOT NULL DEFAULT 0 COMMENT '已完成核销期数',
    MODIFY COLUMN allow_meeting_over_budget TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否允许单场超支',
    MODIFY COLUMN over_budget_threshold_ratio DECIMAL(8,6) NOT NULL DEFAULT 0.100000 COMMENT '超支阈值比例',
    MODIFY COLUMN over_budget_approval_chain_json TEXT NULL COMMENT '超支审批链（JSON）',
    MODIFY COLUMN budget_execution_ratio DECIMAL(8,6) NOT NULL DEFAULT 0.000000 COMMENT '预算执行率',
    MODIFY COLUMN risk_flags_json TEXT NULL COMMENT '风险标记（JSON）',
    MODIFY COLUMN payment_status VARCHAR(32) NOT NULL DEFAULT 'WAIT_SUBMIT' COMMENT '支付状态',
    MODIFY COLUMN labor_fee_ratio DECIMAL(8,6) NOT NULL DEFAULT 0.000000 COMMENT '劳务费用占比',
    MODIFY COLUMN key_change_log_json TEXT NULL COMMENT '关键变更日志（JSON）',
    MODIFY COLUMN status VARCHAR(32) NOT NULL COMMENT '项目状态';
