liding
8 天以前 e3bf21045fc29371449c7e888d786370dc962ae1
feat(inspection): 优化检验数据保存逻辑
已修改4个文件
193 ■■■■ 文件已修改
src/api/business/inspectionTask.js 20 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/views/business/inspectionTask/inspection.vue 103 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/views/business/inspectionView/index.vue 54 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/workers/InspectionWorker.worker.js 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/api/business/inspectionTask.js
@@ -235,13 +235,21 @@
  });
}
// 保存检验内容
// 保存检验内容。检验值输入会递归触发多个公式计算,保存请求必须按触发顺序执行,
// 否则较早的请求可能晚于新请求完成并覆盖最新结果。
let saveInsContextQueue = Promise.resolve();
export function saveInsContext(data) {
  return request({
    url: "/insOrderPlan/saveInsContext",
    method: "post",
    data: data,
  });
  const saveRequest = saveInsContextQueue.then(() =>
    request({
      url: "/insOrderPlan/saveInsContext",
      method: "post",
      data: data,
    })
  );
  // 单次保存失败不能阻断后续输入的保存。
  saveInsContextQueue = saveRequest.catch(() => undefined);
  return saveRequest;
}
// 附件下载
src/views/business/inspectionTask/inspection.vue
@@ -144,7 +144,7 @@
          <el-select v-if="cableTagList.length > 0" v-model="currentTab" clearable placeholder="请选择" size="small"
            @change="(m) => handleChangeCableTag(currentSample.id, 0, 'cableTag', m)
              " @focus="getCableTag(currentSample.id)">
            <el-option v-for="item in cableTagList" :key="item.cableTag" :label="item.cableTag" :value="item.cableTag">
            <el-option v-for="(item, index) in cableTagList" :key="index" :label="item.cableTag" :value="item.cableTag">
              <span style="float: left">{{ item.cableTag }}</span>
              <el-tag v-if="item.status == 0" size="small" style="float: right; margin-top: 5px"
                type="danger">未检</el-tag>
@@ -159,7 +159,7 @@
            size="small" @change="(m) =>
              handleChangeCableTag(currentSample.id, 0, 'repetitionTag', m)
              " @focus="getRepetitionTag(currentSample.id)">
            <el-option v-for="item in repetitionTagList" :key="item.repetitionTag" :label="item.radius"
            <el-option v-for="(item, index) in repetitionTagList" :key="index" :label="item.radius"
              :value="item.repetitionTag">
              <span style="float: left">{{ item.radius }}</span>
              <el-tag v-if="item.status == 0" size="small" style="float: right; margin-top: 5px"
@@ -173,7 +173,7 @@
          <span v-if="typeSource == '1'">&nbsp;&nbsp;待检验数量:</span>
          <el-select v-if="typeSource == '1'" v-model="rawMaterialTag" placeholder="请选择" size="small" @change="(m) => handleChangeCableTag(currentSample.id, 4, 'cableTag', m)
            " @focus="getRawMaterialTag(currentSample.id)">
            <el-option v-for="item in rawMaterialTagList" :key="item.rawMaterialTag" :label="item.rawMaterialTag"
            <el-option v-for="(item, index) in rawMaterialTagList" :key="index" :label="item.rawMaterialTag"
              :value="item.rawMaterialTag">
              <span style="float: left">{{ item.rawMaterialTag }}</span>
              <el-tag v-if="item.status == 0" size="small" style="float: right; margin-top: 5px"
@@ -756,6 +756,8 @@
      result: null,
      worker: null,
      worker0: null,
      // 检验值按触发顺序串行保存,避免同一检验项的旧请求覆盖新请求。
      saveQueue: null,
      wareLength: [],
      dataAcquisitionInfo: {},
      dataAcquisitionInfoNew: {},
