#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
标准库数据访问对象
用于从MySQL一次性加载所有标准数据到内存
"""
from typing import List, Dict


class StandardDAO:
    """标准库数据访问对象 - 负责从数据库加载数据"""

    def __init__(self, db_pool):
        self.db_pool = db_pool
        self.table_name = "t_samp_standard_base_info"

    async def load_all_standards(self) -> List[Dict]:
        """
        一次性从MySQL加载所有标准数据到内存

        Returns:
            标准列表，每个标准包含:
                - id: 序号
                - standard_name: 标准名称(chinese_name)
                - standard_number: 标准号
                - validity: 时效性(XH/SX/FZ)
        """
        query = f"""
            SELECT
                id,
                chinese_name AS standard_name,
                standard_number,
                validity
            FROM {self.table_name}
            WHERE validity IS NOT NULL
        """
        try:
            async with self.db_pool.get_cursor() as cursor:
                await cursor.execute(query)
                results = await cursor.fetchall()
                return [dict(row) for row in results] if results else []
        except Exception as e:
            raise RuntimeError(f"加载标准库数据失败: {e}")
