4 Commits

Author SHA1 Message Date
7b5846b489 V3.68,8.3推送 2026-08-03 15:53:52 +08:00
d0776f1036 feat(import): 前端导入策略选择器 + 部分导入支持
- 新增导入策略单选:跳过重复行 / 覆盖更新
- 预览状态新增「将被更新」标签(橙色),区分「通过」「失败」
- 支持部分导入:有错误行时显示「忽略错误,仅导入正确的 N 条」按钮
- 预览时传递 mode 参数给后端
- 导入结果展示新增/更新/跳过数量
2026-08-03 15:53:24 +08:00
22c7d352d6 refactor(import): API 层支持导入策略参数
- /preview 端点接收 mode 参数,update 模式下 DB 重复行不视为错误
- /execute 端点接收 mode 参数传递给 service 层
- execute 仅导入 status 为 success/update 的行,error 行不再被提交
2026-08-03 15:53:15 +08:00
95057dedf5 refactor(import): 导入服务核心逻辑改造
- 预览阶段增加 Excel 内部重复检测 + 空规格去重 + 字段长度校验
- 执行阶段支持 skip 模式(跳过重复行继续导入)+ update 模式(覆盖更新已有记录)
- update 模式空值保护:Excel 空单元格不覆盖已有数据
- DB commit 异常防御:字段超长等异常优雅返回而非 500
- 去重规则改为仅依据 spec_model(货号唯一键)
- BOM 导入物料查找同步改为 spec-only
2026-08-03 15:53:10 +08:00
4 changed files with 282 additions and 59 deletions

View File

