"""
data_money_supply 表 Model

货币供应量（原 price_indexes 拆分）。
"""
from typing import List, Dict, Any, Optional
from core.infra.db import DbBaseModel

from core.tables.macro.money_supply.schema import schema as _schema


class DataMoneySupplyModel(DbBaseModel):
    """货币供应量表 Model（表名 data_money_supply）"""

    def __init__(self, db=None):
        super().__init__(_schema["name"], db)

    def load_by_month(self, month: str) -> Optional[Dict[str, Any]]:
        """查询指定月份"""
        return self.load_one("date = %s", (month,))

    def load_by_date_range(
        self, start_month: str, end_month: str
    ) -> List[Dict[str, Any]]:
        """查询月份范围"""
        return self.load(
            "date BETWEEN %s AND %s",
            (start_month, end_month),
            order_by="date ASC",
        )

    def load_latest(self) -> Optional[Dict[str, Any]]:
        """查询最新一条"""
        return self.load_one("1=1", order_by="date DESC")

    def save_records(self, records: List[Dict[str, Any]]) -> int:
        """批量保存（按 date 去重）"""
        return self.upsert_many(records, unique_keys=["date"])
