fix(科目映射): 未映射清单改为按月手动生成 (#1689)
This commit is contained in:
parent
045e5cc393
commit
7c2a1a66ae
@ -84,6 +84,7 @@ class AccountMappingActions extends Controller
|
||||
return response()->json($this->coverage->missingForMonth(
|
||||
trim((string) ($data['month'] ?? '')) ?: null,
|
||||
(bool) ($data['refresh'] ?? false),
|
||||
false,
|
||||
));
|
||||
} catch (\InvalidArgumentException $exception) {
|
||||
throw ValidationException::withMessages(['month' => $exception->getMessage()]);
|
||||
|
||||
@ -107,7 +107,11 @@ class AccountMappingCoverage
|
||||
* months:list<string>
|
||||
* }
|
||||
*/
|
||||
public function missingForMonth(?string $month = null, bool $refresh = false): array
|
||||
public function missingForMonth(
|
||||
?string $month = null,
|
||||
bool $refresh = false,
|
||||
bool $queueIfMissing = false,
|
||||
): array
|
||||
{
|
||||
$period = $this->parseMonth($month);
|
||||
$sourceKey = $this->rawCacheKey($period);
|
||||
@ -143,6 +147,9 @@ class AccountMappingCoverage
|
||||
if ($this->hasLiveRefreshRequest($period)) {
|
||||
return $this->waitingPayload($period, 'refreshing');
|
||||
}
|
||||
if (! $refresh && ! $queueIfMissing) {
|
||||
return $this->waitingPayload($period, 'missing');
|
||||
}
|
||||
|
||||
$requested = $this->requestRefresh($period);
|
||||
if (($requested['status'] ?? '') === 'failed') {
|
||||
@ -402,10 +409,7 @@ class AccountMappingCoverage
|
||||
}
|
||||
|
||||
$refreshedAt = (string) ($snapshot['refreshed_at'] ?? '');
|
||||
$refreshedAtDate = CarbonImmutable::parse($refreshedAt);
|
||||
if ($refreshedAtDate->lt(now()->subHours(self::CACHE_HOURS))) {
|
||||
return null;
|
||||
}
|
||||
CarbonImmutable::parse($refreshedAt);
|
||||
|
||||
return [
|
||||
'raw' => $snapshot['raw'],
|
||||
|
||||
@ -879,7 +879,6 @@
|
||||
var coverageData = null;
|
||||
var coverageLoading = false;
|
||||
var coverageError = '';
|
||||
var coveragePollTimer = null;
|
||||
var returnToCoverageAfterCreate = false;
|
||||
|
||||
// 大类 → 预期方向基准:改大类时顺手把基准带过去,避免两者打架
|
||||
@ -1134,6 +1133,7 @@
|
||||
var coverageStateEl = document.getElementById('amCoverageState');
|
||||
var coverageBodyEl = document.getElementById('amCoverageBody');
|
||||
var coverageTableWrapEl = document.getElementById('amCoverageTableWrap');
|
||||
var coverageRefreshEl = document.getElementById('amCoverageRefresh');
|
||||
|
||||
function selectView(name, updateUrl) {
|
||||
var selected = name === 'unmapped' ? 'unmapped' : 'rules';
|
||||
@ -1152,13 +1152,8 @@
|
||||
url.searchParams.set('am_month', coverageMonthEl ? coverageMonthEl.value : INITIAL_COVERAGE_MONTH);
|
||||
window.history.replaceState({}, '', url);
|
||||
}
|
||||
if (selected === 'unmapped'
|
||||
&& (!coverageData || coverageData.status === 'refreshing')
|
||||
&& !coverageLoading) {
|
||||
if (selected === 'unmapped' && !coverageData && !coverageLoading) {
|
||||
loadCoverage(coverageMonthEl ? coverageMonthEl.value : INITIAL_COVERAGE_MONTH);
|
||||
} else if (selected !== 'unmapped' && coveragePollTimer) {
|
||||
clearTimeout(coveragePollTimer);
|
||||
coveragePollTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1169,9 +1164,11 @@
|
||||
var title = document.createElement('strong');
|
||||
title.textContent = month;
|
||||
coverageSummaryEl.appendChild(title);
|
||||
if (data && (data.status === 'refreshing' || data.status === 'failed')) {
|
||||
if (data && (data.status === 'refreshing' || data.status === 'failed' || data.status === 'missing')) {
|
||||
var state = document.createElement('span');
|
||||
state.textContent = data.status === 'refreshing' ? '生成一次中……' : '生成失败';
|
||||
state.textContent = data.status === 'refreshing'
|
||||
? '等待生成完成……'
|
||||
: (data.status === 'missing' ? '尚未生成' : '生成失败');
|
||||
if (data.status === 'failed') { state.className = 'is-warning'; }
|
||||
coverageSummaryEl.appendChild(state);
|
||||
return;
|
||||
@ -1252,7 +1249,7 @@
|
||||
coverageCountEl.textContent = '—';
|
||||
coverageStateEl.hidden = false;
|
||||
coverageStateEl.className = 'am-coverage-state';
|
||||
coverageStateEl.textContent = '切换到此视图后开始读取账单项目。';
|
||||
coverageStateEl.textContent = '切换到此视图后读取该月已有快照。';
|
||||
coverageTableWrapEl.hidden = true;
|
||||
return;
|
||||
}
|
||||
@ -1261,7 +1258,17 @@
|
||||
coverageCountEl.textContent = '…';
|
||||
coverageStateEl.hidden = false;
|
||||
coverageStateEl.className = 'am-coverage-state';
|
||||
coverageStateEl.textContent = '正在生成一次该月未映射清单;完成后会持久缓存,之后再次访问直接读取。';
|
||||
coverageStateEl.textContent = '后台正在按月生成。批处理完成后,点击“读取生成结果”;本页不会自动轮询。';
|
||||
coverageTableWrapEl.hidden = true;
|
||||
renderCoverageSummary(coverageData);
|
||||
return;
|
||||
}
|
||||
|
||||
if (coverageData.status === 'missing') {
|
||||
coverageCountEl.textContent = '—';
|
||||
coverageStateEl.hidden = false;
|
||||
coverageStateEl.className = 'am-coverage-state';
|
||||
coverageStateEl.textContent = '所选月份尚未生成未映射清单,请点击“生成最新清单”。';
|
||||
coverageTableWrapEl.hidden = true;
|
||||
renderCoverageSummary(coverageData);
|
||||
return;
|
||||
@ -1311,10 +1318,6 @@
|
||||
renderCoverage();
|
||||
return;
|
||||
}
|
||||
if (coveragePollTimer) {
|
||||
clearTimeout(coveragePollTimer);
|
||||
coveragePollTimer = null;
|
||||
}
|
||||
coverageLoading = true;
|
||||
coverageError = '';
|
||||
renderCoverage();
|
||||
@ -1336,14 +1339,10 @@
|
||||
}).finally(function () {
|
||||
coverageLoading = false;
|
||||
renderCoverage();
|
||||
var unmappedButton = document.querySelector('[data-am-view-tab="unmapped"]');
|
||||
var unmappedSelected = unmappedButton?.getAttribute('aria-selected') === 'true';
|
||||
if (coverageData && coverageData.status === 'refreshing' && unmappedSelected) {
|
||||
var retrySeconds = Math.max(2, Number(coverageData.retry_after || 4));
|
||||
coveragePollTimer = setTimeout(function () {
|
||||
coveragePollTimer = null;
|
||||
loadCoverage(month, true, false);
|
||||
}, retrySeconds * 1000);
|
||||
if (coverageRefreshEl) {
|
||||
coverageRefreshEl.textContent = coverageData && coverageData.status === 'refreshing'
|
||||
? '读取生成结果'
|
||||
: (coverageData && coverageData.status === 'failed' ? '重新生成' : '生成最新清单');
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -1367,8 +1366,10 @@
|
||||
coverageData = null;
|
||||
loadCoverage(coverageMonthEl.value, true, false);
|
||||
});
|
||||
document.getElementById('amCoverageRefresh')?.addEventListener('click', function () {
|
||||
loadCoverage(coverageMonthEl ? coverageMonthEl.value : INITIAL_COVERAGE_MONTH, true, true);
|
||||
coverageRefreshEl?.addEventListener('click', function () {
|
||||
var month = coverageMonthEl ? coverageMonthEl.value : INITIAL_COVERAGE_MONTH;
|
||||
var readOnly = coverageData && coverageData.status === 'refreshing';
|
||||
loadCoverage(month, true, !readOnly);
|
||||
});
|
||||
|
||||
/* ---------- 核心:按账单实际收支 + 本行基准,决定照抄还是互换 ---------- */
|
||||
|
||||
@ -106,7 +106,9 @@ class AccountMappingPageTest extends FeatureTestCase
|
||||
->assertSee('id="amCoverageMonth"', false)
|
||||
->assertSee('data-am-coverage-add', false)
|
||||
->assertSee('生成最新清单')
|
||||
->assertSee('完成后会持久缓存,之后再次访问直接读取', false)
|
||||
->assertSee('本页不会自动轮询', false)
|
||||
->assertSee('读取生成结果', false)
|
||||
->assertDontSee('coveragePollTimer', false)
|
||||
->assertSee('data-am-memo-picker', false)
|
||||
->assertSee('输入新备注,或点 ▾ 选择', false)
|
||||
->assertSee('商品名包含词;不限填 -', false)
|
||||
@ -132,7 +134,7 @@ class AccountMappingPageTest extends FeatureTestCase
|
||||
{
|
||||
$this->grantAdminPermission();
|
||||
$coverage = Mockery::mock(AccountMappingCoverage::class);
|
||||
$coverage->shouldReceive('missingForMonth')->once()->with('2026-11', false)->andReturn([
|
||||
$coverage->shouldReceive('missingForMonth')->once()->with('2026-11', false, false)->andReturn([
|
||||
'status' => 'ready',
|
||||
'month' => '2026-11',
|
||||
'source_rows' => 120,
|
||||
@ -162,6 +164,31 @@ class AccountMappingPageTest extends FeatureTestCase
|
||||
->assertJsonPath('items.0.source_rows', 7);
|
||||
}
|
||||
|
||||
public function test_coverage_endpoint_can_queue_a_manual_month_refresh(): void
|
||||
{
|
||||
$this->grantAdminPermission();
|
||||
$coverage = Mockery::mock(AccountMappingCoverage::class);
|
||||
$coverage->shouldReceive('missingForMonth')->once()->with('2026-11', true, false)->andReturn([
|
||||
'status' => 'refreshing',
|
||||
'month' => '2026-11',
|
||||
'source_rows' => 0,
|
||||
'missing_item_count' => 0,
|
||||
'missing_row_count' => 0,
|
||||
'pending_sources' => [],
|
||||
'items' => [],
|
||||
'months' => ['2026-11', '2026-10'],
|
||||
'retry_after' => 8,
|
||||
'message' => '',
|
||||
'refreshed_at' => null,
|
||||
]);
|
||||
$this->app->instance(AccountMappingCoverage::class, $coverage);
|
||||
|
||||
$this->loginAs()->getJson($this->actionUrl('coverage') . '?month=2026-11&refresh=1')
|
||||
->assertOk()
|
||||
->assertJsonPath('status', 'refreshing')
|
||||
->assertJsonPath('month', '2026-11');
|
||||
}
|
||||
|
||||
public function test_coverage_endpoint_rejects_invalid_month(): void
|
||||
{
|
||||
$this->grantAdminPermission();
|
||||
|
||||
@ -73,8 +73,12 @@ class AccountMappingCoverageTest extends TestCase
|
||||
|
||||
$coverage = app(AccountMappingCoverage::class);
|
||||
$cold = $coverage->missingForMonth('2026-11');
|
||||
$this->assertSame('refreshing', $cold['status']);
|
||||
$this->assertSame(8, $cold['retry_after']);
|
||||
$this->assertSame('missing', $cold['status']);
|
||||
$this->assertNull($coverage->claimPendingRefresh());
|
||||
|
||||
$queued = $coverage->missingForMonth('2026-11', false, true);
|
||||
$this->assertSame('refreshing', $queued['status']);
|
||||
$this->assertSame(8, $queued['retry_after']);
|
||||
|
||||
$request = $coverage->claimPendingRefresh();
|
||||
$this->assertNotNull($request);
|
||||
@ -99,14 +103,19 @@ class AccountMappingCoverageTest extends TestCase
|
||||
$this->assertNotContains('上期项目', array_column($first['items'], 'item'));
|
||||
$this->assertNotContains('wx商户费', array_column($first['items'], 'item'));
|
||||
|
||||
$snapshotFiles = glob(
|
||||
$this->storageRoot . '/app/account-mapping/coverage-snapshots/*.json',
|
||||
) ?: [];
|
||||
$this->assertCount(1, $snapshotFiles);
|
||||
$snapshot = json_decode(File::get($snapshotFiles[0]), true, 512, JSON_THROW_ON_ERROR);
|
||||
$snapshot['refreshed_at'] = '2025-01-01 00:00:00';
|
||||
File::put($snapshotFiles[0], json_encode($snapshot, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR));
|
||||
|
||||
Cache::flush();
|
||||
$persisted = app(AccountMappingCoverage::class)->missingForMonth('2026-11');
|
||||
$this->assertSame('ready', $persisted['status']);
|
||||
$this->assertSame(9, $persisted['source_rows']);
|
||||
$this->assertSame($this->missingSignature($first['items']), $this->missingSignature($persisted['items']));
|
||||
$this->assertCount(1, glob(
|
||||
$this->storageRoot . '/app/account-mapping/coverage-snapshots/*.json',
|
||||
) ?: []);
|
||||
|
||||
$this->rule($store, '天猫', '商家货款入账');
|
||||
$second = app(AccountMappingCoverage::class)->missingForMonth('2026-11');
|
||||
@ -119,7 +128,7 @@ class AccountMappingCoverageTest extends TestCase
|
||||
public function test_interrupted_refresh_request_is_recovered_after_stale_timeout(): void
|
||||
{
|
||||
$coverage = app(AccountMappingCoverage::class);
|
||||
$coverage->missingForMonth('2026-11');
|
||||
$coverage->missingForMonth('2026-11', false, true);
|
||||
$claimed = $coverage->claimPendingRefresh();
|
||||
$this->assertNotNull($claimed);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user