from src.repositories.base_repository import BaseRepository
from src.model.db.master_mente_count import MasterMenteCountModel
from src.logging.get_logger import get_logger

logger = get_logger('ビジネスユニットマスタ')


class BuMasterRepository(BaseRepository):

    FETCH_COUNT_SQL = """\
        SELECT
            COUNT(*) AS count
        FROM
            src05.bu
        WHERE
            bu.bu_cd = :bu_cd
    """

    def fetch_count(self, bu_cd) -> MasterMenteCountModel:
        try:
            query = self.FETCH_COUNT_SQL
            result = self._database.execute_select(query, {'bu_cd': bu_cd})
            models = [MasterMenteCountModel(**r) for r in result]
            if len(models) == 0:
                return 0
            return models[0].count
        except Exception as e:
            logger.exception(f"DB Error : Exception={e.args}")
            raise e
