186 lines
6.0 KiB
Python
186 lines
6.0 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
import json
|
|||
|
|
import os
|
|||
|
|
from datetime import datetime
|
|||
|
|
|
|||
|
|
# 读取mock文件
|
|||
|
|
mock_file = 'src/mocks/projectLibraryMock.js'
|
|||
|
|
with open(mock_file, 'r', encoding='utf-8') as f:
|
|||
|
|
content = f.read()
|
|||
|
|
|
|||
|
|
# 创建备份
|
|||
|
|
backup_file = f"{mock_file}.backup_modal_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
|||
|
|
with open(backup_file, 'w', encoding='utf-8') as f:
|
|||
|
|
f.write(content)
|
|||
|
|
print(f"已创建备份:{backup_file}")
|
|||
|
|
|
|||
|
|
# 岗位等级排序权重
|
|||
|
|
level_order = {
|
|||
|
|
"普通岗": 1,
|
|||
|
|
"技术骨干岗": 2,
|
|||
|
|
"储备干部岗": 3
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 查找getMockProjectDetail函数
|
|||
|
|
detail_start = content.find('export const getMockProjectDetail = (id) => {')
|
|||
|
|
if detail_start == -1:
|
|||
|
|
print("未找到getMockProjectDetail函数")
|
|||
|
|
exit(1)
|
|||
|
|
|
|||
|
|
# 找到projects数组的开始
|
|||
|
|
projects_start = content.find('const projects = [', detail_start)
|
|||
|
|
if projects_start == -1:
|
|||
|
|
print("未找到projects数组")
|
|||
|
|
exit(1)
|
|||
|
|
|
|||
|
|
# 找到projects数组的结束
|
|||
|
|
array_end = -1
|
|||
|
|
bracket_count = 0
|
|||
|
|
in_string = False
|
|||
|
|
escape_next = False
|
|||
|
|
|
|||
|
|
for i in range(projects_start, len(content)):
|
|||
|
|
char = content[i]
|
|||
|
|
|
|||
|
|
if escape_next:
|
|||
|
|
escape_next = False
|
|||
|
|
continue
|
|||
|
|
|
|||
|
|
if char == '\\':
|
|||
|
|
escape_next = True
|
|||
|
|
continue
|
|||
|
|
|
|||
|
|
if char == '"' and not in_string:
|
|||
|
|
in_string = True
|
|||
|
|
elif char == '"' and in_string:
|
|||
|
|
in_string = False
|
|||
|
|
elif not in_string:
|
|||
|
|
if char == '[':
|
|||
|
|
bracket_count += 1
|
|||
|
|
elif char == ']':
|
|||
|
|
bracket_count -= 1
|
|||
|
|
if bracket_count == 0:
|
|||
|
|
array_end = i
|
|||
|
|
break
|
|||
|
|
|
|||
|
|
if array_end == -1:
|
|||
|
|
print("未找到projects数组结束")
|
|||
|
|
exit(1)
|
|||
|
|
|
|||
|
|
# 提取projects数组内容
|
|||
|
|
projects_content = content[projects_start:array_end+1]
|
|||
|
|
|
|||
|
|
# 修改每个项目的数据结构
|
|||
|
|
import re
|
|||
|
|
|
|||
|
|
# 为每个项目添加applicablePositions字段(复制positions数据并排序)
|
|||
|
|
def process_project(project_text):
|
|||
|
|
# 查找positions数组
|
|||
|
|
positions_match = re.search(r'"positions"\s*:\s*\[(.*?)\]', project_text, re.DOTALL)
|
|||
|
|
if positions_match:
|
|||
|
|
positions_content = positions_match.group(1)
|
|||
|
|
|
|||
|
|
# 检查是否已经存在applicablePositions
|
|||
|
|
if '"applicablePositions"' not in project_text:
|
|||
|
|
# 在positions后面添加applicablePositions
|
|||
|
|
# 提取positions数组内容并排序
|
|||
|
|
positions_items = []
|
|||
|
|
|
|||
|
|
# 查找所有position对象
|
|||
|
|
position_pattern = r'\{\s*"level"\s*:\s*"([^"]+)"\s*,\s*"position"\s*:\s*"([^"]+)"\s*\}'
|
|||
|
|
for match in re.finditer(position_pattern, positions_content):
|
|||
|
|
level = match.group(1)
|
|||
|
|
position = match.group(2)
|
|||
|
|
positions_items.append({
|
|||
|
|
'level': level,
|
|||
|
|
'position': position,
|
|||
|
|
'order': level_order.get(level, 99)
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
# 按等级排序
|
|||
|
|
positions_items.sort(key=lambda x: x['order'])
|
|||
|
|
|
|||
|
|
# 构建新的applicablePositions数组
|
|||
|
|
applicable_positions = []
|
|||
|
|
for item in positions_items:
|
|||
|
|
applicable_positions.append(f'''{{
|
|||
|
|
"level": "{item['level']}",
|
|||
|
|
"position": "{item['position']}"
|
|||
|
|
}}''')
|
|||
|
|
|
|||
|
|
applicable_str = f'''"applicablePositions": [
|
|||
|
|
{','.join(applicable_positions)}
|
|||
|
|
],'''
|
|||
|
|
|
|||
|
|
# 在positions数组后插入applicablePositions
|
|||
|
|
insert_pos = positions_match.end()
|
|||
|
|
project_text = project_text[:insert_pos] + ',\n ' + applicable_str + project_text[insert_pos:]
|
|||
|
|
|
|||
|
|
# 处理unit字段,将其拆分为compoundUnits和verticalUnits
|
|||
|
|
unit_match = re.search(r'"unit"\s*:\s*"([^"]+)"', project_text)
|
|||
|
|
if unit_match:
|
|||
|
|
unit_content = unit_match.group(1)
|
|||
|
|
|
|||
|
|
# 如果包含" / ",说明有两部分
|
|||
|
|
if ' / ' in unit_content:
|
|||
|
|
parts = unit_content.split(' / ')
|
|||
|
|
compound_unit = parts[0].strip()
|
|||
|
|
vertical_unit = parts[1].strip()
|
|||
|
|
else:
|
|||
|
|
# 只有一个单元,默认为复合能力课
|
|||
|
|
compound_unit = unit_content.strip()
|
|||
|
|
vertical_unit = ""
|
|||
|
|
|
|||
|
|
# 构建新的单元字段
|
|||
|
|
units_str = f'''"compoundUnits": ["{compound_unit}"],
|
|||
|
|
"verticalUnits": [{"'"+vertical_unit+"'" if vertical_unit else ""}]'''
|
|||
|
|
|
|||
|
|
# 检查是否已经存在这些字段
|
|||
|
|
if '"compoundUnits"' not in project_text and '"verticalUnits"' not in project_text:
|
|||
|
|
# 在unit后面添加新字段
|
|||
|
|
insert_pos = unit_match.end()
|
|||
|
|
project_text = project_text[:insert_pos] + ',\n ' + units_str + project_text[insert_pos:]
|
|||
|
|
|
|||
|
|
return project_text
|
|||
|
|
|
|||
|
|
# 处理整个projects数组
|
|||
|
|
new_content = content[:projects_start]
|
|||
|
|
|
|||
|
|
# 提取每个项目并处理
|
|||
|
|
project_pattern = r'(\{\s*"id".*?\}(?=,\s*\{|\s*\];))'
|
|||
|
|
projects = re.findall(project_pattern, projects_content, re.DOTALL)
|
|||
|
|
|
|||
|
|
processed_projects = []
|
|||
|
|
for project in projects:
|
|||
|
|
processed = process_project(project)
|
|||
|
|
processed_projects.append(processed)
|
|||
|
|
|
|||
|
|
# 重建projects数组
|
|||
|
|
new_projects_array = 'const projects = [\n ' + ',\n '.join(processed_projects) + '\n ];'
|
|||
|
|
|
|||
|
|
# 找到原数组结束位置的分号
|
|||
|
|
semicolon_pos = content.find(';', array_end)
|
|||
|
|
if semicolon_pos == -1:
|
|||
|
|
semicolon_pos = array_end + 1
|
|||
|
|
|
|||
|
|
# 替换内容
|
|||
|
|
new_content = content[:projects_start] + new_projects_array + content[semicolon_pos:]
|
|||
|
|
|
|||
|
|
# 写入文件
|
|||
|
|
with open(mock_file, 'w', encoding='utf-8') as f:
|
|||
|
|
f.write(new_content)
|
|||
|
|
|
|||
|
|
# 验证语法
|
|||
|
|
result = os.popen(f'node -c {mock_file} 2>&1').read()
|
|||
|
|
if result:
|
|||
|
|
print(f"❌ 语法错误:{result}")
|
|||
|
|
# 恢复备份
|
|||
|
|
with open(backup_file, 'r', encoding='utf-8') as f:
|
|||
|
|
content = f.read()
|
|||
|
|
with open(mock_file, 'w', encoding='utf-8') as f:
|
|||
|
|
f.write(content)
|
|||
|
|
print("已恢复备份")
|
|||
|
|
else:
|
|||
|
|
print("✓ 语法验证通过")
|
|||
|
|
print("✓ 成功添加applicablePositions和单元数组字段")
|
|||
|
|
print("✓ 岗位已按照\"普通岗-技术骨干岗-储备干部岗\"排序")
|