fix(hrm): 导入部门改为仅新增必填,修复更新被拦截

之前“所属部门必填”会拦掉部门列为空的更新行,导致已有员工无法被覆盖。
改为:填了才校验是否存在;为空时——更新保留原部门、新增才报错。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
zty 2026-06-03 14:45:01 +08:00
parent b395db7a28
commit 301e886a9e
1 changed files with 8 additions and 6 deletions

View File

@ -419,13 +419,12 @@ class EmployeeViewSet(CustomModelViewSet):
data['type'] = TYPE_MAPPING[excel_type]
else:
raise ParseError(f'{row_num}行,人员类型"{excel_type}"无效,有效类型:{", ".join(TYPE_MAPPING.keys())}')
# 处理部门外键(所属部门必填
# 处理部门外键:填了就校验是否存在并赋值;为空时不动(新增场景的必填在下方创建处校验
dept_name = data.pop('belong_dept', None)
if not dept_name:
raise ParseError(f'{row_num}行,所属部门为空')
if dept_name not in dept_map:
raise ParseError(f'{row_num}行,部门"{dept_name}"不存在')
data['belong_dept_id'] = dept_map[dept_name]
if dept_name:
if dept_name not in dept_map:
raise ParseError(f'{row_num}行,部门"{dept_name}"不存在')
data['belong_dept_id'] = dept_map[dept_name]
# 数据验证
if data.get('phone'):
@ -470,6 +469,9 @@ class EmployeeViewSet(CustomModelViewSet):
myLogger.info(f"⏭️ 第{row_num}行无变化:{name}")
created = False
else:
# 新增人员时所属部门必填
if 'belong_dept_id' not in data:
raise ParseError(f'{row_num}行,新增人员时所属部门不能为空')
Employee.objects.create(**data)
created = True
except Exception as e: