增加字段匹配模态框

This commit is contained in:
ah--desktop
2025-09-29 00:22:46 +08:00
parent 0dfd0be32f
commit 97ffe06578
13 changed files with 1398 additions and 23 deletions
@@ -0,0 +1,473 @@
<template>
<Modal
v-model:open="visible"
title="字段匹配配置"
width="1200px"
:footer="null"
@cancel="handleCancel"
>
<div class="field-mapping-container">
<!-- 说明信息 -->
<div class="mb-4 p-3 bg-blue-50 border border-blue-200 rounded-lg">
<div class="flex items-center space-x-2">
<InfoIcon class="w-4 h-4 text-blue-500" />
<span class="text-sm text-blue-800">
请将左侧系统字段与右侧CSV文件字段进行匹配,重新组织数据结构以正确进行实体匹配
</span>
</div>
</div>
<!-- 字段映射区域 -->
<div class="grid grid-cols-2 gap-6">
<!-- 左侧:系统字段 -->
<div class="system-fields">
<div class="mb-3">
<h3 class="text-lg font-medium text-gray-800">系统字段</h3>
<p class="text-sm text-gray-600">系统预定义的订单字段,必须映射</p>
</div>
<div class="space-y-3 max-h-96 overflow-y-auto">
<div
v-for="field in systemFields"
:key="field.key"
class="field-item p-3 border rounded-lg"
:class="{
'border-green-200 bg-green-50': fieldMappings[field.key],
'border-gray-200 bg-gray-50': !fieldMappings[field.key]
}"
>
<div class="flex items-center justify-between">
<div class="flex-1">
<div class="flex items-center space-x-2">
<span class="font-medium text-gray-800">{{ field.label }}</span>
<span
v-if="field.required"
class="text-red-500 text-xs"
>
*
</span>
</div>
<div class="text-xs text-gray-500 mt-1">{{ field.description }}</div>
</div>
<div class="flex items-center space-x-2">
<CheckIcon
v-if="fieldMappings[field.key]"
class="w-4 h-4 text-green-500"
/>
<XIcon
v-else
class="w-4 h-4 text-gray-400"
/>
</div>
</div>
</div>
</div>
</div>
<!-- 右侧:CSV字段映射 -->
<div class="csv-fields">
<div class="mb-3">
<h3 class="text-lg font-medium text-gray-800">CSV字段映射</h3>
<p class="text-sm text-gray-600">选择CSV文件中的对应字段</p>
</div>
<div class="space-y-3 max-h-96 overflow-y-auto">
<div
v-for="field in systemFields"
:key="field.key"
class="field-mapping-item p-3 border rounded-lg"
>
<div class="flex items-center justify-between mb-2">
<div class="flex items-center space-x-2">
<span class="font-medium text-gray-800">{{ field.label }}</span>
<span
v-if="field.required"
class="text-red-500 text-xs"
>
*
</span>
</div>
<div class="flex items-center space-x-2">
<CheckIcon
v-if="fieldMappings[field.key]"
class="w-4 h-4 text-green-500"
/>
<XIcon
v-else
class="w-4 h-4 text-gray-400"
/>
</div>
</div>
<Select
v-model:value="fieldMappings[field.key]"
placeholder="请选择CSV字段"
style="width: 100%"
:options="csvFieldOptions"
:filter-option="filterOption"
show-search
allow-clear
@change="(value: any) => handleFieldMappingChange(field.key, value)"
>
<template #option="{ label, value }">
<div class="flex items-center justify-between">
<span>{{ label }}</span>
<span class="text-xs text-gray-500">{{ value }}</span>
</div>
</template>
</Select>
<!-- 字段预览 -->
<div v-if="fieldMappings[field.key]" class="mt-2">
<div class="text-xs text-gray-600 mb-1">数据预览:</div>
<div class="text-xs bg-gray-100 p-2 rounded max-h-20 overflow-y-auto">
<div v-for="(sample, index) in getFieldSamples(fieldMappings[field.key] || '')" :key="index">
{{ sample }}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- 映射统计 -->
<div class="mt-6 p-4 bg-gray-50 border rounded-lg">
<div class="grid grid-cols-3 gap-4 text-center">
<div>
<div class="text-2xl font-bold text-blue-600">{{ systemFields.length }}</div>
<div class="text-sm text-gray-600">总字段数</div>
</div>
<div>
<div class="text-2xl font-bold text-green-600">{{ mappedFieldsCount }}</div>
<div class="text-sm text-gray-600">已映射</div>
</div>
<div>
<div class="text-2xl font-bold text-red-600">{{ unmappedFieldsCount }}</div>
<div class="text-sm text-gray-600">未映射</div>
</div>
</div>
</div>
<!-- 操作按钮 -->
<div class="flex justify-end space-x-3 mt-6">
<Button @click="handleCancel">
取消
</Button>
<Button @click="handleReset" type="default">
重置
</Button>
<Button
type="primary"
@click="handleConfirm"
:disabled="unmappedFieldsCount > 0"
>
确认匹配 ({{ mappedFieldsCount }}/{{ systemFields.length }})
</Button>
</div>
</div>
</Modal>
</template>
<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { Modal, Button, Select } from 'ant-design-vue'
import { createIconifyIcon } from '@vben/icons'
// 创建图标组件
const InfoIcon = createIconifyIcon('lucide:info')
const CheckIcon = createIconifyIcon('lucide:check')
const XIcon = createIconifyIcon('lucide:x')
interface SystemField {
key: string
label: string
description: string
required: boolean
dataType: string
}
interface Props {
visible: boolean
csvHeaders: string[]
csvData: any[]
}
interface Emits {
(e: 'update:visible', value: boolean): void
(e: 'confirm', mappings: Record<string, string>): void
(e: 'cancel'): void
}
const props = defineProps<Props>()
const emit = defineEmits<Emits>()
const visible = ref(false)
// 系统字段定义
const systemFields: SystemField[] = [
{
key: 'shopName',
label: '店铺名称',
description: '订单所属店铺名称',
required: true,
dataType: 'string'
},
{
key: 'productName',
label: '商品名称',
description: '订单商品名称',
required: true,
dataType: 'string'
},
{
key: 'productSpecName',
label: '商品规格',
description: '商品规格名称',
required: false,
dataType: 'string'
},
{
key: 'specCode',
label: '规格编码',
description: '商品规格编码',
required: false,
dataType: 'string'
},
{
key: 'orderNumber',
label: '订单编号',
description: '平台订单编号',
required: true,
dataType: 'string'
},
{
key: 'orderStatusName',
label: '订单状态',
description: '订单状态名称',
required: true,
dataType: 'string'
},
{
key: 'quantity',
label: '商品数量',
description: '订单商品数量',
required: true,
dataType: 'number'
},
{
key: 'shopActualAmount',
label: '实收金额',
description: '商家实际收到金额',
required: true,
dataType: 'number'
},
{
key: 'unitPrice',
label: '销售单价',
description: '商品销售单价',
required: false,
dataType: 'number'
},
{
key: 'costPrice',
label: '成本价',
description: '商品成本价格',
required: false,
dataType: 'number'
},
{
key: 'orderDate',
label: '下单时间',
description: '订单创建时间',
required: false,
dataType: 'date'
},
{
key: 'shippingFee',
label: '运费',
description: '订单运费',
required: false,
dataType: 'number'
}
]
// 字段映射状态
const fieldMappings = ref<Record<string, string>>({})
// CSV字段选项
const csvFieldOptions = computed(() => {
return props.csvHeaders.map(header => ({
label: header,
value: header
}))
})
// 已映射字段数量
const mappedFieldsCount = computed(() => {
return Object.values(fieldMappings.value).filter(value => value).length
})
// 未映射字段数量
const unmappedFieldsCount = computed(() => {
return systemFields.length - mappedFieldsCount.value
})
// 监听visible变化
watch(() => props.visible, (newVal) => {
visible.value = newVal
if (newVal) {
// 初始化字段映射
initializeFieldMappings()
}
})
watch(visible, (newVal) => {
emit('update:visible', newVal)
})
// 初始化字段映射
const initializeFieldMappings = () => {
const mappings: Record<string, string> = {}
// 尝试自动匹配字段
systemFields.forEach(field => {
const matchedHeader = findBestMatch(field.label, props.csvHeaders)
if (matchedHeader) {
mappings[field.key] = matchedHeader
}
})
fieldMappings.value = mappings
}
// 查找最佳匹配
const findBestMatch = (systemFieldLabel: string, csvHeaders: string[]): string | null => {
const systemFieldLower = systemFieldLabel.toLowerCase()
// 精确匹配
for (const header of csvHeaders) {
if (header.toLowerCase() === systemFieldLower) {
return header
}
}
// 包含匹配
for (const header of csvHeaders) {
if (header.toLowerCase().includes(systemFieldLower) ||
systemFieldLower.includes(header.toLowerCase())) {
return header
}
}
// 关键词匹配
const keywords = {
'店铺名称': ['店铺', 'shop', 'store'],
'商品名称': ['商品', 'product', 'item'],
'商品规格': ['规格', 'spec', '规格名称'],
'规格编码': ['编码', 'code', '规格编码'],
'订单编号': ['订单', 'order', '编号', 'number'],
'订单状态': ['状态', 'status'],
'商品数量': ['数量', 'quantity', 'qty'],
'实收金额': ['实收', '金额', 'amount', 'received'],
'销售单价': ['单价', 'price', 'unit'],
'成本价': ['成本', 'cost'],
'下单时间': ['时间', 'date', 'time', '下单'],
'运费': ['运费', 'shipping', 'freight']
}
const fieldKeywords = keywords[systemFieldLabel as keyof typeof keywords] || []
for (const header of csvHeaders) {
for (const keyword of fieldKeywords) {
if (header.toLowerCase().includes(keyword)) {
return header
}
}
}
return null
}
// 下拉框搜索过滤
const filterOption = (input: string, option: any) => {
return option.label.toLowerCase().includes(input.toLowerCase())
}
// 处理字段映射变化
const handleFieldMappingChange = (fieldKey: string, value: string | undefined) => {
fieldMappings.value[fieldKey] = value || ''
}
// 获取字段数据样本
const getFieldSamples = (fieldName: string) => {
if (!fieldName || !props.csvData.length) return []
return props.csvData.slice(0, 3).map(row => row[fieldName] || '').filter(val => val !== '')
}
// 处理确认
const handleConfirm = () => {
if (unmappedFieldsCount.value > 0) {
return
}
emit('confirm', { ...fieldMappings.value })
visible.value = false
}
// 处理取消
const handleCancel = () => {
visible.value = false
emit('cancel')
}
// 处理重置
const handleReset = () => {
fieldMappings.value = {}
}
</script>
<style scoped>
.field-mapping-container {
max-height: 80vh;
overflow-y: auto;
}
.field-item {
transition: all 0.2s ease;
}
.field-item:hover {
border-color: #3b82f6;
background-color: #eff6ff;
}
.field-mapping-item {
transition: all 0.2s ease;
}
.field-mapping-item:hover {
border-color: #3b82f6;
background-color: #eff6ff;
}
/* 滚动条样式 */
.overflow-y-auto::-webkit-scrollbar {
width: 6px;
}
.overflow-y-auto::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 3px;
}
.overflow-y-auto::-webkit-scrollbar-thumb {
background: #c1c1c1;
border-radius: 3px;
}
.overflow-y-auto::-webkit-scrollbar-thumb:hover {
background: #a8a8a8;
}
</style>
@@ -35,8 +35,8 @@
<Button @click="clearSelection">
清空选择
</Button>
<Button @click="showFieldMapping = true" :disabled="selectedRows.length === 0">
配置字段映射 ({{ selectedRows.length }})
<Button @click="showFieldMapping = true" :disabled="csvData.length === 0">
字段匹配
</Button>
</Space>
</template>
@@ -223,19 +223,13 @@
</template>
</Grid>
<!-- 字段映射弹窗 -->
<Modal
v-model:open="showFieldMapping"
title="字段映射配置"
width="1000px"
:footer="null"
>
<FieldMapping
:csv-headers="csvHeaders"
:csv-data="selectedRows"
@confirm="handleFieldMapping"
@cancel="showFieldMapping = false"
/>
</Modal>
<FieldMappingModal
v-model:visible="showFieldMapping"
:csv-headers="csvHeaders"
:csv-data="csvData"
@confirm="handleFieldMapping"
@cancel="showFieldMapping = false"
/>
<!-- 文件上传模态框 -->
<FileUploadModal
@@ -269,10 +263,10 @@
defineOptions({ name: 'DataImport' })
import { ref } from 'vue'
import { message, Progress, Button, Modal, Space } from 'ant-design-vue'
import { message, Progress, Button, Space } from 'ant-design-vue'
import { Page } from '@vben/common-ui'
import { createIconifyIcon } from '@vben/icons'
import FieldMapping from '#/views/ecommerce/data-import/components/FieldMapping.vue'
import FieldMappingModal from '#/views/ecommerce/data-import/components/FieldMappingModal.vue'
import FileUploadArea from './components/FileUploadArea.vue'
import FileUploadModal from './components/FileUploadModal.vue'
import EntityCreationModal from './components/EntityCreationModal.vue'
@@ -497,12 +491,37 @@ const clearData = () => {
gridApi.grid?.clearCheckboxRow()
}
// 字段映射处理
const handleFieldMapping = (mappedData: any) => {
// 字段匹配处理
const handleFieldMapping = (fieldMappings: Record<string, string>) => {
showFieldMapping.value = false
// 这里可以处理映射后的数据
console.log('映射后的数据:', mappedData)
message.success('字段映射配置完成')
// 应用字段匹配到CSV数据
const matchedCsvData = csvData.value.map(row => {
const matchedRow: any = {}
// 根据字段匹配重新组织数据
Object.entries(fieldMappings).forEach(([systemField, csvField]) => {
if (csvField && row[csvField] !== undefined) {
matchedRow[systemField] = row[csvField]
}
})
// 保留原始ID
matchedRow.id = row.id
return matchedRow
})
// 更新CSV数据
csvData.value = matchedCsvData
// 更新表格显示
updateTableData()
// 重新进行实体匹配
performEntityMatching()
message.success(`字段匹配完成,已匹配 ${Object.keys(fieldMappings).length} 个字段,正在重新进行实体匹配...`)
}
// 执行实体匹配