refactor(order-import): 字段映射模板改为单行添加到模板

1. 选择模板后,每个字段行显示是否为模板映射(蓝色标签)或已变更(黄色标签)
2. 移除顶部「更新模板」按钮
3. 每个已映射的字段行显示「添加到模板」按钮,可单独将该字段加入模板
4. 模板选择下拉框不再自动选中第一个模板
This commit is contained in:
小蟑螂
2026-03-29 19:47:12 +08:00
parent 5095c6f397
commit 4f3259dae1
@@ -97,11 +97,16 @@ const fieldMappings = ref<
>({});
// 映射模板相关
const mappingTemplates = ref<ImportMappingVO.ImportMappingInfoVO[]>([]);
const mappingTemplates = ref<ImportMappingVO.ImportMappingVO[]>([]);
const selectedTemplateId = ref<number | undefined>();
const selectedTemplateName = ref('');
const templateLoading = ref(false);
const savingTemplate = ref(false);
const templateName = ref('');
// 保存单个字段映射到模板
const savingFieldToTemplate = ref<number | null>(null);
// 当前模板的原始映射数据(用于对比哪些字段是模板的)
const currentTemplateMappings = ref<FieldMappingItem[]>([]);
// 第三步:订单状态映射
const statusMappings = ref<Record<string, number>>({});
@@ -169,6 +174,40 @@ const canGoToNextStep = computed(() => {
return true;
});
// 判断某个字段的当前映射是否来自模板(即与模板原始定义一致)
const isMappingFromTemplate = (fieldDefId: number): boolean => {
if (!selectedTemplateId.value || currentTemplateMappings.value.length === 0) {
return false;
}
const fieldDef = fieldDefs.value.find((f) => f.id === fieldDefId);
if (!fieldDef) return false;
const templateItem = currentTemplateMappings.value.find(
(item) => item.fieldName === fieldDef.fieldName,
);
if (!templateItem) return false;
const currentMapping = fieldMappings.value[fieldDefId];
return currentMapping?.mappedHeader === templateItem.fileFieldName;
};
// 判断某个字段的映射与模板原始定义是否相同(用于提示"已变更")
const isMappingChangedFromTemplate = (fieldDefId: number): boolean => {
if (!selectedTemplateId.value || currentTemplateMappings.value.length === 0) {
return false;
}
const fieldDef = fieldDefs.value.find((f) => f.id === fieldDefId);
if (!fieldDef) return false;
const templateItem = currentTemplateMappings.value.find(
(item) => item.fieldName === fieldDef.fieldName,
);
if (!templateItem) return false;
const currentMapping = fieldMappings.value[fieldDefId];
// 模板有定义,但当前映射与模板不同
if (templateItem.fileFieldName && currentMapping?.mappedHeader !== templateItem.fileFieldName) {
return true;
}
return false;
};
// 初始化字段映射
const initFieldMappings = () => {
const mappings: typeof fieldMappings.value = {};
@@ -277,14 +316,6 @@ const fetchMappingTemplates = async () => {
});
mappingTemplates.value = result.records || [];
// 修复:自动选择第一个模板并应用
if (mappingTemplates.value.length > 0 && !selectedTemplateId.value) {
const firstTemplate = mappingTemplates.value[0];
if (firstTemplate?.id) {
selectedTemplateId.value = firstTemplate.id;
await handleSelectTemplate(firstTemplate.id);
}
}
} catch (error) {
console.error('获取映射模板列表失败:', error);
message.error('获取映射模板列表失败');
@@ -346,6 +377,8 @@ const handleSelectTemplate = async (value: any) => {
const templateId = typeof value === 'number' ? value : undefined;
if (!templateId) {
// 如果清空选择,重置映射
selectedTemplateName.value = '';
currentTemplateMappings.value = [];
if (fieldDefs.value.length > 0) {
initFieldMappings();
}
@@ -361,6 +394,10 @@ const handleSelectTemplate = async (value: any) => {
// 解析模板的 mapping JSON
const mappingItems: FieldMappingItem[] = JSON.parse(template.mapping);
// 保存模板名称和原始映射(用于对比)
selectedTemplateName.value = template.name;
currentTemplateMappings.value = mappingItems;
// 根据模板填充字段映射
const mappings: typeof fieldMappings.value = {};
fieldDefs.value.forEach((fieldDef) => {
@@ -393,6 +430,76 @@ const handleSelectTemplate = async (value: any) => {
}
};
// 将单个字段的映射添加到当前模板
const handleAddFieldToTemplate = async (fieldDefId: number) => {
if (!selectedTemplateId.value) {
message.warning('请先选择一个模板');
return;
}
const mapping = fieldMappings.value[fieldDefId];
if (!mapping || !mapping.mappedHeader) {
message.warning('请先完成该字段的映射');
return;
}
const fieldDef = mapping.fieldDef;
const fieldItem: FieldMappingItem = {
fieldName: fieldDef.fieldName,
fieldLabel: fieldDef.fieldLabel,
fileFieldName: mapping.mappedHeader,
required: fieldDef.required,
};
try {
savingFieldToTemplate.value = fieldDefId;
// 获取当前模板的最新数据
const template = mappingTemplates.value.find(
(t) => t.id === selectedTemplateId.value,
);
if (!template) {
message.error('模板不存在');
return;
}
// 解析现有映射
let mappingItems: FieldMappingItem[] = [];
try {
mappingItems = JSON.parse(template.mapping);
} catch {
mappingItems = [];
}
// 更新或添加该字段的映射
const existingIdx = mappingItems.findIndex(
(item) => item.fieldName === fieldDef.fieldName,
);
if (existingIdx >= 0) {
mappingItems[existingIdx] = fieldItem;
} else {
mappingItems.push(fieldItem);
}
const mappingJson = JSON.stringify(mappingItems);
await updateImportMappingApi(selectedTemplateId.value, {
name: template.name,
mapping: mappingJson,
});
// 更新本地缓存的模板数据
template.mapping = mappingJson;
currentTemplateMappings.value = mappingItems;
message.success(`已将「${fieldDef.fieldLabel}」映射添加到模板`);
} catch (error: any) {
console.error('添加到模板失败:', error);
message.error(error?.message || '添加到模板失败');
} finally {
savingFieldToTemplate.value = null;
}
};
// 选择订单状态映射模板
const handleSelectStatusTemplate = async (value: any) => {
const templateId = typeof value === 'number' ? value : undefined;
@@ -429,8 +536,13 @@ const handleSelectStatusTemplate = async (value: any) => {
}
};
// 打开保存/更新模板模态框
// 打开保存模板模态框(仅在未选择模板时可用)
const handleSaveTemplate = () => {
if (selectedTemplateId.value) {
message.info('已有模板选中,请使用每行「添加到模板」按钮单独更新字段映射');
return;
}
// 验证是否有映射数据
const mappingItems: FieldMappingItem[] = Object.values(fieldMappings.value)
.filter((m) => m.mappedHeader)
@@ -446,15 +558,7 @@ const handleSaveTemplate = () => {
return;
}
// 如果选择了模板,使用模板名称;否则清空
if (selectedTemplateId.value) {
const template = mappingTemplates.value.find(
(t) => t.id === selectedTemplateId.value,
);
templateName.value = template?.name || '';
} else {
templateName.value = '';
}
templateName.value = '';
saveTemplateModalApi.open();
};
@@ -522,34 +626,21 @@ async function handleConfirmSaveTemplate() {
const mappingJson = JSON.stringify(mappingItems);
// 如果选择了模板,则更新;否则创建
if (selectedTemplateId.value) {
await updateImportMappingApi(selectedTemplateId.value, {
name: templateName.value.trim(),
mapping: mappingJson,
});
message.success('更新映射模板成功');
} else {
const createData: ImportMappingDTO.CreateImportMappingDTO = {
type: ImportMappingType.ORDER,
name: templateName.value.trim(),
mapping: mappingJson,
};
await createImportMappingApi(createData);
message.success('保存映射模板成功');
}
const createData: ImportMappingDTO.CreateImportMappingDTO = {
type: ImportMappingType.ORDER,
name: templateName.value.trim(),
mapping: mappingJson,
};
await createImportMappingApi(createData);
message.success('保存映射模板成功');
templateName.value = '';
saveTemplateModalApi.close();
// 刷新模板列表
await fetchMappingTemplates();
// 如果是更新,保持选中状态;如果是创建,清空选中
if (!selectedTemplateId.value) {
selectedTemplateId.value = undefined;
}
} catch (error: any) {
console.error('保存/更新映射模板失败:', error);
message.error(error?.message || '保存/更新映射模板失败');
console.error('保存映射模板失败:', error);
message.error(error?.message || '保存映射模板失败');
} finally {
savingTemplate.value = false;
saveTemplateModalApi.unlock();
@@ -960,10 +1051,9 @@ const shopOptions = computed(() => {
</span>
</Select.Option>
</Select>
<Button v-if="!selectedTemplateId" @click="handleSaveTemplate">
<Button @click="handleSaveTemplate">
保存为模板
</Button>
<Button v-else @click="handleSaveTemplate"> 更新模板 </Button>
</div>
</div>
<div class="max-h-[400px] space-y-4 overflow-y-auto">
@@ -974,6 +1064,8 @@ const shopOptions = computed(() => {
:class="{
'border-red-300 bg-red-50':
fieldDef.required && !fieldMappings[fieldDef.id]?.mappedHeader,
'border-blue-200 bg-blue-50': isMappingFromTemplate(fieldDef.id),
'border-yellow-200 bg-yellow-50': isMappingChangedFromTemplate(fieldDef.id),
}"
>
<div class="flex-1">
@@ -982,16 +1074,34 @@ const shopOptions = computed(() => {
<span v-if="fieldDef.required" class="text-xs text-red-500">
(必填)
</span>
<span
v-if="selectedTemplateId && isMappingFromTemplate(fieldDef.id)"
class="ml-1 rounded bg-blue-100 px-1.5 py-0.5 text-xs text-blue-600"
>
模板映射
</span>
<span
v-if="selectedTemplateId && isMappingChangedFromTemplate(fieldDef.id)"
class="ml-1 rounded bg-yellow-100 px-1.5 py-0.5 text-xs text-yellow-600"
>
已变更
</span>
</div>
<div class="mt-1 text-xs text-gray-500">
字段名: {{ fieldDef.fieldName }}
</div>
<div
v-if="selectedTemplateId && currentTemplateMappings.find((m) => m.fieldName === fieldDef.fieldName)"
class="mt-1 text-xs text-gray-400"
>
模板原映射: {{ currentTemplateMappings.find((m) => m.fieldName === fieldDef.fieldName)?.fileFieldName || '(未映射)' }}
</div>
</div>
<Select
v-if="fieldMappings[fieldDef.id]"
v-model:value="fieldMappings[fieldDef.id]!.mappedHeader"
placeholder="请选择CSV列"
class="w-[200px]"
class="w-[160px]"
show-search
allow-clear
:options="
@@ -1001,6 +1111,15 @@ const shopOptions = computed(() => {
}))
"
/>
<Button
v-if="selectedTemplateId && fieldMappings[fieldDef.id]?.mappedHeader"
size="small"
type="link"
:loading="savingFieldToTemplate === fieldDef.id"
@click="handleAddFieldToTemplate(fieldDef.id)"
>
添加到模板
</Button>
</div>
</div>
</div>