-- ============================================================
-- 资金流图谱已建表环境补充脚本
-- 说明：
-- 1. 本脚本用于目标库已存在 lx_fund_flow_* 表时补齐新增字段和索引。
-- 2. 不删除、不重建已有图谱表，不清空 ODPS 基座或生产数据。
-- 3. 新环境初始化仍可参考 01_lx_fund_graph_mysql_ddl.sql。
-- 4. 执行前需人工确认当前库为目标库；涉及中文注释时使用 bin/mysql_utf8_exec.sh 执行。
-- ============================================================

SET @current_schema = DATABASE();

-- ============================================================
-- lx_fund_flow_subject_node 补充字段和索引
-- ============================================================
SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_subject_node' AND COLUMN_NAME = 'cinocsno') = 0,
    'ALTER TABLE lx_fund_flow_subject_node ADD COLUMN cinocsno VARCHAR(64) NULL COMMENT ''客户内部号'' AFTER name',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_subject_node' AND COLUMN_NAME = 'idno_type') = 0,
    'ALTER TABLE lx_fund_flow_subject_node ADD COLUMN idno_type VARCHAR(64) NULL COMMENT ''主体类型，如个人、企业、代理主体'' AFTER cinocsno',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_subject_node' AND COLUMN_NAME = 'staff_id') = 0,
    'ALTER TABLE lx_fund_flow_subject_node ADD COLUMN staff_id VARCHAR(64) NULL COMMENT ''员工标识'' AFTER idno_type',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_subject_node' AND COLUMN_NAME = 'source_type') = 0,
    'ALTER TABLE lx_fund_flow_subject_node ADD COLUMN source_type VARCHAR(64) NULL COMMENT ''来源类型'' AFTER staff_id',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_subject_node' AND COLUMN_NAME = 'created_time') = 0,
    'ALTER TABLE lx_fund_flow_subject_node ADD COLUMN created_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ''创建时间'' AFTER source_type',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_subject_node' AND COLUMN_NAME = 'updated_time') = 0,
    'ALTER TABLE lx_fund_flow_subject_node ADD COLUMN updated_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ''更新时间'' AFTER created_time',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_subject_node' AND INDEX_NAME = 'idx_lx_fund_flow_subject_idnocfno') = 0,
    'ALTER TABLE lx_fund_flow_subject_node ADD KEY idx_lx_fund_flow_subject_idnocfno (idnocfno)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_subject_node' AND INDEX_NAME = 'idx_lx_fund_flow_subject_name') = 0,
    'ALTER TABLE lx_fund_flow_subject_node ADD KEY idx_lx_fund_flow_subject_name (name)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_subject_node' AND INDEX_NAME = 'idx_lx_fund_flow_subject_source_type') = 0,
    'ALTER TABLE lx_fund_flow_subject_node ADD KEY idx_lx_fund_flow_subject_source_type (source_type)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ============================================================
-- lx_fund_flow_account_node 补充字段和索引
-- ============================================================
SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_account_node' AND COLUMN_NAME = 'source') = 0,
    'ALTER TABLE lx_fund_flow_account_node ADD COLUMN source VARCHAR(128) NULL COMMENT ''来源'' AFTER cinocsno',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_account_node' AND COLUMN_NAME = 'acc_type') = 0,
    'ALTER TABLE lx_fund_flow_account_node ADD COLUMN acc_type VARCHAR(64) NULL COMMENT ''账户类型，如INTERNAL/EXTERNAL'' AFTER source',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_account_node' AND COLUMN_NAME = 'acc_idno') = 0,
    'ALTER TABLE lx_fund_flow_account_node ADD COLUMN acc_idno VARCHAR(64) NULL COMMENT ''开户证件号'' AFTER acc_type',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_account_node' AND COLUMN_NAME = 'acc_status') = 0,
    'ALTER TABLE lx_fund_flow_account_node ADD COLUMN acc_status VARCHAR(64) NULL COMMENT ''账户状态'' AFTER acc_idno',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_account_node' AND COLUMN_NAME = 'acc_date') = 0,
    'ALTER TABLE lx_fund_flow_account_node ADD COLUMN acc_date VARCHAR(32) NULL COMMENT ''开户日期'' AFTER acc_status',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_account_node' AND COLUMN_NAME = 'created_time') = 0,
    'ALTER TABLE lx_fund_flow_account_node ADD COLUMN created_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ''创建时间'' AFTER acc_date',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_account_node' AND COLUMN_NAME = 'updated_time') = 0,
    'ALTER TABLE lx_fund_flow_account_node ADD COLUMN updated_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ''更新时间'' AFTER created_time',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_account_node' AND INDEX_NAME = 'idx_lx_fund_flow_account_acc_no') = 0,
    'ALTER TABLE lx_fund_flow_account_node ADD KEY idx_lx_fund_flow_account_acc_no (acc_no)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_account_node' AND INDEX_NAME = 'idx_lx_fund_flow_account_acc_name') = 0,
    'ALTER TABLE lx_fund_flow_account_node ADD KEY idx_lx_fund_flow_account_acc_name (acc_name)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_account_node' AND INDEX_NAME = 'idx_lx_fund_flow_account_acc_idno') = 0,
    'ALTER TABLE lx_fund_flow_account_node ADD KEY idx_lx_fund_flow_account_acc_idno (acc_idno)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ============================================================
