gaoluyang
2026-04-27 b52c08544fd4ab1e8ff0f09efaac160608fd90ce
src/views/productionManagement/productionOrder/New.vue
@@ -1,67 +1,168 @@
<template>
  <div>
    <el-dialog
        v-model="isShow"
        title="新增生产订单"
        width="800"
        @close="closeModal"
      v-model="isShow"
      title="新增生产订单"
      width="1280px"
      @close="closeModal"
    >
      <el-form label-width="140px" :model="formState" label-position="top" ref="formRef">
      <el-form
        ref="formRef"
        :model="formState"
        label-width="140px"
        label-position="top"
      >
        <el-form-item
            label="产品名称"
            prop="productModelId"
            :rules="[
                {
                required: true,
                message: '请选择产品',
                trigger: 'change',
              }
            ]"
          label="产品名称"
          prop="productModelId"
          :rules="[
            {
              required: true,
              message: '请选择产品',
              trigger: 'change',
            },
          ]"
        >
          <el-button type="primary" @click="showProductSelectDialog = true">
            {{ formState.productName ? formState.productName : '选择产品' }}
            {{ formState.productName ? formState.productName : "选择产品" }}
          </el-button>
        </el-form-item>
        <el-form-item
            label="规格"
            prop="productModelName"
          label="规格"
          prop="productModelName"
          :rules="[
            {
              required: true,
              message: '请选择规格',
              trigger: 'change',
            },
          ]"
        >
          <el-input v-model="formState.productModelName"  disabled />
          <el-input v-model="formState.productModelName" disabled />
        </el-form-item>
        <el-form-item
            label="单位"
            prop="unit"
          label="单位"
          prop="unit"
          :rules="[
            {
              required: true,
              message: '请选择单位',
              trigger: 'change',
            },
          ]"
        >
          <el-input v-model="formState.unit"  disabled />
          <el-input v-model="formState.unit" disabled />
        </el-form-item>
        <el-form-item label="工艺路线">
          <el-select v-model="formState.routeId"
                     placeholder="请选择工艺路线"
                     style="width: 100%;"
                     :loading="bindRouteLoading">
            <el-option v-for="item in routeOptions"
                       :key="item.id"
                       :label="`${item.processRouteCode || ''}`"
                       :value="item.id" />
        <el-form-item
          label="工艺路线"
          prop="routeId"
          :rules="[
            {
              required: true,
              message: '请选择工艺路线',
              trigger: 'change',
            },
          ]"
        >
          <el-select
            v-model="formState.routeId"
            placeholder="请选择工艺路线"
            style="width: 100%;"
            :loading="bindRouteLoading"
          >
            <el-option
              v-for="item in routeOptions"
              :key="item.id"
              :label="`${item.processRouteCode || ''}`"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="用料信息">
          <el-table :data="[materialForm]" border size="small" class="material-table">
            <el-table-column label="原纸规格" min-width="180" align="center">
              <template #default>
                <el-tree-select
                  v-model="materialForm.productCategoryId"
                  placeholder="请选择原纸规格"
                  filterable
                  clearable
                  check-strictly
                  :render-after-expand="false"
                  style="width: 100%;"
                  :loading="bindProductLoading"
                  :data="bindProductOptions"
                  @change="handleMaterialCategoryChange"
                />
              </template>
            </el-table-column>
            <el-table-column label="原料型号" min-width="180" align="center">
              <template #default>
                <el-select
                  v-model="materialForm.materialModel"
                  placeholder="请选择原料型号"
                  filterable
                  clearable
                  style="width: 100%;"
                  :loading="bindMaterialModelLoading"
                >
                  <el-option
                    v-for="item in bindMaterialModelOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="原纸需要量" min-width="120" align="center">
              <template #default>
                <el-input-number
                  v-model="materialForm.basePaperQty"
                  :min="0"
                  :precision="2"
                  controls-position="right"
                  style="width: 100%"
                />
              </template>
            </el-table-column>
            <el-table-column label="纸箱需要量" min-width="120" align="center">
              <template #default>
                <el-input-number
                  v-model="materialForm.cartonQty"
                  :min="0"
                  :precision="2"
                  controls-position="right"
                  style="width: 100%"
                />
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item
            label="需求数量"
            prop="quantity"
          label="需求数量"
          prop="quantity"
          :rules="[
            {
              required: true,
              message: '请填写需求数量',
              trigger: 'change',
            },
          ]"
        >
          <el-input-number v-model="formState.quantity" :step="1" :min="1" style="width: 100%" />
        </el-form-item>
      </el-form>
      <!-- 产品选择弹窗 -->
      <ProductSelectDialog
          v-model="showProductSelectDialog"
          @confirm="handleProductSelect"
          single
        v-model="showProductSelectDialog"
        @confirm="handleProductSelect"
        single
      />
      <template #footer>
        <div class="dialog-footer">
