From 0ddf63f3c0427501fae872d5cd91d2b550b17c07 Mon Sep 17 00:00:00 2001 From: colaftc Date: Thu, 25 Dec 2025 19:11:06 +0800 Subject: [PATCH] fix: exclude ran out state_id for api_v2.get_plate_orders_bet_by_state_status --- api_v2/tests.py | 15 +++++++++++++++ api_v2/views/printing.py | 17 ++++++++++++++++- docs/api_v2_plate_orders_by_state_status.md | 2 +- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/api_v2/tests.py b/api_v2/tests.py index c5fa1fe..c1000c4 100644 --- a/api_v2/tests.py +++ b/api_v2/tests.py @@ -1165,6 +1165,21 @@ class PlateOrderByStateStatusV2APITest(TestCase): ids = [item['id'] for item in resp.data['results']] self.assertNotIn(self.po_other_process.id, ids) + def test_completed_status_only_returns_latest_state(self): + resp = self.client.get( + self.url, + { + 'process_id': self.process.id, + 'state_id': self.state_prepare.id, + 'status': 'completed', + } + ) + self.assertEqual(resp.status_code, 200) + ids = [item['id'] for item in resp.data['results']] + self.assertEqual(set(ids), {self.po_not_started.id, self.po_cancelled.id}) + self.assertNotIn(self.po_completed_old.id, ids) + self.assertNotIn(self.po_completed_new.id, ids) + def test_without_state_id_returns_orders_without_any_logs(self): resp = self.client.get( self.url, diff --git a/api_v2/views/printing.py b/api_v2/views/printing.py index 79d8619..bafcbfd 100644 --- a/api_v2/views/printing.py +++ b/api_v2/views/printing.py @@ -3,7 +3,7 @@ import datetime from django.utils import timezone from django.db import transaction from django.db import models as django_models -from django.db.models import Q, Count, CharField, Prefetch, Exists, OuterRef +from django.db.models import Q, Count, CharField, Prefetch, Exists, OuterRef, Subquery from django.db.models.functions import Cast, Coalesce from rest_framework import serializers, status, permissions from rest_framework.pagination import LimitOffsetPagination @@ -1092,6 +1092,21 @@ class PlateOrderByStateStatusView(APIView): ) ) + if status_value == 'completed': + latest_completed_state_subquery = ( + stateflow_models.StateFlowRecord.objects + .filter( + business_object_id=OuterRef('business_object_id'), + is_cancelled=False, + ) + .order_by('-completed_at', '-id') + ) + queryset = queryset.annotate( + latest_completed_state_id=Subquery( + latest_completed_state_subquery.values('state_id')[:1] + ) + ).filter(latest_completed_state_id=state_id_int) + ordering = (qp.get('ordering') or '-created_at').strip() or '-created_at' direction = '-' if ordering.startswith('-') else '' field = ordering[1:] if ordering.startswith('-') else ordering diff --git a/docs/api_v2_plate_orders_by_state_status.md b/docs/api_v2_plate_orders_by_state_status.md index 2e29e55..751e11e 100644 --- a/docs/api_v2_plate_orders_by_state_status.md +++ b/docs/api_v2_plate_orders_by_state_status.md @@ -26,7 +26,7 @@ | `offset` | 否 | int | `0` | LimitOffsetPagination 的 offset | > `status` 说明: -> - `completed`:该节点存在未撤销的 `StateFlowRecord` +> - `completed`:该节点是订单最近一次未撤销完成的节点(同一订单不会因历史记录出现在多个节点) > - `not_started`:从未对该节点留下任何 `StateFlowRecord` > - `cancelled`:最近一次执行已被撤销(存在 `is_cancelled=True` 的记录,且无未撤销记录) > - `in_progress`:为后续扩展保留(当前流程模型中不会命中)