diff --git a/apps/mtm/models.py b/apps/mtm/models.py index 3540ee2f..fa0326ac 100644 --- a/apps/mtm/models.py +++ b/apps/mtm/models.py @@ -298,25 +298,51 @@ class RoutePack(CommonADModel): for final_material_id, data in gjson.items(): materialIdList.append(final_material_id) routeIdList.extend(data['routes']) + + # 获取所有相关路由,并确保关联了material_in和material_out route_qs = Route.objects.filter(id__in=routeIdList).select_related("material_in", "material_out", "process") - matids1 = route_qs.values_list("material_in__id", flat=True).distinct() - matids2 = route_qs.values_list("material_out__id", flat=True).distinct() - materialIdList.extend(list(matids1)) - materialIdList.extend(list(matids2)) - route_dict = {r.id: {"label": r.process.name, "source": r.material_in.id, "target": r.material_out.id} for r in route_qs} + + # 收集所有相关物料ID,并过滤掉None值 + matids1 = [mid for mid in route_qs.values_list("material_in__id", flat=True) if mid is not None] + matids2 = [mid for mid in route_qs.values_list("material_out__id", flat=True) if mid is not None] + materialIdList.extend(matids1) + materialIdList.extend(matids2) + + # 构建路由字典,添加空值检查 + route_dict = {} + for r in route_qs: + if r.material_in and r.material_out: # 只有两个物料都存在时才添加 + route_dict[r.id] = { + "label": r.process.name if r.process else "", + "source": r.material_in.id, + "target": r.material_out.id + } + + # 获取所有物料信息 mat_qs = Material.objects.filter(id__in=materialIdList).order_by("process__sort", "create_time") - mat_dict = {mat.id: {"id": mat.id, "label": str(mat), "shape": "rect"}for mat in mat_qs} + mat_dict = {mat.id: {"id": mat.id, "label": str(mat), "shape": "rect"} for mat in mat_qs} + res = {} for final_material_id, data in gjson.items(): + # 确保最终物料存在于mat_dict中 + if final_material_id not in mat_dict: + continue + item = {"name": mat_dict[final_material_id]["label"]} edges = [] nodes_set = set() + for route_id in data['routes']: - edges.append(route_dict[route_id]) - nodes_set.update([route_dict[route_id]['source'], route_dict[route_id]['target']]) + # 只处理存在于route_dict中的路由 + if route_id in route_dict: + edges.append(route_dict[route_id]) + nodes_set.update([route_dict[route_id]['source'], route_dict[route_id]['target']]) + item['edges'] = edges - item['nodes'] = [mat_dict[node_id] for node_id in nodes_set] + # 只添加存在于mat_dict中的节点 + item['nodes'] = [mat_dict[node_id] for node_id in nodes_set if node_id in mat_dict] res[final_material_id] = item + return res def get_final_material_ids(self): diff --git a/apps/mtm/views.py b/apps/mtm/views.py index ce326a4b..c9f660e2 100644 --- a/apps/mtm/views.py +++ b/apps/mtm/views.py @@ -262,13 +262,13 @@ class RoutePackViewSet(CustomModelViewSet): if f.name in ['process', 'sort', 'is_autotask', 'is_count_utask', 'out_rate', 'div_number', 'hour_work', 'batch_bind']: setattr(route_new, f.name, getattr(route, f.name, None)) route_new.material = matout - material_out = RouteSerializer.gen_material_out(instance=route_new, material_out_tracking=route.material_out.tracking) - route_new.material_out = material_out - if ind == 0: - route_new.material_in = matin - elif route.material_in.process and route.material_in.process.id in genM: - route_new.material_in = genM[route.material_in.process.id] - genM[process.id] = material_out + # material_out = RouteSerializer.gen_material_out(instance=route_new, material_out_tracking=route.material_out.tracking) + # route_new.material_out = material_out + # if ind == 0: + # route_new.material_in = matin + # elif route.material_in.process and route.material_in.process.id in genM: + # route_new.material_in = genM[route.material_in.process.id] + # genM[process.id] = material_out route_new.routepack = obj_c route_new.create_by = user route_new.create_time = now