diff --git a/src/views/mes/moldget/MoldGetForm.vue b/src/views/mes/moldget/MoldGetForm.vue index 370c14df..3a2e3070 100644 --- a/src/views/mes/moldget/MoldGetForm.vue +++ b/src/views/mes/moldget/MoldGetForm.vue @@ -203,16 +203,37 @@ const submitForm = async () => { // 提交请求 formLoading.value = true try { - itemFormRef.value.selectedRows.forEach((item, index) => { - const i = { - warehouseId : formData.value.warehouseId, - productId : item.id, - productPrice: 0, - count: 1, - } - formData.value.items.push(i) - console.log(formData.value.items) -}); + const existingByProductId = new Map( + (formData.value.items ?? []) + .filter((it: any) => it && (it.productId ?? it.id) !== undefined && (it.productId ?? it.id) !== null) + .map((it: any) => [Number(it.productId ?? it.id), it]) + ) + + const selectedProductIds = (itemFormRef.value.selectedRows ?? []) + .map((it: any) => it?.id) + .filter((id: any) => id !== undefined && id !== null) + .map((id: any) => Number(id)) + .filter((id: any) => !Number.isNaN(id)) + + formData.value.items = selectedProductIds.map((productId) => { + const existing = existingByProductId.get(productId) + if (existing) { + return { + id: existing.id, + warehouseId: formData.value.warehouseId, + productId, + productPrice: existing.productPrice ?? 0, + count: existing.count ?? 1, + remark: existing.remark ?? null + } + } + return { + warehouseId: formData.value.warehouseId, + productId, + productPrice: 0, + count: 1 + } + }) const data = formData.value as unknown as StockOutVO if (formType.value === 'create') { await StockOutApi.createStockOut(data) diff --git a/src/views/mes/moldreturn/MoldReturnForm.vue b/src/views/mes/moldreturn/MoldReturnForm.vue index 1e440e8e..271b9bc3 100644 --- a/src/views/mes/moldreturn/MoldReturnForm.vue +++ b/src/views/mes/moldreturn/MoldReturnForm.vue @@ -159,7 +159,12 @@ const open = async (type: string, id?: number) => { if (id) { formLoading.value = true try { - formData.value = await StockInApi.getStockIn(id) + const data = await StockInApi.getStockIn(id) + const itemWarehouseId = (data as any)?.items?.[0]?.warehouseId + formData.value = { + ...(data as any), + warehouseId: itemWarehouseId ?? (data as any)?.warehouseId + } } finally { formLoading.value = false } @@ -179,15 +184,37 @@ const submitForm = async () => { // 提交请求 formLoading.value = true try { - itemFormRef.value.selectedRows.forEach((item, index) => { - const i = { - warehouseId : formData.value.warehouseId, - productId : item.id, - productPrice: 0, - count: 1, - } - formData.value.items.push(i) -}); + const existingByProductId = new Map( + (formData.value.items ?? []) + .filter((it: any) => it && (it.productId ?? it.id) !== undefined && (it.productId ?? it.id) !== null) + .map((it: any) => [Number(it.productId ?? it.id), it]) + ) + + const selectedProductIds = (itemFormRef.value.selectedRows ?? []) + .map((it: any) => it?.id) + .filter((id: any) => id !== undefined && id !== null) + .map((id: any) => Number(id)) + .filter((id: any) => !Number.isNaN(id)) + + formData.value.items = selectedProductIds.map((productId) => { + const existing = existingByProductId.get(productId) + if (existing) { + return { + id: existing.id, + warehouseId: formData.value.warehouseId, + productId, + productPrice: existing.productPrice ?? 0, + count: existing.count ?? 1, + remark: existing.remark ?? null + } + } + return { + warehouseId: formData.value.warehouseId, + productId, + productPrice: 0, + count: 1 + } + }) const data = formData.value as unknown as StockInVO if (formType.value === 'create') { await StockInApi.createStockIn(data)