From 33e166b104636d5d6aacdcdaddd4c5c072a19198 Mon Sep 17 00:00:00 2001 From: ouyangxq <775153912@qq.com> Date: Fri, 6 Nov 2026 11:52:44 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E9=BD=90=E5=A4=9A=E9=A1=B9=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E7=BB=9F=E8=AE=A1=E4=B8=AD=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E5=88=86=E5=B8=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- logistics_chat/order_listing.py | 20 ++++++++++++++------ tests/test_order_statistics.py | 10 ++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/logistics_chat/order_listing.py b/logistics_chat/order_listing.py index 41748c4..aaba595 100644 --- a/logistics_chat/order_listing.py +++ b/logistics_chat/order_listing.py @@ -150,10 +150,10 @@ def read_orders(tenants,query,page=1,size=5,comparisons=()): connection.row_factory=sqlite3.Row connection.execute('BEGIN') total=with_inbound=0;groups=Counter();selected=[];tail=deque(maxlen=size) - counts=[0 for _ in comparisons] + comparison_groups=[Counter() for _ in comparisons] for item in scoped_orders(connection,tenants): for index,comparison in enumerate(comparisons): - counts[index]+=matches_filters(item,comparison) + if matches_filters(item,comparison):comparison_groups[index][item['stage']]+=1 if not matches_filters(item,query):continue total+=1;with_inbound+=bool((item['inbound_code'] or '').strip());groups[item['stage']]+=1 tail.append(item) @@ -161,7 +161,8 @@ def read_orders(tenants,query,page=1,size=5,comparisons=()): last=max(1,(total+size-1)//size) if page>last: page=last;selected=list(tail)[-(total-(last-1)*size):] if total else [] - return {'total':total,'with_inbound':with_inbound,'groups':groups,'rows':selected,'page':page,'counts':counts} + return {'total':total,'with_inbound':with_inbound,'groups':groups,'rows':selected,'page':page, + 'counts':[sum(group.values()) for group in comparison_groups],'comparison_groups':comparison_groups} finally:connection.close() @@ -190,6 +191,11 @@ def summary_text(query,total): return text +def stage_distribution(groups): + return ['按当前实际物流节点分布(每票只计一次):']+[ + f'- {label}:{groups[code]}票' for code,label in STAGE_LABELS.items() if groups[code]] + + def answer_summary(intent,tenants,conv_id=None): from .customer_service import order_login_response if not tenants:return order_login_response() @@ -202,11 +208,13 @@ def answer_summary(intent,tenants,conv_id=None): try: data=read_orders(tenants,query,comparisons=comparisons) total=data['total'] - lines=[summary_text(part,count)+'。' for part,count in zip(comparisons,data['counts'])] + lines=[] + for part,count,groups in zip(summaries[:-1],data['counts'],data['comparison_groups']): + lines.append(summary_text(part,count)+'。') + if part.get('grouped'):lines.extend(stage_distribution(groups)) lines.append(summary_text(query,total)+f',其中 {data["with_inbound"]} 票已登记入仓码。') if summaries[-1].get('grouped'): - lines.append('按当前实际物流节点分布(每票只计一次):') - lines.extend(f'- {label}:{data["groups"][code]}票' for code,label in STAGE_LABELS.items() if data['groups'][code]) + lines.extend(stage_distribution(data['groups'])) for part in requested: if part['unsupported']: question=re.sub(r'\s+',' ',part['question'])[:120] diff --git a/tests/test_order_statistics.py b/tests/test_order_statistics.py index 524d52c..cd80932 100644 --- a/tests/test_order_statistics.py +++ b/tests/test_order_statistics.py @@ -145,6 +145,16 @@ class OrderStatisticsTests(unittest.TestCase): self.assertIn('暂无实际节点:3票',text) self.assertIn('共有 16 票订单',self.ask('或者订单有多少条?')['t']) + def test_combined_statistics_keep_node_distribution_before_exception_count(self): + self.login() + text=self.ask('我的订单各个轨迹节点分别有多少,异常的有多少条')['t'] + self.assertIn('共有 16 票订单',text) + self.assertIn('船舶已到目的港:10票',text) + self.assertIn('已清关、可提柜:2票',text) + self.assertIn('暂无实际节点:3票',text) + self.assertIn('7 票明确标记异常',text) + self.assertEqual(text.count('- 入仓码:'),5) + def test_ambiguous_speech_answers_the_clear_count_and_requests_the_missing_meaning(self): self.login() text=self.ask('当前,我的订单有多少条,一层有多少条?')['t']