-- lx_fund_flow_own_account_edge 补充字段和索引
-- ============================================================
SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_own_account_edge' AND COLUMN_NAME = 'acc_name') = 0,
    'ALTER TABLE lx_fund_flow_own_account_edge ADD COLUMN acc_name VARCHAR(255) NULL COMMENT ''账户名称'' AFTER to_key',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_own_account_edge' AND COLUMN_NAME = 'acc_no') = 0,
    'ALTER TABLE lx_fund_flow_own_account_edge ADD COLUMN acc_no VARCHAR(128) NULL COMMENT ''账号'' AFTER acc_name',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_own_account_edge' AND COLUMN_NAME = 'created_time') = 0,
    'ALTER TABLE lx_fund_flow_own_account_edge ADD COLUMN created_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ''创建时间'' AFTER acc_no',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_own_account_edge' AND COLUMN_NAME = 'updated_time') = 0,
    'ALTER TABLE lx_fund_flow_own_account_edge ADD COLUMN updated_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ''更新时间'' AFTER created_time',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_own_account_edge' AND INDEX_NAME = 'idx_lx_fund_flow_own_from_key') = 0,
    'ALTER TABLE lx_fund_flow_own_account_edge ADD KEY idx_lx_fund_flow_own_from_key (from_key)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_own_account_edge' AND INDEX_NAME = 'idx_lx_fund_flow_own_to_key') = 0,
    'ALTER TABLE lx_fund_flow_own_account_edge ADD KEY idx_lx_fund_flow_own_to_key (to_key)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_own_account_edge' AND INDEX_NAME = 'idx_lx_fund_flow_own_acc_no') = 0,
    'ALTER TABLE lx_fund_flow_own_account_edge ADD KEY idx_lx_fund_flow_own_acc_no (acc_no)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ============================================================
-- lx_fund_flow_detail_edge 补充字段和索引
-- ============================================================
SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_detail_edge' AND COLUMN_NAME = 'family_relation_type') = 0,
    'ALTER TABLE lx_fund_flow_detail_edge ADD COLUMN family_relation_type VARCHAR(64) NULL COMMENT ''家庭关系类型，如配偶、父母、子女'' AFTER to_key',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_detail_edge' AND COLUMN_NAME = 'source_table') = 0,
    'ALTER TABLE lx_fund_flow_detail_edge ADD COLUMN source_table VARCHAR(32) NULL COMMENT ''流水来源，基座可为空，增量可为CCDI_BANK_STATEMENT/FIRST/LEVEL1'' AFTER family_relation_type',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_detail_edge' AND COLUMN_NAME = 'penetrate_level') = 0,
    'ALTER TABLE lx_fund_flow_detail_edge ADD COLUMN penetrate_level INT NOT NULL DEFAULT 0 COMMENT ''穿透层级，一期为0'' AFTER source_table',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_detail_edge' AND COLUMN_NAME = 'bank_statement_id') = 0,
    'ALTER TABLE lx_fund_flow_detail_edge ADD COLUMN bank_statement_id BIGINT NULL COMMENT ''增量来源ccdi_bank_statement主键'' AFTER penetrate_level',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_detail_edge' AND COLUMN_NAME = 'bank_trx_number') = 0,
    'ALTER TABLE lx_fund_flow_detail_edge ADD COLUMN bank_trx_number VARCHAR(128) NULL COMMENT ''银行流水号/交易流水号，如来源存在则记录'' AFTER bank_statement_id',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_detail_edge' AND COLUMN_NAME = 'created_time') = 0,
    'ALTER TABLE lx_fund_flow_detail_edge ADD COLUMN created_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ''创建时间'' AFTER bank_trx_number',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_detail_edge' AND COLUMN_NAME = 'updated_time') = 0,
    'ALTER TABLE lx_fund_flow_detail_edge ADD COLUMN updated_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ''更新时间'' AFTER created_time',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_detail_edge' AND INDEX_NAME = 'idx_lx_fund_flow_detail_from_to') = 0,
    'ALTER TABLE lx_fund_flow_detail_edge ADD KEY idx_lx_fund_flow_detail_from_to (from_key, to_key)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_detail_edge' AND INDEX_NAME = 'idx_lx_fund_flow_detail_from_date') = 0,
    'ALTER TABLE lx_fund_flow_detail_edge ADD KEY idx_lx_fund_flow_detail_from_date (from_key, trx_date)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_detail_edge' AND INDEX_NAME = 'idx_lx_fund_flow_detail_to_date') = 0,
    'ALTER TABLE lx_fund_flow_detail_edge ADD KEY idx_lx_fund_flow_detail_to_date (to_key, trx_date)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_detail_edge' AND INDEX_NAME = 'idx_lx_fund_flow_detail_trx_date') = 0,
    'ALTER TABLE lx_fund_flow_detail_edge ADD KEY idx_lx_fund_flow_detail_trx_date (trx_date)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_detail_edge' AND INDEX_NAME = 'idx_lx_fund_flow_detail_statement') = 0,
    'ALTER TABLE lx_fund_flow_detail_edge ADD KEY idx_lx_fund_flow_detail_statement (bank_statement_id)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_detail_edge' AND INDEX_NAME = 'idx_lx_fund_flow_detail_family') = 0,
    'ALTER TABLE lx_fund_flow_detail_edge ADD KEY idx_lx_fund_flow_detail_family (family_relation_type)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ============================================================
-- lx_fund_flow_manual_edge 如不存在则补建；已存在则补索引
-- ============================================================
CREATE TABLE IF NOT EXISTS lx_fund_flow_manual_edge (
    object_key           VARCHAR(64)    NOT NULL COMMENT '手工资金流向汇总边唯一标识',
    from_object_key      VARCHAR(64)    NOT NULL COMMENT '起点主体object_key，身份证MD5或手工主体MD5',
    to_object_key        VARCHAR(64)    NOT NULL COMMENT '终点主体object_key，身份证MD5或手工主体MD5',
    from_name            VARCHAR(255)   NULL COMMENT '起点主体名称冗余',
    to_name              VARCHAR(255)   NULL COMMENT '终点主体名称冗余',
    amount               DECIMAL(19, 2) NULL COMMENT '手工录入汇总金额',
    transaction_count    INT            NOT NULL DEFAULT 1 COMMENT '手工录入笔数',
    direction            VARCHAR(8)     NOT NULL COMMENT '资金方向，1支出，2收入',
    relation_desc        VARCHAR(255)   NULL COMMENT '资金流向关系说明',
    source_desc          VARCHAR(500)   NULL COMMENT '手工边来源说明',
    remark               VARCHAR(1000)  NULL COMMENT '分析备注',
    source_type          VARCHAR(64)    NOT NULL DEFAULT 'MANUAL' COMMENT '来源类型，固定为MANUAL',
    created_by           VARCHAR(64)    NULL COMMENT '创建人',
    created_time         DATETIME       NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_by           VARCHAR(64)    NULL COMMENT '更新人',
    updated_time         DATETIME       NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (object_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='资金流图谱手工资金流向汇总边表';

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_manual_edge' AND INDEX_NAME = 'idx_lx_fund_flow_manual_from') = 0,
    'ALTER TABLE lx_fund_flow_manual_edge ADD KEY idx_lx_fund_flow_manual_from (from_object_key)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_manual_edge' AND INDEX_NAME = 'idx_lx_fund_flow_manual_to') = 0,
    'ALTER TABLE lx_fund_flow_manual_edge ADD KEY idx_lx_fund_flow_manual_to (to_object_key)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_manual_edge' AND INDEX_NAME = 'idx_lx_fund_flow_manual_pair_direction') = 0,
    'ALTER TABLE lx_fund_flow_manual_edge ADD KEY idx_lx_fund_flow_manual_pair_direction (from_object_key, to_object_key, direction)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl_sql = IF(
    (SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
     WHERE TABLE_SCHEMA = @current_schema AND TABLE_NAME = 'lx_fund_flow_manual_edge' AND INDEX_NAME = 'idx_lx_fund_flow_manual_source_type') = 0,
    'ALTER TABLE lx_fund_flow_manual_edge ADD KEY idx_lx_fund_flow_manual_source_type (source_type)',
    'SELECT 1'
);
PREPARE stmt FROM @ddl_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ============================================================
-- 核对结果
-- ============================================================
SELECT TABLE_NAME, TABLE_COLLATION
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = @current_schema
  AND TABLE_NAME IN (
    'lx_fund_flow_subject_node',
    'lx_fund_flow_account_node',
    'lx_fund_flow_own_account_edge',
    'lx_fund_flow_detail_edge',
    'lx_fund_flow_manual_edge'
  )
ORDER BY TABLE_NAME;
