```
feat(shopro_huifu): 银行支行数据导入功能增强 - 添加增量导入模式,默认跳过已存在的联行号,不清空数据库 - 新增 --update-existing 选项支持覆盖更新已存在记录 - 新增 --check 选项用于校验数据库中的重复联行号 - 改进导入统计信息,区分新增、更新、跳过、重复等状态 - 优化 CSV 文件解析,支持多种字段别名映射 - 添加文件内重复检测和 BOM 头处理 - 更新 README 文档说明新的使用方式和命令选项 ```
这个提交包含在:
@@ -8,8 +8,14 @@ class HuifuOnboardBankBranchImporter
|
||||
{
|
||||
protected static $areaNameMap;
|
||||
|
||||
/** @var array<string, bool> */
|
||||
protected static $existingCodes;
|
||||
|
||||
/** @var array<string, bool> */
|
||||
protected static $seenCodes;
|
||||
|
||||
/**
|
||||
* @return array{inserted:int,updated:int,skipped:int,total:int}
|
||||
* @return array{inserted:int,updated:int,skipped_invalid:int,skipped_existing:int,duplicate_in_file:int,parsed:int,total:int}
|
||||
*/
|
||||
public static function importFile($file, array $options = [])
|
||||
{
|
||||
@@ -27,49 +33,104 @@ class HuifuOnboardBankBranchImporter
|
||||
$batch = max(50, min(2000, (int) ($options['batch'] ?? 500)));
|
||||
$dryRun = !empty($options['dry_run']);
|
||||
$truncate = !empty($options['truncate']);
|
||||
$updateExisting = !empty($options['update_existing']);
|
||||
|
||||
if ($truncate && !$dryRun) {
|
||||
Db::name('shopro_huifu_bank_branch')->where('id', '>', 0)->delete();
|
||||
}
|
||||
|
||||
self::$existingCodes = null;
|
||||
self::$seenCodes = [];
|
||||
if (!$dryRun) {
|
||||
self::loadExistingCodes();
|
||||
}
|
||||
|
||||
$rows = $format === 'json'
|
||||
? self::parseJsonFile($file, $limit)
|
||||
: self::parseCsvFile($file, $limit);
|
||||
|
||||
$inserted = 0;
|
||||
$updated = 0;
|
||||
$skipped = 0;
|
||||
$skippedInvalid = 0;
|
||||
$skippedExisting = 0;
|
||||
$duplicateInFile = 0;
|
||||
$buffer = [];
|
||||
$flushOptions = [
|
||||
'dry_run' => $dryRun,
|
||||
'update_existing' => $updateExisting,
|
||||
];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$normalized = self::normalizeRow($row);
|
||||
if (!$normalized) {
|
||||
$skipped++;
|
||||
$skippedInvalid++;
|
||||
continue;
|
||||
}
|
||||
$code = $normalized['branch_code'];
|
||||
if (isset(self::$seenCodes[$code])) {
|
||||
$duplicateInFile++;
|
||||
continue;
|
||||
}
|
||||
self::$seenCodes[$code] = true;
|
||||
|
||||
$buffer[] = $normalized;
|
||||
if (count($buffer) >= $batch) {
|
||||
$stats = self::flushBuffer($buffer, $dryRun);
|
||||
$stats = self::flushBuffer($buffer, $flushOptions);
|
||||
$inserted += $stats['inserted'];
|
||||
$updated += $stats['updated'];
|
||||
$skippedExisting += $stats['skipped_existing'];
|
||||
$buffer = [];
|
||||
}
|
||||
}
|
||||
|
||||
if ($buffer) {
|
||||
$stats = self::flushBuffer($buffer, $dryRun);
|
||||
$stats = self::flushBuffer($buffer, $flushOptions);
|
||||
$inserted += $stats['inserted'];
|
||||
$updated += $stats['updated'];
|
||||
$skippedExisting += $stats['skipped_existing'];
|
||||
}
|
||||
|
||||
return [
|
||||
'inserted' => $inserted,
|
||||
'updated' => $updated,
|
||||
'skipped' => $skipped,
|
||||
'skipped_invalid' => $skippedInvalid,
|
||||
'skipped_existing' => $skippedExisting,
|
||||
'duplicate_in_file' => $duplicateInFile,
|
||||
'parsed' => count($rows),
|
||||
'total' => $inserted + $updated,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验库内重复联行号 / 同名不同号
|
||||
*
|
||||
* @return array{total:int,duplicate_codes:array,duplicate_names:array}
|
||||
*/
|
||||
public static function checkDatabaseDuplicates($nameSampleLimit = 10)
|
||||
{
|
||||
$nameSampleLimit = max(1, min(50, (int) $nameSampleLimit));
|
||||
$total = (int) Db::name('shopro_huifu_bank_branch')->count();
|
||||
|
||||
$duplicateCodes = Db::name('shopro_huifu_bank_branch')
|
||||
->field('branch_code, COUNT(*) AS cnt')
|
||||
->group('branch_code')
|
||||
->having('cnt > 1')
|
||||
->select();
|
||||
|
||||
$duplicateNames = Db::query(
|
||||
'SELECT branch_name, COUNT(*) AS cnt, COUNT(DISTINCT branch_code) AS code_cnt '
|
||||
. 'FROM `' . config('database.prefix') . 'shopro_huifu_bank_branch` '
|
||||
. 'GROUP BY branch_name HAVING cnt > 1 '
|
||||
. 'ORDER BY cnt DESC LIMIT ' . $nameSampleLimit
|
||||
);
|
||||
|
||||
return [
|
||||
'total' => $total,
|
||||
'duplicate_codes' => $duplicateCodes ?: [],
|
||||
'duplicate_names' => $duplicateNames ?: [],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{inserted:int,updated:int,skipped:int,total:int}
|
||||
*/
|
||||
@@ -207,24 +268,35 @@ class HuifuOnboardBankBranchImporter
|
||||
protected static function normalizeHeaderKey($name)
|
||||
{
|
||||
$name = trim((string) $name);
|
||||
$name = preg_replace('/^\xEF\xBB\xBF/', '', $name);
|
||||
if ($name === '') {
|
||||
return '';
|
||||
}
|
||||
$lower = strtolower($name);
|
||||
$aliases = [
|
||||
'branch_code' => ['branch_code', 'bankid', 'cnaps', 'cnapscode', '联行号', '行号', '开户行联行号', '大额支付行号'],
|
||||
// gweesin/CNAPS: 联行号在 BankCode,BankId 是银行机构号(多支行共用,不可用)
|
||||
'branch_code' => ['branch_code', 'bankcode', 'cnaps', 'cnapscode', '联行号', '行号', '开户行联行号', '大额支付行号'],
|
||||
'branch_name' => ['branch_name', 'lname', 'banknamebranch', '支行名称', '开户行', '开户行支行名称', '支行全称', '网点名称'],
|
||||
'bank_name' => ['bank_name', 'bankname', 'banknamemain', '银行', '银行名称', '银行简称', '总行名称'],
|
||||
'bank_name' => ['bank_name', 'banknamemain', '银行', '银行名称', '银行简称', '总行名称'],
|
||||
'province' => ['province', 'provincename', 'areaprovince', '省份', '省'],
|
||||
'city' => ['city', 'cityname', 'areacity', '城市', '市', '地区'],
|
||||
'prov_id' => ['prov_id', 'provincecode', '省码'],
|
||||
'area_id' => ['area_id', 'citycode', '市码'],
|
||||
'area_id' => ['area_id', '市码'],
|
||||
];
|
||||
foreach ($aliases as $canonical => $list) {
|
||||
if (in_array($lower, $list, true) || in_array($name, $list, true)) {
|
||||
return $canonical;
|
||||
}
|
||||
}
|
||||
if ($lower === 'bankname') {
|
||||
return 'bank_name';
|
||||
}
|
||||
if ($lower === 'citycode') {
|
||||
return 'city_code';
|
||||
}
|
||||
if ($lower === 'bankid') {
|
||||
return 'bank_inst_id';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -252,9 +324,7 @@ class HuifuOnboardBankBranchImporter
|
||||
|
||||
protected static function normalizeRow(array $row)
|
||||
{
|
||||
$branchCode = self::normalizeBranchCode(
|
||||
$row['branch_code'] ?? ($row['BankId'] ?? ($row['cnapsCode'] ?? ''))
|
||||
);
|
||||
$branchCode = self::normalizeBranchCode(self::pickBranchCode($row));
|
||||
$branchName = trim((string) ($row['branch_name'] ?? ($row['LName'] ?? '')));
|
||||
if ($branchCode === '' || $branchName === '') {
|
||||
return null;
|
||||
@@ -295,6 +365,16 @@ class HuifuOnboardBankBranchImporter
|
||||
];
|
||||
}
|
||||
|
||||
protected static function pickBranchCode(array $row)
|
||||
{
|
||||
foreach (['branch_code', 'BankCode', 'cnapsCode', '联行号'] as $key) {
|
||||
if (!empty($row[$key])) {
|
||||
return $row[$key];
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
protected static function normalizeBranchCode($code)
|
||||
{
|
||||
$code = preg_replace('/\D+/', '', (string) $code);
|
||||
@@ -418,36 +498,70 @@ class HuifuOnboardBankBranchImporter
|
||||
return strtolower($level . '|' . HuifuOnboardAreaHelper::toCode($provId) . '|' . $name);
|
||||
}
|
||||
|
||||
protected static function loadExistingCodes()
|
||||
{
|
||||
if (self::$existingCodes !== null) {
|
||||
return;
|
||||
}
|
||||
self::$existingCodes = [];
|
||||
$codes = Db::name('shopro_huifu_bank_branch')->column('branch_code');
|
||||
foreach ($codes ?: [] as $code) {
|
||||
self::$existingCodes[(string) $code] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $rows
|
||||
* @return array{inserted:int,updated:int}
|
||||
* @param array{dry_run?:bool,update_existing?:bool} $options
|
||||
* @return array{inserted:int,updated:int,skipped_existing:int}
|
||||
*/
|
||||
protected static function flushBuffer(array $rows, $dryRun = false)
|
||||
protected static function flushBuffer(array $rows, array $options = [])
|
||||
{
|
||||
$dryRun = !empty($options['dry_run']);
|
||||
$updateExisting = !empty($options['update_existing']);
|
||||
$inserted = 0;
|
||||
$updated = 0;
|
||||
$skippedExisting = 0;
|
||||
|
||||
if ($dryRun) {
|
||||
return ['inserted' => count($rows), 'updated' => 0];
|
||||
foreach ($rows as $row) {
|
||||
if (isset(self::$existingCodes[$row['branch_code']])) {
|
||||
$skippedExisting++;
|
||||
} else {
|
||||
$inserted++;
|
||||
}
|
||||
}
|
||||
return ['inserted' => $inserted, 'updated' => 0, 'skipped_existing' => $skippedExisting];
|
||||
}
|
||||
|
||||
$toInsert = [];
|
||||
foreach ($rows as $row) {
|
||||
$exists = Db::name('shopro_huifu_bank_branch')->where('branch_code', $row['branch_code'])->find();
|
||||
if ($exists) {
|
||||
Db::name('shopro_huifu_bank_branch')->where('id', $exists['id'])->update([
|
||||
'branch_name' => $row['branch_name'],
|
||||
'bank_name' => $row['bank_name'],
|
||||
'prov_id' => $row['prov_id'],
|
||||
'area_id' => $row['area_id'],
|
||||
'status' => 1,
|
||||
]);
|
||||
$updated++;
|
||||
$code = $row['branch_code'];
|
||||
if (isset(self::$existingCodes[$code])) {
|
||||
if ($updateExisting) {
|
||||
Db::name('shopro_huifu_bank_branch')->where('branch_code', $code)->update([
|
||||
'branch_name' => $row['branch_name'],
|
||||
'bank_name' => $row['bank_name'],
|
||||
'prov_id' => $row['prov_id'],
|
||||
'area_id' => $row['area_id'],
|
||||
'status' => 1,
|
||||
]);
|
||||
$updated++;
|
||||
} else {
|
||||
$skippedExisting++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
Db::name('shopro_huifu_bank_branch')->insert($row);
|
||||
$inserted++;
|
||||
$toInsert[] = $row;
|
||||
self::$existingCodes[$code] = true;
|
||||
}
|
||||
|
||||
return ['inserted' => $inserted, 'updated' => $updated];
|
||||
if ($toInsert) {
|
||||
Db::name('shopro_huifu_bank_branch')->insertAll($toInsert);
|
||||
$inserted = count($toInsert);
|
||||
}
|
||||
|
||||
return ['inserted' => $inserted, 'updated' => $updated, 'skipped_existing' => $skippedExisting];
|
||||
}
|
||||
|
||||
protected static function downloadToFile($url, $dest)
|
||||
|
||||
在新工单中引用
屏蔽一个用户