63 lines
2.4 KiB
Python
63 lines
2.4 KiB
Python
from django.core.management.base import BaseCommand
|
||
import pandas as pd
|
||
from resumes.models import ResumeBasic, ResumeDetail
|
||
from django.db import transaction
|
||
|
||
|
||
class Command(BaseCommand):
|
||
help = "导入护理类简历详情数据到 ResumeDetail 模型"
|
||
|
||
def add_arguments(self, parser):
|
||
parser.add_argument("filepath", type=str, help="Excel 文件路径")
|
||
|
||
def handle(self, *args, **options):
|
||
filepath = options["filepath"]
|
||
df = pd.read_excel(filepath)
|
||
|
||
success, skipped, errors = 0, 0, []
|
||
|
||
for _, row in df.iterrows():
|
||
raw_resume_id = row.get("resume_id")
|
||
phone = row.get("phone")
|
||
email = row.get("email")
|
||
|
||
if pd.isna(raw_resume_id):
|
||
skipped += 1
|
||
continue
|
||
|
||
resume_id = None
|
||
try:
|
||
resume_id = int(str(raw_resume_id).strip().split(".")[0]) # 去掉小数点尾巴等干扰
|
||
except (ValueError, TypeError):
|
||
errors.append(f"resume_id 非法格式: {raw_resume_id}")
|
||
continue
|
||
|
||
try:
|
||
basic = ResumeBasic.objects.get(resume_id=resume_id)
|
||
ResumeDetail.objects.update_or_create(
|
||
resume=basic,
|
||
defaults={
|
||
"unlinked_resume_id": None,
|
||
"phone": str(phone).strip() if not pd.isna(phone) else "",
|
||
"email": str(email).strip() if not pd.isna(email) else ""
|
||
}
|
||
)
|
||
success += 1
|
||
except ResumeBasic.DoesNotExist:
|
||
ResumeDetail.objects.update_or_create(
|
||
unlinked_resume_id=resume_id,
|
||
defaults={
|
||
"resume": None,
|
||
"phone": str(phone).strip() if not pd.isna(phone) else "",
|
||
"email": str(email).strip() if not pd.isna(email) else ""
|
||
}
|
||
)
|
||
success += 1
|
||
errors.append(f"resume_id={resume_id} 无对应 ResumeBasic,已记录至 unlinked_resume_id")
|
||
|
||
self.stdout.write(self.style.SUCCESS(f"成功导入 {success} 条,跳过 {skipped} 条"))
|
||
if errors:
|
||
self.stdout.write(self.style.WARNING("以下数据未关联基础简历:"))
|
||
for msg in errors:
|
||
self.stdout.write(f" - {msg}")
|