CREATE TABLE IF NOT EXISTS `ccdi_bank_tag_rule` (
  `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `model_code` VARCHAR(64) NOT NULL COMMENT '模型编码',
  `model_name` VARCHAR(128) NOT NULL COMMENT '模型名称',
  `rule_code` VARCHAR(64) NOT NULL COMMENT '规则编码',
  `rule_name` VARCHAR(128) NOT NULL COMMENT '规则名称',
  `indicator_code` VARCHAR(64) DEFAULT NULL COMMENT '指标编码',
  `result_type` VARCHAR(32) NOT NULL COMMENT '结果类型',
  `risk_level` VARCHAR(32) DEFAULT NULL COMMENT '风险等级',
  `business_caliber` VARCHAR(1000) DEFAULT NULL COMMENT '业务口径',
  `enabled` TINYINT NOT NULL DEFAULT 1 COMMENT '是否启用',
  `sort_order` INT NOT NULL DEFAULT 0 COMMENT '排序',
  `create_by` VARCHAR(64) DEFAULT NULL COMMENT '创建者',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_by` VARCHAR(64) DEFAULT NULL COMMENT '更新者',
  `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `remark` VARCHAR(500) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_ccdi_bank_tag_rule_code` (`rule_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='流水标签规则定义表';

CREATE TABLE IF NOT EXISTS `ccdi_bank_statement_tag_result` (
  `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `project_id` BIGINT NOT NULL COMMENT '项目ID',
  `model_code` VARCHAR(64) NOT NULL COMMENT '模型编码',
  `model_name` VARCHAR(128) NOT NULL COMMENT '模型名称',
  `rule_code` VARCHAR(64) NOT NULL COMMENT '规则编码',
  `rule_name` VARCHAR(128) NOT NULL COMMENT '规则名称',
  `indicator_code` VARCHAR(64) DEFAULT NULL COMMENT '指标编码',
  `result_type` VARCHAR(32) NOT NULL COMMENT '结果类型',
  `risk_level` VARCHAR(32) DEFAULT NULL COMMENT '风险等级',
  `bank_statement_id` BIGINT DEFAULT NULL COMMENT '流水ID',
  `object_type` VARCHAR(64) DEFAULT NULL COMMENT '对象类型',
  `object_key` VARCHAR(128) DEFAULT NULL COMMENT '对象主键',
  `group_id` INT DEFAULT NULL COMMENT '项目分组ID',
  `log_id` INT DEFAULT NULL COMMENT '批次日志ID',
  `reason_detail` VARCHAR(2000) DEFAULT NULL COMMENT '异常原因快照',
  `business_caliber_snapshot` VARCHAR(2000) DEFAULT NULL COMMENT '业务口径快照',
  `hit_value_snapshot` VARCHAR(1000) DEFAULT NULL COMMENT '命中值快照',
  `create_by` VARCHAR(64) DEFAULT NULL COMMENT '创建者',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_by` VARCHAR(64) DEFAULT NULL COMMENT '更新者',
  `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `remark` VARCHAR(500) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_ccdi_bank_tag_statement_hit` (`project_id`, `rule_code`, `bank_statement_id`),
  UNIQUE KEY `uk_ccdi_bank_tag_object_hit` (`project_id`, `rule_code`, `object_type`, `object_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='流水标签结果表';

CREATE TABLE IF NOT EXISTS `ccdi_bank_tag_task` (
  `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `project_id` BIGINT NOT NULL COMMENT '项目ID',
  `trigger_type` VARCHAR(64) NOT NULL COMMENT '触发方式',
  `model_code` VARCHAR(64) DEFAULT NULL COMMENT '模型编码',
  `status` VARCHAR(32) NOT NULL COMMENT '任务状态',
  `need_rerun` TINYINT NOT NULL DEFAULT 0 COMMENT '是否需要补跑',
  `success_rule_count` INT NOT NULL DEFAULT 0 COMMENT '成功规则数',
  `failed_rule_count` INT NOT NULL DEFAULT 0 COMMENT '失败规则数',
  `hit_count` INT NOT NULL DEFAULT 0 COMMENT '命中数量',
  `error_message` VARCHAR(2000) DEFAULT NULL COMMENT '错误信息',
  `start_time` DATETIME DEFAULT NULL COMMENT '开始时间',
  `end_time` DATETIME DEFAULT NULL COMMENT '结束时间',
  `create_by` VARCHAR(64) DEFAULT NULL COMMENT '创建者',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_by` VARCHAR(64) DEFAULT NULL COMMENT '更新者',
  `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_ccdi_bank_tag_task_project_status` (`project_id`, `status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='流水标签任务表';

INSERT INTO `ccdi_bank_tag_rule`
(`model_code`, `model_name`, `rule_code`, `rule_name`, `indicator_code`, `result_type`, `risk_level`, `business_caliber`, `enabled`, `sort_order`, `create_by`, `remark`)
VALUES
('LARGE_TRANSACTION', '大额交易', 'HOUSE_OR_CAR_EXPENSE', '房车消费支出交易', 'HOUSE_OR_CAR_EXPENSE', 'STATEMENT', 'HIGH', '识别房产、购车等大额消费支出流水', 1, 10, 'system', '初始化规则'),
('LARGE_TRANSACTION', '大额交易', 'TAX_EXPENSE', '税务支出交易', 'TAX_EXPENSE', 'STATEMENT', 'HIGH', '识别税务类大额支出流水', 1, 20, 'system', '初始化规则'),
('LARGE_TRANSACTION', '大额交易', 'SINGLE_LARGE_INCOME', '大额单笔收入', 'SINGLE_TRANSACTION_AMOUNT', 'STATEMENT', 'HIGH', '识别超过单笔阈值的收入流水', 1, 30, 'system', '初始化规则'),
('LARGE_TRANSACTION', '大额交易', 'CUMULATIVE_INCOME', '累计收入超限', 'CUMULATIVE_TRANSACTION_AMOUNT', 'OBJECT', 'HIGH', '识别累计收入超过阈值的对象', 1, 40, 'system', '初始化规则'),
('LARGE_TRANSACTION', '大额交易', 'ANNUAL_TURNOVER', '年流水交易额超限', 'annual_turnover', 'OBJECT', 'HIGH', '识别年交易额超过阈值的对象', 1, 50, 'system', '初始化规则'),
('LARGE_TRANSACTION', '大额交易', 'LARGE_CASH_DEPOSIT', '大额存现交易', 'LARGE_CASH_DEPOSIT', 'STATEMENT', 'HIGH', '识别大额现金存入流水', 1, 60, 'system', '初始化规则'),
('LARGE_TRANSACTION', '大额交易', 'FREQUENT_CASH_DEPOSIT', '短时间多次存现', 'FREQUENT_CASH_DEPOSIT', 'OBJECT', 'HIGH', '识别短时间多次现金存入对象', 1, 70, 'system', '初始化规则'),
('LARGE_TRANSACTION', '大额交易', 'LARGE_TRANSFER', '大额转账交易', 'FREQUENT_TRANSFER', 'STATEMENT', 'HIGH', '识别大额转账流水', 1, 80, 'system', '初始化规则');
