mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:59:19 +08:00
feat(order-import): 新增规格编码缺失检测与补录功能
- 新增规格编码预检步骤,在进入确认信息前检测缺失行数并提示 - 新增补录规格编码模态框,支持批量编辑缺失规格编码的行 - 根据商品名称自动填充同商品其他缺失行的规格编码 - 生成补录后的新文件(csv/xlsx)用于本次导入,保留原始文件 - 导入前检查未补全规格编码时阻止提交并提示请先补全所有待处理行的规格编码 - 在第四步确认信息页面展示预检结果和补录状态
This commit is contained in:
+316
@@ -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<string[]>([]);
|
||||
const csvData = ref<any[]>([]);
|
||||
const selectedFile = ref<File | null>(null);
|
||||
|
||||
// 规格编码补录(仅本次导入)
|
||||
interface MissingSpecCodeRow {
|
||||
orderNo: string;
|
||||
productName: string;
|
||||
rowIndex: number;
|
||||
specCode: string;
|
||||
}
|
||||
const missingSpecCodeCount = ref(0);
|
||||
const missingSpecCodeRows = ref<MissingSpecCodeRow[]>([]);
|
||||
const editableMissingSpecCodeRows = ref<MissingSpecCodeRow[]>([]);
|
||||
const specCodePrechecked = ref(false);
|
||||
const hasPatchedFile = ref(false);
|
||||
|
||||
// 第一步:店铺选择
|
||||
const shopList = ref<ShopVO.Info[]>([]);
|
||||
const selectedShopId = ref<number | undefined>();
|
||||
@@ -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(() => {
|
||||
<!-- 第四步:确认信息 -->
|
||||
<div v-if="currentStep === 3" class="py-4">
|
||||
<div class="space-y-4">
|
||||
<Alert
|
||||
v-if="specCodePrechecked && missingSpecCodeCount > 0"
|
||||
type="warning"
|
||||
show-icon
|
||||
:message="`检测到 ${missingSpecCodeCount} 行规格编码为空`"
|
||||
>
|
||||
<template #description>
|
||||
<div class="flex flex-wrap items-center gap-3">
|
||||
<span>可先补充规格编码,再创建导入任务。</span>
|
||||
<Button size="small" type="primary" @click="handleOpenFixSpecCodeModal">
|
||||
去补充规格编码
|
||||
</Button>
|
||||
</div>
|
||||
</template>
|
||||
</Alert>
|
||||
<Alert
|
||||
v-else-if="specCodePrechecked"
|
||||
type="success"
|
||||
show-icon
|
||||
message="规格编码预检通过,未发现空值"
|
||||
/>
|
||||
<Alert
|
||||
v-if="hasPatchedFile"
|
||||
type="info"
|
||||
show-icon
|
||||
:message="`已使用补录后的文件:${selectedFile?.name || ''}`"
|
||||
/>
|
||||
<div class="rounded bg-gray-50 dark:bg-gray-800 p-4">
|
||||
<div class="mb-2 font-medium">店铺信息</div>
|
||||
<div class="text-gray-700 dark:text-gray-200">
|
||||
@@ -1516,5 +1791,46 @@ const shopOptions = computed(() => {
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</SaveStatusTemplateModal>
|
||||
|
||||
<FixSpecCodeModal>
|
||||
<div class="space-y-3">
|
||||
<Alert
|
||||
type="info"
|
||||
show-icon
|
||||
:message="`共 ${editableMissingSpecCodeRows.length} 行缺少规格编码,请补全后应用`"
|
||||
/>
|
||||
<div class="max-h-[420px] overflow-y-auto rounded border border-gray-200 dark:border-gray-700">
|
||||
<table class="w-full text-sm">
|
||||
<thead class="bg-gray-50 dark:bg-gray-800">
|
||||
<tr>
|
||||
<th class="px-3 py-2 text-left font-medium">行号</th>
|
||||
<th class="px-3 py-2 text-left font-medium">订单号</th>
|
||||
<th class="px-3 py-2 text-left font-medium">商品</th>
|
||||
<th class="px-3 py-2 text-left font-medium">规格编码</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr
|
||||
v-for="row in editableMissingSpecCodeRows"
|
||||
:key="row.rowIndex"
|
||||
class="border-t border-gray-100 dark:border-gray-700"
|
||||
>
|
||||
<td class="px-3 py-2">{{ row.rowIndex + 2 }}</td>
|
||||
<td class="px-3 py-2">{{ row.orderNo || '-' }}</td>
|
||||
<td class="px-3 py-2">{{ row.productName || '-' }}</td>
|
||||
<td class="px-3 py-2">
|
||||
<Input
|
||||
v-model:value="row.specCode"
|
||||
placeholder="请输入规格编码"
|
||||
:maxlength="100"
|
||||
@blur="autoFillSpecCodeByProductName(row.productName, row.specCode)"
|
||||
/>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</FixSpecCodeModal>
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user