diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/order-import/components/CreateImportTaskModal.vue b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/order-import/components/CreateImportTaskModal.vue index d7ed52c8..77d029de 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/order-import/components/CreateImportTaskModal.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/order-import/components/CreateImportTaskModal.vue @@ -9,10 +9,13 @@ import type { OrderStatusVO } from '#/api/ecommerce/order-status'; import type { ShopVO } from '#/api/ecommerce/shop/types'; import { computed, ref } from 'vue'; +import Papa from 'papaparse'; +import * as XLSX from 'xlsx'; import { useVbenModal } from '@vben/common-ui'; import { + Alert, Button, Form, Input, @@ -75,6 +78,19 @@ const csvHeaders = ref([]); const csvData = ref([]); const selectedFile = ref(null); +// 规格编码补录(仅本次导入) +interface MissingSpecCodeRow { + orderNo: string; + productName: string; + rowIndex: number; + specCode: string; +} +const missingSpecCodeCount = ref(0); +const missingSpecCodeRows = ref([]); +const editableMissingSpecCodeRows = ref([]); +const specCodePrechecked = ref(false); +const hasPatchedFile = ref(false); + // 第一步:店铺选择 const shopList = ref([]); const selectedShopId = ref(); @@ -156,6 +172,22 @@ const [SaveStatusTemplateModal, saveStatusTemplateModalApi] = useVbenModal({ }, }); +// 创建补录规格编码模态框 +const [FixSpecCodeModal, fixSpecCodeModalApi] = useVbenModal({ + draggable: true, + class: 'w-[900px]', + title: '补充规格编码', + showCancelButton: true, + showConfirmButton: true, + confirmText: '应用并替换导入文件', + cancelText: '取消', + onConfirm: handleConfirmFixSpecCode, + onCancel() { + editableMissingSpecCodeRows.value = []; + fixSpecCodeModalApi.close(); + }, +}); + // 导入任务store const importTaskStore = useImportTaskStore(); @@ -870,6 +902,199 @@ const parseOrderStatuses = () => { statusMappings.value = {}; }; +const getMappedHeaderByFieldCandidates = ( + fieldNames: string[], + fieldLabelKeyword?: string, +) => { + const matchedField = fieldDefs.value.find((field) => { + if (fieldNames.includes(field.fieldName)) return true; + return fieldLabelKeyword ? field.fieldLabel.includes(fieldLabelKeyword) : false; + }); + if (!matchedField) return undefined; + return fieldMappings.value[matchedField.id]?.mappedHeader; +}; + +const isEmptyValue = (value: unknown) => String(value ?? '').trim() === ''; + +const getProductNameHeader = () => { + // 强确定:系统字段里“链接商品名称”= link_product_name + const mapped = fieldMappings.value[ + fieldDefs.value.find((f) => f.fieldName === 'link_product_name')?.id as number + ]?.mappedHeader; + if (mapped) return mapped; + + // 兜底(理论上不应该触发):排除“商品数量”之类字段 + const fallback = fieldDefs.value.find((f) => { + const label = f.fieldLabel || ''; + if (!label.includes('商品')) return false; + if (label.includes('数量')) return false; + return true; + }); + return fallback ? fieldMappings.value[fallback.id]?.mappedHeader : undefined; +}; + +const getOrderNoHeader = () => { + // 强确定:系统字段里“平台订单号”= platform_order_no + const mapped = fieldMappings.value[ + fieldDefs.value.find((f) => f.fieldName === 'platform_order_no')?.id as number + ]?.mappedHeader; + if (mapped) return mapped; + + // 兜底:包含“订单号/订单编号”且排除状态字段 + const fallback = fieldDefs.value.find((f) => { + const label = f.fieldLabel || ''; + const file = f.fileFieldName || ''; + if (label.includes('订单状态') || f.fieldName === 'status') return false; + return label.includes('订单') || file.includes('订单号'); + }); + return fallback ? fieldMappings.value[fallback.id]?.mappedHeader : undefined; +}; + +const runMissingSpecCodePrecheck = () => { + const specCodeHeader = getMappedHeaderByFieldCandidates( + ['spec_code', 'specCode'], + '规格编码', + ); + if (!specCodeHeader) { + missingSpecCodeRows.value = []; + missingSpecCodeCount.value = 0; + specCodePrechecked.value = true; + return; + } + + const orderNoHeader = getOrderNoHeader(); + const productNameHeader = getProductNameHeader(); + + const rows: MissingSpecCodeRow[] = []; + csvData.value.forEach((row, index) => { + if (isEmptyValue(row[specCodeHeader])) { + rows.push({ + rowIndex: index, + orderNo: orderNoHeader ? String(row[orderNoHeader] ?? '') : '', + productName: productNameHeader ? String(row[productNameHeader] ?? '') : '', + specCode: '', + }); + } + }); + + missingSpecCodeRows.value = rows; + missingSpecCodeCount.value = rows.length; + specCodePrechecked.value = true; +}; + +const buildPatchedFileName = (fileName: string) => { + const dot = fileName.lastIndexOf('.'); + if (dot < 0) return `${fileName}_patched`; + return `${fileName.slice(0, dot)}_patched${fileName.slice(dot)}`; +}; + +const createPatchedFile = ( + sourceFile: File, + headers: string[], + rows: any[], +): File => { + const ext = sourceFile.name.toLowerCase().split('.').pop() || ''; + const filename = buildPatchedFileName(sourceFile.name); + + if (ext === 'csv') { + const csvText = Papa.unparse(rows, { columns: headers }); + const blob = new Blob([`\uFEFF${csvText}`], { + type: 'text/csv;charset=utf-8', + }); + return new File([blob], filename, { type: 'text/csv;charset=utf-8' }); + } + + const aoa = [ + headers, + ...rows.map((row) => headers.map((header) => row[header] ?? '')), + ]; + const workbook = XLSX.utils.book_new(); + const worksheet = XLSX.utils.aoa_to_sheet(aoa); + XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1'); + const wbType = ext === 'xls' ? 'xls' : 'xlsx'; + const buffer = XLSX.write(workbook, { + type: 'array', + bookType: wbType, + }); + const blob = new Blob([buffer], { + type: + ext === 'xls' + ? 'application/vnd.ms-excel' + : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + }); + return new File([blob], filename, { type: blob.type }); +}; + +const handleOpenFixSpecCodeModal = () => { + if (missingSpecCodeCount.value === 0) { + message.info('当前不存在需要补充规格编码的行'); + return; + } + editableMissingSpecCodeRows.value = missingSpecCodeRows.value.map((row) => ({ + ...row, + })); + fixSpecCodeModalApi.open(); +}; + +const autoFillSpecCodeByProductName = (productName: string, specCode: string) => { + const code = specCode.trim(); + if (!code) return; + + editableMissingSpecCodeRows.value.forEach((row) => { + if (row.productName === productName && isEmptyValue(row.specCode)) { + row.specCode = code; + } + }); +}; + +async function handleConfirmFixSpecCode() { + const hasEmpty = editableMissingSpecCodeRows.value.some((row) => + isEmptyValue(row.specCode), + ); + if (hasEmpty) { + message.warning('请先补全所有待处理行的规格编码'); + return; + } + if (!selectedFile.value) { + message.error('源文件不存在,请重新上传'); + return; + } + + try { + fixSpecCodeModalApi.lock(); + const specCodeHeader = getMappedHeaderByFieldCandidates( + ['spec_code', 'specCode'], + '规格编码', + ); + if (!specCodeHeader) { + message.error('未找到规格编码映射列,请返回上一步检查字段映射'); + return; + } + + const updatedRows = csvData.value.map((row) => ({ ...row })); + editableMissingSpecCodeRows.value.forEach((item) => { + updatedRows[item.rowIndex][specCodeHeader] = item.specCode.trim(); + }); + + const patchedFile = createPatchedFile( + selectedFile.value, + csvHeaders.value, + updatedRows, + ); + csvData.value = updatedRows; + selectedFile.value = patchedFile; + hasPatchedFile.value = true; + runMissingSpecCodePrecheck(); + message.success('已生成补录后的新文件,将用于本次导入'); + fixSpecCodeModalApi.close(); + } catch (error: any) { + console.error('补录规格编码失败:', error); + message.error(error?.message || '补录规格编码失败'); + } finally { + fixSpecCodeModalApi.unlock(); + } +} + // 下一步 const handleNext = async () => { switch (currentStep.value) { @@ -915,6 +1140,14 @@ const handleNext = async () => { return; } + // 规格编码缺失预检(用于第4步提示与补录) + runMissingSpecCodePrecheck(); + if (missingSpecCodeCount.value > 0) { + message.warning( + `检测到 ${missingSpecCodeCount.value} 行规格编码为空,可在第4步补充后再创建任务`, + ); + } + // 在进入订单状态映射步骤前,解析所有订单状态 parseOrderStatuses(); @@ -1032,6 +1265,11 @@ const handleCreateTask = async () => { fieldMappings.value = {}; statusMappings.value = {}; uniqueStatusValues.value = []; + missingSpecCodeCount.value = 0; + missingSpecCodeRows.value = []; + editableMissingSpecCodeRows.value = []; + specCodePrechecked.value = false; + hasPatchedFile.value = false; csvHeaders.value = []; csvData.value = []; selectedFile.value = null; @@ -1055,6 +1293,11 @@ function open(headers: string[], file?: File, data?: any[]) { csvHeaders.value = headers; csvData.value = data || []; selectedFile.value = file || null; + missingSpecCodeCount.value = 0; + missingSpecCodeRows.value = []; + editableMissingSpecCodeRows.value = []; + specCodePrechecked.value = false; + hasPatchedFile.value = false; // 步骤保持不变,不清空 // 如果 CSV 表头变化了,需要重新初始化字段映射 @@ -1062,6 +1305,11 @@ function open(headers: string[], file?: File, data?: any[]) { fieldMappings.value = {}; statusMappings.value = {}; uniqueStatusValues.value = []; + missingSpecCodeCount.value = 0; + missingSpecCodeRows.value = []; + editableMissingSpecCodeRows.value = []; + specCodePrechecked.value = false; + hasPatchedFile.value = false; // 如果已有字段定义,重新初始化映射 if (fieldDefs.value.length > 0) { initFieldMappings(); @@ -1414,6 +1662,33 @@ const shopOptions = computed(() => {
+ + + + +
店铺信息
@@ -1516,5 +1791,46 @@ const shopOptions = computed(() => { + + +
+ +
+ + + + + + + + + + + + + + + + + +
行号订单号商品规格编码
{{ row.rowIndex + 2 }}{{ row.orderNo || '-' }}{{ row.productName || '-' }} + +
+
+
+