@@ -898,6 +900,7 @@
    },
  },
  created() {
    this.saveQueue = Promise.resolve();
    let { sonLaboratory, orderId, state, inspectorList, typeSource,isSplit } =
      this.$route.query;
    this.sonLaboratory = sonLaboratory;
@@ -932,8 +935,9 @@
      this.refreshView()
    },
    // 监听当前模板变化
    currentTable(val1, val0) {
    async currentTable(val1, val0) {
      if (val0 != null && val1 != val0) {
        await this.waitForPendingSaves();
        if (this.changeType && this.changeType > 0) {
          // 如果是光纤、光纤带,则不执行下面操作
          return;
@@ -1137,7 +1141,8 @@
      this.checkUser = "";
    },
    // 刷新页面
    refreshView() {
    async refreshView() {
      await this.waitForPendingSaves();
      this.loading = true;
      doInsOrder({
        id: this.id,
@@ -1739,6 +1744,7 @@
    },
    // 切换样品
    async handleChangeSample(row, column, event) {
      await this.waitForPendingSaves();
      // 初始化数据
      this.param = {};
      this.sampleVisible = false;
@@ -2463,26 +2469,30 @@
            currentInsItem: n,
          })
        );
        // 先保存主线程中已经输入的原始值。Worker 完成计算后会再次入队保存计算结果。
        // 这样即使用户输入后立即刷新、切换或返回,最后编辑的单元格也不会丢失。
        if (currentInsItemId) {
          this.saveInsContext(currentInsItemId);
        }
      } catch (error) {
        console.log(444, error);
      }
      // 监听 Worker 返回的结果
      this.worker.onmessage = (event) => {
        this.result = JSON.parse(event.data);
        switch (this.result.method) {
        const workerResult = JSON.parse(event.data);
        this.result = workerResult;
        switch (workerResult.method) {
          case "saveInsContext":
            this.$nextTick(() => {
              const resultValue = workerResult.value;
              // this.$delete(this.tableList[0],'arr')
              this.$set(
                this.tableList[0],
                "arr",
                this.result.value.tableList[0].arr
                resultValue.tableList[0].arr
              );
              this.param = this.result.value.param;
              if (this.result.value.currentInsItemId) {
                currentInsItemId = this.result.value.currentInsItemId;
              }
              this.param = resultValue.param;
              // 特殊处理一下结论,会有这种特殊情况
              for (var i in this.param) {
                if (
@@ -2497,23 +2507,28 @@
                  }
                }
              }
              this.saveInsContext(currentInsItemId);
              const sourceInsItemId =
                resultValue.sourceInsItemId != null &&
                  resultValue.sourceInsItemId !== ""
                  ? resultValue.sourceInsItemId
                  : currentInsItemId;
              this.saveInsContext([
                sourceInsItemId,
                resultValue.currentInsItemId,
              ]);
            });
            break;
          case "tableList":
            this.$nextTick(() => {
              // 更新数据
              this.$delete(this.tableList[0], "arr");
              this.$set(this.tableList[0], "arr", this.result.value[0].arr);
              this.$set(this.tableList[0], "arr", workerResult.value[0].arr);
              // this.param = this.result.value.param
              if (this.result.value.currentInsItem) {
                currentInsItemId = this.result.value.currentInsItem.i;
              }
            });
            break;
          case "getCurrentInsProduct":
            // 更新页面数据
            this.getCurrentInsProduct(this.result.value);
            this.getCurrentInsProduct(workerResult.value);
            break;
        }
      };
@@ -2830,7 +2845,7 @@
          this.submitLoading = false;
        });
    },
    submit() {
    async submit() {
      if (this.verifyUser === null || this.verifyUser === "") {
        this.$message.error("请指定复核人员");
        return;
@@ -2851,6 +2866,7 @@
        this.$message.error("请填写IFS库存物料批次属性");
        return;
      }
      await this.waitForPendingSaves();
      this.submitLoading = true;
      checkSubmitPlan({
        orderId: this.orderId,
@@ -2940,14 +2956,26 @@
      return;
    },
    // 统一在这里保存数据
    saveInsContext(currentInsItemId) {
    saveInsContext(currentInsItemIds) {
      try {
        if (this.param) {
          let param = null;
          if (currentInsItemId) {
            param = { [currentInsItemId]: this.param[currentInsItemId] };
          } else {
            param = this.param;
          const ids = (Array.isArray(currentInsItemIds)
            ? currentInsItemIds
            : [currentInsItemIds]
          )
            .filter((id) => id != null && id !== "")
            .map((id) => String(id));
          let param = this.param;
          if (ids.length > 0) {
            param = {};
            [...new Set(ids)].forEach((id) => {
              if (this.param[id]) {
                param[id] = this.param[id];
              }
            });
            if (Object.keys(param).length === 0) {
              return;
            }
          }
          let isNoTestValue = ''
          for (let key in param) {
@@ -2959,16 +2987,25 @@
              }
            }
          }
          saveInsContext({
          const payload = {
            param: JSON.stringify(param),
            currentTable: this.currentTable,
            sampleId: this.currentSample.id,
            orderId: this.orderId,
            sonLaboratory: this.sonLaboratory,
            isNoTestValue: isNoTestValue
          }).then((res) => {
            this.$message.success("已保存");
          });
          };
          const previousSave = this.saveQueue || Promise.resolve();
          this.saveQueue = previousSave
            .catch(() => undefined)
            .then(() => saveInsContext(payload))
            .then(() => {
              this.$message.success("已保存");
            })
            .catch((error) => {
              console.error("检验数据保存失败", error);
              this.$message.error("检验数据保存失败,请重试");
            });
          // 向 Worker 发送消息,开始处理逻辑
          this.worker.postMessage(
            JSON.stringify({
@@ -2982,6 +3019,11 @@
        }
      } catch (error) {
        console.log(999, error);
      }
    },
    async waitForPendingSaves() {
      if (this.saveQueue) {
        await this.saveQueue;
      }
    },
    // 设备改变
@@ -3145,7 +3187,8 @@
        return v;
      }
    },
    goback() {
    async goback() {
      await this.waitForPendingSaves();
      this.$router.go(-1)
    }
  },
src/views/business/inspectionView/index.vue
@@ -2224,20 +2224,19 @@
      // 监听 Worker 返回的结果
      this.worker.onmessage = (event) => {
        this.result = JSON.parse(event.data);
        switch (this.result.method) {
        const workerResult = JSON.parse(event.data);
        this.result = workerResult;
        switch (workerResult.method) {
          case "saveInsContext":
            this.$nextTick(() => {
              const resultValue = workerResult.value;
              // this.$delete(this.tableList[0],'arr')
              this.$set(
                this.tableList[0],
                "arr",
                this.result.value.tableList[0].arr
                resultValue.tableList[0].arr
              );
              this.param = this.result.value.param;
              if (this.result.value.currentInsItemId) {
                currentInsItemId = this.result.value.currentInsItemId;
              }
              this.param = resultValue.param;
              // 特殊处理一下结论,会有这种特殊情况
              for (var i in this.param) {
                if (
@@ -2252,23 +2251,28 @@
                  }
                }
              }
              this.saveInsContext(currentInsItemId);
              const sourceInsItemId =
                resultValue.sourceInsItemId != null &&
                  resultValue.sourceInsItemId !== ""
                  ? resultValue.sourceInsItemId
                  : currentInsItemId;
              this.saveInsContext([
                sourceInsItemId,
                resultValue.currentInsItemId,
              ]);
            });
            break;
          case "tableList":
            this.$nextTick(() => {
              // 更新数据
              this.$delete(this.tableList[0], "arr");
              this.$set(this.tableList[0], "arr", this.result.value[0].arr);
              this.$set(this.tableList[0], "arr", workerResult.value[0].arr);
              // this.param = this.result.value.param
              if (this.result.value.currentInsItem) {
                currentInsItemId = this.result.value.currentInsItem.i;
              }
            });
            break;
          case "getCurrentInsProduct":
            // 更新页面数据
            this.getCurrentInsProduct(this.result.value);
            this.getCurrentInsProduct(workerResult.value);
            break;
        }
      };
@@ -2674,14 +2678,26 @@
      return;
    },
    // 统一在这里保存数据
    saveInsContext(currentInsItemId) {
    saveInsContext(currentInsItemIds) {
      try {
        if (this.param) {
          let param = null;
          if (currentInsItemId) {
            param = { [currentInsItemId]: this.param[currentInsItemId] };
          } else {
            param = this.param;
          const ids = (Array.isArray(currentInsItemIds)
            ? currentInsItemIds
            : [currentInsItemIds]
          )
            .filter((id) => id != null && id !== "")
            .map((id) => String(id));
          let param = this.param;
          if (ids.length > 0) {
            param = {};
            [...new Set(ids)].forEach((id) => {
              if (this.param[id]) {
                param[id] = this.param[id];
              }
            });
            if (Object.keys(param).length === 0) {
              return;
            }
          }
          saveInsContext({
            param: JSON.stringify(param),
src/workers/InspectionWorker.worker.js
@@ -30,6 +30,9 @@
let getDataTypeId = null;
//当前检验项
let currentInsItem = null;
// 当前这次输入对应的原始检验项。计算过程中 pId 会切换到下游计算项,
// 保存时必须同时带回原始检验项,否则只会保存计算结果。
let sourceInsItemId = null;
// 接收到主线程处理逻辑的消息
self.onmessage = function (event) {
  // 保存主线程传过来的值
@@ -54,6 +57,11 @@
  }
  modelType = data.modelType;
  code = data.code;
  const codeParts = typeof code === "string" ? code.split("-") : [];
  sourceInsItemId =
    data.currentInsItem && data.currentInsItem.i != null
      ? data.currentInsItem.i
      : codeParts[3];
  if (data.getDataTypeId) {
    // 记录 优化数采较多数据时-记录最后一个检验项的id
    getDataTypeId = data.getDataTypeId;
@@ -490,6 +498,7 @@
                param: getParam(), //传给后端的参数
                getDataTypeId: getDataTypeId,
                currentInsItemId: item.i,
                sourceInsItemId: sourceInsItemId,
              },
            };
            // 发消息给主线程
@@ -703,6 +712,7 @@
        param: getParam(), //传给后端的参数
        getDataTypeId: getDataTypeId,
        currentInsItemId: currentInsItemId,
        sourceInsItemId: sourceInsItemId,
      },
    };
    // 发消息给主线程
@@ -847,7 +857,11 @@
        b.v.ps.value.includes("检验值")
      ) {
        // 赋值检验值
        b.i && b.v.v && param[b.i].insValue.push(b);
        b.i &&
          b.v.v !== "" &&
          b.v.v !== null &&
          b.v.v !== undefined &&
          param[b.i].insValue.push(b);
      }
      if (b.v.ps != undefined && b.v.ps.value === "计算值") {
        // 赋值计算值