zhangwencui
昨天 a80991b51ae07a17de7c9060fb922c1c4a5e85c8
src/views/inventoryManagement/receiptManagement/index.vue
@@ -246,23 +246,22 @@
        <el-form :model="directQuery"
                 class="mb-2">
          <el-form-item label="产品大类">
            <el-select v-model="directQuery.productCategory"
                       placeholder="请选择产品大类"
                       clearable
                       filterable
                       @change="handleProductCategoryChange">
              <el-option v-for="item in productList"
                         :key="item.id"
                         :label="item.productName"
                         :value="item.productName" />
            </el-select>
            <el-tree-select v-model="directQuery.productId"
                            placeholder="请选择产品大类"
                            clearable
                            check-strictly
                            @change="handleProductCategoryChange"
                            :data="productList"
                            :disabled="typeValue === 'edit'"
                            :render-after-expand="false"
                            style="width: 100%" />
          </el-form-item>
          <el-form-item label="规格型号">
            <el-select v-model="directQuery.productModelId"
                       placeholder="请先选择产品大类"
                       clearable
                       filterable
                       :disabled="!directQuery.productCategory">
                       :disabled="!directQuery.productId || typeValue === 'edit'">
              <el-option v-for="item in productModelList"
                         :key="item.id"
                         :label="item.model"
@@ -663,14 +662,16 @@
      openDirectForm(type, row);
    }
  };
  const typeValue = ref("add");
  // 打开直接新增入库弹框
  const openDirectForm = (type, row) => {
  const openDirectForm = async (type, row) => {
    typeValue.value = type;
    directDialogVisible.value = true;
    directSelectedRows.value = [];
    directQuery.value = {
      productId: null,
      productModelId: null,
      productCategory: "",
      inboundQuantity: 0,
      warnNum: 0,
      outStockQuantity: 0,
@@ -688,36 +689,85 @@
      entryDate: getCurrentDate(),
      remark: "",
    };
    // 确保产品大类数据已加载
    // if (productList.value.length === 0) {
    //   await loadProductList();
    // }
    if (type === "edit" && row) {
      // 编辑模式,回显数据
      console.log(row, "==============");
      directForm.value = { ...row };
      // 回显产品大类
      const selectedProduct = productList.value.find(
        item => item.productName === row.productCategory
      );
      if (selectedProduct) {
        directQuery.value.productId = selectedProduct.id;
        // 根据产品大类加载规格型号列表并回显
        loadProductModelList(selectedProduct.id).then(() => {
          // 回显规格型号
          directQuery.value.productCategory = row.productCategory || "";
      // 回显其他字段
      directQuery.value.inboundQuantity = row.inboundNum || row.inboundQuantity;
      directQuery.value.warnNum = row.warnNum || 0;
      directQuery.value.outStockQuantity = row.outStockQuantity || 0;
      directQuery.value.shortageDescription = row.shortageDescription || "";
      // 回显产品大类和规格型号
      if (row.productModelId) {
        // 这里需要根据实际情况获取产品大类ID,暂时假设row中有productId字段
        if (row.productId) {
          directQuery.value.productId = row.productId;
          // 更新产品类别名称
          const productCategory = findNodeById(productList.value, row.productId);
          if (productCategory) {
            directQuery.value.productCategory = productCategory;
          }
          // 根据产品大类加载规格型号列表并回显
          await loadProductModelList(row.productId);
          directQuery.value.productModelId = row.productModelId;
          // 回显其他字段
          directQuery.value.inboundQuantity =
            row.inboundNum || row.inboundQuantity;
          directQuery.value.warnNum = row.warnNum || 0;
          directQuery.value.outStockQuantity = row.outStockQuantity || 0;
          directQuery.value.shortageDescription = row.shortageDescription || "";
        });
        } else {
          // 如果没有productId字段,尝试根据productCategory查找
          // 这里需要根据实际情况调整查找逻辑
          directQuery.value.productId = row.productCategory || "";
          directQuery.value.productModelId = row.specificationModel;
        }
      }
    }
  };
  // 转换产品树数据格式为 el-tree-select 所需格式
  function convertIdToValue(data) {
    return data.map(item => {
      const { id, children, ...rest } = item;
      const newItem = {
        ...rest,
        value: id,
        label: item.productName,
      };
      if (children && children.length > 0) {
        newItem.children = convertIdToValue(children);
      }
      return newItem;
    });
  }
  // 根据 ID 查找节点名称
  const findNodeById = (nodes, productId) => {
    for (let i = 0; i < nodes.length; i++) {
      if (nodes[i].value === productId) {
        return nodes[i].label;
      }
      if (nodes[i].children && nodes[i].children.length > 0) {
        const foundNode = findNodeById(nodes[i].children, productId);
        if (foundNode) {
          return foundNode;
        }
      }
    }
    return null;
  };
  // 加载产品大类列表
  const loadProductList = async () => {
    try {
      const res = await productTreeList();
      productList.value = res;
      // 确保数据格式符合 el-tree-select 要求
      if (Array.isArray(res)) {
        productList.value = convertIdToValue(res);
      } else {
        productList.value = [];
      }
    } catch (error) {
      console.error("加载产品大类失败:", error);
      proxy.$modal.msgError("加载产品大类失败");
@@ -726,16 +776,18 @@
  // 处理产品大类变化
  const handleProductCategoryChange = value => {
    directQuery.value.specificationModel = "";
    directQuery.value.productModelId = "";
    productModelList.value = [];
    if (value) {
      // 根据产品大类加载规格型号列表
      const selectedProduct = productList.value.find(
        item => item.productName === value
      );
      if (selectedProduct) {
        loadProductModelList(selectedProduct.id);
      loadProductModelList(value);
      // 更新产品类别名称
      const productCategory = findNodeById(productList.value, value);
      if (productCategory) {
        directQuery.value.productCategory = productCategory;
      }
    } else {
      directQuery.value.productCategory = "";
    }
  };