feat(dayend): 切日端点改 catch-up 语义——循环三步(日结余额快照→总账→日期+1)直至追平今天,停机多日/cron漏跑一次调用补齐不漏日;同日重放0步跳过;business_date超前今天拒绝;_MAX_CATCHUP=400防御异常配置(达上限时已推进天数完整生效,报错要求人工核查续跑)
This commit is contained in:
parent
c40f7f665a
commit
2624f9189b
@ -11,11 +11,14 @@ if _ip not in ('127.0.0.1', '::1', 'localhost'):
|
|||||||
|
|
||||||
from sqlor.dbpools import get_sor_context
|
from sqlor.dbpools import get_sor_context
|
||||||
|
|
||||||
# 切日三步(同一 sor 上下文,全成才提交):
|
# 切日(catch-up 语义,同一 sor 上下文,全成才提交):
|
||||||
# 1. dayend_balance —— 上一营业日余额快照(acc_balance 补齐无当日行的账户)
|
# 循环执行三步直至 business_date 追平今天:
|
||||||
# 2. accounting_ledger —— 上一营业日总账(delete+insert 幂等重建 ledger 当日行)
|
# 1. dayend_balance —— 上一营业日余额快照(acc_balance 补齐无当日行的账户)
|
||||||
# 3. new_business_date —— params.business_date 推进 +1 天
|
# 2. accounting_ledger —— 上一营业日总账(delete+insert 幂等重建 ledger 当日行)
|
||||||
# 顺序不可颠倒:前两步都读「上一营业日」,推进日期必须在最后。
|
# 3. new_business_date —— params.business_date 推进 +1 天
|
||||||
|
# 停机多日/cron 漏跑后一次调用自动追平,不会漏日;同日重放 0 步跳过(幂等)。
|
||||||
|
# _MAX_CATCHUP 防御异常配置(如 business_date 误写远古值)导致超长循环。
|
||||||
|
_MAX_CATCHUP = 400
|
||||||
async with get_sor_context(request._run_ns, 'accounting') as sor:
|
async with get_sor_context(request._run_ns, 'accounting') as sor:
|
||||||
try:
|
try:
|
||||||
from datetime import date
|
from datetime import date
|
||||||
@ -23,25 +26,47 @@ async with get_sor_context(request._run_ns, 'accounting') as sor:
|
|||||||
from accounting.ledger import accounting_ledger
|
from accounting.ledger import accounting_ledger
|
||||||
from appbase.businessdate import get_business_date, new_business_date
|
from appbase.businessdate import get_business_date, new_business_date
|
||||||
|
|
||||||
old_bd = await get_business_date(sor)
|
|
||||||
# 幂等守卫:business_date 已到今天则跳过(cron 重跑/手工重放不会多跳日期)
|
|
||||||
today = date.today().isoformat()
|
today = date.today().isoformat()
|
||||||
if old_bd >= today:
|
bd = await get_business_date(sor)
|
||||||
|
start_bd = bd
|
||||||
|
if bd > today:
|
||||||
|
return json.dumps({
|
||||||
|
'success': False,
|
||||||
|
'business_date': bd,
|
||||||
|
'message': 'business_date 超前于今天(配置异常),拒绝切日',
|
||||||
|
}, ensure_ascii=False)
|
||||||
|
steps = 0
|
||||||
|
snapshots = []
|
||||||
|
while bd < today and steps < _MAX_CATCHUP:
|
||||||
|
snap_date = await dayend_balance(sor)
|
||||||
|
await accounting_ledger(sor)
|
||||||
|
await new_business_date(sor)
|
||||||
|
bd = await get_business_date(sor)
|
||||||
|
snapshots.append(snap_date)
|
||||||
|
steps += 1
|
||||||
|
if steps == 0:
|
||||||
return json.dumps({
|
return json.dumps({
|
||||||
'success': True, 'skipped': True,
|
'success': True, 'skipped': True,
|
||||||
'business_date': old_bd,
|
'business_date': bd,
|
||||||
'message': '已是当日营业日期,跳过切日(幂等)',
|
'message': '已是当日营业日期,跳过切日(幂等)',
|
||||||
}, ensure_ascii=False)
|
}, ensure_ascii=False)
|
||||||
|
if bd < today:
|
||||||
snap_date = await dayend_balance(sor)
|
# 触达防御上限仍未追平:已推进的天数是完整日结单元、照常生效(可续跑),
|
||||||
await accounting_ledger(sor)
|
# 但 business_date 疑似配置异常(如误写远古值),必须人工核查后再续。
|
||||||
await new_business_date(sor)
|
return json.dumps({
|
||||||
new_bd = await get_business_date(sor)
|
'success': False,
|
||||||
|
'business_date': '%s -> %s' % (start_bd, bd),
|
||||||
|
'steps': steps,
|
||||||
|
'message': 'catch-up 达 %d 天上限仍未追平今天,business_date 疑似配置异常,'
|
||||||
|
'请人工核查后重跑(已推进的 %d 天日结完整生效,重跑自动续推)'
|
||||||
|
% (_MAX_CATCHUP, steps),
|
||||||
|
}, ensure_ascii=False)
|
||||||
await sor.sqlExe("COMMIT", {})
|
await sor.sqlExe("COMMIT", {})
|
||||||
return json.dumps({
|
return json.dumps({
|
||||||
'success': True,
|
'success': True,
|
||||||
'business_date': '%s -> %s' % (old_bd, new_bd),
|
'business_date': '%s -> %s' % (start_bd, bd),
|
||||||
'snapshot_date': snap_date,
|
'steps': steps,
|
||||||
|
'snapshots': snapshots[-5:],
|
||||||
}, ensure_ascii=False)
|
}, ensure_ascii=False)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return json.dumps({'success': False, 'message': 'dayend failed: %s' % str(e)[:200]},
|
return json.dumps({'success': False, 'message': 'dayend failed: %s' % str(e)[:200]},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user