-- 项目模块二期字段补齐（核销状态/进度、审批人、备份执行人、中止冻结归档等）

SET @c := (SELECT COUNT(1) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'project' AND COLUMN_NAME = 'write_off_status');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN write_off_status VARCHAR(32) NOT NULL DEFAULT ''WAITING'' 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 = 'write_off_not_started_count');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN write_off_not_started_count INT NOT NULL DEFAULT 0 AFTER write_off_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 = 'write_off_in_progress_count');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN write_off_in_progress_count INT NOT NULL DEFAULT 0 AFTER write_off_not_started_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 = 'project' AND COLUMN_NAME = 'write_off_completed_count');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN write_off_completed_count INT NOT NULL DEFAULT 0 AFTER write_off_in_progress_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 = 'project' AND COLUMN_NAME = 'allow_project_over_budget');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN allow_project_over_budget TINYINT(1) NOT NULL DEFAULT 0 AFTER labor_fee_ratio', '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 = 'invoice_info');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN invoice_info TEXT NULL AFTER allow_project_over_budget', '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 = 'expense_ratio_json');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN expense_ratio_json TEXT NULL AFTER invoice_info', '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 = 'host_backup_executor_user_id');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN host_backup_executor_user_id BIGINT UNSIGNED NULL AFTER host_executor_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 = 'project' AND COLUMN_NAME = 'partner_backup_executor_user_id');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN partner_backup_executor_user_id BIGINT UNSIGNED NULL AFTER partner_executor_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 = 'project' AND COLUMN_NAME = 'project_owner_approver_user_id');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN project_owner_approver_user_id BIGINT UNSIGNED NULL AFTER over_budget_approval_chain_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 = 'finance_approver_user_id');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN finance_approver_user_id BIGINT UNSIGNED NULL AFTER project_owner_approver_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 = 'project' AND COLUMN_NAME = 'terminated_reason');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN terminated_reason VARCHAR(500) NULL AFTER 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 = 'freeze_reason');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN freeze_reason VARCHAR(500) NULL AFTER terminated_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 = 'project' AND COLUMN_NAME = 'archived_at');
SET @sql := IF(@c = 0, 'ALTER TABLE project ADD COLUMN archived_at DATETIME NULL AFTER freeze_reason', '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_write_off_status');
SET @sql := IF(@idx = 0, 'ALTER TABLE project ADD KEY idx_tenant_write_off_status (tenant_id, write_off_status)', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 字段注释
ALTER TABLE project
    MODIFY COLUMN write_off_status VARCHAR(32) NOT NULL DEFAULT 'WAITING' COMMENT '核销状态',
    MODIFY COLUMN write_off_not_started_count INT NOT NULL DEFAULT 0 COMMENT '核销进度-未开始场次',
    MODIFY COLUMN write_off_in_progress_count INT NOT NULL DEFAULT 0 COMMENT '核销进度-核销中场次',
    MODIFY COLUMN write_off_completed_count INT NOT NULL DEFAULT 0 COMMENT '核销进度-核销完成场次',
    MODIFY COLUMN allow_project_over_budget TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否允许超过项目总费用',
    MODIFY COLUMN invoice_info TEXT NULL COMMENT '发票信息快照（用于一键复制）',
    MODIFY COLUMN expense_ratio_json TEXT NULL COMMENT '费用占比（JSON）',
    MODIFY COLUMN host_backup_executor_user_id BIGINT UNSIGNED NULL COMMENT '主办单位备份执行人用户ID',
    MODIFY COLUMN partner_backup_executor_user_id BIGINT UNSIGNED NULL COMMENT '合作企业备份执行人用户ID',
    MODIFY COLUMN project_owner_approver_user_id BIGINT UNSIGNED NULL COMMENT '项目负责人审批人用户ID',
    MODIFY COLUMN finance_approver_user_id BIGINT UNSIGNED NULL COMMENT '财务审批人用户ID',
    MODIFY COLUMN terminated_reason VARCHAR(500) NULL COMMENT '项目中止原因',
    MODIFY COLUMN freeze_reason VARCHAR(500) NULL COMMENT '项目冻结原因',
    MODIFY COLUMN archived_at DATETIME NULL COMMENT '归档时间';
