| | |
| | | import type { FormType } from "../data"; |
| | | |
| | | import type { MesProWorkOrderApi } from "#/api/mes/pro/workorder"; |
| | | import type { MesProWorkOrderProcessApi } from "#/api/mes/pro/workorder/process"; |
| | | |
| | | import { computed, ref } from "vue"; |
| | | |
| | | import { confirm, useVbenModal } from "@vben/common-ui"; |
| | | import { |
| | | BarcodeBizTypeEnum, |
| | | MesProWorkOrderStatusEnum, |
| | | } from "@vben/constants"; |
| | | import { isAiEnable } from "@vben/hooks"; |
| | | |
| | | import { Button, message, Tabs } from "ant-design-vue"; |
| | | import { Button, message, Modal as AntModal, Tabs } from "ant-design-vue"; |
| | | |
| | | import { useVbenForm } from "#/adapter/form"; |
| | | import { |
| | |
| | | getWorkOrder, |
| | | updateWorkOrder, |
| | | } from "#/api/mes/pro/workorder"; |
| | | import { batchSaveWorkOrderProcess, getWorkOrderProcessListByWorkOrderId } from "#/api/mes/pro/workorder/process"; |
| | | import { $t } from "#/locales"; |
| | | import { BarcodeDetail } from "#/views/wls/barcode/components"; |
| | | // import { BarcodeDetail } from "#/views/wls/barcode/components"; |
| | | |
| | | import { useFormSchema } from "../data"; |
| | | import PredictDelivery from "./ai/predict-delivery.vue"; |
| | | import PredictDuration from "./ai/predict-duration.vue"; |
| | | import PredictMaterial from "./ai/predict-material.vue"; |
| | | import PredictRisk from "./ai/predict-risk.vue"; |
| | | import { getWorkOrderBomItemPreview } from '#/api/mes/pro/workorder/bom'; |
| | | |
| | | import ItemList from "./item-list.vue"; |
| | | import ProcessList from "./process-list.vue"; |
| | | import MaterialPreview from "./material-preview.vue"; |
| | | |
| | | const emit = defineEmits(["success"]); |
| | | const formType = ref<FormType>("create"); |
| | | const formData = ref<MesProWorkOrderApi.WorkOrder>(); |
| | | const originalSnapshot = ref(""); |
| | | const subTabsName = ref("process"); |
| | | const barcodeDetailRef = ref<InstanceType<typeof BarcodeDetail>>(); |
| | | const processListRef = ref<InstanceType<typeof ProcessList>>(); |
| | | const currentProductId = ref<number>(); // 当前选中的产品 ID(用于工序加载) |
| | | const tempProcessList = ref<MesProWorkOrderProcessApi.WorkOrderProcess[]>([]); // 临时工序列表 |
| | | const subTabsName = ref("item"); |
| | | // const barcodeDetailRef = ref<InstanceType<typeof BarcodeDetail>>(); |
| | | const previewParams = ref<{ productId?: number; quantity?: number }>({}); // 辅料预览参数 |
| | | |
| | | const isEditable = computed(() => |
| | | ["create", "update"].includes(formType.value) |
| | |
| | | ); |
| | | const canUseAi = computed( |
| | | () => |
| | | isAiEnable() && |
| | | formData.value?.id && |
| | | (formData.value?.status === MesProWorkOrderStatusEnum.PREPARE || |
| | | formData.value?.status === MesProWorkOrderStatusEnum.CONFIRMED) |
| | |
| | | schema: [], |
| | | showDefaultActions: false, |
| | | wrapperClass: "grid-cols-3", |
| | | handleValuesChange: (values, fieldsChanged) => { |
| | | // 产品变更时,触发工序加载 |
| | | if (isCreateMode.value && fieldsChanged.includes('productId')) { |
| | | const newProductId = values.productId; |
| | | if (newProductId && newProductId !== currentProductId.value) { |
| | | currentProductId.value = newProductId; |
| | | } |
| | | handleValuesChange: async (values, fieldsChanged) => { |
| | | // 工厂变更时,清空已选车间(车间必须属于所选工厂) |
| | | if (fieldsChanged.includes('factoryId')) { |
| | | await formApi.setFieldValue('workshopId', undefined); |
| | | } |
| | | // 同步辅料预览参数(产品 / 数量变化时自动重新计算辅料与库存) |
| | | if ( |
| | | fieldsChanged.includes('productId') || |
| | | fieldsChanged.includes('quantity') |
| | | ) { |
| | | previewParams.value = { |
| | | productId: values.productId, |
| | | quantity: values.quantity, |
| | | }; |
| | | } |
| | | }, |
| | | }); |
| | |
| | | if (!valid || !formData.value?.id) return; |
| | | modalApi.lock(); |
| | | try { |
| | | const data = (await formApi.getValues()) as MesProWorkOrderApi.WorkOrder; |
| | | await updateWorkOrder({ ...formData.value, ...data }); |
| | | if (tempProcessList.value.length > 0) { |
| | | const processList = tempProcessList.value.map(item => ({ |
| | | ...item, |
| | | workOrderId: formData.value!.id, |
| | | })); |
| | | await batchSaveWorkOrderProcess(processList); |
| | | const data = (await formApi.getValues()) as any; |
| | | if (Array.isArray(data.feedbackRole)) { |
| | | data.feedbackRole = data.feedbackRole.join(','); |
| | | } |
| | | await updateWorkOrder({ ...formData.value, ...data }); |
| | | formData.value = { ...formData.value, ...data }; |
| | | originalSnapshot.value = JSON.stringify(await formApi.getValues()); |
| | | emit('success'); |
| | |
| | | } |
| | | |
| | | /** 查看订单条码 */ |
| | | function handleBarcode() { |
| | | if (!formData.value?.id) { |
| | | return; |
| | | } |
| | | barcodeDetailRef.value?.openByBusiness( |
| | | formData.value.id, |
| | | BarcodeBizTypeEnum.WORKORDER, |
| | | formData.value.code, |
| | | formData.value.name |
| | | ); |
| | | } |
| | | // function handleBarcode() { |
| | | // if (!formData.value?.id) { |
| | | // return; |
| | | // } |
| | | // barcodeDetailRef.value?.openByBusiness( |
| | | // formData.value.id, |
| | | // BarcodeBizTypeEnum.WORKORDER, |
| | | // formData.value.code, |
| | | // formData.value.name |
| | | // ); |
| | | // } |
| | | |
| | | /** 工序列表变化回调 */ |
| | | function handleProcessListChange(list: MesProWorkOrderProcessApi.WorkOrderProcess[]) { |
| | | tempProcessList.value = list; |
| | | /** |
| | | * 提交前校验辅料库存:库存不足时提示先行采购,由用户确认是否继续提交 |
| | | * |
| | | * @returns true=可以继续提交;false=用户取消 |
| | | */ |
| | | async function checkMaterialStock( |
| | | data: MesProWorkOrderApi.WorkOrder |
| | | ): Promise<boolean> { |
| | | if (!data.productId || !data.quantity) { |
| | | return true; |
| | | } |
| | | let list: Awaited<ReturnType<typeof getWorkOrderBomItemPreview>> = []; |
| | | try { |
| | | list = await getWorkOrderBomItemPreview(data.productId, data.quantity); |
| | | } catch { |
| | | // 预览失败不阻断工单创建,交由后端在领料环节校验 |
| | | return true; |
| | | } |
| | | const insufficient = list.filter((item) => item.stockSufficient === false); |
| | | if (insufficient.length === 0) { |
| | | return true; |
| | | } |
| | | const detail = insufficient |
| | | .map((item) => { |
| | | const gap = |
| | | (item.quantity ?? 0) - |
| | | (item.stockQuantity ?? 0) - |
| | | (item.purchaseOrderQuantity ?? 0); |
| | | return `${item.itemName ?? item.itemCode}(可用库存 ${item.stockQuantity ?? 0},在途采购 ${item.purchaseOrderQuantity ?? 0},需求 ${item.quantity ?? 0},还差 ${gap})`; |
| | | }) |
| | | .join(';'); |
| | | return new Promise<boolean>((resolve) => { |
| | | AntModal.confirm({ |
| | | title: '辅料库存不足', |
| | | content: `以下辅料库存不足:${detail}。系统仍会生成工单,但需先完成采购后再领料,是否继续提交?`, |
| | | okText: '仍然提交', |
| | | cancelText: '返回修改', |
| | | onCancel: () => resolve(false), |
| | | onOk: () => resolve(true), |
| | | }); |
| | | }); |
| | | } |
| | | |
| | | const [Modal, modalApi] = useVbenModal({ |
| | |
| | | if (isEditable.value) { |
| | | const current = JSON.stringify(await formApi.getValues()); |
| | | if (current !== originalSnapshot.value) { |
| | | const data = (await formApi.getValues()) as MesProWorkOrderApi.WorkOrder; |
| | | const data = (await formApi.getValues()) as any; |
| | | if (Array.isArray(data.feedbackRole)) { |
| | | data.feedbackRole = data.feedbackRole.join(','); |
| | | } |
| | | await updateWorkOrder({ ...formData.value, ...data }); |
| | | originalSnapshot.value = current; |
| | | } |
| | | } |
| | | // 2. 批量保存工序(如果有) |
| | | if (tempProcessList.value.length > 0) { |
| | | const processList = tempProcessList.value.map(item => ({ |
| | | ...item, |
| | | workOrderId: formData.value!.id, |
| | | })); |
| | | await batchSaveWorkOrderProcess(processList); |
| | | } |
| | | // 3. 校验是否有工序 |
| | | const processes = await getWorkOrderProcessListByWorkOrderId(formData.value.id); |
| | | if (!processes || processes.length === 0) { |
| | | message.warning('当前订单未配置工序,请先在工单中维护工序信息后再确认'); |
| | | return; |
| | | } |
| | | // 4. 确认工单 |
| | | // 2. 确认工单(未配置工序时按单工序生产) |
| | | await confirmWorkOrder(formData.value.id); |
| | | message.success('订单已确认'); |
| | | await modalApi.close(); |
| | |
| | | // ---- 完成订单模式 ---- |
| | | if (isFinish.value) { |
| | | if (!formData.value?.id) return; |
| | | // 校验是否有工序 |
| | | const processes = await getWorkOrderProcessListByWorkOrderId(formData.value.id); |
| | | if (!processes || processes.length === 0) { |
| | | message.warning('当前订单未配置工序,请先在工单中维护工序信息后再完成'); |
| | | return; |
| | | } |
| | | try { |
| | | await confirm('确认要完成该订单吗?'); |
| | | } catch { |
| | |
| | | const { valid } = await formApi.validate(); |
| | | if (!valid) return; |
| | | modalApi.lock(); |
| | | const data = (await formApi.getValues()) as MesProWorkOrderApi.WorkOrder; |
| | | const data = (await formApi.getValues()) as any; |
| | | if (Array.isArray(data.feedbackRole)) { |
| | | data.feedbackRole = data.feedbackRole.join(','); |
| | | } |
| | | try { |
| | | if (formData.value?.id) { |
| | | // 编辑模式 |
| | | await updateWorkOrder({ ...formData.value, ...data }); |
| | | // 批量保存工序 |
| | | if (tempProcessList.value.length > 0) { |
| | | const processList = tempProcessList.value.map(item => ({ |
| | | ...item, |
| | | workOrderId: formData.value!.id, |
| | | })); |
| | | await batchSaveWorkOrderProcess(processList); |
| | | } |
| | | formData.value = { ...formData.value, ...data }; |
| | | emit("success"); |
| | | message.success($t("ui.actionMessage.operationSuccess")); |
| | | await modalApi.close(); |
| | | } else { |
| | | // 新增模式 |
| | | // 新增模式:先校验辅料库存,不足时提示先行采购 |
| | | const insufficient = await checkMaterialStock(data); |
| | | if (!insufficient) return; |
| | | const id = await createWorkOrder(data); |
| | | // 批量保存工序 |
| | | if (tempProcessList.value.length > 0) { |
| | | const processList = tempProcessList.value.map(item => ({ |
| | | ...item, |
| | | workOrderId: id, |
| | | })); |
| | | await batchSaveWorkOrderProcess(processList); |
| | | } |
| | | formData.value = { |
| | | ...data, |
| | | id, |
| | |
| | | if (!isOpen) { |
| | | formData.value = undefined; |
| | | originalSnapshot.value = ""; |
| | | currentProductId.value = undefined; |
| | | tempProcessList.value = []; |
| | | previewParams.value = {}; |
| | | return; |
| | | } |
| | | const data = modalApi.getData<{ |
| | |
| | | parentRow?: MesProWorkOrderApi.WorkOrder; |
| | | }>(); |
| | | formType.value = data.formType; |
| | | subTabsName.value = "process"; |
| | | tempProcessList.value = []; |
| | | subTabsName.value = "item"; |
| | | applySchema(); |
| | | if (data?.id) { |
| | | modalApi.lock(); |
| | | try { |
| | | formData.value = await getWorkOrder(data.id); |
| | | await formApi.setValues(formData.value); |
| | | currentProductId.value = formData.value.productId; |
| | | const formValues: any = { ...formData.value }; |
| | | if (formValues.feedbackRole && typeof formValues.feedbackRole === 'string') { |
| | | formValues.feedbackRole = formValues.feedbackRole.split(','); |
| | | } |
| | | await formApi.setValues(formValues); |
| | | } finally { |
| | | modalApi.unlock(); |
| | | } |
| | |
| | | class="w-3/5" |
| | | content-class="!max-h-[80vh] overflow-y-auto"> |
| | | <Form class="mx-4" /> |
| | | <!-- 工序 / 物料需求 Tab --> |
| | | <!-- 物料需求(辅料明细)--> |
| | | <template v-if="showTabs"> |
| | | <Tabs v-model:active-key="subTabsName" |
| | | class="mx-4"> |
| | | <Tabs.TabPane key="process" |
| | | tab="工序" |
| | | forceRender> |
| | | <ProcessList |
| | | ref="processListRef" |
| | | :form-type="formType" |
| | | :work-order="formData || {}" |
| | | :work-order-id="formData?.id" |
| | | :product-id="currentProductId" |
| | | @process-list-change="handleProcessListChange" |
| | | /> |
| | | </Tabs.TabPane> |
| | | <Tabs.TabPane key="item" |
| | | tab="物料需求"> |
| | | <p class="mb-3 text-xs text-gray-400 leading-relaxed"> |
| | | 新增工单时会按所选产品与数量预先计算辅料用量及库存情况;工单生成后,物料需求按产品用量自动汇总,仅供查看。 |
| | | </p> |
| | | <ItemList v-if="formData?.id" |
| | | :work-order-id="formData.id" /> |
| | | <div v-else class="text-gray-400 text-center py-8"> |
| | | 请先保存工单后再查看物料需求 |
| | | </div> |
| | | <MaterialPreview v-else |
| | | :product-id="previewParams.productId" |
| | | :quantity="previewParams.quantity" /> |
| | | </Tabs.TabPane> |
| | | </Tabs> |
| | | </template> |
| | | <template #prepend-footer> |
| | | <div class="flex flex-auto flex-col gap-2"> |
| | | <div v-if="canUseAi" class="flex items-center gap-2"> |
| | | <PredictMaterial :work-order-id="formData!.id" /> |
| | | <PredictDuration :work-order-id="formData!.id" /> |
| | | <PredictRisk :work-order-id="formData!.id" /> |
| | | <PredictDelivery :work-order-id="formData!.id" /> |
| | | <div v-if="canUseAi" class="flex flex-wrap items-center gap-x-2 gap-y-1"> |
| | | <span class="text-xs font-semibold text-gray-800 dark:text-gray-100">AI 智能分析</span> |
| | | <div class="flex items-center gap-2"> |
| | | <PredictMaterial :work-order-id="formData!.id" /> |
| | | <PredictDuration :work-order-id="formData!.id" /> |
| | | <PredictRisk :work-order-id="formData!.id" /> |
| | | <PredictDelivery :work-order-id="formData!.id" /> |
| | | </div> |
| | | <span class="text-xs text-gray-400">基于历史生产数据估算,仅供参考,不参与业务计算</span> |
| | | </div> |
| | | <div v-if="canUseAi && showMainBtn" class="border-t border-gray-200" /> |
| | | <div class="flex items-center"> |
| | |
| | | @click="handleSave"> |
| | | 保存修改 |
| | | </Button> |
| | | <Button v-if="formType === 'detail' && formData?.id" |
| | | <!-- <Button v-if="formType === 'detail' && formData?.id" |
| | | @click="handleBarcode"> |
| | | 查看条码 |
| | | </Button> |
| | | </Button> --> |
| | | <Button @click="modalApi.close()">取消</Button> |
| | | </div> |
| | | </template> |
| | | </Modal> |
| | | <BarcodeDetail ref="barcodeDetailRef" /> |
| | | <!-- <BarcodeDetail ref="barcodeDetailRef" /> --> |
| | | </template> |