CaiWuXiTong/app/Console/Commands/RefreshAccountMappingCoverage.php

51 lines
1.6 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Modules\EcommerceFinance\Services\AccountMappingCoverage;
use Throwable;
/**
* 后台重建某月账单项目覆盖快照,供科目映射页即时读取。
*/
class RefreshAccountMappingCoverage extends Command
{
protected $signature = 'account-mapping:refresh-coverage {month? : 账单月份,格式 YYYY-MM}';
protected $description = '刷新科目映射的未映射账单项目清单';
public function handle(AccountMappingCoverage $coverage): int
{
$month = trim((string) $this->argument('month')) ?: null;
try {
$raw = $coverage->refreshForMonth($month);
if (($raw['status'] ?? '') === 'refreshing') {
$this->warn('已有刷新任务正在执行,本次跳过。');
return self::SUCCESS;
}
$summary = $coverage->missingForMonth($month);
$this->info(sprintf(
'%s 未映射账单项目刷新完成:%d 个项目,涉及 %d 行,账单总行数 %d。',
(string) $summary['month'],
(int) $summary['missing_item_count'],
(int) $summary['missing_row_count'],
(int) $summary['source_rows'],
));
return self::SUCCESS;
} catch (Throwable $exception) {
report($exception);
if ($month !== null) {
$coverage->markFailed($month, $exception->getMessage());
}
$this->error('未映射账单项目刷新失败:' . $exception->getMessage());
return self::FAILURE;
}
}
}