feat(shopro_huifu): 银行支行数据导入功能增强

- 添加增量导入模式,默认跳过已存在的联行号,不清空数据库
- 新增 --update-existing 选项支持覆盖更新已存在记录
- 新增 --check 选项用于校验数据库中的重复联行号
- 改进导入统计信息,区分新增、更新、跳过、重复等状态
- 优化 CSV 文件解析,支持多种字段别名映射
- 添加文件内重复检测和 BOM 头处理
- 更新 README 文档说明新的使用方式和命令选项
```
这个提交包含在:
可乐加冰
2026-09-30 16:09:40 +08:00
父节点 21d6da86f1
当前提交 5dbf6a346b
共有 3 个文件被更改,包括 203 次插入 和 34 次删除

查看文件

@@ -19,7 +19,9 @@ class ImportBankBranch extends Command
->addOption('file', 'f', Option::VALUE_OPTIONAL, 'CSV/JSON source file path')
->addOption('download', 'd', Option::VALUE_NONE, 'Download gweesin/CNAPS cnaps.csv then import')
->addOption('seed', null, Option::VALUE_NONE, 'Import bundled data/bank_branches.json seed only')
->addOption('truncate', 't', Option::VALUE_NONE, 'Clear table before import')
->addOption('truncate', 't', Option::VALUE_NONE, 'Clear table before import (optional, default skip existing)')
->addOption('update-existing', 'u', Option::VALUE_NONE, 'Update rows when branch_code already exists')
->addOption('check', 'c', Option::VALUE_NONE, 'Check duplicate branch_code / branch_name in DB only')
->addOption('limit', 'l', Option::VALUE_OPTIONAL, 'Max rows to import (0=all)', 0)
->addOption('batch', 'b', Option::VALUE_OPTIONAL, 'Batch size', 500)
->addOption('format', null, Option::VALUE_OPTIONAL, 'Force format: csv|json|auto', 'auto')
@@ -35,6 +37,10 @@ class ImportBankBranch extends Command
return $this->exportJson($output, $exportJson, (int) $input->getOption('export-limit'));
}
if ((bool) $input->getOption('check')) {
return $this->checkDuplicates($output);
}
try {
Db::execute('SELECT 1 FROM `' . config('database.prefix') . 'shopro_huifu_bank_branch` LIMIT 1');
} catch (\Throwable $e) {
@@ -82,6 +88,7 @@ class ImportBankBranch extends Command
$options = [
'truncate' => (bool) $input->getOption('truncate'),
'update_existing' => (bool) $input->getOption('update-existing'),
'limit' => (int) $input->getOption('limit'),
'batch' => (int) $input->getOption('batch'),
'format' => (string) $input->getOption('format'),
@@ -94,6 +101,8 @@ class ImportBankBranch extends Command
}
if ($options['truncate']) {
$output->writeln('[truncate] 导入前清空旧数据');
} else {
$output->writeln('已存在联行号将自动跳过(可用 --update-existing 覆盖更新)');
}
try {
@@ -104,12 +113,17 @@ class ImportBankBranch extends Command
}
$output->writeln(sprintf(
'完成: 新增 %d,更新 %d,跳过 %d,有效 %d',
'完成: 解析 %d 行,新增 %d,更新 %d,跳过已存在 %d,文件内重复 %d,无效 %d',
$stats['parsed'] ?? 0,
$stats['inserted'],
$stats['updated'],
$stats['skipped'],
$stats['total']
$stats['skipped_existing'] ?? 0,
$stats['duplicate_in_file'] ?? 0,
$stats['skipped_invalid'] ?? 0
));
if (($stats['parsed'] ?? 0) > 1000 && ($stats['inserted'] ?? 0) < 1000 && ($stats['skipped_existing'] ?? 0) < 100) {
$output->writeln('警告: 解析行数很多但新增很少,请确认 CSV 联行号列为 BankCode(非 BankId)');
}
$count = Db::name('shopro_huifu_bank_branch')->where('status', 1)->count();
$output->writeln('当前库内有效支行: ' . $count);
@@ -117,6 +131,44 @@ class ImportBankBranch extends Command
return 0;
}
protected function checkDuplicates(Output $output)
{
try {
$result = HuifuOnboardBankBranchImporter::checkDatabaseDuplicates();
} catch (\Throwable $e) {
$output->error($e->getMessage());
return 1;
}
$output->writeln('当前库内支行总数: ' . $result['total']);
$codeDupes = $result['duplicate_codes'];
if ($codeDupes) {
$output->writeln('发现重复联行号 ' . count($codeDupes) . ' 组(不应出现,表有唯一索引):');
foreach ($codeDupes as $item) {
$output->writeln(' ' . $item['branch_code'] . ' x' . $item['cnt']);
}
} else {
$output->writeln('联行号 branch_code 无重复');
}
$nameDupes = $result['duplicate_names'];
if ($nameDupes) {
$output->writeln('同名不同号(展示前 ' . count($nameDupes) . ' 组,通常可忽略):');
foreach ($nameDupes as $item) {
$output->writeln(sprintf(
' %s(%d 条,%d 个联行号)',
$item['branch_name'],
$item['cnt'],
$item['code_cnt']
));
}
} else {
$output->writeln('支行名称无重复');
}
return 0;
}
protected function exportJson(Output $output, $dest, $limit)
{
try {