src/views/productionManagement/processRoute/Edit.vue
@@ -3,7 +3,7 @@
    <el-dialog
        v-model="isShow"
        title="编辑工艺路线"
        width="400"
        width="800"
        @close="closeModal"
    >
      <el-form label-width="140px" :model="formState" label-position="top" ref="formRef">
@@ -25,35 +25,54 @@
          </el-button>
        </el-form-item>
        <el-form-item
            label="BOM"
            prop="bomId"
            :rules="[
                {
                required: true,
                message: '请选择BOM',
                trigger: 'change',
              }
            ]"
        >
          <el-select
              v-model="formState.bomId"
              placeholder="请选择BOM"
              clearable
              :disabled="!formState.productModelId || bomOptions.length === 0"
              style="width: 100%"
          >
            <el-option
                v-for="item in bomOptions"
                :key="item.id"
                :label="item.bomNo || `BOM-${item.id}`"
                :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input v-model="formState.description" type="textarea" />
        </el-form-item>
        <!-- 工序配置 -->
        <el-form-item label="工序配置" required>
          <el-table :data="formState.processRouteItemList" border size="small" style="width: 100%">
            <el-table-column label="部件" min-width="200">
              <template #default="{ row }">
                <el-select
                    v-model="row.processId"
                    placeholder="请选择部件"
                    clearable
                    filterable
                    style="width: 100%"
                >
                  <el-option
                      v-for="process in processOptions"
                      :key="process.id"
                      :label="formatProcessOptionLabel(process)"
                      :value="process.id"
                  />
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="质检" width="80" align="center">
              <template #default="{ row }">
                <el-switch v-model="row.isQuality" :active-value="true" inactive-value="false"/>
              </template>
            </el-table-column>
            <el-table-column label="备注" min-width="150">
              <template #default="{ row }">
                <el-input v-model="row.remark" placeholder="请输入备注" size="small"/>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="60" align="center">
              <template #default="{ $index }">
                <el-button type="danger" link size="small" @click="removeProcessItem($index)">
                  <el-icon><Delete /></el-icon>
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <div style="margin-top: 8px;">
            <el-button type="primary" link size="small" @click="addProcessItem">
              <el-icon><Plus /></el-icon> 添加工序
            </el-button>
          </div>
        </el-form-item>
      </el-form>
      
@@ -76,7 +95,7 @@
<script setup>
import {ref, computed, getCurrentInstance, onMounted, nextTick, watch} from "vue";
import {update} from "@/api/productionManagement/processRoute.js";
import {getByModel} from "@/api/productionManagement/productBom.js";
import {processList} from "@/api/productionManagement/productionProcess.js";
import ProductSelectDialog from "@/views/basicData/product/ProductSelectDialog.vue";
const props = defineProps({
@@ -99,8 +118,8 @@
  productModelId: undefined,
  productName: "",
  productModelName: "",
  bomId: undefined,
  description: '',
  processRouteItemList: [],
});
const isShow = computed({
@@ -113,9 +132,49 @@
});
const showProductSelectDialog = ref(false);
const bomOptions = ref([]);
const processOptions = ref([]);
let { proxy } = getCurrentInstance()
// 获取工序列表
const getProcessOptions = async () => {
  try {
    const res = await processList();
    processOptions.value = res.data || [];
  } catch (error) {
    console.error("获取工序列表失败", error);
    processOptions.value = [];
  }
};
// 格式化工序选项标签
const formatProcessOptionLabel = (process) => {
  if (!process) return '';
  const typeMap = {
    1: '加工',
    2: '刮板冷芯制作',
    3: '管路组对',
    4: '罐体连接及调试',
    5: '测试打压',
    6: '其他',
  };
  const typeText = typeMap[process.type] || '';
  return `${process.name} ${process.no ? '(' + process.no + ')' : ''} ${typeText ? '[' + typeText + ']' : ''}`;
};
// 添加工序
const addProcessItem = () => {
  formState.value.processRouteItemList.push({
    processId: undefined,
    isQuality: false,
    remark: '',
  });
};
// 移除工序
const removeProcessItem = (index) => {
  formState.value.processRouteItemList.splice(index, 1);
};
const closeModal = () => {
  isShow.value = false;
@@ -131,90 +190,42 @@
      productName: props.record.productName || "",
      // 注意:record中的字段是model,需要映射到productModelName
      productModelName: props.record.model || props.record.productModelName || "",
      bomId: props.record.bomId,
      description: props.record.description || '',
      processRouteItemList: props.record.processRouteItemList || [],
    };
    // 如果有产品型号ID,加载BOM列表
    if (props.record.productModelId) {
      loadBomList(props.record.productModelId);
    }
  }
}
// 加载BOM列表
const loadBomList = async (productModelId) => {
  if (!productModelId) {
    bomOptions.value = [];
    return;
  }
  try {
    const res = await getByModel(productModelId);
    // 处理返回的BOM数据:可能是数组、对象或包含data字段
    let bomList = [];
    if (Array.isArray(res)) {
      bomList = res;
    } else if (res && res.data) {
      bomList = Array.isArray(res.data) ? res.data : [res.data];
    } else if (res && typeof res === 'object') {
      bomList = [res];
    }
    bomOptions.value = bomList;
  } catch (error) {
    console.error("加载BOM列表失败:", error);
    bomOptions.value = [];
  }
};
// 产品选择处理
const handleProductSelect = async (products) => {
  if (products && products.length > 0) {
    const product = products[0];
    // 先查询BOM列表(必选)
    try {
      const res = await getByModel(product.id);
      // 处理返回的BOM数据:可能是数组、对象或包含data字段
      let bomList = [];
      if (Array.isArray(res)) {
        bomList = res;
      } else if (res && res.data) {
        bomList = Array.isArray(res.data) ? res.data : [res.data];
      } else if (res && typeof res === 'object') {
        bomList = [res];
      }
      if (bomList.length > 0) {
        formState.value.productModelId = product.id;
        formState.value.productName = product.productName;
        formState.value.productModelName = product.model;
        // 如果当前选择的BOM不在新列表中,则重置BOM选择
        const currentBomExists = bomList.some(bom => bom.id === formState.value.bomId);
        if (!currentBomExists) {
          formState.value.bomId = undefined;
        }
        bomOptions.value = bomList;
        showProductSelectDialog.value = false;
        // 触发表单验证更新
        proxy.$refs["formRef"]?.validateField('productModelId');
      } else {
        proxy.$modal.msgError("该产品没有BOM,请先创建BOM");
      }
    } catch (error) {
      // 如果接口返回404或其他错误,说明没有BOM
      proxy.$modal.msgError("该产品没有BOM,请先创建BOM");
    }
    formState.value.productModelId = product.id;
    formState.value.productName = product.productName;
    formState.value.productModelName = product.model;
    showProductSelectDialog.value = false;
    // 触发表单验证更新
    proxy.$refs["formRef"]?.validateField('productModelId');
  }
};
const handleSubmit = () => {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      // 验证是否选择了产品和BOM
      // 验证是否选择了产品
      if (!formState.value.productModelId) {
        proxy.$modal.msgError("请选择产品");
        return;
      }
      if (!formState.value.bomId) {
        proxy.$modal.msgError("请选择BOM");
      // 验证是否配置了工序
      if (!formState.value.processRouteItemList || formState.value.processRouteItemList.length === 0) {
        proxy.$modal.msgError("请至少配置一个工序");
        return;
      }
      // 验证所有工序是否选择了部件
      const invalidItem = formState.value.processRouteItemList.find(item => !item.processId);
      if (invalidItem) {
        proxy.$modal.msgError("请选择所有工序的部件");
        return;
      }
      update(formState.value).then(res => {
@@ -245,6 +256,7 @@
}, { immediate: true });
onMounted(() => {
  getProcessOptions();
  if (props.visible && props.record) {
    setFormData();
  }