From 5424d61de9010e340f8523878a0e1750824f1628 Mon Sep 17 00:00:00 2001 From: cheney Date: Mon, 13 Jul 2026 21:47:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=80=BB=E8=BF=9B=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- duizhang.js | 79 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/duizhang.js b/duizhang.js index 5ddf5d7..85074bc 100644 --- a/duizhang.js +++ b/duizhang.js @@ -122,43 +122,66 @@ async function confirmReconciliation(statementSeq, id) { // ======================== 主流程 ======================== async function main() { const teamIds = readTeamIds(); - console.log(`\nTEAM IDs: ${teamIds.join(', ')}\n`); - - const allResults = []; + console.log(` +TEAM IDs: ${teamIds.join(', ')} +`); + // 预查询所有结算单,计算总任务数 + console.log('预查询结算单列表...'); + const allTasks = []; for (const teamId of teamIds) { - console.log(`${'='.repeat(50)}\n 处理 ${teamId}\n${'='.repeat(50)}`); - const accounts = await querySettlementAccountIds(teamId); - if (!accounts.length) { console.log(' ⚠ 未找到'); continue; } - + if (!accounts.length) continue; for (const account of accounts) { - const accountId = account.id; - console.log(`\n --> 结算单列表 settlementAccountId: ${accountId}`); - const statements = await listReceiveStatements(accountId); - console.log(` --> 共 ${statements.length} 条`); - + const statements = await listReceiveStatements(account.id); for (const st of statements) { - const { id, statementSeq, statementName, receivableTotalPrice, statementStatus } = st; - console.log(`\n --- [${statementSeq}] ${statementName}`); - console.log(` 应收: \u00a5${receivableTotalPrice} | 状态: ${statementStatus}`); - - if (statementStatus === '5') { - console.log(` \u26a0 已完成,跳过`); - allResults.push({ teamId, settlementAccountId: accountId, statementSeq, statementName, receivableTotalPrice, autoReconResult: { code: 'SKIP' }, confirmResult: { code: 'SKIP' } }); - continue; - } - - const autoResult = await autoReconciliation(statementSeq, id); - console.log(` \u23f3 等待 ${STEP_DELAY_MS / 1000}s...`); - await sleep(STEP_DELAY_MS); - const confirmResult = await confirmReconciliation(statementSeq, id); - - allResults.push({ teamId, settlementAccountId: accountId, statementSeq, statementName, receivableTotalPrice, autoReconResult: autoResult, confirmResult }); + allTasks.push({ teamId, accountId: account.id, statement: st }); } } } + const total = allTasks.length; + const pendingTasks = allTasks.filter(t => t.statement.statementStatus !== '5'); + const pendingTotal = pendingTasks.length; + const skipCount = total - pendingTotal; + console.log(`总任务: ${total} | 跳过(已完成): ${skipCount} | 待处理: ${pendingTotal} +`); + + if (pendingTotal === 0) { + console.log('所有结算单已完成,无需处理。'); + return; + } + + const allResults = []; + let completed = 0; + + for (const task of allTasks) { + const { teamId, accountId, statement: st } = task; + const { id, statementSeq, statementName, receivableTotalPrice, statementStatus } = st; + + const pct = Math.round((completed / pendingTotal) * 100); + console.log(` +${'='.repeat(50)}`); + console.log(` 进度: ${pct}% (${completed}/${pendingTotal}) | ${teamId} | ${statementSeq}`); + console.log(`${'='.repeat(50)}`); + console.log(` 结算单: ${statementName}`); + console.log(` 应收: \u00a5${receivableTotalPrice} | 状态: ${statementStatus}`); + + if (statementStatus === '5') { + console.log(` \u26a0 已完成,跳过`); + allResults.push({ teamId, settlementAccountId: accountId, statementSeq, statementName, receivableTotalPrice, autoReconResult: { code: 'SKIP' }, confirmResult: { code: 'SKIP' } }); + continue; + } + + const autoResult = await autoReconciliation(statementSeq, id); + console.log(` \u23f3 等待 ${STEP_DELAY_MS / 1000}s...`); + await sleep(STEP_DELAY_MS); + const confirmResult = await confirmReconciliation(statementSeq, id); + + allResults.push({ teamId, settlementAccountId: accountId, statementSeq, statementName, receivableTotalPrice, autoReconResult: autoResult, confirmResult }); + completed++; + console.log(` >>> 进度: ${Math.round((completed / pendingTotal) * 100)}% (${completed}/${pendingTotal})`); + } // ======================== 汇总 ======================== console.log('\n\n' + '='.repeat(70)); console.log(' 最终汇总');