from src.batch.ultmarc.utmp_tables.table_mapper.ultmarc_table_mapper import \
    UltmarcTableMapper
from src.batch.ultmarc.utmp_tables.tables.com_inst_att import ComInstAtt


class ComInstAttMapper(UltmarcTableMapper):
    """レイアウト区分111: COM_施設属性 登録処理"""

    # レコード存在確認SQL
    RECORD_EXISTS_QUERY = """\
    SELECT
        COUNT(*) AS count_num
    FROM
        src05.com_inst_att
    WHERE
        dcf_dsf_inst_cd = :dcfdsf_inst_code
    """
    # データ登録用SQL
    INSERT_QUERY = """\
    INSERT INTO src05.com_inst_att
    (
        dcf_dsf_inst_cd,
        dpc_flag,
        dpc_specday,
        dpc_cancelday,
        predpc_flag,
        predpc_specday,
        predpc_cancelday,
        cancer_flag,
        cancer_specday,
        cancer_cancelday,
        funchpflag,
        funchpokdate,
        funchpcandate,
        areasuphpflag,
        areasuphpintrate,
        areasuphpokdate,
        areasuphpcandate,
        acuthpaddflag,
        acuthpaddokdate,
        acuthpaddcandate,
        genadmisiionflag,
        genadmisiionfokdate,
        genadmisiionfcandate,
        assistanceflag,
        assistanceokdate,
        assistancecandate,
        diagnosistreatment_flag,
        diagnosistreatment_specday,
        diagnosistreatment_cancelday,
        safety_flag,
        safety_specday,
        safety_cancelday,
        highrisk_flag,
        highrisk_specday,
        highrisk_cancelday,
        infantandholiday_flag,
        infantandholiday_specday,
        infantandholiday_cancelday,
        ophpflag,
        ophpokdate,
        ophpcandate,
        critical_flag,
        critical_code01,
        critical_code02,
        critical_code03,
        critical_code04,
        critical_code05,
        critical_code06,
        critical_code07,
        critical_code08,
        critical_code09,
        critical_code10,
        critical_code11,
        critical_code12,
        critical_code13,
        critical_code14,
        critical_code15,
        critical_code16,
        critical_code17,
        critical_code18,
        critical_code19,
        critical_code20,
        drgmgthpflag,
        drgmgthpokdate,
        drgmgthpcandate,
        imagediagnosis_flag,
        imagediagnosis_specifiedday,
        imagediagnosis_cancelday,
        chemotherapy_flag,
        chemotherapy_specday,
        chemotherapy_cancelday,
        rehabilitation_flag,
        rehabilitation_code01,
        rehabilitation_code02,
        rehabilitation_code03,
        rehabilitation_code04,
        rehabilitation_code05,
        rehabilitation_code06,
        rehabilitation_code07,
        rehabilitation_code08,
        rehabilitation_code09,
        rehabilitation_code10,
        anesthetizingmanage_flag,
        anesthetizingmanage_specday,
        anesthetizingmanage_cancelday,
        homerecuperation_flag,
        homerecuperation_specday,
        homerecuperation_cancelday,
        synthesiswhenstaying_flag,
        synthesiswhenstaying_specday,
        synthesiswhenstaying_cancelday,
        homelateflag,
        homelateokday,
        homelatecanday,
        caremixkind,
        fullmoveflag,
        resthpflag,
        resthpcarekind,
        resthpbednum,
        resthpokdate,
        resthpcandate,
        resthpcarenrskind,
        resthpcarebednum,
        resthpcareokdate,
        resthpcarecandate,
        resthpsbednum,
        nrmhpflag,
        nrmhpcarekind,
        nrmhpbedtotalnum,
        menthpflag,
        menthpcarekind,
        menthpbednum,
        tubhpflag,
        tubhpcarekind,
        tubhpbednum,
        infhpflag,
        infhpflag1,
        infhpflag2,
        infhpbedflag,
        infhpbednum,
        hospiceflag,
        hospicebednum,
        hospiceokdate,
        hospicecandate,
        hpfuncestflag,
        hpfuncestkind,
        hpfuncestokdate,
        hpfuncestcandate,
        clolyhpkind,
        clolyhpokdate,
        clolyhpcandate,
        clhpkind,
        clhpokdate,
        clhpcandate,
        cldephpkind,
        cldephpokdate,
        cldephpcandate,
        disasthpflag,
        d1emerhpflag,
        d2emerhpflag,
        d3emerhpflag,
        emergencyclinic,
        trialcoreflag,
        trialcore_div,
        trialcoreokdate,
        trialcorecandate,
        dementiaflag,
        dementiaokdate,
        dementiacandate,
        sphealth_exploration,
        sphealth_guidance,
        hiadhpflag,
        hiadhpcode1,
        hiadhpkind1,
        hiadhpcode2,
        hiadhpkind2,
        hiadhpcode3,
        hiadhpkind3,
        hiadhpcode4,
        hiadhpkind4,
        hiadhpcode5,
        hiadhpkind5,
        hiadhpcode6,
        hiadhpkind6,
        hiadhpcode7,
        hiadhpkind7,
        hiadhpcode8,
        hiadhpkind8,
        hiadhpcode9,
        hiadhpkind9,
        hiadhpcode10,
        hiadhpkind10,
        hiadhpcode11,
        hiadhpkind11,
        hiadhpcode12,
        hiadhpkind12,
        hiadhpcode13,
        hiadhpkind13,
        hiadhpcode14,
        hiadhpkind14,
        hiadhpcode15,
        hiadhpkind15,
        hiadhpcode16,
        hiadhpkind16,
        hiadhpcode17,
        hiadhpkind17,
        hiadhpcode18,
        hiadhpkind18,
        hiadhpcode19,
        hiadhpkind19,
        hiadhpcode20,
        hiadhpkind20,
        hiadhpcode21,
        hiadhpkind21,
        hiadhpcode22,
        hiadhpkind22,
        hiadhpcode23,
        hiadhpkind23,
        hiadhpcode24,
        hiadhpkind24,
        hiadhpcode25,
        hiadhpkind25,
        hiadhpcode26,
        hiadhpkind26,
        hiadhpcode27,
        hiadhpkind27,
        hiadhpcode28,
        hiadhpkind28,
        hiadhpcode29,
        hiadhpkind29,
        hiadhpcode30,
        hiadhpkind30,
        hiadhpcode31,
        hiadhpkind31,
        hiadhpcode32,
        hiadhpkind32,
        hiadhpcode33,
        hiadhpkind33,
        hiadhpcode34,
        hiadhpkind34,
        hiadhpcode35,
        hiadhpkind35,
        hiadhpcode36,
        hiadhpkind36,
        hiadhpcode37,
        hiadhpkind37,
        hiadhpcode38,
        hiadhpkind38,
        hiadhpcode39,
        hiadhpkind39,
        hiadhpcode40,
        hiadhpkind40,
        hitechhpflag,
        hitechhpkind1,
        hitechhpkind2,
        hitechhpkind3,
        hitechhpkind4,
        hitechhpkind5,
        hitechhpkind6,
        hitechhpkind7,
        hitechhpkind8,
        hitechhpkind9,
        hitechhpkind10,
        hitechhpkind11,
        hitechhpkind12,
        hitechhpkind13,
        hitechhpkind14,
        hitechhpkind15,
        hitechhpkind16,
        hitechhpkind17,
        hitechhpkind18,
        hitechhpkind19,
        hitechhpkind20,
        policymedical_flag,
        policymedical_code01,
        policymedical_content01,
        policymedical_code02,
        policymedical_content02,
        policymedical_code03,
        policymedical_content03,
        policymedical_code04,
        policymedical_content04,
        policymedical_code05,
        policymedical_content05,
        policymedical_code06,
        policymedical_content06,
        policymedical_code07,
        policymedical_content07,
        policymedical_code08,
        policymedical_content08,
        policymedical_code09,
        policymedical_content09,
        policymedical_code10,
        policymedical_content10,
        policymedical_code11,
        policymedical_content11,
        policymedical_code12,
        policymedical_content12,
        policymedical_code13,
        policymedical_content13,
        policymedical_code14,
        policymedical_content14,
        policymedical_code15,
        policymedical_content15,
        policymedical_code16,
        policymedical_content16,
        policymedical_code17,
        policymedical_content17,
        policymedical_code18,
        policymedical_content18,
        policymedical_code19,
        policymedical_content19,
        policymedical_code20,
        policymedical_content20,
        visitcarestflag,
        visitcarestation_id,
        visitcarestation_code,
        visitcarestation_yobi,
        opendate,
        regist_ymd,
        update_ymd,
        sys_regist_date,
        regist_prgm_id,
        sys_update_date,
        update_prgm_id
    )
    VALUES (
        :dcfdsf_inst_code,
        :dpc_flag,
        :dpc_spec_day,
        :dpc_cancel_day,
        :predpc_flag,
        :predpc_spec_day,
        :predpc_cancel_day,
        :cancer_flag,
        :cancer_spec_day,
        :cancer_cancel_day,
        :funchp_flag,
        :funchp_ok_date,
        :funchp_can_date,
        :areasuphp_flag,
        :areasuphp_intrate,
        :areasuphp_ok_date,
        :areasuphp_can_date,
        :acuthp_add_flag,
        :acuthp_add_ok_date,
        :acuthp_add_can_date,
        :genadmisiion_flag,
        :genadmisiion_ok_date,
        :genadmisiion_can_date,
        :assistance_flag,
        :assistance_ok_date,
        :assistance_can_date,
        :diagnosis_treatment_flag,
        :diagnosis_treatment_spec_day,
        :diagnosis_treatment_cancel_day,
        :safety_flag,
        :safety_spec_day,
        :safety_cancel_day,
        :highrisk_flag,
        :highrisk_spec_day,
        :highrisk_cancel_day,
        :infant_and_holiday_flag,
        :infant_and_holiday_spec_day,
        :infant_and_holiday_cancel_day,
        :ophp_flag,
        :ophpok_date,
        :ophpcan_date,
        :critical_flag,
        :critical_code01,
        :critical_code02,
        :critical_code03,
        :critical_code04,
        :critical_code05,
        :critical_code06,
        :critical_code07,
        :critical_code08,
        :critical_code09,
        :critical_code10,
        :critical_code11,
        :critical_code12,
        :critical_code13,
        :critical_code14,
        :critical_code15,
        :critical_code16,
        :critical_code17,
        :critical_code18,
        :critical_code19,
        :critical_code20,
        :drgmgthp_flag,
        :drgmgthp_ok_date,
        :drgmgthp_can_date,
        :image_diagnosis_flag,
        :image_diagnosis_specified_day,
        :image_diagnosis_cancel_day,
        :chemotherapy_flag,
        :chemotherapy_spec_day,
        :chemotherapy_cancel_day,
        :rehabilitation_flag,
        :rehabilitation_code01,
        :rehabilitation_code02,
        :rehabilitation_code03,
        :rehabilitation_code04,
        :rehabilitation_code05,
        :rehabilitation_code06,
        :rehabilitation_code07,
        :rehabilitation_code08,
        :rehabilitation_code09,
        :rehabilitation_code10,
        :anesthetizing_manage_flag,
        :anesthetizing_manage_spec_day,
        :anesthetizing_manage_can_day,
        :home_recuperation_flag,
        :home_recuperation_spec_day,
        :home_recuperation_cancel_day,
        :synthesis_when_staying_flag,
        :synthesis_when_staying_spec_day,
        :synthesis_when_staying_can_day,
        :home_late_flag,
        :home_late_ok_day,
        :home_late_can_day,
        :caremix_kind,
        :fullmove_flag,
        :resthp_flag,
        :resthp_care_kind,
        :resthp_bed_num,
        :resthp_ok_date,
        :resthp_can_date,
        :resthp_care_nrs_kind,
        :resthp_care_bed_num,
        :resthp_care_ok_date,
        :resthp_care_can_date,
        :resthp_sbed_num,
        :nrmhp_flag,
        :nrmhp_care_kind,
        :nrmhp_bed_total_num,
        :menthp_flag,
        :menthp_care_kind,
        :menthp_bed_num,
        :tubhp_flag,
        :tubhp_care_kind,
        :tubhp_bed_num,
        :infhp_flag,
        :infhp_flag1,
        :infhp_flag2,
        :infhp_bed_flag,
        :infhp_bed_num,
        :hospice_flag,
        :hospice_bed_num,
        :hospice_ok_date,
        :hospice_can_date,
        :hpfunce_st_flag,
        :hpfunce_st_kind,
        :hpfunce_st_ok_date,
        :hpfunce_st_can_date,
        :clolyhp_kind,
        :clolyhp_ok_date,
        :clolyhp_can_date,
        :clhp_kind,
        :clhp_ok_date,
        :clhp_can_date,
        :cldephp_kind,
        :cldephp_ok_date,
        :cldephp_can_date,
        :disasthp_flag,
        :d1emerhp_flag,
        :d2emerhp_flag,
        :d3emerhp_flag,
        :emergency_clinic,
        :trial_core_flag,
        :trial_core_div,
        :trial_core_ok_date,
        :trial_core_can_date,
        :dementia_flag,
        :dementia_okd_ate,
        :dementia_can_date,
        :sphealth_exploration,
        :sphealth_guidance,
        :hiadhp_flag,
        :hiadhpcode1,
        :hiadhpkind1,
        :hiadhpcode2,
        :hiadhpkind2,
        :hiadhpcode3,
        :hiadhpkind3,
        :hiadhpcode4,
        :hiadhpkind4,
        :hiadhpcode5,
        :hiadhpkind5,
        :hiadhpcode6,
        :hiadhpkind6,
        :hiadhpcode7,
        :hiadhpkind7,
        :hiadhpcode8,
        :hiadhpkind8,
        :hiadhpcode9,
        :hiadhpkind9,
        :hiadhpcode10,
        :hiadhpkind10,
        :hiadhpcode11,
        :hiadhpkind11,
        :hiadhpcode12,
        :hiadhpkind12,
        :hiadhpcode13,
        :hiadhpkind13,
        :hiadhpcode14,
        :hiadhpkind14,
        :hiadhpcode15,
        :hiadhpkind15,
        :hiadhpcode16,
        :hiadhpkind16,
        :hiadhpcode17,
        :hiadhpkind17,
        :hiadhpcode18,
        :hiadhpkind18,
        :hiadhpcode19,
        :hiadhpkind19,
        :hiadhpcode20,
        :hiadhpkind20,
        :hiadhpcode21,
        :hiadhpkind21,
        :hiadhpcode22,
        :hiadhpkind22,
        :hiadhpcode23,
        :hiadhpkind23,
        :hiadhpcode24,
        :hiadhpkind24,
        :hiadhpcode25,
        :hiadhpkind25,
        :hiadhpcode26,
        :hiadhpkind26,
        :hiadhpcode27,
        :hiadhpkind27,
        :hiadhpcode28,
        :hiadhpkind28,
        :hiadhpcode29,
        :hiadhpkind29,
        :hiadhpcode30,
        :hiadhpkind30,
        :hiadhpcode31,
        :hiadhpkind31,
        :hiadhpcode32,
        :hiadhpkind32,
        :hiadhpcode33,
        :hiadhpkind33,
        :hiadhpcode34,
        :hiadhpkind34,
        :hiadhpcode35,
        :hiadhpkind35,
        :hiadhpcode36,
        :hiadhpkind36,
        :hiadhpcode37,
        :hiadhpkind37,
        :hiadhpcode38,
        :hiadhpkind38,
        :hiadhpcode39,
        :hiadhpkind39,
        :hiadhpcode40,
        :hiadhpkind40,
        :hitechhp_flag,
        :hitechhpkind1,
        :hitechhpkind2,
        :hitechhpkind3,
        :hitechhpkind4,
        :hitechhpkind5,
        :hitechhpkind6,
        :hitechhpkind7,
        :hitechhpkind8,
        :hitechhpkind9,
        :hitechhpkind10,
        :hitechhpkind11,
        :hitechhpkind12,
        :hitechhpkind13,
        :hitechhpkind14,
        :hitechhpkind15,
        :hitechhpkind16,
        :hitechhpkind17,
        :hitechhpkind18,
        :hitechhpkind19,
        :hitechhpkind20,
        :policy_medical_flag,
        :policymedical_code01,
        :policymedical_content01,
        :policymedical_code02,
        :policymedical_content02,
        :policymedical_code03,
        :policymedical_content03,
        :policymedical_code04,
        :policymedical_content04,
        :policymedical_code05,
        :policymedical_content05,
        :policymedical_code06,
        :policymedical_content06,
        :policymedical_code07,
        :policymedical_content07,
        :policymedical_code08,
        :policymedical_content08,
        :policymedical_code09,
        :policymedical_content09,
        :policymedical_code10,
        :policymedical_content10,
        :policymedical_code11,
        :policymedical_content11,
        :policymedical_code12,
        :policymedical_content12,
        :policymedical_code13,
        :policymedical_content13,
        :policymedical_code14,
        :policymedical_content14,
        :policymedical_code15,
        :policymedical_content15,
        :policymedical_code16,
        :policymedical_content16,
        :policymedical_code17,
        :policymedical_content17,
        :policymedical_code18,
        :policymedical_content18,
        :policymedical_code19,
        :policymedical_content19,
        :policymedical_code20,
        :policymedical_content20,
        :visitcarest_flag,
        :visitcarestation_id,
        :visitcarestation_code,
        :visitcarestation_yobi,
        :open_date,
        :execute_date_str_ymd,
        :execute_date_str_ymd,
        :execute_datetime,
        :program_name,
        :execute_datetime,
        :program_name
    )
    """

    UPDATE_QUERY = """\
    UPDATE
        src05.com_inst_att
    SET
        {update_columns}
        update_ymd = :execute_date_str_ymd,
        sys_update_date = :execute_datetime,
        update_prgm_id = :program_name
    WHERE
        dcf_dsf_inst_cd = :dcfdsf_inst_code
    """

    # 『修正区分がB(修正)かつ追加削除区分が1(退職)』の場合、物理削除
    PHYSICAL_DELETE_QUERY = """\
    DELETE FROM
        src05.com_inst_att
    WHERE
        dcf_dsf_inst_cd = :dcfdsf_inst_code
    """

    record: ComInstAtt

    def __init__(self, record: list[str], db) -> None:
        super().__init__(record, db, ComInstAtt)
        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):
        # 『修正区分がB(修正)かつ追加削除区分が1(退職)』の場合、物理削除
        if self.record.maint_flag == 'B' and self.record.adddel_div == '1':
            self.queries.append(self.PHYSICAL_DELETE_QUERY)
            return

        # 追加、更新の場合
        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:
            self.queries.append(self.INSERT_QUERY)
            return

        # 存在する場合はUpdate
        self.queries.append(self.__make_update_query())
        return

    def __make_update_query(self):
        set_clauses = []
        # DPC対象病院
        if len(self.record.dpc_flag) > 0:
            set_clauses.append('dpc_flag = :dpc_flag')
            set_clauses.append('dpc_specday = :dpc_spec_day')
            set_clauses.append('dpc_cancelday = :dpc_cancel_day')
            if self.record.dpc_flag == '@':
                self.query_parameter['dpc_flag'] = None
                self.query_parameter['dpc_spec_day'] = None
                self.query_parameter['dpc_cancel_day'] = None

        # DPC準備病院
        if len(self.record.predpc_flag) > 0:
            set_clauses.append('predpc_flag = :predpc_flag')
            set_clauses.append('predpc_specday = :predpc_spec_day')
            set_clauses.append('predpc_cancelday = :predpc_cancel_day')
            if self.record.predpc_flag == '@':
                self.query_parameter['predpc_flag'] = None
                self.query_parameter['predpc_spec_day'] = None
                self.query_parameter['predpc_cancel_day'] = None

        # がん診療連携拠点病院
        if len(self.record.cancer_flag) > 0:
            set_clauses.append('cancer_flag = :cancer_flag')
            set_clauses.append('cancer_specday = :cancer_spec_day')
            set_clauses.append('cancer_cancelday = :cancer_cancel_day')
            if self.record.cancer_flag == '@':
                self.query_parameter['cancer_flag'] = None
                self.query_parameter['cancer_spec_day'] = None
                self.query_parameter['cancer_cancel_day'] = None

        # 特定機能病院
        if len(self.record.funchp_flag) > 0:
            set_clauses.append('funchpflag = :funchp_flag')
            set_clauses.append('funchpokdate = :funchp_ok_date')
            set_clauses.append('funchpcandate = :funchp_can_date')
            if self.record.funchp_flag == '@':
                self.query_parameter['funchp_flag'] = None
                self.query_parameter['funchp_ok_date'] = None
                self.query_parameter['funchp_can_date'] = None

        # 地域医療支援病院
        if len(self.record.areasuphp_flag) > 0:
            set_clauses.append('areasuphpflag = :areasuphp_flag')
            set_clauses.append('areasuphpintrate = :areasuphp_intrate')
            set_clauses.append('areasuphpokdate = :areasuphp_ok_date')
            set_clauses.append('areasuphpcandate = :areasuphp_can_date')
            if self.record.areasuphp_flag == '@':
                self.query_parameter['areasuphp_flag'] = None
                self.query_parameter['areasuphp_intrate'] = None
                self.query_parameter['areasuphp_ok_date'] = None
                self.query_parameter['areasuphp_can_date'] = None

        # 超急性期脳卒中加算
        if len(self.record.acuthp_add_flag) > 0:
            set_clauses.append('acuthpaddflag = :acuthp_add_flag')
            set_clauses.append('acuthpaddokdate = :acuthp_add_ok_date')
            set_clauses.append('acuthpaddcandate = :acuthp_add_can_date')
            if self.record.acuthp_add_flag == '@':
                self.query_parameter['acuthp_add_flag'] = None
                self.query_parameter['acuthp_add_ok_date'] = None
                self.query_parameter['acuthp_add_can_date'] = None

        # 総合入院体制加算
        if len(self.record.genadmisiion_flag) > 0:
            set_clauses.append('genadmisiionflag = :genadmisiion_flag')
            set_clauses.append('genadmisiionfokdate = :genadmisiion_ok_date')
            set_clauses.append('genadmisiionfcandate = :genadmisiion_can_date')
            if self.record.genadmisiion_flag == '@':
                self.query_parameter['genadmisiion_flag'] = None
                self.query_parameter['genadmisiion_ok_date'] = None
                self.query_parameter['genadmisiion_can_date'] = None

        # 医師事務作業補助体制加算
        if len(self.record.assistance_flag) > 0:
            set_clauses.append('assistanceflag = :assistance_flag')
            set_clauses.append('assistanceokdate = :assistance_ok_date')
            set_clauses.append('assistancecandate = :assistance_can_date')
            if self.record.assistance_flag == '@':
                self.query_parameter['assistance_flag'] = None
                self.query_parameter['assistance_ok_date'] = None
                self.query_parameter['assistance_can_date'] = None

        # 診療録管理体制加算
        if len(self.record.diagnosis_treatment_flag) > 0:
            set_clauses.append('diagnosistreatment_flag = :diagnosis_treatment_flag')
            set_clauses.append('diagnosistreatment_specday = :diagnosis_treatment_spec_day')
            set_clauses.append('diagnosistreatment_cancelday = :diagnosis_treatment_cancel_day')
            if self.record.diagnosis_treatment_flag == '@':
                self.query_parameter['diagnosis_treatment_flag'] = None
                self.query_parameter['diagnosis_treatment_spec_day'] = None
                self.query_parameter['diagnosis_treatment_cancel_day'] = None

        # 医療安全対策加算
        if len(self.record.safety_flag) > 0:
            set_clauses.append('safety_flag = :safety_flag')
            set_clauses.append('safety_specday = :safety_spec_day')
            set_clauses.append('safety_cancelday = :safety_cancel_day')
            if self.record.safety_flag == '@':
                self.query_parameter['safety_flag'] = None
                self.query_parameter['safety_spec_day'] = None
                self.query_parameter['safety_cancel_day'] = None

        # 褥瘡ﾊｲﾘｽｸ患者ｹｱ加算
        if len(self.record.highrisk_flag) > 0:
            set_clauses.append('highrisk_flag = :highrisk_flag')
            set_clauses.append('highrisk_specday = :highrisk_spec_day')
            set_clauses.append('highrisk_cancelday = :highrisk_cancel_day')
            if self.record.highrisk_flag == '@':
                self.query_parameter['highrisk_flag'] = None
                self.query_parameter['highrisk_spec_day'] = None
                self.query_parameter['highrisk_cancel_day'] = None

        # 地域連携小児夜間･休日診療料
        if len(self.record.infant_and_holiday_flag) > 0:
            set_clauses.append('infantandholiday_flag = :infant_and_holiday_flag')
            set_clauses.append('infantandholiday_specday = :infant_and_holiday_spec_day')
            set_clauses.append('infantandholiday_cancelday = :infant_and_holiday_cancel_day')
            if self.record.infant_and_holiday_flag == '@':
                self.query_parameter['infant_and_holiday_flag'] = None
                self.query_parameter['infant_and_holiday_spec_day'] = None
                self.query_parameter['infant_and_holiday_cancel_day'] = None

        # 開放型病院
        if len(self.record.ophp_flag) > 0:
            set_clauses.append('ophpflag = :ophp_flag')
            set_clauses.append('ophpokdate = :ophpok_date')
            set_clauses.append('ophpcandate = :ophpcan_date')
            if self.record.ophp_flag == '@':
                self.query_parameter['ophp_flag'] = None
                self.query_parameter['ophpok_date'] = None
                self.query_parameter['ophpcan_date'] = None

        # 地域連携ｸﾘﾃｨｶﾙﾊﾟｽ
        if len(self.record.critical_flag) > 0:
            set_clauses.append('critical_flag = :critical_flag')
            # 項目クリア判断
            is_aggregate_item_clear = self.record.critical_flag == '@'
            if is_aggregate_item_clear:
                self.query_parameter['critical_flag'] = None
            # 20個分の項目設定(rangeの第2引数の数字はループに含まれないため、+1を指定する)
            for i in range(1, 21):
                num = str(i).zfill(2)           # 2桁で0埋め
                setting_item_name = f'critical_code{num}'
                set_clauses.append(f'{setting_item_name} = :{setting_item_name}')
                if is_aggregate_item_clear:
                    self.query_parameter[setting_item_name] = None

        # 薬剤管理指導料
        if len(self.record.drgmgthp_flag) > 0:
            set_clauses.append('drgmgthpflag = :drgmgthp_flag')
            set_clauses.append('drgmgthpokdate = :drgmgthp_ok_date')
            set_clauses.append('drgmgthpcandate = :drgmgthp_can_date')
            if self.record.drgmgthp_flag == '@':
                self.query_parameter['drgmgthp_flag'] = None
                self.query_parameter['drgmgthp_ok_date'] = None
                self.query_parameter['drgmgthp_can_date'] = None

        # 画像診断管理加算
        if len(self.record.image_diagnosis_flag) > 0:
            set_clauses.append('imagediagnosis_flag = :image_diagnosis_flag')
            set_clauses.append('imagediagnosis_specifiedday = :image_diagnosis_specified_day')
            set_clauses.append('imagediagnosis_cancelday = :image_diagnosis_cancel_day')
            if self.record.image_diagnosis_flag == '@':
                self.query_parameter['image_diagnosis_flag'] = None
                self.query_parameter['image_diagnosis_specified_day'] = None
                self.query_parameter['image_diagnosis_cancel_day'] = None

        # 外来化学療法加算
        if len(self.record.chemotherapy_flag) > 0:
            set_clauses.append('chemotherapy_flag = :chemotherapy_flag')
            set_clauses.append('chemotherapy_specday = :chemotherapy_spec_day')
            set_clauses.append('chemotherapy_cancelday = :chemotherapy_cancel_day')
            if self.record.chemotherapy_flag == '@':
                self.query_parameter['chemotherapy_flag'] = None
                self.query_parameter['chemotherapy_spec_day'] = None
                self.query_parameter['chemotherapy_cancel_day'] = None

        # 疾患別ﾘﾊﾋﾞﾘﾃｰｼｮﾝ料
        if len(self.record.rehabilitation_flag) > 0:
            set_clauses.append('rehabilitation_flag = :rehabilitation_flag')
            # 項目クリア判断
            is_aggregate_item_clear = self.record.rehabilitation_flag == '@'
            if is_aggregate_item_clear:
                self.query_parameter['rehabilitation_flag'] = None
            # 10個分の項目設定(rangeの第2引数の数字はループに含まれないため、+1を指定する)
            for i in range(1, 11):
                num = str(i).zfill(2)           # 2桁で0埋め
                setting_item_name = f'rehabilitation_code{num}'
                set_clauses.append(f'{setting_item_name} = :{setting_item_name}')
                if is_aggregate_item_clear:
                    self.query_parameter[setting_item_name] = None

        # 麻酔管理料
        if len(self.record.anesthetizing_manage_flag) > 0:
            set_clauses.append('anesthetizingmanage_flag = :anesthetizing_manage_flag')
            set_clauses.append('anesthetizingmanage_specday = :anesthetizing_manage_spec_day')
            set_clauses.append('anesthetizingmanage_cancelday = :anesthetizing_manage_can_day')
            if self.record.anesthetizing_manage_flag == '@':
                self.query_parameter['anesthetizing_manage_flag'] = None
                self.query_parameter['anesthetizing_manage_spec_day'] = None
                self.query_parameter['anesthetizing_manage_can_day'] = None

        # 在宅療養支援病院･診療所
        if len(self.record.home_recuperation_flag) > 0:
            set_clauses.append('homerecuperation_flag = :home_recuperation_flag')
            set_clauses.append('homerecuperation_specday = :home_recuperation_spec_day')
            set_clauses.append('homerecuperation_cancelday = :home_recuperation_cancel_day')
            if self.record.home_recuperation_flag == '@':
                self.query_parameter['home_recuperation_flag'] = None
                self.query_parameter['home_recuperation_spec_day'] = None
                self.query_parameter['home_recuperation_cancel_day'] = None

        # 在宅時医学総合管理料
        if len(self.record.synthesis_when_staying_flag) > 0:
            set_clauses.append('synthesiswhenstaying_flag = :synthesis_when_staying_flag')
            set_clauses.append('synthesiswhenstaying_specday = :synthesis_when_staying_spec_day')
            set_clauses.append('synthesiswhenstaying_cancelday = :synthesis_when_staying_can_day')
            if self.record.synthesis_when_staying_flag == '@':
                self.query_parameter['synthesis_when_staying_flag'] = None
                self.query_parameter['synthesis_when_staying_spec_day'] = None
                self.query_parameter['synthesis_when_staying_can_day'] = None

        # 在宅末期医療総合診療料
        if len(self.record.home_late_flag) > 0:
            set_clauses.append('homelateflag = :home_late_flag')
            set_clauses.append('homelateokday = :home_late_ok_day')
            set_clauses.append('homelatecanday = :home_late_can_day')
            if self.record.home_late_flag == '@':
                self.query_parameter['home_late_flag'] = None
                self.query_parameter['home_late_ok_day'] = None
                self.query_parameter['home_late_can_day'] = None

        # ｹｱﾐｯｸｽ区分
        if len(self.record.caremix_kind) > 0:
            set_clauses.append('caremixkind = :caremix_kind')
            if self.record.caremix_kind == '@':
                self.query_parameter['caremix_kind'] = None

        # 完全型･移行型区分
        if len(self.record.fullmove_flag) > 0:
            set_clauses.append('fullmoveflag = :fullmove_flag')
            if self.record.fullmove_flag == '@':
                self.query_parameter['fullmove_flag'] = None

        # 療養型病床群
        if sum(len(item) for item in self.record.resthp_items) > 0:
            set_clauses.append('resthpflag = :resthp_flag')
            set_clauses.append('resthpcarekind = :resthp_care_kind')
            set_clauses.append('resthpbednum = :resthp_bed_num')
            set_clauses.append('resthpokdate = :resthp_ok_date')
            set_clauses.append('resthpcandate = :resthp_can_date')
            set_clauses.append('resthpcarenrskind = :resthp_care_nrs_kind')
            set_clauses.append('resthpcarebednum = :resthp_care_bed_num')
            set_clauses.append('resthpcareokdate = :resthp_care_ok_date')
            set_clauses.append('resthpcarecandate = :resthp_care_can_date')
            set_clauses.append('resthpsbednum = :resthp_sbed_num')
            if self.record.resthp_flag == '@':
                self.query_parameter['resthp_flag'] = None
                self.query_parameter['resthp_care_kind'] = None
                self.query_parameter['resthp_bed_num'] = None
                self.query_parameter['resthp_ok_date'] = None
                self.query_parameter['resthp_can_date'] = None
                self.query_parameter['resthp_care_nrs_kind'] = None
                self.query_parameter['resthp_care_bed_num'] = None
                self.query_parameter['resthp_care_ok_date'] = None
                self.query_parameter['resthp_care_can_date'] = None
                self.query_parameter['resthp_sbed_num'] = None

        # 一般病棟
        if len(self.record.nrmhp_flag) > 0:
            set_clauses.append('nrmhpflag = :nrmhp_flag')
            set_clauses.append('nrmhpcarekind = :nrmhp_care_kind')
            set_clauses.append('nrmhpbedtotalnum = :nrmhp_bed_total_num')
            if self.record.nrmhp_flag == '@':
                self.query_parameter['nrmhp_flag'] = None
                self.query_parameter['nrmhp_care_kind'] = None
                self.query_parameter['nrmhp_bed_total_num'] = None

        # 精神病棟
        if len(self.record.menthp_flag) > 0:
            set_clauses.append('menthpflag = :menthp_flag')
            set_clauses.append('menthpcarekind = :menthp_care_kind')
            set_clauses.append('menthpbednum = :menthp_bed_num')
            if self.record.menthp_flag == '@':
                self.query_parameter['menthp_flag'] = None
                self.query_parameter['menthp_care_kind'] = None
                self.query_parameter['menthp_bed_num'] = None

        # 結核病棟
        if len(self.record.tubhp_flag) > 0:
            set_clauses.append('tubhpflag = :tubhp_flag')
            set_clauses.append('tubhpcarekind = :tubhp_care_kind')
            set_clauses.append('tubhpbednum = :tubhp_bed_num')
            if self.record.tubhp_flag == '@':
                self.query_parameter['tubhp_flag'] = None
                self.query_parameter['tubhp_care_kind'] = None
                self.query_parameter['tubhp_bed_num'] = None

        # 感染症指定医療機関
        if len(self.record.infhp_flag) > 0:
            set_clauses.append('infhpflag = :infhp_flag')
            set_clauses.append('infhpflag1 = :infhp_flag1')
            set_clauses.append('infhpflag2 = :infhp_flag2')
            if self.record.infhp_flag == '@':
                self.query_parameter['infhp_flag'] = None
                self.query_parameter['infhp_flag1'] = None
                self.query_parameter['infhp_flag2'] = None

        # 感染症病床
        if len(self.record.infhp_bed_flag) > 0:
            set_clauses.append('infhpbedflag = :infhp_bed_flag')
            set_clauses.append('infhpbednum = :infhp_bed_num')
            if self.record.infhp_bed_flag == '@':
                self.query_parameter['infhp_bed_flag'] = None
                self.query_parameter['infhp_bed_num'] = None

        # 緩和ｹｱ病棟設置病院
        if len(self.record.hospice_flag) > 0:
            set_clauses.append('hospiceflag = :hospice_flag')
            set_clauses.append('hospicebednum = :hospice_bed_num')
            set_clauses.append('hospiceokdate = :hospice_ok_date')
            set_clauses.append('hospicecandate = :hospice_can_date')
            if self.record.hospice_flag == '@':
                self.query_parameter['hospice_flag'] = None
                self.query_parameter['hospice_bed_num'] = None
                self.query_parameter['hospice_ok_date'] = None
                self.query_parameter['hospice_can_date'] = None

        # 医療機能評価
        if len(self.record.hpfunce_st_flag) > 0:
            set_clauses.append('hpfuncestflag = :hpfunce_st_flag')
            set_clauses.append('hpfuncestkind = :hpfunce_st_kind')
            set_clauses.append('hpfuncestokdate = :hpfunce_st_ok_date')
            set_clauses.append('hpfuncestcandate = :hpfunce_st_can_date')
            if self.record.hpfunce_st_flag == '@':
                self.query_parameter['hpfunce_st_flag'] = None
                self.query_parameter['hpfunce_st_kind'] = None
                self.query_parameter['hpfunce_st_ok_date'] = None
                self.query_parameter['hpfunce_st_can_date'] = None

        # 臨床研修指定病院
        if sum(len(item) for item in self.record.cl_items) > 0:
            set_clauses.append('clolyhpkind = :clolyhp_kind')
            set_clauses.append('clolyhpokdate = :clolyhp_ok_date')
            set_clauses.append('clolyhpcandate = :clolyhp_can_date')
            set_clauses.append('clhpkind = :clhp_kind')
            set_clauses.append('clhpokdate = :clhp_ok_date')
            set_clauses.append('clhpcandate = :clhp_can_date')
            set_clauses.append('cldephpkind = :cldephp_kind')
            set_clauses.append('cldephpokdate = :cldephp_ok_date')
            set_clauses.append('cldephpcandate = :cldephp_can_date')
            if self.record.clolyhp_kind == '@':
                self.query_parameter['clolyhp_kind'] = None
                self.query_parameter['clolyhp_ok_date'] = None
                self.query_parameter['clolyhp_can_date'] = None
                self.query_parameter['clhp_kind'] = None
                self.query_parameter['clhp_ok_date'] = None
                self.query_parameter['clhp_can_date'] = None
                self.query_parameter['cldephp_kind'] = None
                self.query_parameter['cldephp_ok_date'] = None
                self.query_parameter['cldephp_can_date'] = None

        # 災害拠点病院
        if len(self.record.disasthp_flag) > 0:
            set_clauses.append('disasthpflag = :disasthp_flag')
            if self.record.disasthp_flag == '@':
                self.query_parameter['disasthp_flag'] = None

        # 救急医療
        if len(self.record.d1emerhp_flag) > 0:
            set_clauses.append('d1emerhpflag = :d1emerhp_flag')
            set_clauses.append('d2emerhpflag = :d2emerhp_flag')
            set_clauses.append('d3emerhpflag = :d3emerhp_flag')
            if self.record.d1emerhp_flag == '@':
                self.query_parameter['d1emerhp_flag'] = None
                self.query_parameter['d2emerhp_flag'] = None
                self.query_parameter['d3emerhp_flag'] = None

        # 救急告示診療所
        if len(self.record.emergency_clinic) > 0:
            set_clauses.append('emergencyclinic = :emergency_clinic')
            if self.record.emergency_clinic == '@':
                self.query_parameter['emergency_clinic'] = None

        # 治験中核病院
        if len(self.record.trial_core_flag) > 0:
            set_clauses.append('trialcoreflag = :trial_core_flag')
            set_clauses.append('trialcore_div = :trial_core_div')
            set_clauses.append('trialcoreokdate = :trial_core_ok_date')
            set_clauses.append('trialcorecandate = :trial_core_can_date')
            if self.record.trial_core_flag == '@':
                self.query_parameter['trial_core_flag'] = None
                self.query_parameter['trial_core_div'] = None
                self.query_parameter['trial_core_ok_date'] = None
                self.query_parameter['trial_core_can_date'] = None

        # 認知症疾患医療ｾﾝﾀｰ
        if len(self.record.dementia_flag) > 0:
            set_clauses.append('dementiaflag = :dementia_flag')
            set_clauses.append('dementiaokdate = :dementia_okd_ate')
            set_clauses.append('dementiacandate = :dementia_can_date')
            if self.record.dementia_flag == '@':
                self.query_parameter['dementia_flag'] = None
                self.query_parameter['dementia_okd_ate'] = None
                self.query_parameter['dementia_can_date'] = None

        # 特定健康診査実施機関
        if len(self.record.sphealth_exploration) > 0:
            set_clauses.append('sphealth_exploration = :sphealth_exploration')
            if self.record.sphealth_exploration == '@':
                self.query_parameter['sphealth_exploration'] = None

        # 特定保健指導実施機関
        if len(self.record.sphealth_guidance) > 0:
            set_clauses.append('sphealth_guidance = :sphealth_guidance')
            if self.record.sphealth_guidance == '@':
                self.query_parameter['sphealth_guidance'] = None

        # 先進医療実施医療機関
        if len(self.record.hiadhp_flag) > 0:
            set_clauses.append('hiadhpflag = :hiadhp_flag')
            # 項目クリア判断
            is_aggregate_item_clear = self.record.hiadhp_flag == '@'
            if is_aggregate_item_clear:
                self.query_parameter['hiadhp_flag'] = None
            # 40個分の項目設定(rangeの第2引数の数字はループに含まれないため、+1を指定する)
            for i in range(1, 41):
                setting_item_name_code = f'hiadhpcode{i}'
                setting_item_name_kind = f'hiadhpkind{i}'
                set_clauses.append(f'{setting_item_name_code} = :{setting_item_name_code}')
                set_clauses.append(f'{setting_item_name_kind} = :{setting_item_name_kind}')
                if is_aggregate_item_clear:
                    self.query_parameter[setting_item_name_code] = None
                    self.query_parameter[setting_item_name_kind] = None

        # 先端医療実施医療機関
        if len(self.record.hitechhp_flag) > 0:
            set_clauses.append('hitechhpflag = :hitechhp_flag')
            # 項目クリア判断
            is_aggregate_item_clear = self.record.hitechhp_flag == '@'
            if is_aggregate_item_clear:
                self.query_parameter['hitechhp_flag'] = None
            # 20個分の項目設定(rangeの第2引数の数字はループに含まれないため、+1を指定する)
            for i in range(1, 21):
                setting_item_name = f'hitechhpkind{i}'
                set_clauses.append(f'{setting_item_name} = :{setting_item_name}')
                if is_aggregate_item_clear:
                    self.query_parameter[setting_item_name] = None

        # 政策医療
        if len(self.record.policy_medical_flag) > 0:
            set_clauses.append('policymedical_flag = :policy_medical_flag')
            # 項目クリア判断
            is_aggregate_item_clear = self.record.policy_medical_flag == '@'
            if is_aggregate_item_clear:
                self.query_parameter['policy_medical_flag'] = None
            # 20個分の項目設定(rangeの第2引数の数字はループに含まれないため、+1を指定する)
            for i in range(1, 21):
                num = str(i).zfill(2)           # 2桁で0埋め
                setting_item_name_code = f'policymedical_code{num}'
                setting_item_name_content = f'policymedical_content{num}'
                set_clauses.append(f'{setting_item_name_code} = :{setting_item_name_code}')
                set_clauses.append(f'{setting_item_name_content} = :{setting_item_name_content}')
                if is_aggregate_item_clear:
                    self.query_parameter[setting_item_name_code] = None
                    self.query_parameter[setting_item_name_content] = None

        # 訪問看護ステーション
        if len(self.record.visitcarest_flag) > 0:
            set_clauses.append('visitcarestflag = :visitcarest_flag')
            set_clauses.append('visitcarestation_id = :visitcarestation_id')
            set_clauses.append('visitcarestation_code = :visitcarestation_code')
            set_clauses.append('visitcarestation_yobi = :visitcarestation_yobi')
            if self.record.visitcarest_flag == '@':
                self.query_parameter['visitcarest_flag'] = None
                self.query_parameter['visitcarestation_id'] = None
                self.query_parameter['visitcarestation_code'] = None
                self.query_parameter['visitcarestation_yobi'] = None

        # 開設年月
        if len(self.record.open_date) > 0:
            set_clauses.append('opendate = :open_date')
            if self.record.open_date == '@':
                self.query_parameter['open_date'] = None

        update_columns = ','.join(set_clauses)
        # 何も更新項目が無い場合はNoneとする（更新処理は行わない）
        if len(update_columns) == 0:
            return None
        else:
            # 末尾にカンマを付けてSET句を完成させる
            update_columns += ','
            update_query = self.UPDATE_QUERY.format(
                update_columns=update_columns
            )
            return update_query
