diff --git a/src/repositories/sur_config_repository.py b/src/repositories/sur_config_repository.py new file mode 100644 index 0000000..9f2a783 --- /dev/null +++ b/src/repositories/sur_config_repository.py @@ -0,0 +1,74 @@ +""" +配置相关数据访问层 +""" + +from typing import List, Dict, Optional +from sqlalchemy.orm import Session +from models.sur_config import SurConfig, SurConfigBase +from config import settings + + +class SurConfigRepository: + """配置数据访问类""" + + def __init__(self, db: Session): + self.db = db + + def get_face_config_params(self) -> Dict[str, str]: + """ + 获取人脸识别配置参数 + + 返回: + 配置参数字典 {config_key: config_value} + """ + try: + # 查询全局人脸配置 + config_records = self.db.query(SurConfig).filter( + SurConfig.scope == settings.SUR_CONFIG_SCOPE_GLOBAL, + SurConfig.config_type == settings.SUR_CONFIG_TYPE_FACE + ).all() + + # 查询配置组对应的基础配置 + config_group_ids = [record.config_group_id for record in config_records if record.config_group_id] + + if config_group_ids: + base_configs = self.db.query(SurConfigBase).filter( + SurConfigBase.group_id.in_(config_group_ids) + ).all() + + # 合并配置 + config_dict = {} + for record in config_records: + if record.config_key and record.config_value: + config_dict[record.config_key] = record.config_value + + return config_dict + + return {} + + except Exception as e: + print(f"获取人脸配置参数失败: {e}") + return {} + + def get_face_config_value(self, config_key: str) -> Optional[str]: + """ + 获取指定配置键的值 + + 参数: + config_key: 配置键 + + 返回: + 配置值,如果不存在返回None + """ + try: + config_record = self.db.query(SurConfig).filter( + SurConfig.scope == settings.SUR_CONFIG_SCOPE_GLOBAL, + SurConfig.config_type == settings.SUR_CONFIG_TYPE_FACE, + SurConfig.config_key == config_key + ).first() + + return config_record.config_value if config_record else None + + except Exception as e: + print(f"获取配置值失败 {config_key}: {e}") + return None \ No newline at end of file diff --git a/src/repositories/sur_person_repository.py b/src/repositories/sur_person_repository.py new file mode 100644 index 0000000..3bebe76 --- /dev/null +++ b/src/repositories/sur_person_repository.py @@ -0,0 +1,89 @@ +""" +人员相关数据访问层 +""" + +from typing import List, Dict, Optional +from sqlalchemy.orm import Session +from models.sur_person import SurPersonBlacklist, SurFaceFeature +from config import settings + + +class SurPersonRepository: + """人员数据访问类""" + + def __init__(self, db: Session): + self.db = db + + def get_blacklist_face_features(self) -> Dict[int, str]: + """ + 获取黑名单人员的人脸特征 + + 返回: + 字典 {person_id: feature_data} + """ + try: + # 查询启用的黑名单人员 + blacklist_persons = self.db.query(SurPersonBlacklist).filter( + SurPersonBlacklist.status == 1 + ).all() + + if not blacklist_persons: + return {} + + person_ids = [person.person_id for person in blacklist_persons] + + # 查询对应的人脸特征 + face_features = self.db.query(SurFaceFeature).filter( + SurFaceFeature.person_id.in_(person_ids), + SurFaceFeature.feature_type == settings.FACE_MODEL_VERSION, + SurFaceFeature.status == 2 # 计算成功的特征 + ).all() + + # 构建特征字典 + feature_dict = {} + for feature in face_features: + if feature.feature_data: + feature_dict[feature.person_id] = feature.feature_data + + return feature_dict + + except Exception as e: + print(f"获取黑名单人脸特征失败: {e}") + return {} + + def get_blacklist_person_count(self) -> int: + """获取黑名单人员数量""" + try: + count = self.db.query(SurPersonBlacklist).filter( + SurPersonBlacklist.status == 1 + ).count() + return count + except Exception as e: + print(f"获取黑名单人员数量失败: {e}") + return 0 + + def get_blacklist_face_feature_count(self) -> int: + """获取有特征数据的黑名单人员数量""" + try: + # 查询启用的黑名单人员 + blacklist_persons = self.db.query(SurPersonBlacklist).filter( + SurPersonBlacklist.status == 1 + ).all() + + if not blacklist_persons: + return 0 + + person_ids = [person.person_id for person in blacklist_persons] + + # 查询对应的人脸特征数量 + count = self.db.query(SurFaceFeature).filter( + SurFaceFeature.person_id.in_(person_ids), + SurFaceFeature.feature_type == settings.FACE_MODEL_VERSION, + SurFaceFeature.status == 2 # 计算成功的特征 + ).count() + + return count + + except Exception as e: + print(f"获取黑名单人脸特征数量失败: {e}") + return 0 \ No newline at end of file