from src.batch.ultmarc.utmp_tables.table_mapper.ultmarc_table_mapper import \
    UltmarcTableMapper
from src.batch.ultmarc.utmp_tables.tables.com_med_prefc import ComMedPrefc


class ComMedPrefcMapper(UltmarcTableMapper):
    """レイアウト区分121: COM_医療圏都道府県 登録処理"""

    # レコード存在確認SQL
    RECORD_EXISTS_QUERY = """\
    SELECT
        COUNT(*) AS count_num
    FROM
        src05.com_med_prefc
    WHERE
        pref_code = :pref_code
    """
    # データ登録用SQL
    INSERT_QUERY = """\
    INSERT INTO src05.com_med_prefc
    (
        pref_code,
        rev_date,
        post_date,
        psy_bednumtg,
        psy_bednumgen,
        psy_bednumdate,
        psy_eqbednum,
        tb_bednumtg,
        tb_bednumgen,
        tb_bednumdate,
        tb_eqbednum,
        inf_bednumtg,
        inf_bednumgen,
        inf_bednumdate,
        inf_eqbednum,
        regist_ymd,
        sys_regist_date,
        regist_prgm_id,
        sys_update_date,
        update_prgm_id
    )
    VALUES (
        :pref_code,
        :rev_date,
        :post_date,
        :psy_bednum_tg,
        :psy_bednum_gen,
        :psy_bednum_date,
        :psy_eqbed_num,
        :tb_bednum_tg,
        :tb_bednum_gen,
        :tb_bednum_date,
        :tb_eqbed_num,
        :inf_bednum_tg,
        :inf_bednum_gen,
        :inf_bednum_date,
        :inf_eqbed_num,
        :execute_date_str_ymd,
        :execute_datetime,
        :program_name,
        :execute_datetime,
        :program_name
    )
    """

    # データ更新用SQL
    UPDATE_QUERY = """\
    UPDATE
        src05.com_med_prefc
    SET
        rev_date = :rev_date,
        post_date = :post_date,
        psy_bednumtg = :psy_bednum_tg,
        psy_bednumgen = :psy_bednum_gen,
        psy_bednumdate = :psy_bednum_date,
        psy_eqbednum = :psy_eqbed_num,
        tb_bednumtg = :tb_bednum_tg,
        tb_bednumgen = :tb_bednum_gen,
        tb_bednumdate = :tb_bednum_date,
        tb_eqbednum = :tb_eqbed_num,
        inf_bednumtg = :inf_bednum_tg,
        inf_bednumgen = :inf_bednum_gen,
        inf_bednumdate = :inf_bednum_date,
        inf_eqbednum = :inf_eqbed_num,
        update_ymd = :execute_date_str_ymd,
        sys_update_date = :execute_datetime,
        update_prgm_id = :program_name
    WHERE
        pref_code = :pref_code
    """

    # 修正区分が「C(削除)」の場合、物理削除
    PHYSICAL_DELETE_QUERY = """\
    DELETE FROM
        src05.com_med_prefc
    WHERE
        pref_code = :pref_code
    """

    record: ComMedPrefc

    def __init__(self, record: list[str], db) -> None:
        super().__init__(record, db, ComMedPrefc)
        program_name = __name__.split('.')[-1]  # 当モジュール名(現行から変わっている)
        # モジュール名をクエリパラメータに設定
        self.query_parameter['program_name'] = program_name
        # 読み込んだレコード値もクエリパラメータに追加
        self.query_parameter = {**self.query_parameter, **self.record.to_sql_parameter()}

    def make_query(self):
        # 修正区分がC(削除)の場合、物理削除
        if self.record.maintflag == 'C':
            self.queries.append(self.PHYSICAL_DELETE_QUERY)
            return

        # 追加、更新の場合
        self.queries.append(self.__make_upsert_query())
        return

    def __make_upsert_query(self):
        # レコードの存在確認
        record_count = self.db.execute_select(self.RECORD_EXISTS_QUERY, self.query_parameter)
        # 存在しない場合はInsert
        if record_count[0]['count_num'] == 0:
            return self.INSERT_QUERY

        # 存在する場合はUpdate
        return self.UPDATE_QUERY
