forked from erp-dev/erp
feat: added jobs_status_summary to printing orders api
This commit is contained in:
@@ -137,6 +137,7 @@ class PrintingOrderListSerializer(serializers.ModelSerializer):
|
||||
customer_phone = serializers.CharField(source='customer.mobile', read_only=True)
|
||||
process_name = serializers.CharField(source='process.name', read_only=True)
|
||||
progress = serializers.IntegerField(read_only=True)
|
||||
jobs_status_summary = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = models.PrintingOrder
|
||||
@@ -145,10 +146,38 @@ class PrintingOrderListSerializer(serializers.ModelSerializer):
|
||||
'fabric', 'width', 'is_urgent', 'area', 'address', 'curve',
|
||||
'is_fabric_received', 'outgoing_date', 'is_invalid', 'new_curve',
|
||||
'process', 'process_name', 'progress', 'position', 'print_count',
|
||||
'jobs_status_summary',
|
||||
'created_at', 'updated_at',
|
||||
]
|
||||
read_only_fields = ['id', 'human_id', 'created_at', 'updated_at', 'progress', 'print_count']
|
||||
|
||||
def get_jobs_status_summary(self, obj):
|
||||
"""
|
||||
返回订单下所有 PrintingJob 按当前状态分组的数量汇总
|
||||
|
||||
返回格式: [{"state_name": "进度一", "state_id": 1, "count": 3}, ...]
|
||||
- state_name: 状态名称(已完成的返回"已完成")
|
||||
- state_id: 状态ID(已完成的返回 None)
|
||||
- count: 该状态下的 job 数量
|
||||
"""
|
||||
# 使用预取的 printing_jobs(避免 N+1)
|
||||
jobs = getattr(obj, '_prefetched_objects_cache', {}).get('printing_jobs') or obj.printing_jobs.all()
|
||||
|
||||
from collections import Counter
|
||||
status_counter = Counter()
|
||||
|
||||
for job in jobs:
|
||||
# 获取 job 的当前状态显示名称
|
||||
status_name = job.status # 调用 @property,返回状态名或"已完成"
|
||||
status_id = job.status_id # 状态ID,已完成时为 None
|
||||
status_counter[(status_name, status_id)] += 1
|
||||
|
||||
# 转换为列表格式
|
||||
return [
|
||||
{'state_name': name, 'state_id': sid, 'count': count}
|
||||
for (name, sid), count in status_counter.items()
|
||||
]
|
||||
|
||||
|
||||
class PrintingOrderDetailSerializer(serializers.ModelSerializer):
|
||||
"""印染订单详情序列化器"""
|
||||
|
||||
Reference in New Issue
Block a user