按源表行序定向更新过账标签,逐行校验订单身份,仅修改过账标签与 row_hash。 验证:新增单元测试 4 个、41 条断言通过;真实 13,949 行隔离导入通过。
72 lines
2.6 KiB
PHP
72 lines
2.6 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use App\Business\KukaPostingBase\KukaPostingLabelImporter;
|
||
use Illuminate\Console\Command;
|
||
use Throwable;
|
||
|
||
class ImportKukaPostingLabels extends Command
|
||
{
|
||
protected $signature = 'business-mirror:import-kuka-posting-labels
|
||
{path : 带月度过账标签列的 XLSX 文件}
|
||
{--month= : 账期 YYYY-MM}
|
||
{--sheet=Sheet1 : 工作表名称}
|
||
{--column= : 标签列名,默认按账期生成“YYYY年M月过账”}
|
||
{--company=1 : 公司 ID}
|
||
{--dry-run : 只校验文件和共享镜像,不写入过账标签}';
|
||
|
||
protected $description = '按源表行序更新顾家过账基础数据的过账标签';
|
||
|
||
public function handle(KukaPostingLabelImporter $importer): int
|
||
{
|
||
$period = trim((string) $this->option('month'));
|
||
if ($period === '') {
|
||
$this->error('必须指定 --month=YYYY-MM。');
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
try {
|
||
$result = $importer->import(
|
||
(string) $this->argument('path'),
|
||
$period,
|
||
(string) ($this->option('sheet') ?: 'Sheet1'),
|
||
$this->option('column') ? (string) $this->option('column') : null,
|
||
max(1, (int) $this->option('company')),
|
||
(bool) $this->option('dry-run'),
|
||
);
|
||
} catch (Throwable $exception) {
|
||
$this->error('顾家过账标签未导入,原有数据未改变。' . $exception->getMessage());
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
$labelRows = [];
|
||
foreach ($result['labels'] as $label => $count) {
|
||
$labelRows[] = [$label, number_format($count)];
|
||
}
|
||
|
||
$storeRows = [];
|
||
foreach ($result['stores'] as $store => $count) {
|
||
$storeRows[] = [$store, number_format($count)];
|
||
}
|
||
|
||
$this->table(['项目', '值'], [
|
||
['模式', $result['dry_run'] ? '仅校验' : '正式导入'],
|
||
['账期', $result['period']],
|
||
['标签列', $result['label_column']],
|
||
['数据行数', number_format($result['rows'])],
|
||
['店铺数', number_format($result['store_count'])],
|
||
['需要更新', number_format($result['updated'])],
|
||
['已一致', number_format($result['unchanged'])],
|
||
['文件 SHA-256', $result['file_sha256']],
|
||
]);
|
||
$this->table(['过账标签', '行数'], $labelRows);
|
||
$this->table(['店铺', '行数'], $storeRows);
|
||
$this->info($result['dry_run'] ? '顾家过账标签校验完成。' : '顾家过账标签已更新。');
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
}
|