150 lines
5.2 KiB
Python
150 lines
5.2 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
import json
|
|||
|
|
import re
|
|||
|
|
import datetime
|
|||
|
|
import shutil
|
|||
|
|
|
|||
|
|
def get_correct_units_from_json():
|
|||
|
|
"""从化工项目案例.json获取每个项目的正确单元名称"""
|
|||
|
|
|
|||
|
|
# 读取化工项目案例数据
|
|||
|
|
with open('网页未导入数据/化工产业/化工项目案例.json', 'r', encoding='utf-8') as f:
|
|||
|
|
data = json.load(f)
|
|||
|
|
|
|||
|
|
project_units = {}
|
|||
|
|
|
|||
|
|
for i, project in enumerate(data, 1):
|
|||
|
|
unit_name = project.get('对应单元名称(复合能力课)', '').strip()
|
|||
|
|
|
|||
|
|
# 如果有多个单元名称,取第一个作为主要单元
|
|||
|
|
if unit_name:
|
|||
|
|
# 用逗号分割,取第一个
|
|||
|
|
primary_unit = unit_name.split(',')[0].strip()
|
|||
|
|
else:
|
|||
|
|
primary_unit = '化工单元操作' # 默认单元
|
|||
|
|
|
|||
|
|
project_units[i] = primary_unit
|
|||
|
|
|
|||
|
|
return project_units
|
|||
|
|
|
|||
|
|
def update_units_precisely():
|
|||
|
|
"""精确更新unit字段"""
|
|||
|
|
|
|||
|
|
# 获取正确的单元数据
|
|||
|
|
correct_units = get_correct_units_from_json()
|
|||
|
|
|
|||
|
|
# 读取mock文件
|
|||
|
|
with open('src/mocks/projectLibraryMock.js', 'r', encoding='utf-8') as f:
|
|||
|
|
content = f.read()
|
|||
|
|
|
|||
|
|
# 统计更新
|
|||
|
|
updates_made = 0
|
|||
|
|
|
|||
|
|
print("开始精确更新unit字段:")
|
|||
|
|
|
|||
|
|
# 逐个项目更新
|
|||
|
|
for project_id, unit_name in correct_units.items():
|
|||
|
|
print(f"\n项目 {project_id}: {unit_name}")
|
|||
|
|
|
|||
|
|
# 更新项目列表中的unit字段(在getMockProjectsList函数中)
|
|||
|
|
# 查找模式:项目ID后面的unit字段
|
|||
|
|
list_pattern = rf'("id":\s*{project_id},[\s\S]*?)"unit":\s*"[^"]*"'
|
|||
|
|
|
|||
|
|
def list_replacer(match):
|
|||
|
|
return f'{match.group(1)}"unit": "{unit_name}"'
|
|||
|
|
|
|||
|
|
new_content = re.sub(list_pattern, list_replacer, content)
|
|||
|
|
|
|||
|
|
if new_content != content:
|
|||
|
|
content = new_content
|
|||
|
|
updates_made += 1
|
|||
|
|
print(f" ✅ 已更新项目列表中的unit")
|
|||
|
|
|
|||
|
|
# 更新项目详情中的unit字段(在getMockProjectDetail函数中)
|
|||
|
|
# 查找模式:在getMockProjectDetail函数内的项目ID后的unit字段
|
|||
|
|
detail_pattern = rf'(export const getMockProjectDetail[\s\S]*?"id":\s*{project_id},[\s\S]*?)"unit":\s*"[^"]*"'
|
|||
|
|
|
|||
|
|
def detail_replacer(match):
|
|||
|
|
return f'{match.group(1)}"unit": "{unit_name}"'
|
|||
|
|
|
|||
|
|
new_content_detail = re.sub(detail_pattern, detail_replacer, content)
|
|||
|
|
|
|||
|
|
if new_content_detail != content:
|
|||
|
|
content = new_content_detail
|
|||
|
|
updates_made += 1
|
|||
|
|
print(f" ✅ 已更新项目详情中的unit")
|
|||
|
|
|
|||
|
|
# 保存更新后的文件
|
|||
|
|
with open('src/mocks/projectLibraryMock.js', 'w', encoding='utf-8') as f:
|
|||
|
|
f.write(content)
|
|||
|
|
|
|||
|
|
print(f"\n{'=' * 60}")
|
|||
|
|
print(f"✅ 共执行了 {updates_made} 次更新操作")
|
|||
|
|
|
|||
|
|
# 验证语法
|
|||
|
|
import subprocess
|
|||
|
|
result = subprocess.run(['node', '-c', 'src/mocks/projectLibraryMock.js'],
|
|||
|
|
capture_output=True, text=True)
|
|||
|
|
if result.returncode == 0:
|
|||
|
|
print("✅ JavaScript语法检查通过")
|
|||
|
|
else:
|
|||
|
|
print(f"❌ JavaScript语法错误: {result.stderr}")
|
|||
|
|
|
|||
|
|
def verify_units_update():
|
|||
|
|
"""验证单元名称更新是否成功"""
|
|||
|
|
correct_units = get_correct_units_from_json()
|
|||
|
|
|
|||
|
|
print("\n" + "=" * 60)
|
|||
|
|
print("验证unit字段更新结果")
|
|||
|
|
print("=" * 60)
|
|||
|
|
|
|||
|
|
# 读取更新后的文件
|
|||
|
|
with open('src/mocks/projectLibraryMock.js', 'r', encoding='utf-8') as f:
|
|||
|
|
content = f.read()
|
|||
|
|
|
|||
|
|
all_correct = True
|
|||
|
|
|
|||
|
|
for project_id, expected_unit in correct_units.items():
|
|||
|
|
# 查找项目详情中的unit字段
|
|||
|
|
pattern = rf'"id":\s*{project_id},[\s\S]*?"unit":\s*"([^"]*)"'
|
|||
|
|
matches = re.findall(pattern, content)
|
|||
|
|
|
|||
|
|
if matches:
|
|||
|
|
actual_unit = matches[0] # 取第一个匹配(项目列表中的)
|
|||
|
|
if len(matches) > 1:
|
|||
|
|
detail_unit = matches[1] # 第二个匹配(项目详情中的)
|
|||
|
|
if actual_unit == expected_unit and detail_unit == expected_unit:
|
|||
|
|
print(f"✅ 项目 {project_id}: {actual_unit} (列表+详情)")
|
|||
|
|
else:
|
|||
|
|
print(f"❌ 项目 {project_id}: 列表[{actual_unit}] 详情[{detail_unit}] 期望[{expected_unit}]")
|
|||
|
|
all_correct = False
|
|||
|
|
else:
|
|||
|
|
if actual_unit == expected_unit:
|
|||
|
|
print(f"✅ 项目 {project_id}: {actual_unit}")
|
|||
|
|
else:
|
|||
|
|
print(f"❌ 项目 {project_id}: 实际[{actual_unit}] 期望[{expected_unit}]")
|
|||
|
|
all_correct = False
|
|||
|
|
else:
|
|||
|
|
print(f"❌ 项目 {project_id}: 未找到unit字段")
|
|||
|
|
all_correct = False
|
|||
|
|
|
|||
|
|
if all_correct:
|
|||
|
|
print("\n✅ 所有项目的unit字段已正确更新!")
|
|||
|
|
else:
|
|||
|
|
print("\n❌ 部分项目的unit字段更新有问题")
|
|||
|
|
|
|||
|
|
return all_correct
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
# 备份原文件
|
|||
|
|
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
|||
|
|
backup_file = f"src/mocks/projectLibraryMock.js.backup_units_precise_{timestamp}"
|
|||
|
|
shutil.copy('src/mocks/projectLibraryMock.js', backup_file)
|
|||
|
|
print(f"✅ 已备份到: {backup_file}")
|
|||
|
|
|
|||
|
|
# 执行更新
|
|||
|
|
update_units_precisely()
|
|||
|
|
|
|||
|
|
# 验证更新结果
|
|||
|
|
verify_units_update()
|