@ -47,9 +47,16 @@ def download_template():
@jwt_required()
@permission_required('material_list:operation')
def preview_import():
"""Dry-Run 预览 + 验证(不写入数据库)"""
"""Dry-Run 预览 + 验证(不写入数据库)
FormData:
type: 'material' | 'bom'
file: Excel 文件
mode: 'skip' (默认) | 'update' — update 模式下 DB 重复行显示为「将被更新」而非错误
"""
try:
import_type = request.form.get('type', 'material').strip()
mode = request.form.get('mode', 'skip').strip()
file = request.files.get('file')
if not file:
return jsonify({'code': 400, 'msg': '请上传文件'}), 400
@ -60,11 +67,11 @@ def preview_import():
from io import BytesIO
if import_type == 'material':
results = preview_material(BytesIO(file_stream))
results = preview_material(BytesIO(file_stream), mode=mode)
else:
results = preview_bom(BytesIO(file_stream))
success_count = sum(1 for r in results if r['status'] == 'success')
success_count = sum(1 for r in results if r['status'] in ('success', 'update'))
error_count = sum(1 for r in results if r['status'] == 'error')
return jsonify({
@ -85,24 +92,33 @@ def preview_import():
@jwt_required()
@permission_required('material_list:operation')
def execute_import():
"""确认导入执行(事务保护)"""
"""确认导入执行
Body:
type: 'material' | 'bom'
rows: 待导入行列表
mode: 'skip' (跳过重复,默认) | 'update' (覆盖更新已有记录)
"""
try:
data = request.get_json() or {}
import_type = data.get('type', 'material').strip()
rows = data.get('rows', [])
mode = data.get('mode', 'skip').strip()
if not rows:
return jsonify({'code': 400, 'msg': '导入数据不能为空'}), 400
if import_type not in ('material', 'bom'):
return jsonify({'code': 400, 'msg': 'type 必须为 material 或 bom'}), 400
if mode not in ('skip', 'update'):
return jsonify({'code': 400, 'msg': 'mode 必须为 skip 或 update'}), 400
# 仅导入状态为 success 的行
valid_rows = [r for r in rows if r.get('status') == 'success']
# 仅导入状态为 success 或 update 的行error 行不参与导入
valid_rows = [r for r in rows if r.get('status') in ('success', 'update')]
if not valid_rows:
return jsonify({'code': 400, 'msg': '没有可导入的有效数据全部为error状态'}), 400
return jsonify({'code': 400, 'msg': '没有可导入的有效数据'}), 400
if import_type == 'material':
result = execute_material_import(valid_rows)
result = execute_material_import(valid_rows, mode=mode)
else:
result = execute_bom_import(valid_rows)

View File

@ -116,8 +116,14 @@ def generate_template(import_type: str) -> io.BytesIO:
return output
def preview_material(file_stream) -> list:
"""Dry-Run 预览基础信息导入"""
def preview_material(file_stream, mode: str = 'skip') -> list:
"""Dry-Run 预览基础信息导入(含 Excel 内部重复检测 + 空规格去重)
Args:
file_stream: Excel 文件流
mode: 'skip' — DB 重复视为错误(默认)
'update' — DB 重复标记为 'update' 状态(将被覆盖更新,不视为错误)
"""
import pandas as pd
df = pd.read_excel(file_stream, header=0)
@ -138,15 +144,19 @@ def preview_material(file_stream) -> list:
}
df.rename(columns=col_map, inplace=True)
# 批量预查已存在的 name+spec 组合(用于重复检测)
existing = db.session.query(MaterialBase.name, MaterialBase.spec_model).all()
existing_set = {(r.name, r.spec_model or '') for r in existing}
# 批量预查已存在的 spec_model作为唯一货号用于重复检测)
existing = db.session.query(MaterialBase.spec_model).all()
existing_set = {r.spec_model for r in existing if r.spec_model}
# Excel 文件内部去重映射spec → 首次出现行号)
excel_seen_map = {}
results = []
for idx, row in df.iterrows():
row_num = idx + 2 # Excel 行号
errors = []
name = str(row.get('name', '')).strip() if pd.notna(row.get('name')) else ''
# spec_model 作为唯一货号,空值严格拦截
spec = str(row.get('spec', '')).strip() if pd.notna(row.get('spec')) else ''
category = str(row.get('category', '')).strip() if pd.notna(row.get('category')) else ''
mat_type = str(row.get('type', '')).strip() if pd.notna(row.get('type')) else ''
@ -155,7 +165,7 @@ def preview_material(file_stream) -> list:
if not name:
errors.append("名称不能为空")
if not spec:
errors.append("规格型号不能为空")
errors.append("缺少必填项: 规格型号 (作为唯一货号不能为空)")
if not category:
errors.append("类别不能为空")
if not mat_type:
@ -163,8 +173,40 @@ def preview_material(file_stream) -> list:
if not unit:
errors.append("计量单位不能为空")
if name and spec and (name, spec) in existing_set:
errors.append(f"已存在相同名称和规格的数据: {name} / {spec}")
# ★ 新增:字段长度校验(预防 DB commit 时因字段超长回滚)
MAX_LENS = {
'名称': (name, 255), '规格型号': (spec, 255),
'所属公司': (str(row.get('company_name', '')) if pd.notna(row.get('company_name')) else '', 255),
'专业名称': (str(row.get('common_name', '')) if pd.notna(row.get('common_name')) else '', 255),
'类别': (category, 100), '类型': (mat_type, 100),
'计量单位': (unit, 50),
}
for field_label, (val, max_len) in MAX_LENS.items():
if len(val) > max_len:
errors.append(f"{field_label}超出数据库限制({len(val)}>{max_len}字符)")
# ★ 去重检测(仅依据 spec_model 作为唯一货号)
if spec:
is_db_dup = spec in existing_set
if is_db_dup:
if mode == 'update':
# 覆盖更新模式DB 重复不视为错误
if spec in excel_seen_map:
first_row = excel_seen_map[spec]
errors.append(f"与表格内第 {first_row} 行的规格型号重复: {spec}")
else:
excel_seen_map[spec] = row_num
else:
# 跳过模式DB 重复是错误
errors.append(f"数据库已存在相同规格型号 (货号) 的数据: {spec}")
else:
# 不在 DB 中 → 检查是否与 Excel 内前面行重复
if spec in excel_seen_map:
first_row = excel_seen_map[spec]
errors.append(f"与表格内第 {first_row} 行的规格型号重复: {spec}")
else:
excel_seen_map[spec] = row_num
company = str(row.get('company_name', '')).strip() if pd.notna(row.get('company_name')) else ''
common = str(row.get('common_name', '')).strip() if pd.notna(row.get('common_name')) else ''
@ -173,10 +215,18 @@ def preview_material(file_stream) -> list:
is_enabled = parse_boolean(row.get('is_enabled', True))
is_inspection = parse_boolean(row.get('is_inspection_required', False))
# 决定行状态update 模式下 DB 重复但不含其他错误的行 → 'update'
if mode == 'update' and not errors and spec and spec in existing_set:
row_status = 'update'
elif errors:
row_status = 'error'
else:
row_status = 'success'
results.append({
'row': row_num,
'status': 'error' if errors else 'success',
'error_msg': '; '.join(errors) if errors else '',
'status': row_status,
'error_msg': '; '.join(errors) if errors else ('将被覆盖更新' if row_status == 'update' else ''),
'data': {
'company_name': company,
'name': name,
@ -221,14 +271,14 @@ def preview_bom(file_stream) -> list:
}
df.rename(columns=col_map, inplace=True)
# 批量预查所有 MaterialBase
# 批量预查所有 MaterialBase(仅依据 spec_model 查找)
all_materials = db.session.query(
MaterialBase.id, MaterialBase.name, MaterialBase.spec_model
).all()
mat_lookup = {}
for m in all_materials:
key = (m.name, m.spec_model or '')
mat_lookup[key] = m.id
if m.spec_model:
mat_lookup[m.spec_model] = m.id
# 预查所有已存在的 (bom_no, version, parent_id, child_id) 组合
all_bom_pairs = db.session.query(
@ -259,15 +309,13 @@ def preview_bom(file_stream) -> list:
if pd.isna(dosage) or float(dosage) <= 0:
errors.append("用量必须大于0")
parent_key = (parent_name, parent_spec)
parent_id = mat_lookup.get(parent_key) if parent_name else None
parent_id = mat_lookup.get(parent_spec) if parent_spec else None
if parent_name and parent_id is None:
errors.append(f"父件不存在: {parent_name} / {parent_spec}")
errors.append(f"父件不存在 (规格型号): {parent_spec}")
child_key = (child_name, child_spec)
child_id = mat_lookup.get(child_key) if child_name else None
child_id = mat_lookup.get(child_spec) if child_spec else None
if child_name and child_id is None:
errors.append(f"子件不存在: {child_name} / {child_spec}")
errors.append(f"子件不存在 (规格型号): {child_spec}")
if parent_id and child_id and parent_id == child_id:
errors.append("父件与子件不能是同一物料")
@ -298,21 +346,75 @@ def preview_bom(file_stream) -> list:
return results
def execute_material_import(rows: list) -> dict:
"""执行基础信息批量导入(事务保护)"""
def execute_material_import(rows: list, mode: str = 'skip') -> dict:
"""执行基础信息批量导入
Args:
rows: 待导入行数据
mode: 'skip' - 跳过重复行,继续导入其余数据(默认)
'update' - 匹配到重复时覆盖更新已有记录
"""
import pandas as pd
def _has_value(val) -> bool:
"""判断值是否有效Excel 单元格非空、非 NaN"""
if val is None:
return False
try:
if pd.isna(val):
return False
except Exception:
pass
s = str(val).strip()
return s != '' and s != 'nan' and s != 'None'
inserted = 0
updated = 0
skipped = 0
errors = []
try:
for i, item in enumerate(rows):
d = item.get('data', item)
spec = d.get('spec_model', '')
try:
existing = MaterialBase.query.filter_by(
name=d['name'], spec_model=d['spec_model']
).first()
# ★ 仅依据 spec_model唯一货号查找已有记录
existing = MaterialBase.query.filter_by(spec_model=spec).first() if spec else None
if existing:
errors.append({'row': i + 2, 'msg': f"已存在: {d['name']} / {d['spec_model']}"})
continue
if mode == 'update':
# ★ 覆盖更新模式:用 Excel 数据更新已有记录(含名称)
# 仅当 Excel 单元格有实际值时覆盖,空值不覆盖已有数据
if _has_value(d.get('name')):
existing.name = str(d['name']).strip()
if _has_value(d.get('company_name')):
existing.company_name = str(d['company_name']).strip()
if _has_value(d.get('common_name')):
existing.common_name = str(d['common_name']).strip()
if _has_value(d.get('category')):
existing.category = str(d['category']).strip()
if _has_value(d.get('material_type')):
existing.material_type = str(d['material_type']).strip()
if _has_value(d.get('spec_model')):
existing.spec_model = str(d['spec_model']).strip()
if _has_value(d.get('unit')):
existing.unit = str(d['unit']).strip()
if d.get('reference_price') is not None and _has_value(d.get('reference_price')):
try:
existing.reference_price = float(d['reference_price'])
except Exception:
pass
if _has_value(d.get('is_inspection_required')):
existing.is_inspection_required = parse_boolean(d.get('is_inspection_required'))
if _has_value(d.get('is_enabled')):
existing.is_enabled = parse_boolean(d.get('is_enabled'))
updated += 1
continue
else:
# ★ 跳过模式(默认):记录跳过,继续导入其余行
skipped += 1
errors.append({'row': i + 2, 'msg': f"重复数据已跳过 (货号): {spec}"})
continue
mat = MaterialBase(
company_name=d.get('company_name', ''),
@ -331,18 +433,43 @@ def execute_material_import(rows: list) -> dict:
except Exception as e:
errors.append({'row': i + 2, 'msg': str(e)})
if errors:
# ★ 改造:不再"一刀切"回滚,只提交成功的行
# 防御:捕获 DB 级异常(字段超长、约束冲突等),优雅返回而非 500
try:
db.session.commit()
except Exception as commit_err:
db.session.rollback()
return {'success': False, 'inserted': 0, 'errors': errors,
'msg': f'导入失败,{len(errors)} 条数据校验未通过'}
return {
'success': False,
'inserted': 0,
'updated': 0,
'skipped': 0,
'errors': [{'row': 0, 'msg': f'数据库写入失败: {str(commit_err)}'}],
'msg': f'导入失败,数据库写入异常(可能字段超长或约束冲突): {str(commit_err)}',
}
db.session.commit()
return {'success': True, 'inserted': inserted, 'errors': [],
'msg': f'成功导入 {inserted} 条基础信息'}
# 构建结果消息
parts = []
if inserted > 0:
parts.append(f"新增 {inserted}")
if updated > 0:
parts.append(f"更新 {updated}")
if skipped > 0:
parts.append(f"跳过 {skipped} 条重复")
msg = "成功: " + "".join(parts) if parts else "没有数据被导入"
return {
'success': True,
'inserted': inserted,
'updated': updated,
'skipped': skipped,
'errors': errors,
'msg': msg,
}
except Exception as e:
db.session.rollback()
return {'success': False, 'inserted': 0,
return {'success': False, 'inserted': 0, 'updated': 0, 'skipped': 0,
'errors': [{'row': 0, 'msg': str(e)}], 'msg': f'导入异常: {str(e)}'}
@ -354,7 +481,8 @@ def execute_bom_import(rows: list) -> dict:
).all()
mat_lookup = {}
for m in all_materials:
mat_lookup[(m.name, m.spec_model or '')] = m.id
if m.spec_model:
mat_lookup[m.spec_model] = m.id
inserted = 0
errors = []
@ -369,11 +497,11 @@ def execute_bom_import(rows: list) -> dict:
version = d.get('version', 'V1.0')
if not parent_id:
pk = (d.get('parent_name', ''), d.get('parent_spec', ''))
parent_id = mat_lookup.get(pk)
parent_spec = d.get('parent_spec', '')
parent_id = mat_lookup.get(parent_spec) if parent_spec else None
if not child_id:
ck = (d.get('child_name', ''), d.get('child_spec', ''))
child_id = mat_lookup.get(ck)
child_spec = d.get('child_spec', '')
child_id = mat_lookup.get(child_spec) if child_spec else None
if not parent_id or not child_id:
errors.append({'row': i + 2, 'msg': '父件或子件在系统中不存在'})

View File

@ -239,7 +239,7 @@ const handleLogout = () => {
<footer v-if="!isLoginPage" class="app-footer">
<span class="version-tag">
<el-icon style="vertical-align: middle; margin-right: 4px"><InfoFilled /></el-icon>
当前版本:V3.67
当前版本:V3.68
</span>
</footer>

View File

@ -42,6 +42,22 @@
</el-upload>
</div>
<!-- 导入策略选择仅基础信息 -->
<div v-if="importType === 'material'" class="import-strategy">
<el-divider />
<span class="step-label">3. 导入策略</span>
<el-radio-group v-model="importMode" class="strategy-group">
<el-radio value="skip">
<span class="strategy-label">跳过重复行</span>
<span class="strategy-desc">仅新增不重复的数据已存在的直接跳过</span>
</el-radio>
<el-radio value="update">
<span class="strategy-label">覆盖更新</span>
<span class="strategy-desc"> Excel 中的新数据覆盖更新数据库已有记录</span>
</el-radio>
</el-radio-group>
</div>
<div v-if="previewLoading" class="preview-loading">
<el-icon class="is-loading" style="font-size:20px"><Loading /></el-icon>
<span style="margin-left:8px">正在解析和验证数据...</span>
@ -60,11 +76,11 @@
<el-table :data="previewRows" border stripe max-height="450" style="width:100%">
<el-table-column type="index" label="#" width="50" align="center" />
<el-table-column label="状态" width="90" align="center">
<el-table-column label="状态" width="100" align="center">
<template #default="{ row }">
<el-tag :type="row.status === 'success' ? 'success' : 'danger'" size="small">
{{ row.status === 'success' ? '通过' : '失败' }}
</el-tag>
<el-tag v-if="row.status === 'success'" type="success" size="small">通过</el-tag>
<el-tag v-else-if="row.status === 'update'" type="warning" size="small">将被更新</el-tag>
<el-tag v-else type="danger" size="small">失败</el-tag>
</template>
</el-table-column>
<el-table-column label="错误信息" min-width="200" show-overflow-tooltip>
@ -91,12 +107,24 @@
<!-- Footer -->
<template #footer>
<el-button @click="handleClose" :disabled="executing">取消</el-button>
<!-- Step 1: 预览按钮 -->
<el-button v-if="step === 1" type="primary" @click="handlePreview" :loading="previewLoading" :disabled="!fileReady">
预览验证
</el-button>
<el-button v-if="step === 2" type="primary" @click="handleExecute" :loading="executing" :disabled="errorCount > 0 || executing">
{{ executing ? '导入中...' : `确认导入 (${successCount} )` }}
<!-- Step 2: 全部通过/将被更新 直接导入 -->
<el-button v-if="step === 2 && errorCount === 0" type="primary" @click="handleExecute" :loading="executing">
{{ executing ? '导入中...' : `全部导入 (${importableCount} )` }}
</el-button>
<!-- Step 2: 部分通过 允许跳过错误行导入 -->
<el-button v-else-if="step === 2 && importableCount > 0" type="warning" @click="handleExecute" :loading="executing">
{{ executing ? '导入中...' : `忽略错误仅导入正确的 ${importableCount} ` }}
</el-button>
<!-- Step 2: 全部失败 禁用 -->
<el-button v-else-if="step === 2" disabled>无可导入数据</el-button>
</template>
</el-dialog>
</template>
@ -138,12 +166,21 @@ const fileList = ref<any[]>([])
const fileReady = ref(false)
const rawFile = ref<File | null>(null)
// ★ 新增:导入策略(仅 material 类型生效)
const importMode = ref<'skip' | 'update'>('skip')
const previewRows = ref<any[]>([])
const successCount = computed(() => previewRows.value.filter(r => r.status === 'success').length)
const updateCount = computed(() => previewRows.value.filter(r => r.status === 'update').length)
const errorCount = computed(() => previewRows.value.filter(r => r.status === 'error').length)
const previewSummary = computed(() =>
`${previewRows.value.length} 条: ${successCount.value} 条通过, ${errorCount.value} 条失败`
)
const importableCount = computed(() => successCount.value + updateCount.value)
const previewSummary = computed(() => {
const parts = [`${previewRows.value.length}`]
if (successCount.value > 0) parts.push(`${successCount.value} 条通过`)
if (updateCount.value > 0) parts.push(`${updateCount.value} 条将被更新`)
if (errorCount.value > 0) parts.push(`${errorCount.value} 条失败`)
return parts.join('')
})
// ═══ Preview Columns (depends on type) ═══
const materialColumns = [
@ -218,6 +255,7 @@ const handlePreview = async () => {
const formData = new FormData()
formData.append('file', rawFile.value)
formData.append('type', props.importType)
formData.append('mode', importMode.value) // ★ 预览时传递导入策略
const res: any = await request({
url: '/v1/import/preview',
@ -240,7 +278,7 @@ const handlePreview = async () => {
}
const handleExecute = async () => {
if (errorCount.value > 0) return
if (importableCount.value === 0) return
executing.value = true
try {
const res: any = await request({
@ -248,12 +286,19 @@ const handleExecute = async () => {
method: 'post',
data: {
type: props.importType,
rows: previewRows.value
rows: previewRows.value,
mode: importMode.value,
}
})
if (res.code === 200) {
ElMessage.success(res.msg || '导入成功')
const detail = res.data || {}
// ★ 构建详细结果提示
const parts: string[] = []
if (detail.inserted > 0) parts.push(`新增 ${detail.inserted}`)
if (detail.updated > 0) parts.push(`更新 ${detail.updated}`)
if (detail.skipped > 0) parts.push(`跳过 ${detail.skipped} 条重复`)
ElMessage.success(parts.length > 0 ? parts.join('') : (res.msg || '导入完成'))
emit('success')
handleClose()
} else {
@ -272,6 +317,7 @@ const handleClose = () => {
rawFile.value = null
fileReady.value = false
previewRows.value = []
importMode.value = 'skip'
visible.value = false
}
@ -283,6 +329,7 @@ watch(visible, (val) => {
rawFile.value = null
fileReady.value = false
previewRows.value = []
importMode.value = 'skip'
}
})
</script>
@ -309,6 +356,38 @@ watch(visible, (val) => {
color: #909399;
font-size: 14px;
}
.import-strategy {
margin-top: 8px;
}
.strategy-group {
display: flex;
flex-direction: column;
gap: 12px;
margin-top: 8px;
}
.strategy-group .el-radio {
margin-right: 0;
height: auto;
padding: 10px 14px;
border: 1px solid #DCDFE6;
border-radius: 6px;
transition: border-color 0.2s;
}
.strategy-group .el-radio.is-checked {
border-color: #409EFF;
background: #ECF5FF;
}
.strategy-label {
font-weight: 600;
font-size: 14px;
display: block;
}
.strategy-desc {
font-size: 12px;
color: #909399;
display: block;
margin-top: 2px;
}
:deep(.el-upload-dragger) {
width: 100%;
}