Преглед изворни кода

pms 物料消耗 立即刷新当前显示的物料列表

zhangcl пре 1 недеља
родитељ
комит
02f5d4c315
1 измењених фајлова са 56 додато и 25 уклоњено
  1. 56 25
      src/views/pms/iotmainworkorder/IotMainWorkOrderOptimize.vue

+ 56 - 25
src/views/pms/iotmainworkorder/IotMainWorkOrderOptimize.vue

@@ -938,8 +938,10 @@ const toggleShowAllMaterials = () => {
   // 切换显示模式时,需要同步更新 materialList
   if (!showAllMaterials.value && currentBomItem.value) {
     // 切换到显示当前保养项物料
-    const uniqueKey = `${currentBomItem.value.deviceId}${currentBomItem.value.bomNodeId}`;
-    materialList.value = bomMaterialsMap.value[uniqueKey] || [];
+    // const uniqueKey = `${currentBomItem.value.deviceId}${currentBomItem.value.bomNodeId}`;
+    // materialList.value = bomMaterialsMap.value[uniqueKey] || [];
+    // 切换到显示当前保养项物料时,使用新的刷新方法
+    refreshCurrentBomItemMaterials();
   } else if (showAllMaterials.value) {
     // 切换到显示所有物料
     const allMaterials = [];
@@ -1346,20 +1348,54 @@ const openDeviceBomMaterials = (row: any) => {
   deviceBomMaterialsRef.value.open(formData.value.deptId, bomNodeId.value, row, type)
 }
 
+// 刷新当前保养项的物料列表显示
+const refreshCurrentBomItemMaterials = () => {
+  if (!currentBomItem.value) return;
+
+  const uniqueKey = `${currentBomItem.value.deviceId}${currentBomItem.value.bomNodeId}`;
+
+  // 强制更新 materialList 的引用,确保响应式更新
+  materialList.value = bomMaterialsMap.value[uniqueKey] ?
+    [...bomMaterialsMap.value[uniqueKey]] : [];
+
+  console.log('刷新物料列表:', {
+    currentBomItem: currentBomItem.value.name,
+    materialCount: materialList.value.length,
+    uniqueKey: uniqueKey
+  });
+}
+
 const selectChoose = (selectedMaterial) => {
+  // 检查当前是否有选中的保养项
+  if (!currentBomItem.value) {
+    message.warning('请先选择一个保养项');
+    return;
+  }
+
+  const targetBomNodeId = currentBomItem.value.bomNodeId;
+  const targetDeviceId = currentBomItem.value.deviceId;
+
   selectedMaterial.bomNodeId = bomNodeId.value
   // 关联 bomNodeId
   const processedMaterials = selectedMaterial.map(material => ({
     ...material,
-    bomNodeId: bomNodeId.value // 统一关联当前行的 bomNodeId
+    bomNodeId: targetBomNodeId, // 统一关联当前行的 bomNodeId
+    deviceId: targetDeviceId // 确保 deviceId 也正确关联
   }));
 
+  // 生成唯一键
+  const uniqueKey = `${targetDeviceId}${targetBomNodeId}`;
+  // 初始化物料映射(如果不存在)
+  if (!bomMaterialsMap.value[uniqueKey]) {
+    bomMaterialsMap.value[uniqueKey] = [];
+  }
+
   // 避免重复添加
   processedMaterials.forEach(newMaterial => {
-    const uniqueKey = `${newMaterial.deviceId}${bomNodeId.value}`;
-    if (!bomMaterialsMap.value[uniqueKey]) {
+    // const uniqueKey = `${newMaterial.deviceId}${bomNodeId.value}`;
+    /* if (!bomMaterialsMap.value[uniqueKey]) {
       bomMaterialsMap.value[uniqueKey] = [];
-    }
+    } */
     // 检查是否已存在相同 工厂+成本中心+库存地点+bomNodeId + materialCode 的条目
     const isExist = bomMaterialsMap.value[uniqueKey].some(item =>
       item.bomNodeId === bomNodeId.value &&
@@ -1375,9 +1411,12 @@ const selectChoose = (selectedMaterial) => {
   });
 
   // 同步到当前显示的 materialList(如果当前选中了该保养项)
-  if (currentBomItem.value && currentBomItem.value.bomNodeId === bomNodeId.value) {
+  /* if (currentBomItem.value && currentBomItem.value.bomNodeId === bomNodeId.value) {
     materialList.value = bomMaterialsMap.value[`${currentBomItem.value.deviceId}${bomNodeId.value}`] || [];
-  }
+  } */
+
+  // 关键修复:立即更新当前显示的物料列表
+  refreshCurrentBomItemMaterials();
 
   // 选择物料后立即计算费用
   nextTick(() => {
@@ -1547,7 +1586,8 @@ const handleDeleteMaterialInTable = (material) => {
   // 确认删除对话框
   message.confirm('确认删除该物料吗?', { title: '提示' }).then(() => {
     const targetBomNodeId = material.bomNodeId;
-    const uniqueKey = `${material.deviceId}${targetBomNodeId}`;
+    const targetDeviceId = material.deviceId;
+    const uniqueKey = `${targetDeviceId}${targetBomNodeId}`;
 
     // 从 bomMaterialsMap 中删除
     if (bomMaterialsMap.value[uniqueKey]) {
@@ -1565,20 +1605,8 @@ const handleDeleteMaterialInTable = (material) => {
       }
     }
 
-    // 从 materialList 中删除
-    const index = materialList.value.findIndex(item =>
-      (item.isNew && item.tempId === material.tempId) ||
-      (!item.isNew &&
-        item.bomNodeId === targetBomNodeId &&
-        item.factoryId === material.factoryId &&
-        item.costCenterId === material.costCenterId &&
-        item.storageLocationId === material.storageLocationId &&
-        item.materialCode === material.materialCode)
-    );
-
-    if (index !== -1) {
-      materialList.value.splice(index, 1);
-    }
+    // 使用新的刷新方法更新显示
+    refreshCurrentBomItemMaterials();
     // 删除物料后重新计算费用
     nextTick(() => {
       calculateTotalCost();
@@ -2320,10 +2348,13 @@ const handleBomItemClick = (item) => {
   // 切换到当前保养项物料视图
   showAllMaterials.value = false;
   // 生成唯一键
-  const uniqueKey = `${item.deviceId}${item.bomNodeId}`;
+  // const uniqueKey = `${item.deviceId}${item.bomNodeId}`;
 
   // 更新物料列表
-  materialList.value = bomMaterialsMap.value[uniqueKey] || [];
+  // materialList.value = bomMaterialsMap.value[uniqueKey] || [];
+
+  // 使用新的刷新方法
+  refreshCurrentBomItemMaterials();
 
   // 切换保养项时重新计算费用
   nextTick(() => {