CaiWuXiTong/app/Console/Commands/ImportAccountShortRenames.php

60 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Modules\EcommerceFinance\Services\FinanceLedgerAccountEditor;
use RuntimeException;
use Throwable;
class ImportAccountShortRenames extends Command
{
protected $signature = 'finance-ledger:import-account-short-renames
{path : 账户简称批量修改 JSON 文件}
{--apply : 实际写入;未指定时只校验}';
protected $description = '按账户 ID 批量修改账户简称,并同步所有关联数据';
public function handle(FinanceLedgerAccountEditor $editor): int
{
try {
$path = (string) $this->argument('path');
if (! is_file($path) || ! is_readable($path)) {
throw new RuntimeException('账户简称批量修改文件不存在或不可读取。');
}
if (strtolower(pathinfo($path, PATHINFO_EXTENSION)) !== 'json') {
throw new RuntimeException('账户简称批量修改文件必须为 JSON。');
}
if (filesize($path) > 10 * 1024 * 1024) {
throw new RuntimeException('账户简称批量修改文件不能超过 10 MB。');
}
$content = file_get_contents($path);
if (! is_string($content)) {
throw new RuntimeException('无法读取账户简称批量修改文件。');
}
$mappings = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
if (! is_array($mappings) || ! array_is_list($mappings)) {
throw new RuntimeException('账户简称批量修改文件必须是 JSON 数组。');
}
$apply = (bool) $this->option('apply');
$result = $editor->renameShorts($mappings, $apply);
} catch (Throwable $exception) {
$this->error($exception->getMessage());
return self::FAILURE;
}
$this->info(sprintf(
'%s账户简称:共 %d 户,计划修改 %d 户,未变化 %d 户。',
$result['dry_run'] ? '校验完成,' : '已完成',
$result['requested'],
$result['changed'],
$result['unchanged'],
));
return self::SUCCESS;
}
}