Prechádzať zdrojové kódy

fix:修复大田选中,果树没有的问题

wangsisi 3 týždňov pred
rodič
commit
469838a2d5

+ 43 - 2
src/views/old_mini/entry_information/components/selectEquipment.vue

@@ -300,6 +300,29 @@ function getCurrentScope() {
     return "";
 }
 
+function getSelectedNames(list) {
+    return new Set((list || []).map((item) => item?.name).filter(Boolean));
+}
+
+function getFruitItemsByFieldNames(fieldItems) {
+    const names = getSelectedNames(fieldItems);
+    if (!names.size) return [];
+    return FRUIT_GROUPS.flatMap((group) =>
+        group.items
+            .filter((item) => names.has(item.name))
+            .map((item) => ({ ...item, scope: "fruit", groupName: group.name }))
+    );
+}
+
+function mergeEquipmentById(existing, extra) {
+    const map = new Map();
+    [...(existing || []), ...(extra || [])].forEach((item) => {
+        if (item?.id == null) return;
+        map.set(String(item.id), item);
+    });
+    return Array.from(map.values());
+}
+
 function restoreSelection() {
     const cached = readJson(EQUIPMENT_KEY);
     const scope = getCurrentScope();
@@ -319,6 +342,19 @@ function restoreSelection() {
     });
 }
 
+function applyFieldOverlapToFruit() {
+    if (!props.isService || getCurrentScope() !== "fruit") return;
+    const fieldNames = getSelectedNames(getScopeList(readJson(EQUIPMENT_KEY), "field"));
+    if (!fieldNames.size) return;
+    let changed = false;
+    allEquipmentItems.value.forEach((item) => {
+        if (!fieldNames.has(item.name) || selectedIds.has(String(item.id))) return;
+        selectedIds.add(String(item.id));
+        changed = true;
+    });
+    if (changed) saveSelectionDraft();
+}
+
 function saveSelectionDraft() {
     const scope = getCurrentScope();
     const selected = allEquipmentItems.value
@@ -338,8 +374,12 @@ function saveSelectionDraft() {
         field: Array.isArray(cached) ? cached : cached?.field || [],
         fruit: Array.isArray(cached) ? [] : cached?.fruit || [],
     };
-    if (scope === "fruit") next.fruit = selected;
-    else next.field = selected;
+    if (scope === "fruit") {
+        next.fruit = selected;
+    } else {
+        next.field = selected;
+        next.fruit = mergeEquipmentById(next.fruit, getFruitItemsByFieldNames(selected));
+    }
     sessionStorage.setItem(EQUIPMENT_KEY, JSON.stringify(next));
 }
 
@@ -538,6 +578,7 @@ const handleSubmit = () => {
 onMounted(() => {
     fetchEquipmentList();
     restoreSelection();
+    applyFieldOverlapToFruit();
 });
 </script>