#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 更新剩余的两个岗位群:产品设计和自动化控制 """ import json import re from datetime import datetime print("=== 更新产品设计和自动化控制岗位群 ===\n") # 1. 读取源数据 print("1. 读取智能制造岗位简历.json...") with open('网页未导入数据/智能制造产业/智能制造岗位简历.json', 'r', encoding='utf-8') as f: source_data = json.load(f) # 2. 解析这两个岗位群的面试题 interview_questions_map = {} for item in source_data: job_group = item.get('简历岗位群', '') if job_group not in ['产品设计', '自动化控制']: continue if job_group in interview_questions_map: continue # 跳过重复的 interview_content = item.get('面试题内容', '') if not interview_content: continue # 收集所有问题 all_questions = [] lines = interview_content.split('\n') i = 0 while i < len(lines): line = lines[i].strip() # 匹配问题编号 question_match = re.match(r'^(\d+)[\.、]\s*(.+)', line) if question_match: question_text = question_match.group(2).strip() # 查找答案 answer = "" i += 1 while i < len(lines): next_line = lines[i].strip() if re.match(r'^\d+[\.、]\s*', next_line) or next_line.startswith('#'): i -= 1 break if next_line and (next_line.startswith('答案') or next_line.startswith('示例答案')): answer = re.sub(r'^(答案|示例答案)[::]?\s*', '', next_line).strip() if not answer: i += 1 if i < len(lines): answer = lines[i].strip() break elif next_line and not answer: answer = next_line break i += 1 if not answer: answer = "请根据实际情况回答" all_questions.append({ 'question': question_text, 'answer': answer }) i += 1 if all_questions: interview_questions_map[job_group] = all_questions print(f" - {job_group}: {len(all_questions)} 道题") print(f"\n共解析 {len(interview_questions_map)} 个岗位群") # 3. 读取当前mock文件 print("\n3. 读取当前mock文件...") with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f: mock_content = f.read() # 4. 备份 backup_time = datetime.now().strftime('%Y%m%d_%H%M%S') backup_file = f'src/mocks/resumeInterviewMock.js.backup_two_{backup_time}' print(f"4. 创建备份: {backup_file}") with open(backup_file, 'w', encoding='utf-8') as f: f.write(mock_content) # 5. 更新这两个岗位群 print("\n5. 开始更新...") for job_group, questions in interview_questions_map.items(): print(f"\n处理 {job_group} ({len(questions)} 道题)...") # 构建questions数组 sub_questions_list = [] for idx, q in enumerate(questions, 1): question_text = q['question'].replace('\\', '\\\\').replace('"', '\\"') answer_text = q['answer'].replace('\\', '\\\\').replace('"', '\\"').replace('\n', ' ') sub_questions_list.append(f'''{{ "id": "q1_{idx}", "question": "{question_text}", "answer": "{answer_text}" }}''') new_questions_str = f'''[ {{ "id": "group_q1", "question": "# {job_group}岗位面试题", "subQuestions": [ {','.join(sub_questions_list)} ] }} ]''' # 对于产品设计,它已有questions字段,需要替换 # 对于自动化控制,它没有questions字段,需要添加 if job_group == '产品设计': # 产品设计已有questions,直接替换 pattern = f'"name": "{job_group}"' start_pos = mock_content.find(pattern) if start_pos == -1: print(f" ❌ 未找到 {job_group}") continue # 找到questions字段 questions_pos = mock_content.find('"questions":', start_pos) if questions_pos == -1: print(f" ❌ 未找到 {job_group} 的questions字段") continue # 找到数组开始和结束 array_start = mock_content.find('[', questions_pos) bracket_count = 1 pos = array_start + 1 array_end = -1 while pos < len(mock_content) and bracket_count > 0: if mock_content[pos] == '[': bracket_count += 1 elif mock_content[pos] == ']': bracket_count -= 1 if bracket_count == 0: array_end = pos break pos += 1 if array_end != -1: before = mock_content[:array_start] after = mock_content[array_end + 1:] mock_content = before + new_questions_str + after print(f" ✅ 成功更新 {job_group}: {len(questions)} 道题") else: print(f" ❌ 未找到 {job_group} 的数组结束") elif job_group == '自动化控制': # 自动化控制没有questions字段,需要添加 pattern = f'"name": "{job_group}"' start_pos = mock_content.find(pattern) if start_pos == -1: print(f" ❌ 未找到 {job_group}") continue # 找到positions数组的结束位置 positions_pos = mock_content.find('"positions":', start_pos) if positions_pos == -1: print(f" ❌ 未找到 {job_group} 的positions字段") continue # 找到positions数组的结束] array_start = mock_content.find('[', positions_pos) bracket_count = 1 pos = array_start + 1 positions_end = -1 while pos < len(mock_content) and bracket_count > 0: if mock_content[pos] == '[': bracket_count += 1 elif mock_content[pos] == ']': bracket_count -= 1 if bracket_count == 0: positions_end = pos break pos += 1 if positions_end != -1: # 在positions数组结束后添加questions字段 insert_pos = positions_end + 1 # 跳过可能的空格和逗号 while insert_pos < len(mock_content) and mock_content[insert_pos] in ' \n\r\t': insert_pos += 1 # 添加questions字段 before = mock_content[:insert_pos] after = mock_content[insert_pos:] mock_content = before + ',\n "questions": ' + new_questions_str + after print(f" ✅ 成功添加 {job_group}: {len(questions)} 道题") else: print(f" ❌ 未找到 {job_group} 的positions数组结束") # 6. 保存文件 print(f"\n6. 保存更新后的文件...") with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f: f.write(mock_content) # 7. 验证语法 print("\n7. 验证语法...") import subprocess result = subprocess.run(['node', '-c', 'src/mocks/resumeInterviewMock.js'], capture_output=True, text=True) if result.returncode == 0: print("✅ 语法检查通过!") # 验证最终结果 print("\n=== 验证最终结果 ===") with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f: final_content = f.read() for group in interview_questions_map.keys(): # 查找该岗位群 group_pos = final_content.find(f'"name": "{group}"') if group_pos != -1: # 向后查找最近的questions questions_pos = final_content.find('"questions":', group_pos) if questions_pos != -1: # 统计题目数量 search_area = final_content[questions_pos:questions_pos + 50000] next_name = search_area.find('"name":') if next_name != -1: search_area = search_area[:next_name] actual_count = len(re.findall(r'"id": "q1_\d+"', search_area)) expected_count = len(interview_questions_map[group]) status = "✅" if actual_count == expected_count else f"⚠️" print(f" {status} {group}: {actual_count}/{expected_count} 道题") else: print("❌ 语法错误:") print(result.stderr) print("\n正在恢复备份...") with open(backup_file, 'r', encoding='utf-8') as f: mock_content = f.read() with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f: f.write(mock_content) print("已恢复到备份版本")