-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_data.py
More file actions
executable file
·446 lines (393 loc) · 14.8 KB
/
init_data.py
File metadata and controls
executable file
·446 lines (393 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/usr/bin/env python
"""
数据库初始化脚本
创建测试数据和初始设置
"""
import os
import sys
import django
from datetime import date, timedelta, time
from decimal import Decimal
# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hr_system.settings')
django.setup()
from django.contrib.auth.models import User
from django.db import transaction
from departments.models import Department
from employees.models import Employee
from attendance.models import AttendanceRule, AttendanceRecord, LeaveType
from salary.models import SalaryStructure, PayrollPeriod
from recruitment.models import JobPosition
def create_superuser():
"""创建超级用户"""
print("创建超级用户...")
if not User.objects.filter(username='admin').exists():
User.objects.create_superuser(
username='admin',
email='admin@example.com',
password='admin123',
first_name='系统',
last_name='管理员'
)
print("✓ 超级用户创建成功: admin / admin123")
else:
print("✓ 超级用户已存在")
def create_departments():
"""创建部门数据"""
print("创建部门数据...")
departments = [
{'name': '总经理办', 'code': 'CEO', 'parent': None, 'description': '公司最高管理层'},
{'name': '人力资源部', 'code': 'HR', 'parent': None, 'description': '负责人员招聘、培训、薪酬管理等'},
{'name': '技术部', 'code': 'TECH', 'parent': None, 'description': '负责产品研发和技术支持'},
{'name': '市场部', 'code': 'MKT', 'parent': None, 'description': '负责市场推广和销售'},
{'name': '财务部', 'code': 'FIN', 'parent': None, 'description': '负责财务管理和会计核算'},
{'name': '前端开发组', 'code': 'TECH-FE', 'parent': 'TECH', 'description': '负责前端界面开发'},
{'name': '后端开发组', 'code': 'TECH-BE', 'parent': 'TECH', 'description': '负责后端服务开发'},
{'name': '测试组', 'code': 'TECH-QA', 'parent': 'TECH', 'description': '负责产品测试和质量保证'},
]
created_depts = {}
# 先创建顶级部门
for dept_data in departments:
if dept_data['parent'] is None:
dept, created = Department.objects.get_or_create(
code=dept_data['code'],
defaults={
'name': dept_data['name'],
'description': dept_data['description']
}
)
created_depts[dept_data['code']] = dept
if created:
print(f"✓ 创建部门: {dept.name}")
# 再创建子部门
for dept_data in departments:
if dept_data['parent'] is not None:
parent_dept = created_depts[dept_data['parent']]
dept, created = Department.objects.get_or_create(
code=dept_data['code'],
defaults={
'name': dept_data['name'],
'parent': parent_dept,
'description': dept_data['description']
}
)
created_depts[dept_data['code']] = dept
if created:
print(f"✓ 创建子部门: {dept.name}")
return created_depts
def create_employees(departments):
"""创建员工数据"""
print("创建员工数据...")
employees_data = [
{
'username': 'zhang_ceo',
'first_name': '明',
'last_name': '张',
'email': 'zhang.ming@example.com',
'employee_id': 'E001',
'department': 'CEO',
'position': '总经理',
'level': '高级管理',
'phone': '13800138001',
'gender': 'M',
'hire_date': date(2020, 1, 1),
'base_salary': Decimal('50000.00'),
'id_card': '110101199001011001'
},
{
'username': 'li_hr',
'first_name': '娜',
'last_name': '李',
'email': 'li.na@example.com',
'employee_id': 'E002',
'department': 'HR',
'position': 'HR经理',
'level': '中级管理',
'phone': '13800138002',
'gender': 'F',
'hire_date': date(2020, 3, 15),
'base_salary': Decimal('15000.00'),
'id_card': '110101199002021002'
},
{
'username': 'wang_tech',
'first_name': '强',
'last_name': '王',
'email': 'wang.qiang@example.com',
'employee_id': 'E003',
'department': 'TECH',
'position': '技术总监',
'level': '高级管理',
'phone': '13800138003',
'gender': 'M',
'hire_date': date(2020, 2, 1),
'base_salary': Decimal('25000.00'),
'id_card': '110101199003031003'
},
{
'username': 'chen_dev1',
'first_name': '磊',
'last_name': '陈',
'email': 'chen.lei@example.com',
'employee_id': 'E004',
'department': 'TECH-FE',
'position': '前端工程师',
'level': '高级工程师',
'phone': '13800138004',
'gender': 'M',
'hire_date': date(2021, 6, 1),
'base_salary': Decimal('12000.00'),
'id_card': '110101199004041004'
},
{
'username': 'liu_dev2',
'first_name': '静',
'last_name': '刘',
'email': 'liu.jing@example.com',
'employee_id': 'E005',
'department': 'TECH-BE',
'position': '后端工程师',
'level': '中级工程师',
'phone': '13800138005',
'gender': 'F',
'hire_date': date(2021, 8, 15),
'base_salary': Decimal('10000.00'),
'id_card': '110101199005051005'
},
{
'username': 'zhou_qa',
'first_name': '伟',
'last_name': '周',
'email': 'zhou.wei@example.com',
'employee_id': 'E006',
'department': 'TECH-QA',
'position': '测试工程师',
'level': '中级工程师',
'phone': '13800138006',
'gender': 'M',
'hire_date': date(2021, 10, 1),
'base_salary': Decimal('9000.00'),
'id_card': '110101199006061006'
},
{
'username': 'xu_mkt',
'first_name': '芳',
'last_name': '徐',
'email': 'xu.fang@example.com',
'employee_id': 'E007',
'department': 'MKT',
'position': '市场专员',
'level': '初级',
'phone': '13800138007',
'gender': 'F',
'hire_date': date(2022, 1, 15),
'base_salary': Decimal('8000.00'),
'id_card': '110101199007071007'
},
{
'username': 'sun_fin',
'first_name': '华',
'last_name': '孙',
'email': 'sun.hua@example.com',
'employee_id': 'E008',
'department': 'FIN',
'position': '会计',
'level': '中级',
'phone': '13800138008',
'gender': 'F',
'hire_date': date(2022, 3, 1),
'base_salary': Decimal('7500.00'),
'id_card': '110101199008081008'
}
]
created_employees = {}
for emp_data in employees_data:
# 创建用户账号
user, user_created = User.objects.get_or_create(
username=emp_data['username'],
defaults={
'first_name': emp_data['first_name'],
'last_name': emp_data['last_name'],
'email': emp_data['email'],
'password': 'pbkdf2_sha256$320000$xyz$abc123' # 默认密码: 123456
}
)
if user_created:
user.set_password('123456')
user.save()
# 创建员工信息
employee, emp_created = Employee.objects.get_or_create(
employee_id=emp_data['employee_id'],
defaults={
'user': user,
'department': departments[emp_data['department']],
'position': emp_data['position'],
'level': emp_data['level'],
'phone': emp_data['phone'],
'gender': emp_data['gender'],
'hire_date': emp_data['hire_date'],
'base_salary': emp_data['base_salary'],
'id_card': emp_data['id_card']
}
)
created_employees[emp_data['employee_id']] = employee
if emp_created:
print(f"✓ 创建员工: {employee.full_name} ({employee.employee_id})")
# 设置管理关系
management_relations = [
('E002', 'E001'), # HR经理 -> 总经理
('E003', 'E001'), # 技术总监 -> 总经理
('E004', 'E003'), # 前端工程师 -> 技术总监
('E005', 'E003'), # 后端工程师 -> 技术总监
('E006', 'E003'), # 测试工程师 -> 技术总监
]
for subordinate_id, manager_id in management_relations:
subordinate = created_employees[subordinate_id]
manager = created_employees[manager_id]
subordinate.manager = manager
subordinate.save()
# 设置部门负责人
departments['CEO'].manager = created_employees['E001'].user
departments['HR'].manager = created_employees['E002'].user
departments['TECH'].manager = created_employees['E003'].user
departments['CEO'].save()
departments['HR'].save()
departments['TECH'].save()
return created_employees
def create_attendance_data():
"""创建考勤相关数据"""
print("创建考勤数据...")
# 创建考勤规则
rule, created = AttendanceRule.objects.get_or_create(
name='标准考勤规则',
defaults={
'work_start_time': time(9, 0),
'work_end_time': time(18, 0),
'break_start_time': time(12, 0),
'break_end_time': time(13, 0),
'late_threshold': 10,
'early_leave_threshold': 10,
'is_default': True
}
)
if created:
print("✓ 创建考勤规则")
# 创建请假类型
leave_types = [
{'name': '年假', 'code': 'ANNUAL', 'days_per_year': 5, 'is_paid': True},
{'name': '病假', 'code': 'SICK', 'days_per_year': 0, 'is_paid': True},
{'name': '事假', 'code': 'PERSONAL', 'days_per_year': 0, 'is_paid': False},
{'name': '婚假', 'code': 'MARRIAGE', 'days_per_year': 3, 'is_paid': True},
{'name': '产假', 'code': 'MATERNITY', 'days_per_year': 98, 'is_paid': True},
{'name': '陪产假', 'code': 'PATERNITY', 'days_per_year': 15, 'is_paid': True},
]
for leave_data in leave_types:
leave_type, created = LeaveType.objects.get_or_create(
code=leave_data['code'],
defaults=leave_data
)
if created:
print(f"✓ 创建请假类型: {leave_type.name}")
def create_salary_data():
"""创建薪资相关数据"""
print("创建薪资数据...")
# 创建薪资结构
structures = [
{
'name': '管理层薪资结构',
'code': 'MGT',
'base_salary_rate': Decimal('0.6'),
'performance_rate': Decimal('0.3'),
'allowance_rate': Decimal('0.1'),
},
{
'name': '技术人员薪资结构',
'code': 'TECH',
'base_salary_rate': Decimal('0.7'),
'performance_rate': Decimal('0.2'),
'allowance_rate': Decimal('0.1'),
},
{
'name': '普通员工薪资结构',
'code': 'GENERAL',
'base_salary_rate': Decimal('0.8'),
'performance_rate': Decimal('0.15'),
'allowance_rate': Decimal('0.05'),
}
]
for struct_data in structures:
structure, created = SalaryStructure.objects.get_or_create(
code=struct_data['code'],
defaults=struct_data
)
if created:
print(f"✓ 创建薪资结构: {structure.name}")
# 创建工资周期
today = date.today()
period, created = PayrollPeriod.objects.get_or_create(
year=today.year,
month=today.month,
defaults={
'name': f'{today.year}年{today.month}月工资',
'start_date': date(today.year, today.month, 1),
'end_date': today
}
)
if created:
print(f"✓ 创建工资周期: {period.name}")
def create_attendance_records(employees):
"""创建考勤记录"""
print("创建考勤记录...")
# 为每个员工创建最近7天的考勤记录
today = date.today()
for i in range(7):
check_date = today - timedelta(days=i)
# 跳过周末
if check_date.weekday() >= 5:
continue
for emp_id, employee in employees.items():
# 90%的概率正常出勤
import random
if random.random() < 0.9:
check_in_time = time(9, random.randint(0, 30)) # 9:00-9:30之间签到
check_out_time = time(18, random.randint(0, 60)) # 18:00-19:00之间签退
record, created = AttendanceRecord.objects.get_or_create(
employee=employee,
date=check_date,
defaults={
'check_in_time': check_in_time,
'check_out_time': check_out_time,
'status': 'PRESENT'
}
)
def main():
"""主函数"""
print("开始初始化HR管理系统数据...")
print("=" * 50)
try:
with transaction.atomic():
# 创建超级用户
create_superuser()
# 创建部门
departments = create_departments()
# 创建员工
employees = create_employees(departments)
# 创建考勤数据
create_attendance_data()
# 创建薪资数据
create_salary_data()
# 创建考勤记录
create_attendance_records(employees)
print("=" * 50)
print("✓ 数据初始化完成!")
print("\n登录信息:")
print("- 管理员账号: admin / admin123")
print("- 员工账号: 各员工用户名 / 123456")
print(" 例如: zhang_ceo / 123456")
print("\n请访问 http://127.0.0.1:8000/ 开始使用系统")
except Exception as e:
print(f"✗ 数据初始化失败: {e}")
import traceback
traceback.print_exc()
if __name__ == '__main__':
main()