CaiWuXiTong/app/Console/Commands/RefreshAccountMappingCoverage.php
yueyang c24615450b fix(科目映射): 保留未映射清单并改为每月刷新 (#1725)
未映射清单持久保留;本月首次自动刷新一次,页面进入只读,手动刷新另排队。
2026-12-04 10:56:41 +08:00

94 lines
3.3 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\AccountMappingCoverage;
use Throwable;
/**
* 后台重建跨月或指定月份账单项目覆盖快照,供科目映射页即时读取。
*/
class RefreshAccountMappingCoverage extends Command
{
protected $signature = 'account-mapping:refresh-coverage
{month? : all 或账单月份 YYYY-MM}
{--pending : 处理页面登记的下一个刷新请求}
{--monthly : 本月尚未成功刷新时执行跨月刷新}';
protected $description = '刷新科目映射的未映射账单项目清单';
public function handle(AccountMappingCoverage $coverage): int
{
$runningPath = null;
$month = trim((string) $this->argument('month')) ?: null;
$monthly = (bool) $this->option('monthly');
if ($monthly && $this->option('pending')) {
$this->error('--monthly 与 --pending 不能同时使用。');
return self::FAILURE;
}
if ($this->option('pending')) {
$request = $coverage->claimPendingRefresh();
if ($request === null) {
$this->line('没有待处理的未映射清单刷新请求。');
return self::SUCCESS;
}
$month = $request['month'];
$runningPath = $request['path'];
}
if ($monthly) {
if (! $coverage->monthlyRefreshDue()) {
$this->line('本月未映射清单已生成,无需重复刷新。');
return self::SUCCESS;
}
$month = 'all';
}
try {
$isAll = $month === 'all';
$raw = $isAll
? $coverage->refreshAllMonths()
: $coverage->refreshForMonth($month);
if (($raw['status'] ?? '') === 'refreshing') {
$this->warn('已有刷新任务正在执行,本次跳过。');
if ($runningPath !== null) {
$coverage->requeuePendingRefresh($runningPath);
}
return self::SUCCESS;
}
$summary = $isAll
? $coverage->missingAcrossMonths()
: $coverage->missingForMonth($month);
$this->info(sprintf(
'%s 未映射账单项目刷新完成:%d 个项目,涉及 %d 行,账单总行数 %d;PHP 峰值内存 %.1f MiB。',
$isAll ? '跨月全部账单' : (string) $summary['month'],
(int) $summary['missing_item_count'],
(int) $summary['missing_row_count'],
(int) $summary['source_rows'],
memory_get_peak_usage(true) / 1048576,
));
if ($runningPath !== null) {
$coverage->finishPendingRefresh($runningPath, true);
}
return self::SUCCESS;
} catch (Throwable $exception) {
report($exception);
if ($month !== null) {
$coverage->markFailed($month, $exception->getMessage());
}
if ($runningPath !== null) {
$coverage->finishPendingRefresh($runningPath, false, $exception->getMessage());
}
$this->error('未映射账单项目刷新失败:' . $exception->getMessage());
return self::FAILURE;
}
}
}