147 lines
4.7 KiB
Python
147 lines
4.7 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
为 ContactRecord 表添加测试数据的脚本。
|
|
用法: python manage.py shell < scripts/seed_contact_data.py
|
|
或 django-admin shell --settings=server.settings < scripts/seed_contact_data.py
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import django
|
|
from datetime import datetime, timedelta
|
|
|
|
# 配置 Django 环境
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings")
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
django.setup()
|
|
|
|
from server.models import ContactRecord
|
|
|
|
# 测试数据集合:包含微信号和电话号码
|
|
test_data = [
|
|
{
|
|
"name": "张三",
|
|
"position": "Python 开发工程师",
|
|
"contact": "13800138001", # 电话号码
|
|
"reply_status": "已回复",
|
|
"wechat_exchanged": True,
|
|
"worker_id": "worker_001",
|
|
"account_id": 1,
|
|
"notes": "积极主动,回复迅速",
|
|
"contacted_at": datetime.now() - timedelta(days=1),
|
|
},
|
|
{
|
|
"name": "李四",
|
|
"position": "前端开发工程师",
|
|
"contact": "wx_li_si_2023", # 微信号
|
|
"reply_status": "已回复",
|
|
"wechat_exchanged": True,
|
|
"worker_id": "worker_001",
|
|
"account_id": 1,
|
|
"notes": "已交换微信,约好周五面试",
|
|
"contacted_at": datetime.now() - timedelta(days=2),
|
|
},
|
|
{
|
|
"name": "王五",
|
|
"position": "Java 开发工程师",
|
|
"contact": "13900139001", # 电话号码
|
|
"reply_status": "未回复",
|
|
"wechat_exchanged": False,
|
|
"worker_id": "worker_002",
|
|
"notes": "待跟进",
|
|
"contacted_at": datetime.now() - timedelta(days=3),
|
|
},
|
|
{
|
|
"name": "赵六",
|
|
"position": "数据分析师",
|
|
"contact": "wx_zhao_liu", # 微信号
|
|
"reply_status": "已回复",
|
|
"wechat_exchanged": False,
|
|
"worker_id": "worker_002",
|
|
"account_id": 2,
|
|
"notes": "表示有兴趣,但尚未交换微信",
|
|
"contacted_at": datetime.now() - timedelta(days=4),
|
|
},
|
|
{
|
|
"name": "孙七",
|
|
"position": "产品经理",
|
|
"contact": "13600136001", # 电话号码
|
|
"reply_status": "已回复",
|
|
"wechat_exchanged": True,
|
|
"worker_id": "worker_001",
|
|
"account_id": 1,
|
|
"notes": "已交换微信,线索质量高",
|
|
"contacted_at": datetime.now() - timedelta(days=5),
|
|
},
|
|
{
|
|
"name": "周八",
|
|
"position": "设计师",
|
|
"contact": "wx_zhou_ba_design", # 微信号
|
|
"reply_status": "已回复",
|
|
"wechat_exchanged": True,
|
|
"worker_id": "worker_003",
|
|
"account_id": 3,
|
|
"notes": "已交换微信,待进一步沟通",
|
|
"contacted_at": datetime.now() - timedelta(days=6),
|
|
},
|
|
{
|
|
"name": "吴九",
|
|
"position": "后端开发工程师",
|
|
"contact": "18800188001", # 电话号码
|
|
"reply_status": "未回复",
|
|
"wechat_exchanged": False,
|
|
"worker_id": "worker_002",
|
|
"notes": "一小时前才联系",
|
|
"contacted_at": datetime.now() - timedelta(hours=1),
|
|
},
|
|
{
|
|
"name": "郑十",
|
|
"position": "运维工程师",
|
|
"contact": "wx_zheng_shi_ops", # 微信号
|
|
"reply_status": "已回复",
|
|
"wechat_exchanged": False,
|
|
"worker_id": "worker_001",
|
|
"account_id": 1,
|
|
"notes": "表示最近比较忙,下周可联系",
|
|
"contacted_at": datetime.now() - timedelta(days=2),
|
|
},
|
|
]
|
|
|
|
def seed_data():
|
|
"""插入测试数据到数据库"""
|
|
print("⏳ 正在添加测试数据...")
|
|
|
|
created_count = 0
|
|
for item in test_data:
|
|
# 检查该联系人是否已存在(基于名字和岗位)
|
|
existing = ContactRecord.objects.filter(
|
|
name=item["name"],
|
|
position=item["position"]
|
|
).exists()
|
|
|
|
if not existing:
|
|
contact = ContactRecord(**item)
|
|
contact.save()
|
|
created_count += 1
|
|
print(f"✓ 创建: {item['name']} ({item['position']}) - {item['contact']}")
|
|
else:
|
|
print(f"⊘ 跳过: {item['name']} (已存在)")
|
|
|
|
# 统计信息
|
|
total_contacts = ContactRecord.objects.count()
|
|
wechat_count = ContactRecord.objects.filter(contact__contains="wx").count()
|
|
phone_count = total_contacts - wechat_count
|
|
exchanged_count = ContactRecord.objects.filter(wechat_exchanged=True).count()
|
|
|
|
print(f"\n✅ 添加完成!")
|
|
print(f" 本次新增: {created_count} 条记录")
|
|
print(f" 总记录数: {total_contacts}")
|
|
print(f" 微信号数: {wechat_count}")
|
|
print(f" 电话号数: {phone_count}")
|
|
print(f" 已交换微信: {exchanged_count}")
|
|
|
|
if __name__ == "__main__":
|
|
seed_data()
|