@@ -74,26 +175,21 @@
</template>
<script setup>
import {ref, computed, getCurrentInstance} from "vue";
import { computed, getCurrentInstance, reactive, ref, watch } from "vue";
import ProductSelectDialog from "@/views/basicData/product/ProductSelectDialog.vue";
import {addProductOrder, listProcessRoute} from "@/api/productionManagement/productionOrder.js";
import { addProductOrder, listProcessRoute } from "@/api/productionManagement/productionOrder.js";
import { modelList, productTreeList } from "@/api/basicData/product.js";
const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
  },
  type: {
    type: String,
    required: true,
    default: 'qualified',
  },
});
const emit = defineEmits(['update:visible', 'completed']);
const emit = defineEmits(["update:visible", "completed"]);
// 响应式数据(替代选项式的 data)
const formRef = ref();
const formState = ref({
  productId: undefined,
  productModelId: undefined,
@@ -104,85 +200,255 @@
  quantity: 0,
});
const materialForm = reactive({
  productCategoryId: null,
  productCategory: "",
  materialModel: "",
  basePaperQty: null,
  cartonQty: null,
});
const bindProductOptions = ref([]);
const bindProductLoading = ref(false);
const bindMaterialModelOptions = ref([]);
const bindMaterialModelLoading = ref(false);
const routeOptions = ref([]);
const bindRouteLoading = ref(false);
const isShow = computed({
  get() {
    return props.visible;
  },
  set(val) {
    emit('update:visible', val);
    emit("update:visible", val);
  },
});
const showProductSelectDialog = ref(false);
const { proxy } = getCurrentInstance();
let { proxy } = getCurrentInstance()
const resetMaterialForm = () => {
  materialForm.productCategoryId = null;
  materialForm.productCategory = "";
  materialForm.materialModel = "";
  materialForm.basePaperQty = null;
  materialForm.cartonQty = null;
  bindMaterialModelOptions.value = [];
};
const closeModal = () => {
  // 重置表单数据
  formState.value = {
    productId: undefined,
    productModelId: undefined,
    routeId: undefined,
    productName: "",
    productModelName: "",
    quantity: '',
    unit: "",
    quantity: 0,
  };
  routeOptions.value = [];
  resetMaterialForm();
  isShow.value = false;
};
// 产品选择处理
const handleProductSelect = async (products) => {
  if (products && products.length > 0) {
    const product = products[0];
    formState.value.productId = product.productId;
    formState.value.productName = product.productName;
    formState.value.productModelName = product.model;
    formState.value.productModelId = product.id;
    formState.value.unit = product.unit;
    showProductSelectDialog.value = false;
    fetchRouteOptions( product.id);
    // 触发表单验证更新
    proxy.$refs["formRef"]?.validateField('productModelId');
  }
  if (!products || products.length === 0) return;
  const product = products[0];
  const selectedProductModelId = product.productModelId ?? product.id;
  const selectedProductId = product.productId ?? product.parentId ?? product.topProductParentId;
  formState.value.productId = selectedProductId;
  formState.value.productName = product.productName;
  formState.value.productModelName = product.model;
  formState.value.productModelId = selectedProductModelId;
  formState.value.unit = product.unit;
  showProductSelectDialog.value = false;
  fetchRouteOptions(selectedProductModelId);
  formRef.value?.validateField("productModelId");
};
const routeOptions = ref([]);
const bindRouteLoading = ref(false);
const fetchRouteOptions = (productModelId) => {
  if (!productModelId) {
    routeOptions.value = [];
    formState.value.routeId = undefined;
    return;
  }
  formState.value.routeId = undefined;
  routeOptions.value = []
  routeOptions.value = [];
  bindRouteLoading.value = true;
  listProcessRoute({ productModelId: productModelId }).then(res => {
  listProcessRoute({ productModelId }).then((res) => {
    routeOptions.value = res.data || [];
  }).finally(() => {
    bindRouteLoading.value = false;
  })
}
const handleSubmit = () => {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      // 验证是否选择了产品和规格
      if (!formState.value.productModelId) {
        proxy.$modal.msgError("请选择产品");
        return;
      }
      if (!formState.value.productModelId) {
        proxy.$modal.msgError("请选择规格");
        return;
      }
      addProductOrder(formState.value).then(res => {
        // 关闭模态框
        isShow.value = false;
        // 告知父组件已完成
        emit('completed');
        proxy.$modal.msgSuccess("提交成功");
      })
    }
  })
  });
};
function convertIdToValue(data) {
  return (data || []).map((item) => {
    const { id, children, ...rest } = item;
    const node = {
      ...rest,
      value: id,
    };
    if (children && children.length > 0) {
      node.children = convertIdToValue(children);
    }
    return node;
  });
}
const findNodeById = (nodes, productId) => {
  const tree = nodes || [];
  for (let i = 0; i < tree.length; i++) {
    if (String(tree[i].value) === String(productId)) {
      return tree[i].label;
    }
    if (tree[i].children && tree[i].children.length > 0) {
      const found = findNodeById(tree[i].children, productId);
      if (found) return found;
    }
  }
  return "";
};
const loadBindProductOptions = async () => {
  bindProductLoading.value = true;
  try {
    const res = await productTreeList();
    bindProductOptions.value = convertIdToValue(res || []);
  } catch (e) {
    console.error("获取产品选项失败:", e);
  } finally {
    bindProductLoading.value = false;
  }
};
const loadBindMaterialModelOptions = async (productCategoryId) => {
  if (!productCategoryId) {
    bindMaterialModelOptions.value = [];
    materialForm.materialModel = "";
    return;
  }
  bindMaterialModelLoading.value = true;
  try {
    const res = await modelList({ id: productCategoryId });
    const list = Array.isArray(res) ? res : (Array.isArray(res?.data) ? res.data : []);
    const mapped = list
      .map((item) => ({
        label: item.model || "",
        value: item.model || "",
      }))
      .filter((item) => item.value);
    const unique = [];
    const seen = new Set();
    mapped.forEach((item) => {
      if (!seen.has(item.value)) {
        seen.add(item.value);
        unique.push(item);
      }
    });
    bindMaterialModelOptions.value = unique;
    if (!unique.some((item) => item.value === materialForm.materialModel)) {
      materialForm.materialModel = "";
    }
  } catch (e) {
    console.error("获取原料型号失败:", e);
    bindMaterialModelOptions.value = [];
    materialForm.materialModel = "";
  } finally {
    bindMaterialModelLoading.value = false;
  }
};
const handleMaterialCategoryChange = async (value) => {
  materialForm.productCategoryId = value;
  materialForm.productCategory = findNodeById(bindProductOptions.value, value);
  materialForm.materialModel = "";
  await loadBindMaterialModelOptions(value);
};
const isEmptyField = (value) => value === null || value === undefined || value === "";
const validateMaterialForm = () => {
  if (!materialForm.productCategoryId && !materialForm.productCategory) {
    return { valid: false, message: "请选择原纸规格" };
  }
  if (!materialForm.materialModel) {
    return { valid: false, message: "请选择原料型号" };
  }
  if (isEmptyField(materialForm.basePaperQty)) {
    return { valid: false, message: "请填写原纸需要量" };
  }
  if (isEmptyField(materialForm.cartonQty)) {
    return { valid: false, message: "请填写纸箱需要量" };
  }
  return { valid: true, message: "" };
};
const validateOrderForm = () => {
  if (!formState.value.productModelId) {
    return { valid: false, message: "请选择产品" };
  }
  if (!formState.value.productModelName) {
    return { valid: false, message: "请选择规格" };
  }
  if (!formState.value.unit) {
    return { valid: false, message: "请选择单位" };
  }
  if (!formState.value.routeId) {
    return { valid: false, message: "请选择工艺路线" };
  }
  if (!(Number(formState.value.quantity || 0) > 0)) {
    return { valid: false, message: "请填写需求数量" };
  }
  return { valid: true, message: "" };
};
watch(
  () => isShow.value,
  (visible) => {
    if (visible) {
      loadBindProductOptions();
    }
  },
  { immediate: true }
);
const handleSubmit = () => {
  formRef.value?.validate((valid) => {
    if (!valid) return;
    const orderValidateResult = validateOrderForm();
    if (!orderValidateResult.valid) {
      proxy.$modal.msgWarning(orderValidateResult.message);
      return;
    }
    const materialValidateResult = validateMaterialForm();
    if (!materialValidateResult.valid) {
      proxy.$modal.msgWarning(materialValidateResult.message);
      return;
    }
    const submitData = {
      ...formState.value,
      productId: formState.value.productId,
      productModelId: formState.value.productModelId,
      materialList: [
        {
          productModelId: formState.value.productModelId,
          productCategoryId: materialForm.productCategoryId,
          productCategory: materialForm.productCategory,
          materialName: materialForm.productCategory,
          materialModel: materialForm.materialModel,
          basePaperQty: materialForm.basePaperQty,
          cartonQty: materialForm.cartonQty,
        },
      ],
    };
    addProductOrder(submitData).then(() => {
      isShow.value = false;
      emit("completed");
      proxy.$modal.msgSuccess("提交成功");
    });
  });
};
defineExpose({
  closeModal,
@@ -190,3 +456,9 @@
  isShow,
});
</script>
<style scoped lang="scss">
.material-table {
  width: 100%;
}
</style>