import os import django import random from datetime import date, timedelta # 设置 Django 环境 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'XH_Digital_Management.settings') django.setup() from application.hrm_mgnt.models import Position, Rank, EmployeeInformation, EmployeeAttendanceRecord, \ OtherLeaveDetails, AnnualLeaveRecord def generate_test_data(): # 创建职务 positions = [] for i in range(5): position = Position.objects.create( position_name=f"职务名称{i}", position_description=f"职务描述{i}" ) positions.append(position) print(f"Created Position: {position}") # 创建职级 ranks = [] for i in range(5): rank = Rank.objects.create( rank_name=f"职级名称{i}", rank_description=f"职级描述{i}" ) ranks.append(rank) print(f"Created Rank: {rank}") # 创建员工基本信息 employees = [] for i in range(10): employee = EmployeeInformation.objects.create( name=f"员工姓名{i}", id_number=f"身份证号{i}", gender=random.choice(['男', '女', '其他']), birthday=date.today() - timedelta(days=random.randint(7000, 15000)), age=random.randint(20, 60), height=random.uniform(150, 190), weight=random.uniform(50, 100), blood_type=random.choice(['A', 'B', 'AB', 'O', '其他']), ethnicity=f"民族{i}", domicile=f"户籍地{i}", marital_status=random.choice(['未婚', '已婚未育', '已婚已育', '离婚', '其他']), political_affiliation=random.choice(['共产党员', '共青团员', '群众', '其他']), entry_date=date.today() - timedelta(days=random.randint(1, 1000)), regularization_date=date.today() - timedelta(days=random.randint(1, 1000)) if random.choice( [True, False]) else None, departure_date=date.today() - timedelta(days=random.randint(1, 1000)) if random.choice( [True, False]) else None, employment_type=random.choice(['全职', '兼职', '实习']), status=random.choice(['在职', '离职']), primary_department=f"一级部门{i}", secondary_department=f"二级部门{i}" if random.choice([True, False]) else '', position=random.choice(positions).position_name, grade=random.choice(ranks).rank_name if random.choice([True, False]) else '', contract_end_date=date.today() + timedelta(days=random.randint(100, 1000)) if random.choice( [True, False]) else None, mobile_number=f"手机号{i}", email=f"email{i}@example.com", mailing_address=f"通信地址{i}" if random.choice([True, False]) else '', emergency_contact=f"紧急联系人{i}" if random.choice([True, False]) else '', relation_with_contact=f"关系{i}" if random.choice([True, False]) else '', emergency_contact_phone=f"紧急联系人电话{i}" if random.choice([True, False]) else '', education=random.choice(['高中', '大专', '本科', '硕士研究生', '博士研究生', '其他']), undergraduate_school=f"本科院校{i}" if random.choice([True, False]) else '', graduate_school=f"研究生院校{i}" if random.choice([True, False]) else '', major=f"专业{i}" if random.choice([True, False]) else '', technical_title=f"技术职称{i}" if random.choice([True, False]) else '', base_salary=random.uniform(3000, 10000) if random.choice([True, False]) else None, salary_account_number=f"工资卡号{i}" if random.choice([True, False]) else '', bank_of_salary_account=f"开户行{i}" if random.choice([True, False]) else '', resignation_type=random.choice( ['合同期满', '主动辞职', '无条件辞职', '试用未通过', '辞退', '其他']) if random.choice( [True, False]) else '', resignation_reason=f"离职原因{i}" if random.choice([True, False]) else '' ) employees.append(employee) print(f"Created EmployeeInformation: {employee}") # 创建员工考勤记录 for i in range(20): attendance_record = EmployeeAttendanceRecord.objects.create( employee=random.choice(employees), year_month=date.today() - timedelta(days=random.randint(1, 365)), late=random.randint(0, 5), early_leave=random.randint(0, 5), absenteeism=random.randint(0, 5), annual_leave=random.randint(0, 5), personal_leave=random.randint(0, 5), sick_leave=random.randint(0, 5) ) print(f"Created EmployeeAttendanceRecord: {attendance_record}") # 创建其他假期详细记录 for i in range(10): other_leave = OtherLeaveDetails.objects.create( attendance_record=random.choice(EmployeeAttendanceRecord.objects.all()), leave_type=random.choice(['事假', '年假', '病假', '婚假', '丧假']), days=random.randint(1, 10), description=f"假期说明{i}" ) print(f"Created OtherLeaveDetails: {other_leave}") # 创建年假使用记录 for i in range(5): annual_leave_record = AnnualLeaveRecord.objects.create( year=random.randint(2020, 2024), employee_name=random.choice(employees).name, primary_department=f"一级部门{random.randint(0, 4)}", total_annual_leave=random.randint(5, 20), used_annual_leave=random.randint(0, 15), remaining_annual_leave=random.randint(0, 10) # 这里将会在 save() 中自动调整 ) annual_leave_record.save() print(f"Created AnnualLeaveRecord: {annual_leave_record}") if __name__ == "__main__": generate_test_data()