fix(billing): separate ns for stats query, avoid sqlor ns mutation

This commit is contained in:
yumoqing 2026-07-16 17:32:05 +08:00
parent 1ffdda5ffe
commit 47844253fe

View File

@ -28,18 +28,23 @@ where a.orgid = ${orgid}$
and d.acc_date <= ${end_date}$"""
rows = await sor.sqlExe(sql, ns)
# 统计数据
# 统计数据 — 独立 ns 避免被 sqlor 修改
stats_ns = {
'orgid': userorgid,
'start_date': start_date,
'end_date': end_date,
'sort': '1'
}
stats_sql = """select
count(*) as total_count,
coalesce(sum(case when d.acc_dir = '1' then d.amount else 0 end), 0) as debit_sum,
coalesce(sum(case when d.acc_dir = '0' then d.amount else 0 end), 0) as credit_sum,
min(d.acc_date) as acc_date
coalesce(sum(case when d.acc_dir = '0' then d.amount else 0 end), 0) as credit_sum
from acc_detail d
join account a on d.accountid = a.id COLLATE utf8mb4_unicode_ci
where a.orgid = ${orgid}$
and d.acc_date >= ${start_date}$
and d.acc_date <= ${end_date}$"""
stats_recs = await sor.sqlExe(stats_sql, ns)
stats_recs = await sor.sqlExe(stats_sql, stats_ns)
stats = {
'total_count': int(stats_recs[0].total_count) if stats_recs else 0,
'debit_sum': float(stats_recs[0].debit_sum) if stats_recs else 0,