|
@@ -140,7 +140,90 @@ class locationViewSet(viewsets.ModelViewSet):
|
|
|
else:
|
|
|
location_group_obj.status = 'available'
|
|
|
location_group_obj.save()
|
|
|
-
|
|
|
+
|
|
|
+ def batch_status_location(self, request):
|
|
|
+ """
|
|
|
+ 优化版:批量获取库位批次状态
|
|
|
+ 基于模型结构优化查询
|
|
|
+ """
|
|
|
+ layer = request.data.get('layer')
|
|
|
+
|
|
|
+ # 使用反向关系名 'container_links' 进行预取
|
|
|
+ locations = LocationModel.objects.filter(
|
|
|
+ layer=layer
|
|
|
+ ).prefetch_related(
|
|
|
+ Prefetch(
|
|
|
+ 'container_links', # 使用模型定义的 related_name
|
|
|
+ queryset=LocationContainerLink.objects.filter(is_active=True)
|
|
|
+ .select_related('container'),
|
|
|
+ to_attr='active_links'
|
|
|
+ )
|
|
|
+ )
|
|
|
+
|
|
|
+ # 收集所有激活链接的容器ID
|
|
|
+ container_ids = set()
|
|
|
+ for loc in locations:
|
|
|
+ if loc.active_links: # 每个库位最多只有一个激活链接
|
|
|
+ container_ids.add(loc.active_links[0].container_id)
|
|
|
+
|
|
|
+ # 批量查询容器详情及其批次状态
|
|
|
+ container_batch_status = defaultdict(dict) # 改为字典存储,避免重复记录
|
|
|
+ if container_ids:
|
|
|
+ container_details = ContainerDetailModel.objects.filter(
|
|
|
+ container_id__in=container_ids
|
|
|
+ ).select_related('batch').exclude(status=3) # 排除已删除或不合格的托盘
|
|
|
+
|
|
|
+ for detail in container_details:
|
|
|
+ if detail.batch_id:
|
|
|
+ # 创建唯一标识的键
|
|
|
+ status_key = (
|
|
|
+ detail.batch.check_status if detail.batch else "404",
|
|
|
+ detail.batch.bound_number if detail.batch else "no_batch"
|
|
|
+ )
|
|
|
+
|
|
|
+ # 如果这个状态尚未添加过,或者需要更新
|
|
|
+ if status_key not in container_batch_status[detail.container_id]:
|
|
|
+ container_batch_status[detail.container_id][status_key] = (
|
|
|
+ detail.batch.check_status if detail.batch else "404",
|
|
|
+ detail.batch.check_time if detail.batch else "no_check_time",
|
|
|
+ detail.batch.bound_number if detail.batch else "no_batch"
|
|
|
+ )
|
|
|
+
|
|
|
+ # 构造返回数据
|
|
|
+ return_data = []
|
|
|
+ for loc in locations:
|
|
|
+ batch_statuses = []
|
|
|
+
|
|
|
+ if loc.active_links:
|
|
|
+ container_id = loc.active_links[0].container_id
|
|
|
+ # 从字典中提取值并转换为列表
|
|
|
+ if container_id in container_batch_status:
|
|
|
+ batch_statuses = list(container_batch_status[container_id].values())
|
|
|
+ else:
|
|
|
+ batch_statuses = [("404", "no_check_time", "no_batch")]
|
|
|
+
|
|
|
+ # 使用Django模型自带的model_to_dict转换基础字段
|
|
|
+ from django.forms.models import model_to_dict
|
|
|
+ location_data = model_to_dict(loc, fields=[
|
|
|
+ "id", "shelf_type", "row", "col", "layer", "update_time",
|
|
|
+ "empty_label", "location_code", "location_group", "location_type",
|
|
|
+ "status", "max_capacity", "current_quantity", "c_number",
|
|
|
+ "coordinate", "access_priority", "is_active"
|
|
|
+ ])
|
|
|
+
|
|
|
+ # 添加批次状态字段 - 存储所有信息
|
|
|
+ location_data["batch_statuses"] = batch_statuses
|
|
|
+
|
|
|
+ return_data.append(location_data)
|
|
|
+ data = {
|
|
|
+ "code": "200",
|
|
|
+ "msg": "Success Create",
|
|
|
+ "data": return_data
|
|
|
+ }
|
|
|
+
|
|
|
+ return Response(data, status=200)
|
|
|
+
|
|
|
+
|
|
|
class locationGroupViewSet(viewsets.ModelViewSet):
|
|
|
"""
|
|
|
retrieve:
|