-- 合并账户库双表：将 ccdi_account_result 分析字段并回 ccdi_account_info，并删除旧表。
-- 执行说明：涉及中文内容时请使用 bin/mysql_utf8_exec.sh 执行，确保会话字符集为 utf8mb4。

DELIMITER //

DROP PROCEDURE IF EXISTS `merge_ccdi_account_result_into_info`//
CREATE PROCEDURE `merge_ccdi_account_result_into_info`()
BEGIN
  DECLARE duplicate_count INT DEFAULT 0;
  DECLARE result_table_exists INT DEFAULT 0;

  SELECT COUNT(*)
    INTO duplicate_count
  FROM (
    SELECT account_no
    FROM ccdi_account_info
    WHERE account_no IS NOT NULL
      AND account_no <> ''
    GROUP BY account_no
    HAVING COUNT(*) > 1
  ) duplicated_accounts;

  IF duplicate_count > 0 THEN
    SIGNAL SQLSTATE '45000'
      SET MESSAGE_TEXT = 'ccdi_account_info.account_no 存在重复数据，禁止执行账户库合表迁移。';
  END IF;

  IF NOT EXISTS (
      SELECT 1
      FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'ccdi_account_info'
        AND column_name = 'is_self_account'
  ) THEN
    ALTER TABLE `ccdi_account_info`
      ADD COLUMN `is_self_account` TINYINT NOT NULL DEFAULT 1 COMMENT '是否本人账户' AFTER `currency`;
  END IF;

  IF NOT EXISTS (
      SELECT 1
      FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'ccdi_account_info'
        AND column_name = 'monthly_avg_trans_count'
  ) THEN
    ALTER TABLE `ccdi_account_info`
      ADD COLUMN `monthly_avg_trans_count` INT DEFAULT NULL COMMENT '月均交易笔数' AFTER `is_self_account`;
  END IF;

  IF NOT EXISTS (
      SELECT 1
      FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'ccdi_account_info'
        AND column_name = 'monthly_avg_trans_amount'
  ) THEN
    ALTER TABLE `ccdi_account_info`
      ADD COLUMN `monthly_avg_trans_amount` DECIMAL(18, 2) DEFAULT NULL COMMENT '月均交易金额' AFTER `monthly_avg_trans_count`;
  END IF;

  IF NOT EXISTS (
      SELECT 1
      FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'ccdi_account_info'
        AND column_name = 'trans_freq_type'
  ) THEN
    ALTER TABLE `ccdi_account_info`
      ADD COLUMN `trans_freq_type` VARCHAR(32) DEFAULT NULL COMMENT '交易频率类型' AFTER `monthly_avg_trans_amount`;
  END IF;

  IF NOT EXISTS (
      SELECT 1
      FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'ccdi_account_info'
        AND column_name = 'dr_max_single_amount'
  ) THEN
    ALTER TABLE `ccdi_account_info`
      ADD COLUMN `dr_max_single_amount` DECIMAL(18, 2) DEFAULT NULL COMMENT '最大单笔支出金额' AFTER `trans_freq_type`;
  END IF;

  IF NOT EXISTS (
      SELECT 1
      FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'ccdi_account_info'
        AND column_name = 'cr_max_single_amount'
  ) THEN
    ALTER TABLE `ccdi_account_info`
      ADD COLUMN `cr_max_single_amount` DECIMAL(18, 2) DEFAULT NULL COMMENT '最大单笔收入金额' AFTER `dr_max_single_amount`;
  END IF;

  IF NOT EXISTS (
      SELECT 1
      FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'ccdi_account_info'
        AND column_name = 'dr_max_daily_amount'
  ) THEN
    ALTER TABLE `ccdi_account_info`
      ADD COLUMN `dr_max_daily_amount` DECIMAL(18, 2) DEFAULT NULL COMMENT '最大单日支出金额' AFTER `cr_max_single_amount`;
  END IF;

  IF NOT EXISTS (
      SELECT 1
      FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'ccdi_account_info'
        AND column_name = 'cr_max_daily_amount'
  ) THEN
    ALTER TABLE `ccdi_account_info`
      ADD COLUMN `cr_max_daily_amount` DECIMAL(18, 2) DEFAULT NULL COMMENT '最大单日收入金额' AFTER `dr_max_daily_amount`;
  END IF;

  IF NOT EXISTS (
      SELECT 1
      FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'ccdi_account_info'
        AND column_name = 'trans_risk_level'
  ) THEN
    ALTER TABLE `ccdi_account_info`
      ADD COLUMN `trans_risk_level` VARCHAR(32) DEFAULT NULL COMMENT '交易风险等级' AFTER `cr_max_daily_amount`;
  END IF;

  SELECT COUNT(*)
    INTO result_table_exists
  FROM information_schema.tables
  WHERE table_schema = DATABASE()
    AND table_name = 'ccdi_account_result';

  IF result_table_exists > 0 THEN
    UPDATE `ccdi_account_info` ai
    JOIN `ccdi_account_result` ar
      ON ai.account_no = ar.account_no
    SET ai.is_self_account = ar.is_self_account,
        ai.monthly_avg_trans_count = ar.monthly_avg_trans_count,
        ai.monthly_avg_trans_amount = ar.monthly_avg_trans_amount,
        ai.trans_freq_type = ar.trans_freq_type,
        ai.dr_max_single_amount = ar.dr_max_single_amount,
        ai.cr_max_single_amount = ar.cr_max_single_amount,
        ai.dr_max_daily_amount = ar.dr_max_daily_amount,
        ai.cr_max_daily_amount = ar.cr_max_daily_amount,
        ai.trans_risk_level = ar.trans_risk_level,
        ai.update_by = COALESCE(ar.update_by, ai.update_by),
        ai.update_time = COALESCE(ar.update_time, ai.update_time);

    DROP TABLE `ccdi_account_result`;
  END IF;
END//

CALL `merge_ccdi_account_result_into_info`()//
DROP PROCEDURE IF EXISTS `merge_ccdi_account_result_into_info`//

DELIMITER ;
