debug(f'{params_kw=}') userid = await get_user() userorgid = await get_userorgid() start_date = params_kw.get('start_date', '') end_date = params_kw.get('end_date', '') if not start_date or not end_date: return json.dumps({'total': 0, 'rows': [], 'stats': {'total_count': 0, 'debit_sum': 0, 'credit_sum': 0}}, ensure_ascii=False, default=str) ns = { 'orgid': userorgid, 'start_date': start_date, 'end_date': end_date, 'page': int(params_kw.get('page', 1)), 'rows': int(params_kw.get('rows', 30)), 'sort': 'acc_date desc' } async with get_sor_context(request._run_ns, 'accounting') as sor: sql = """select d.acc_date, d.acc_timestamp, d.acc_dir, d.summary, d.amount, d.balance, s.name as subject_name from acc_detail d join account a on d.accountid = a.id COLLATE utf8mb4_unicode_ci join subject s on a.subjectid = s.id COLLATE utf8mb4_unicode_ci where a.orgid = ${orgid}$ and d.acc_date >= ${start_date}$ and d.acc_date <= ${end_date}$""" rows = await sor.sqlExe(sql, ns) # 统计数据 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 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 = { '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, 'credit_sum': float(stats_recs[0].credit_sum) if stats_recs else 0 } result = { 'total': len(rows) if isinstance(rows, list) else 0, 'rows': rows if isinstance(rows, list) else [], 'stats': stats } return json.dumps(result, ensure_ascii=False, default=str)