2025-04-21 16:07:41 +08:00
|
|
|
|
# globalData.py
|
2025-04-21 18:26:29 +08:00
|
|
|
|
import json
|
2025-04-27 07:52:07 +08:00
|
|
|
|
import logging
|
|
|
|
|
|
|
2025-04-21 18:26:29 +08:00
|
|
|
|
import redis
|
2025-04-24 13:55:49 +08:00
|
|
|
|
|
2025-04-27 07:52:07 +08:00
|
|
|
|
from logger_util import setup_logger
|
2025-04-21 18:26:29 +08:00
|
|
|
|
from config import redis_url
|
|
|
|
|
|
|
2025-04-27 07:52:07 +08:00
|
|
|
|
logger = setup_logger("GlobalData", level=logging.DEBUG)
|
2025-04-21 16:07:41 +08:00
|
|
|
|
|
2025-05-22 18:33:10 +08:00
|
|
|
|
|
2025-04-21 16:07:41 +08:00
|
|
|
|
class GlobalData:
|
|
|
|
|
|
# 数据字段
|
|
|
|
|
|
standard_company_program = {}
|
|
|
|
|
|
standard_company_name_list = []
|
|
|
|
|
|
simply_to_standard_company_name_map = {}
|
|
|
|
|
|
pinyin_simply_to_standard_company_name_map = {}
|
|
|
|
|
|
|
|
|
|
|
|
standard_project_name_list = []
|
|
|
|
|
|
simply_to_standard_project_name_map = {}
|
|
|
|
|
|
pinyin_simply_to_standard_project_name_map = {}
|
|
|
|
|
|
|
|
|
|
|
|
standard_construct_name_list = []
|
|
|
|
|
|
simply_to_standard_construct_name_map = {}
|
|
|
|
|
|
pinyin_simply_to_standard_construct_name_map = {}
|
|
|
|
|
|
|
|
|
|
|
|
standard_constractor_name_list = []
|
|
|
|
|
|
simply_to_standard_constractor_name_map = {}
|
|
|
|
|
|
pinyin_simply_to_standard_constractor_name_map = {}
|
|
|
|
|
|
|
2025-04-24 13:55:49 +08:00
|
|
|
|
standard_team_leader_name_list = []
|
|
|
|
|
|
simply_to_standard_team_leader_name_map = {}
|
|
|
|
|
|
pinyin_simply_to_standard_team_leader_name_map = {}
|
|
|
|
|
|
|
2025-05-22 18:33:10 +08:00
|
|
|
|
standard_design_pic = {}
|
|
|
|
|
|
|
2025-04-21 16:07:41 +08:00
|
|
|
|
@classmethod
|
2025-04-24 13:55:49 +08:00
|
|
|
|
def update_from_redis(cls):
|
|
|
|
|
|
import sys
|
2025-04-21 16:07:41 +08:00
|
|
|
|
from utils import (
|
|
|
|
|
|
clean_useless_company_name,
|
|
|
|
|
|
clean_useless_project_name,
|
2025-04-24 13:55:49 +08:00
|
|
|
|
clean_useless_team_leader_name
|
2025-04-21 16:07:41 +08:00
|
|
|
|
)
|
2025-04-24 13:55:49 +08:00
|
|
|
|
#实施组织(分公司)和项目名
|
|
|
|
|
|
cls._update_company_program()
|
|
|
|
|
|
#工程名
|
|
|
|
|
|
cls._update_list_data('SBD_QUERY_DATA:PROJECT_NAME', './standard_data/standard_project.txt',
|
|
|
|
|
|
cls.standard_project_name_list, cls.simply_to_standard_project_name_map,
|
|
|
|
|
|
cls.pinyin_simply_to_standard_project_name_map, clean_useless_project_name)
|
2025-04-27 07:52:07 +08:00
|
|
|
|
logger.info(f"工程名数量:{len(cls.standard_project_name_list)}")
|
2025-04-24 13:55:49 +08:00
|
|
|
|
|
|
|
|
|
|
#建管单位
|
|
|
|
|
|
cls._update_list_data('SBD_QUERY_DATA:CONSTRUCTION_UNIT', './standard_data/construct_unit.txt',
|
2025-05-22 18:33:10 +08:00
|
|
|
|
cls.standard_construct_name_list, cls.simply_to_standard_construct_name_map,
|
|
|
|
|
|
cls.pinyin_simply_to_standard_construct_name_map, clean_useless_company_name)
|
2025-04-27 07:52:07 +08:00
|
|
|
|
logger.info(f"建管单位数量:{len(cls.standard_construct_name_list)}")
|
2025-04-24 13:55:49 +08:00
|
|
|
|
|
|
|
|
|
|
#分包单位
|
|
|
|
|
|
cls._update_list_data('SBD_QUERY_DATA:SUBCONTRACTOR', './standard_data/sub_contract.txt',
|
2025-05-22 18:33:10 +08:00
|
|
|
|
cls.standard_constractor_name_list, cls.simply_to_standard_constractor_name_map,
|
|
|
|
|
|
cls.pinyin_simply_to_standard_constractor_name_map, clean_useless_company_name)
|
2025-04-27 07:52:07 +08:00
|
|
|
|
logger.info(f"分包单位数量:{len(cls.standard_constractor_name_list)}")
|
2025-04-24 13:55:49 +08:00
|
|
|
|
|
|
|
|
|
|
#班组名称
|
|
|
|
|
|
cls._update_list_data('SBD_QUERY_DATA:TEAM', './standard_data/team_leader.txt',
|
2025-05-22 18:33:10 +08:00
|
|
|
|
cls.standard_team_leader_name_list, cls.simply_to_standard_team_leader_name_map,
|
|
|
|
|
|
cls.pinyin_simply_to_standard_team_leader_name_map, clean_useless_team_leader_name)
|
|
|
|
|
|
|
|
|
|
|
|
cls.update_design_pic_info()
|
2025-04-27 07:52:07 +08:00
|
|
|
|
logger.info(f"班组名称数量:{len(cls.standard_team_leader_name_list)}")
|
2025-04-21 18:26:29 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2025-04-24 13:55:49 +08:00
|
|
|
|
def _update_company_program(cls):
|
2025-04-21 18:26:29 +08:00
|
|
|
|
from utils import (
|
2025-04-24 13:55:49 +08:00
|
|
|
|
load_standard_json_data,
|
|
|
|
|
|
save_dict_to_file,
|
2025-04-21 18:26:29 +08:00
|
|
|
|
clean_useless_company_name,
|
|
|
|
|
|
text_to_pinyin
|
|
|
|
|
|
)
|
2025-04-24 13:55:49 +08:00
|
|
|
|
# 公司与项目关系数据
|
|
|
|
|
|
try:
|
|
|
|
|
|
r = redis.from_url(redis_url, decode_responses=True)
|
|
|
|
|
|
json_str = r.get('SBD_QUERY_DATA:STANDARD_COMPANY_PROGRAM')
|
|
|
|
|
|
if json_str:
|
|
|
|
|
|
temp_data = json.loads(json_str)
|
2025-05-22 18:33:10 +08:00
|
|
|
|
save_dict_to_file(temp_data, "./standard_data/standard_company_program.json")
|
2025-04-27 07:52:07 +08:00
|
|
|
|
logger.info("[Info] Loaded STANDARD_COMPANY_PROGRAM from Redis")
|
2025-04-24 13:55:49 +08:00
|
|
|
|
else:
|
|
|
|
|
|
raise ValueError("Redis key not found")
|
|
|
|
|
|
except Exception as e:
|
2025-04-27 07:52:07 +08:00
|
|
|
|
logger.error(f"[Error] Error loading STANDARD_COMPANY_PROGRAM: {e}")
|
2025-04-24 13:55:49 +08:00
|
|
|
|
temp_data = load_standard_json_data("./standard_data/standard_company_program.json")
|
|
|
|
|
|
|
|
|
|
|
|
if temp_data != cls.standard_company_program:
|
2025-04-21 18:26:29 +08:00
|
|
|
|
cls.standard_company_program.clear()
|
2025-04-24 13:55:49 +08:00
|
|
|
|
cls.standard_company_program.update(temp_data)
|
2025-04-21 18:26:29 +08:00
|
|
|
|
|
|
|
|
|
|
cls.standard_company_name_list.clear()
|
2025-04-24 13:55:49 +08:00
|
|
|
|
cls.standard_company_name_list.extend(list(temp_data.keys()))
|
2025-04-21 18:26:29 +08:00
|
|
|
|
|
|
|
|
|
|
cls.simply_to_standard_company_name_map.clear()
|
|
|
|
|
|
cls.simply_to_standard_company_name_map.update({
|
|
|
|
|
|
clean_useless_company_name(kw): kw for kw in cls.standard_company_name_list
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
cls.pinyin_simply_to_standard_company_name_map.clear()
|
|
|
|
|
|
cls.pinyin_simply_to_standard_company_name_map.update({
|
|
|
|
|
|
text_to_pinyin(clean_useless_company_name(kw)): kw for kw in cls.standard_company_name_list
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-04-24 13:55:49 +08:00
|
|
|
|
@classmethod
|
|
|
|
|
|
def _update_list_data(cls, redis_key, local_path, target_list, simple_map, pinyin_map, cleaner):
|
|
|
|
|
|
from utils import (
|
|
|
|
|
|
load_standard_name_list,
|
|
|
|
|
|
save_standard_name_list_to_file,
|
|
|
|
|
|
text_to_pinyin
|
|
|
|
|
|
)
|
|
|
|
|
|
try:
|
|
|
|
|
|
r = redis.from_url(redis_url, decode_responses=True)
|
|
|
|
|
|
json_str = r.get(redis_key)
|
|
|
|
|
|
except Exception as e:
|
2025-04-27 07:52:07 +08:00
|
|
|
|
logger.info(f"[Error] Redis error on key '{redis_key}': {e}")
|
2025-04-24 13:55:49 +08:00
|
|
|
|
json_str = None
|
2025-04-21 18:26:29 +08:00
|
|
|
|
|
|
|
|
|
|
if json_str:
|
2025-04-24 13:55:49 +08:00
|
|
|
|
try:
|
|
|
|
|
|
temp_list = json.loads(json_str)
|
2025-05-22 18:33:10 +08:00
|
|
|
|
save_standard_name_list_to_file(temp_list, local_path)
|
2025-04-27 07:52:07 +08:00
|
|
|
|
logger.info(f"[Info] Loaded {redis_key} from Redis")
|
2025-04-24 13:55:49 +08:00
|
|
|
|
except json.JSONDecodeError as e:
|
2025-04-27 07:52:07 +08:00
|
|
|
|
logger.info(f"[Warning] JSON decode error on key '{redis_key}': {e}")
|
2025-04-24 13:55:49 +08:00
|
|
|
|
temp_list = load_standard_name_list(local_path)
|
2025-04-21 18:26:29 +08:00
|
|
|
|
else:
|
2025-04-27 07:52:07 +08:00
|
|
|
|
logger.info(f"[Info] Redis key '{redis_key}' not found. Loading from local file...")
|
2025-04-24 13:55:49 +08:00
|
|
|
|
temp_list = load_standard_name_list(local_path)
|
2025-04-21 18:26:29 +08:00
|
|
|
|
|
2025-04-24 13:55:49 +08:00
|
|
|
|
if temp_list != target_list:
|
|
|
|
|
|
target_list.clear()
|
|
|
|
|
|
target_list.extend(temp_list)
|
2025-04-21 18:26:29 +08:00
|
|
|
|
|
2025-04-24 13:55:49 +08:00
|
|
|
|
simple_map.clear()
|
|
|
|
|
|
simple_map.update({
|
|
|
|
|
|
cleaner(kw): kw for kw in temp_list
|
2025-04-21 18:26:29 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
2025-04-24 13:55:49 +08:00
|
|
|
|
pinyin_map.clear()
|
|
|
|
|
|
pinyin_map.update({
|
|
|
|
|
|
text_to_pinyin(cleaner(kw)): kw for kw in temp_list
|
2025-04-21 18:26:29 +08:00
|
|
|
|
})
|
2025-05-22 18:33:10 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
def update_design_pic_info(cls):
|
|
|
|
|
|
|
|
|
|
|
|
from utils import (
|
|
|
|
|
|
load_standard_json_data,
|
|
|
|
|
|
save_dict_to_file,
|
|
|
|
|
|
clean_useless_company_name,
|
|
|
|
|
|
text_to_pinyin
|
|
|
|
|
|
)
|
|
|
|
|
|
# 公司与工程关系数据
|
|
|
|
|
|
try:
|
|
|
|
|
|
r = redis.from_url(redis_url, decode_responses=True)
|
|
|
|
|
|
json_str = r.get('SBD_QUERY_DATA:STANDARD_DESIGN_PIC_INFO')
|
|
|
|
|
|
if json_str:
|
|
|
|
|
|
temp_data = json.loads(json_str)
|
|
|
|
|
|
save_dict_to_file(temp_data, "./standard_data/standard_project_info.json")
|
|
|
|
|
|
logger.info("[Info] Loaded STANDARD_DESIGN_PIC_INFO from Redis")
|
|
|
|
|
|
else:
|
|
|
|
|
|
raise ValueError("Redis key not found")
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"[Error] Error loading STANDARD_DESIGN_PIC_INFO: {e}")
|
|
|
|
|
|
temp_data = load_standard_json_data("./standard_data/standard_project_info.json")
|
|
|
|
|
|
|
|
|
|
|
|
print(f"STANDARD_DESIGN_PIC_INFO:{temp_data}")
|
|
|
|
|
|
if temp_data != cls.standard_design_pic:
|
|
|
|
|
|
cls.standard_design_pic.clear()
|
|
|
|
|
|
cls.standard_design_pic.update(temp_data)
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
def get_all_company_from_design_info(cls):
|
|
|
|
|
|
#获取所有分公司信息
|
|
|
|
|
|
return list(cls.standard_design_pic.keys())
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
def get_project_from_design_info(cls):
|
|
|
|
|
|
#获取所有工程信息
|
|
|
|
|
|
project_list = []
|
|
|
|
|
|
|
|
|
|
|
|
for company, projects in cls.standard_design_pic.items():
|
|
|
|
|
|
project_list.extend(projects.keys())
|
|
|
|
|
|
return project_list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
def get_contents_by_company_proj(cls, company_name, project_name):
|
|
|
|
|
|
#根据分公司名和工程名 获取方案,图纸和规范规程
|
|
|
|
|
|
global company_projects
|
|
|
|
|
|
if company_name and project_name:
|
|
|
|
|
|
return cls.standard_design_pic.get(company_name, {}).get(project_name, {})
|
|
|
|
|
|
elif not company_name and project_name:
|
|
|
|
|
|
for company, projects in cls.standard_design_pic.items():
|
|
|
|
|
|
if project_name in projects:
|
|
|
|
|
|
return projects[project_name] # 返回该工程下的“方案/图纸/规范规程”
|
|
|
|
|
|
elif company_name and not project_name:
|
|
|
|
|
|
result = {
|
|
|
|
|
|
"方案": [],
|
|
|
|
|
|
"图纸": [],
|
|
|
|
|
|
"规范规程": []
|
|
|
|
|
|
}
|
|
|
|
|
|
company_projects = cls.standard_design_pic.get(company_name, {})
|
|
|
|
|
|
# 忽略 "@type" 字段
|
|
|
|
|
|
if "@type" in company_projects:
|
|
|
|
|
|
del company_projects["@type"]
|
|
|
|
|
|
|
|
|
|
|
|
for proj_key, project in company_projects.items():
|
|
|
|
|
|
if "@type" in project:
|
|
|
|
|
|
del project["@type"]
|
|
|
|
|
|
for key in result:
|
|
|
|
|
|
result[key].extend(project.get(key, []))
|
|
|
|
|
|
|
|
|
|
|
|
# print(f"最终result:{result}")
|
|
|
|
|
|
return result
|
|
|
|
|
|
else:
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
def get_contents_by_proj(cls, project_name):
|
|
|
|
|
|
#根据工程名 获取方案,图纸和规范规程
|
|
|
|
|
|
for company, projects in cls.standard_design_pic.items():
|
|
|
|
|
|
if project_name in projects:
|
|
|
|
|
|
return projects[project_name] # 返回该工程下的“方案/图纸/规范规程”
|
|
|
|
|
|
return None # 没找到
|