# 银行流水中间表 `ccdi_bank_statement`

## 去重相关字段

- `project_id`：业务项目主键，导入时由后端写入，迁移后要求 `NOT NULL`。
- `LE_ACCOUNT_NO`：企业银行账号，作为去重键之一；入库前服务层会先执行 `trim`，迁移后要求 `NOT NULL DEFAULT ''`。
- `ACCOUNTING_DATE_ID`：账期日期 ID，作为去重键之一，迁移后要求 `NOT NULL`。
- 去重唯一键：`(project_id, LE_ACCOUNT_NO, ACCOUNTING_DATE_ID, AMOUNT_DR, AMOUNT_CR)`。
- 唯一键语义：同一项目、同一账号、同一账期、同一借贷金额的流水只保留一条；重复导入时应通过 no-op upsert 跳过，不改写已有记录。

## 迁移后的关键结构

```sql
CREATE TABLE `ccdi_bank_statement` (
  `bank_statement_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `project_id` bigint(20) NOT NULL COMMENT '关联项目ID',
  `LE_ID` int(10) unsigned DEFAULT '0' COMMENT '企业ID',
  `ACCOUNT_ID` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '账号ID',
  `LE_ACCOUNT_NAME` varchar(240) DEFAULT 'NONE' COMMENT '企业账号名称',
  `LE_ACCOUNT_NO` varchar(240) NOT NULL DEFAULT '' COMMENT '企业银行账号',
  `ACCOUNTING_DATE_ID` int(11) NOT NULL COMMENT '账号日期ID',
  `ACCOUNTING_DATE` varchar(10) DEFAULT '0000-00-00' COMMENT '账号日期',
  `TRX_DATE` varchar(20) NOT NULL COMMENT '交易日期',
  `CURRENCY` varchar(10) DEFAULT NULL COMMENT '币种',
  `AMOUNT_DR` decimal(19,2) NOT NULL DEFAULT '0.00' COMMENT '付款金额',
  `AMOUNT_CR` decimal(19,2) NOT NULL DEFAULT '0.00' COMMENT '收款金额',
  `AMOUNT_BALANCE` decimal(19,2) NOT NULL COMMENT '余额',
  `CASH_TYPE` varchar(500) DEFAULT NULL COMMENT '交易类型',
  `CUSTOMER_LE_ID` int(11) DEFAULT '-1' COMMENT '对手方企业ID',
  `CUSTOMER_ACCOUNT_NAME` varchar(240) DEFAULT NULL COMMENT '对手方企业名称',
  `CUSTOMER_ACCOUNT_NO` varchar(240) DEFAULT NULL COMMENT '对手方账号',
  `customer_bank` varchar(300) DEFAULT NULL COMMENT '对手方银行',
  `customer_reference` varchar(500) DEFAULT NULL COMMENT '对手方备注',
  `USER_MEMO` varchar(1000) DEFAULT NULL COMMENT '用户交易摘要',
  `BANK_COMMENTS` varchar(240) DEFAULT NULL COMMENT '银行交易摘要',
  `BANK_TRX_NUMBER` varchar(240) DEFAULT NULL COMMENT '银行交易号',
  `BANK` varchar(250) NOT NULL DEFAULT '' COMMENT '所属银行缩写',
  `TRX_FLAG` varchar(2) DEFAULT '0' COMMENT '交易标志位',
  `TRX_TYPE` int(11) NOT NULL DEFAULT '0' COMMENT '分类ID',
  `EXCEPTION_TYPE` varchar(50) NOT NULL DEFAULT '' COMMENT '异常类型',
  `internal_flag` tinyint(1) DEFAULT '0' COMMENT '"是否为内部交易1 是 0 否"',
  `batch_id` int(11) NOT NULL DEFAULT '0' COMMENT '上传logId对应upload_log',
  `batch_sequence` int(11) NOT NULL COMMENT '每次上传在文件中的line',
  `CREATE_DATE` datetime DEFAULT NULL COMMENT '创建时间',
  `created_by` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '创建者',
  `meta_json` text COMMENT '"meta json"',
  `no_balance` tinyint(1) DEFAULT '0' COMMENT '是否包含余额',
  `begin_balance` tinyint(1) DEFAULT '0' COMMENT '初始余额',
  `end_balance` tinyint(1) DEFAULT '0' COMMENT '结束余额',
  `group_id` int(11) DEFAULT '0' COMMENT '项目id',
  `override_bs_id` bigint(20) DEFAULT '0' COMMENT '=0表示该数据未覆盖主表，>0表示覆盖主表，<0表示被主表覆盖',
  `payment_method` varchar(500) DEFAULT NULL COMMENT '微信、支付宝流水字段，交易方式',
  `cret_no` varchar(20) DEFAULT NULL COMMENT '身份证号',
  PRIMARY KEY (`bank_statement_id`),
  KEY `idx_batch_id_account` (`batch_id`, `LE_ACCOUNT_NO`, `ACCOUNTING_DATE_ID`),
  KEY `GROUP_ID` (`group_id`),
  KEY `c4c_bank_statement_stg_batch_id_IDX` (`batch_id`, `LE_ACCOUNT_NO`) USING BTREE,
  KEY `idx_project_id` (`project_id`),
  UNIQUE KEY `uk_bank_statement_dedup` (
    `project_id`,
    `LE_ACCOUNT_NO`,
    `ACCOUNTING_DATE_ID`,
    `AMOUNT_DR`,
    `AMOUNT_CR`
  )
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='银行流水的中间处理表';
```

## 字段映射

| 序号 | ccdi_bank_statement | 流水分析返回字段 |
| --- | --- | --- |
| 1 | bank_statement_id | bankStatementId |
| 2 | project_id | 后端按业务写入 |
| 3 | LE_ID | leId |
| 4 | ACCOUNT_ID | accountId |
| 5 | LE_ACCOUNT_NAME | leName |
| 6 | LE_ACCOUNT_NO | accountMaskNo |
| 7 | ACCOUNTING_DATE_ID | accountingDateId |
| 8 | ACCOUNTING_DATE | accountingDate |
| 9 | TRX_DATE | trxDate |
| 10 | CURRENCY | currency |
| 11 | AMOUNT_DR | drAmount |
| 12 | AMOUNT_CR | crAmount |
| 13 | AMOUNT_BALANCE | balanceAmount |
| 14 | CASH_TYPE | cashType |
| 15 | CUSTOMER_LE_ID | customerId |
| 16 | CUSTOMER_ACCOUNT_NAME | customerName |
| 17 | CUSTOMER_ACCOUNT_NO | customerAccountMaskNo |
| 18 | customer_bank | customerBank |
| 19 | customer_reference | customerReference |
| 20 | USER_MEMO | userMemo |
| 21 | BANK_COMMENTS | bankComments |
| 22 | BANK_TRX_NUMBER | bankTrxNumber |
| 23 | BANK | bank |
| 24 | TRX_FLAG | transFlag |
| 25 | TRX_TYPE | transTypeId |
| 26 | EXCEPTION_TYPE | exceptionType |
| 27 | internal_flag | internalFlag |
| 28 | batch_id | batchId |
| 29 | batch_sequence | uploadSequnceNumber |
| 30 | CREATE_DATE | createDate |
| 31 | created_by | createdBy |
| 32 | meta_json | 固定写入 `null` |
| 33 | no_balance | noBalance |
| 34 | begin_balance | beginBalance |
| 35 | end_balance | endBalance |
| 36 | group_id | groupId |
| 37 | override_bs_id | overrideBsId |
| 38 | payment_method | paymentMethod |
| 39 | cret_no | cretNo |
