yanghao 6 giorni fa
parent
commit
1106f201c0
2 ha cambiato i file con 190 aggiunte e 333 eliminazioni
  1. 190 139
      pages/recordFilling/detail.vue
  2. 0 194
      store/modules/fillingData.js

+ 190 - 139
pages/recordFilling/detail.vue

@@ -175,14 +175,7 @@
 </template>
 
 <script setup>
-import {
-  ref,
-  reactive,
-  getCurrentInstance,
-  watch,
-  onMounted,
-  nextTick,
-} from "vue";
+import { ref, reactive, getCurrentInstance, watch, onMounted } from "vue";
 import { onReady, onLoad } from "@dcloudio/uni-app";
 import dayjs from "dayjs";
 import {
@@ -200,12 +193,6 @@ import { useDataDictStore } from "@/store/modules/dataDict";
 const { appContext } = getCurrentInstance();
 const t = appContext.config.globalProperties.$t;
 
-// 导入 store
-import { useFillingDataStore } from "@/store/modules/fillingData";
-
-// 获取 store 实例
-const fillingDataStore = useFillingDataStore();
-
 // 获取字典项
 const { getStrDictOptions, getIntDictOptions } = useDataDictStore();
 
@@ -214,6 +201,14 @@ const isFromMsg = ref(false);
 const params = ref({});
 const isView = ref(false); // 是否编辑 -- view == 1为编辑状态
 
+// 累加状态对象,用于在生产日报加载前存储累加值
+const accumulatedValues = reactive({
+  "当日注水量-方": 0,
+  当日用电量kWh: 0,
+  当日运转时间H: 0,
+  "当日注气量-方": 0,
+});
+
 onMounted(() => {
   console.log("onMounted");
 });
@@ -251,18 +246,6 @@ onLoad(async (option) => {
   // 处理是否可编辑 {0: '待填写', 1: '已完成', 2: '填写中', 3: '忽略'}
   isView.value = params.value?.orderStatus % 2 == 0;
   console.log("🚀 ~ isView.value:", isView.value);
-
-  // 清理旧的 store 数据
-  if (params.value.orderId) {
-    fillingDataStore.clearData(params.value.orderId);
-  }
-
-  // 页面加载完成后,延迟一点时间确保数据加载完成后再更新生产日报
-  setTimeout(() => {
-    nextTick(() => {
-      updateProductionReportDisplay();
-    });
-  }, 500);
 });
 const paging = ref(null);
 // v-model绑定的这个变量不要在分页请求结束中自己赋值,直接使用即可
@@ -279,50 +262,32 @@ watch(
   { deep: true }
 );
 
-// 更新生产日报显示
-// 更新生产日报显示 - 更准确的方法
-const updateProductionReportDisplay = () => {
-  // 获取最新的累计值
-  const latestTotals = fillingDataStore.getAllTotals();
-  console.log("更新生产日报显示,最新的累计值:", latestTotals);
-
-  // 在当前页面数据中查找生产日报
-  const reportItem = dataList.value.find(
-    (item) => item.deviceName === "生产日报"
-  );
-
-  if (!reportItem || !reportItem.nonSumList) {
-    console.warn("⚠️ 当前页面未找到生产日报或其 nonSumList");
-    return;
+// 处理fillContent变化的方法
+const handleFillContentChange = (nosum, deviceItem) => {
+  console.log("🚀 ~ nosum, deviceItem:", nosum, deviceItem);
+  // 处理增压机
+  if (
+    deviceItem.deviceName.includes("增压机") &&
+    nosum.name === "当日运转时间"
+  ) {
+    calculateTotalRunTime("增压机", "当日运转时间"); // 计算当日运转时间总和
+  }
+  // 处理提纯撬
+  if (deviceItem.deviceName.includes("提纯撬") && nosum.name === "当日注气量") {
+    calculateTotalRunTime("提纯撬", "当日注气量"); // 计算当日注气量总和
+  }
+  // 处理注水泵
+  if (deviceItem.deviceName.includes("注水泵") && nosum.name === "当日注水量") {
+    calculateTotalRunTime("注水泵", "当日注水量"); // 计算当日注水量总和
+  }
+  // 处理箱式变电站
+  if (
+    deviceItem.deviceName.includes("箱式变电站") &&
+    nosum.name === "当日用电量"
+  ) {
+    calculateTotalRunTime("箱式变电站", "当日用电量"); // 计算当日用电量总和
   }
-
-  // 更新生产日报中的累计字段
-  reportItem.nonSumList.forEach((item) => {
-    // 根据字段名称匹配对应的累计值
-    if (latestTotals.hasOwnProperty(item.name)) {
-      const newValue = toFixed(latestTotals[item.name]);
-      console.log(`更新生产日报字段 ${item.name}: ${newValue}`);
-
-      // 只有当值真正变化时才更新,避免不必要的响应式更新
-      if (item.fillContent !== newValue) {
-        item.fillContent = newValue;
-      }
-    }
-  });
 };
-// 监听 store 中的 totals 变化并更新生产日报
-// 监听 store 中的 totals 变化并更新生产日报显示
-watch(
-  () => fillingDataStore.totals,
-  (newTotals) => {
-    console.log("Store totals 更新:", newTotals);
-    // 使用 nextTick 确保 DOM 更新后再更新显示
-    nextTick(() => {
-      updateProductionReportDisplay();
-    });
-  },
-  { deep: true, immediate: true }
-);
 
 // 防抖函数
 function debounce(func, delay) {
@@ -335,27 +300,129 @@ function debounce(func, delay) {
 
 //防抖
 const debouncedCalculateTotalRunTime = debounce(calculateTotalRunTime, 100);
+const debouncedApplyAccumulatedToReport = debounce(
+  applyAccumulatedToReport,
+  100
+);
+
+/**
+ * 更新累加值
+ * @param fieldName 字段名
+ * @param deviceType 设备类型
+ */
+const updateAccumulatedValue = (fieldName, deviceType) => {
+  let total = 0;
+  dataList.value.forEach((item) => {
+    if (item.deviceName.includes(deviceType) && item.nonSumList) {
+      item.nonSumList.forEach((nonSum) => {
+        if (
+          nonSum.type === "double" &&
+          nonSum.name === fieldName &&
+          nonSum.fillContent !== null &&
+          nonSum.fillContent !== ""
+        ) {
+          const value = parseFloat(nonSum.fillContent) || 0;
+          total += value;
+        }
+      });
+    }
+  });
+  accumulatedValues[fieldName] = total;
+};
+
+/**
+ * 重新计算所有累加值
+ */
+const recalculateAllAccumulatedValues = () => {
+  const fields = [
+    { field: "当日运转时间H", device: "增压机" },
+    { field: "当日注气量-方", device: "提纯撬" },
+    { field: "当日注水量-方", device: "注水泵" },
+    { field: "当日用电量kWh", device: "箱式变电站" },
+  ];
+
+  fields.forEach(({ field, device }) => {
+    updateAccumulatedValue(field, device);
+  });
+};
+
+/**
+ * 将累加值应用到生产日报
+ */
+function applyAccumulatedToReport() {
+  // 先重新计算所有累加值
+  recalculateAllAccumulatedValues();
+
+  const reportItem = dataList.value.find(
+    (item) => item.deviceName === "生产日报"
+  );
+
+  if (!reportItem) {
+    console.warn("⚠️ 未找到生产日报,累加值已存储,等待加载");
+    return;
+  }
+
+  Object.keys(accumulatedValues).forEach((fieldName) => {
+    const targetItem = reportItem.nonSumList.find(
+      (item) => item.name === fieldName
+    );
+    if (targetItem) {
+      console.log(
+        `📊 应用累加值 (${fieldName}):`,
+        accumulatedValues[fieldName]
+      );
+      targetItem.fillContent = toFixed(accumulatedValues[fieldName]);
+    }
+  });
+}
 
-// 修改 handleRealTimeUpdate 方法
 const handleRealTimeUpdate = (nosum, deviceItem) => {
   console.log("🚀 实时更新 ~ nosum, deviceItem:", nosum, deviceItem);
 
-  // 检查是否需要累加到生产日报
-  if (fillingDataStore.isAccumulatedField(deviceItem.deviceName, nosum.name)) {
-    console.log(`需要累加的字段: ${deviceItem.deviceName} - ${nosum.name}`);
+  let fieldName = "";
+  let deviceType = "";
 
-    // 更新到 store
-    fillingDataStore.updateFillingItem(
-      deviceItem.deviceId,
-      deviceItem.deviceName,
-      nosum.name,
-      nosum.fillContent
-    );
+  // 当日运转时间累加
+  if (
+    deviceItem.deviceName.includes("增压机") &&
+    nosum.name === "当日运转时间H"
+  ) {
+    fieldName = "当日运转时间H";
+    deviceType = "增压机";
+  }
 
-    // 立即更新生产日报显示
-    nextTick(() => {
-      updateProductionReportDisplay();
-    });
+  // 当日注气量累加
+  if (
+    deviceItem.deviceName.includes("提纯撬") &&
+    nosum.name === "当日注气量-方"
+  ) {
+    fieldName = "当日注气量-方";
+    deviceType = "提纯撬";
+  }
+
+  // 当日注水量累加
+  if (
+    deviceItem.deviceName.includes("注水泵") &&
+    nosum.name === "当日注水量-方"
+  ) {
+    fieldName = "当日注水量-方";
+    deviceType = "注水泵";
+  }
+
+  // 当日用电量累加
+  if (
+    deviceItem.deviceName.includes("箱式变电站") &&
+    nosum.name === "当日用电量kWh"
+  ) {
+    fieldName = "当日用电量kWh";
+    deviceType = "箱式变电站";
+  }
+
+  if (fieldName) {
+    // 更新累加值
+    updateAccumulatedValue(fieldName, deviceType);
+    // 应用到生产日报(如果已加载)
+    debouncedApplyAccumulatedToReport();
   }
 };
 
@@ -422,6 +489,7 @@ const queryList = (pageNo, pageSize) => {
     pageNo,
     pageSize,
     orderId: params.value.orderId,
+    // deviceCategoryId: 1,
   })
     .then(async (res) => {
       console.log("🚀 ~ res:", res);
@@ -429,7 +497,7 @@ const queryList = (pageNo, pageSize) => {
       const resList = [].concat(data.list);
       // 列表总数
       totalNum.value = data.total;
-
+      // 遍历列表,处理attrsDetail
       resList.map(async (item) => {
         const attrParams = {
           deviceCode: item.deviceCode,
@@ -441,8 +509,12 @@ const queryList = (pageNo, pageSize) => {
           userId: params.value.userId,
           orderId: params.value.orderId,
         };
-
+        // console.log(
+        //   "getRecordFillingDetailGetAttrs- attrParams",
+        //   attrParams
+        // );
         const resAttrs = item?.attrsDetail;
+        // console.log("resAttrs", resAttrs);
         if (resAttrs) {
           attrParams.createTime = attrParams.createTime
             ? dayjs(attrParams.createTime).format("YYYY-MM-DD")
@@ -450,12 +522,17 @@ const queryList = (pageNo, pageSize) => {
           attrParams.id = attrParams.orderId;
           delete attrParams.orderId;
           delete attrParams.deviceName;
-
           resAttrs.map((rtem) => {
+            // 将rtem中sumList和nonSumList两个数组中的
+            // fillContent字段判断是否为null, 如果为null,则赋值为0 不为null则保留两位小数
+            // 将attrParams合并到两个数组的每个对象中
+            // 然后将sumList和nonSumList分别赋值给item的sumList和nonSumList
+
             if (rtem.sumList) {
               rtem.sumList.map((sumItem) => {
                 if (sumItem.fillContent == null || sumItem.fillContent == "") {
-                  // 保留原逻辑
+                  // console.log("🚀 ~ rtem.sumList.map ~ sumItem:", sumItem);
+                  // sumItem.fillContent = 0;
                 } else {
                   // 如果是double类型,保留两位小数
                   if (sumItem.type == "double") {
@@ -464,19 +541,24 @@ const queryList = (pageNo, pageSize) => {
                 }
                 // 将sumItem的id赋值给modelId
                 sumItem.modelId = sumItem.id;
+
                 sumItem.pointName = sumItem.name;
                 // 合并attrParams到sumItem中
-                Object.assign(sumItem, attrParams);
+                sumItem = Object.assign(sumItem, attrParams);
               });
             }
-
             if (rtem.nonSumList) {
+              //
               rtem.nonSumList.map((nonSumItem) => {
                 if (
                   nonSumItem.fillContent == null ||
                   nonSumItem.fillContent == ""
                 ) {
-                  // 保留原逻辑
+                  // console.log(
+                  //   "🚀 ~ rtem.nonSumList.map ~ nonSumItem:",
+                  //   nonSumItem
+                  // );
+                  // nonSumItem.fillContent = 0;
                 } else {
                   // 如果是double类型,保留两位小数
                   if (nonSumItem.type == "double") {
@@ -487,10 +569,10 @@ const queryList = (pageNo, pageSize) => {
                 // 将nonSumItem的id赋值给modelId
                 nonSumItem.modelId = nonSumItem.id;
                 // 合并attrParams到nonSumItem中
-                Object.assign(nonSumItem, attrParams);
-
+                nonSumItem = Object.assign(nonSumItem, attrParams);
                 // 如果是enum类型,且description不为null,则根据description获取对应字典项数组,赋值给enumList
                 if (nonSumItem.type == "enum" && nonSumItem.description) {
+                  console.log("🚀 ~ onSumItem.description:");
                   const dictOptions =
                     nonSumItem.name === "非生产原因"
                       ? getIntDictOptions(nonSumItem.description)
@@ -505,6 +587,7 @@ const queryList = (pageNo, pageSize) => {
 
                   // 确保 fillContent 的类型与 enumList 中的 value 类型匹配
                   if (nonSumItem.name === "非生产原因") {
+                    // 如果是"非生产原因",将 fillContent 转换为数字类型以匹配 getIntDictOptions
                     if (
                       nonSumItem.fillContent !== null &&
                       nonSumItem.fillContent !== ""
@@ -512,64 +595,38 @@ const queryList = (pageNo, pageSize) => {
                       nonSumItem.fillContent = parseInt(nonSumItem.fillContent);
                     }
                   }
+
+                  console.log("🚀 ~ nonSumItem.enumList:", nonSumItem.enumList);
                 }
               });
             }
-
             item.sumList = rtem.sumList;
-            item.nonSumList = rtem.nonSumList;
-          });
-        }
 
-        // 将设备数据存储到 store(只存储需要累加的字段)
-        if (item.deviceName !== "生产日报") {
-          const storeData = {
-            deviceId: item.deviceId,
-            deviceName: item.deviceName,
-            deviceCode: item.deviceCode,
-            nonSumList: [],
-          };
-
-          // 处理 nonSumList
-          if (item.nonSumList && Array.isArray(item.nonSumList)) {
-            storeData.nonSumList = item.nonSumList.filter((nosum) =>
-              fillingDataStore.isAccumulatedField(item.deviceName, nosum.name)
-            );
-          }
-
-          // 如果有需要累加的字段,才存储到 store
-          if (storeData.nonSumList.length > 0) {
-            fillingDataStore.updateDeviceFilling(item.deviceId, storeData);
-          }
-        } else {
-          // 如果是生产日报,也存储到 store 并使用 store 中的累计值更新
-          const updatedReportData =
-            fillingDataStore.updateProductionReportFields(item);
-          fillingDataStore.updateDeviceFilling(item.deviceId, {
-            ...updatedReportData,
-            isProductionReport: true,
+            item.nonSumList = rtem.nonSumList;
           });
+          console.log("resAttrs-modelId", resAttrs);
         }
-
         return item;
       });
 
-      // 重新计算所有累计值并更新生产日报显示
-      const calculatedTotals = fillingDataStore.calculateTotals();
-      console.log("计算后的累计值:", calculatedTotals);
-
-      // 立即更新生产日报显示
-      nextTick(() => {
-        updateProductionReportDisplay();
-      });
-
       console.log("resList--", resList);
+      // 将请求结果通过complete传给z-paging处理,同时也代表请求结束,这一行必须调用
+
       paging.value.completeByNoMore(
         resList,
         pageNo * pageSize >= totalNum.value
       );
+
+      // 如果加载的数据中包含生产日报,应用累加值
+      const hasReport = resList.some((item) => item.deviceName === "生产日报");
+      if (hasReport) {
+        applyAccumulatedToReport();
+      }
     })
     .catch((res) => {
+      // 如果请求失败写paging.value.complete(false);
+      // 注意,每次都需要在catch中写这句话很麻烦,z-paging提供了方案可以全局统一处理
+      // 在底层的网络请求抛出异常时,写uni.$emit('z-paging-error-emit');即可
       paging.value.complete(false);
     });
 };
@@ -617,12 +674,6 @@ const toFixed = (num) => {
 };
 
 const onSubmit = async () => {
-  // 确保 store 中的累计值是最新的
-  const latestTotals = fillingDataStore.calculateTotals();
-  console.log("重新计算后的 totals:", latestTotals);
-
-  // 更新生产日报的数据
-  updateProductionReportDisplay();
   // console.log("onSubmit", dataList.value);
   // 校验是否所有待填写项都已加载
   if (dataList.value.length < totalNum) {

+ 0 - 194
store/modules/fillingData.js

@@ -1,194 +0,0 @@
-// 在 store/modules/fillingData.js 中更新字段映射
-
-import { defineStore } from "pinia";
-export const useFillingDataStore = defineStore("fillingData", {
-  state: () => ({
-    // 存储所有设备的填写数据
-    deviceFillings: new Map(), // key: deviceId, value: {设备数据}
-    // 存储累计值(根据 data.txt 中的生产日报字段调整)
-    totals: {
-      "当日运转时间H": 0, // 对应空压机、空气处理撬、液驱机、中压增压撬等
-      "当日注气量-方": 0, // 对应提纯撬
-      "当日注水量-方": 0, // 对应注水泵(虽然数据中没有,但保留逻辑)
-      "当日用电量kWh": 0, // 对应箱式变电站
-    },
-  }),
-
-  actions: {
-    // 更新设备数据并重新计算累计值
-    updateDeviceFilling(deviceId, deviceData) {
-      this.deviceFillings.set(deviceId, deviceData);
-      this.calculateTotals();
-    },
-
-    // 更新单个填写项
-    updateFillingItem(deviceId, deviceName, itemName, value) {
-      const deviceData = this.deviceFillings.get(deviceId) || {
-        deviceId,
-        deviceName,
-      };
-      if (!deviceData.nonSumList) deviceData.nonSumList = [];
-
-      // 查找或创建对应项
-      const itemIndex = deviceData.nonSumList.findIndex(
-        (item) => item.name === itemName
-      );
-      if (itemIndex >= 0) {
-        deviceData.nonSumList[itemIndex].fillContent = value;
-      } else {
-        deviceData.nonSumList.push({
-          name: itemName,
-          fillContent: value,
-          type: "double",
-        });
-      }
-
-      this.updateDeviceFilling(deviceId, deviceData);
-    },
-
-    // 计算所有累计值(根据 data.txt 数据调整逻辑)
-    calculateTotals() {
-      // 重置累计值
-      const newTotals = {
-        "当日运转时间H": 0,
-        "当日注气量-方": 0,
-        "当日注水量-方": 0,
-        "当日用电量kWh": 0,
-      };
-
-      // 遍历所有设备数据
-      this.deviceFillings.forEach((deviceData) => {
-        if (
-          deviceData.nonSumList &&
-          deviceData.deviceName &&
-          !deviceData.isProductionReport
-        ) {
-          const { deviceName, nonSumList } = deviceData;
-
-          // 根据设备名称和字段名判断是否需要累加
-          nonSumList.forEach((item) => {
-            // 确保 fillContent 是数字类型再进行计算
-            const value = parseFloat(item.fillContent) || 0;
-
-            // 需要累加的设备类型和字段(根据 data.txt 数据调整)
-            if (
-              (deviceName.includes("空压机") ||
-               deviceName.includes("空气处理撬") ||
-               deviceName.includes("液驱机") ||
-               deviceName.includes("中压增压撬") ||
-               deviceName.includes("增压机")) && // 包含所有运转时间的设备
-              item.name === "当日运转时间H"
-            ) {
-              newTotals["当日运转时间H"] += value;
-            } else if (
-              deviceName.includes("提纯撬") &&
-              item.name === "当日注气量-方"
-            ) {
-              newTotals["当日注气量-方"] += value;
-            } else if (
-              deviceName.includes("注水泵") &&
-              item.name === "当日注水量-方"
-            ) {
-              newTotals["当日注水量-方"] += value;
-            } else if (
-              deviceName.includes("箱式变电站") &&
-              item.name === "当日用电量kWh"
-            ) {
-              newTotals["当日用电量kWh"] += value;
-            }
-          });
-        }
-      });
-
-      this.totals = newTotals;
-      return newTotals;
-    },
-
-    // 获取指定字段的累计值
-    getTotal(fieldName) {
-      return this.totals[fieldName] || 0;
-    },
-
-    // 获取所有累计值
-    getAllTotals() {
-      return { ...this.totals };
-    },
-
-    // 检查是否是需要累加的字段
-    isAccumulatedField(deviceName, fieldName) {
-      return (
-        // 空压机等设备 -> 当日运转时间H
-        ((deviceName.includes("空压机") ||
-          deviceName.includes("空气处理撬") ||
-          deviceName.includes("液驱机") ||
-          deviceName.includes("中压增压撬") ||
-          deviceName.includes("增压机")) &&
-         fieldName === "当日运转时间H") ||
-        // 提纯撬 -> 当日注气量-方
-        (deviceName.includes("提纯撬") && fieldName === "当日注气量-方") ||
-        // 注水泵 -> 当日注水量-方
-        (deviceName.includes("注水泵") && fieldName === "当日注水量-方") ||
-        // 箱式变电站 -> 当日用电量kWh
-        (deviceName.includes("箱式变电站") && fieldName === "当日用电量kWh")
-      );
-    },
-
-    // 清理指定工单的数据
-    clearData(orderId) {
-      // 这里可以根据 orderId 清理特定工单的数据
-      // 简单实现:清理所有数据
-      this.deviceFillings.clear();
-      this.totals = {
-        "当日运转时间H": 0,
-        "当日注气量-方": 0,
-        "当日注水量-方": 0,
-        "当日用电量kWh": 0,
-      };
-    },
-
-    // 获取需要同步到生产日报的字段映射(根据 data.txt 中的生产日报字段)
-    getProductionReportMapping() {
-      return {
-        // 设备字段名: 生产日报字段名
-        "当日运转时间H": "当日运转时间H",
-        "当日注气量-方": "当日注气量-方",
-        "当日注水量-方": "当日注水量-方",
-        "当日用电量kWh": "当日用电量kWh",
-      };
-    },
-
-    // 获取生产日报字段到设备字段的反向映射
-    getReverseMapping() {
-      const mapping = this.getProductionReportMapping();
-      const reverseMapping = {};
-      for (const [deviceField, reportField] of Object.entries(mapping)) {
-        reverseMapping[reportField] = deviceField;
-      }
-      return reverseMapping;
-    },
-
-    // 更新生产日报字段
-    updateProductionReportFields(reportData) {
-      // 获取反向映射,从生产日报字段到设备字段
-      const reverseMapping = this.getReverseMapping();
-      const newReportData = { ...reportData };
-
-      if (newReportData.nonSumList && Array.isArray(newReportData.nonSumList)) {
-        newReportData.nonSumList = newReportData.nonSumList.map(item => {
-          // 检查这个字段是否是生产日报字段
-          const deviceField = reverseMapping[item.name];
-          if (deviceField && this.totals.hasOwnProperty(deviceField)) {
-            // 如果是累计字段,使用 store 中的累计值
-            return {
-              ...item,
-              fillContent: this.totals[deviceField].toString()
-            };
-          }
-          return item;
-        });
-      }
-
-      return newReportData;
-    }
-  },
-});