<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.its.vms.dao.mapper.VmsCtlrMapper">

    <select id="selectAll" resultType="com.its.vms.entity.TbVmsCtlr" fetchSize="100">
    <![CDATA[
        select t1.vms_ctlr_nmbr       AS vmsCtlrNmbr,
               t1.vms_ctlr_local_no   AS vmsCtlrLocalNo,
               t1.vms_ctlr_id         AS vmsCtlrId,
               t1.vms_nm              AS vmsNm,
               t1.vms_ctlr_ip         AS vmsCtlrIp,
               t1.vms_usag_type_cd    AS vmsUsagTypeCd,
               t1.vms_frm_type_cd     AS vmsFrmTypeCd,
               t1.vms_type_cd         AS vmsTypeCd,
               t1.vms_modl_type_cd    AS vmsModlTypeCd,
               t1.vms_ctlr_port       AS vmsCtlrPort,
               t1.oper_mode           AS operMode,
               t1.cmnc_fail_rate      AS cmncFailRate,
               t1.brgh_nght_step      AS brghNghtStep,
               t1.brgh_week_step      AS brghWeekStep,
               t1.def_phse_chng_cycl  AS defPhseChngCycl,
               t1.modl_err_rate       AS modlErrRate,
               t1.cmncfail_slot_nmbr  AS cmncfailSlotNmbr,
               t1.pwer_fail_slot_nmbr AS pwerFailSlotNmbr,
               t1.cmtrinfr_cnct_yn    AS cmtrinfrCnctYn,
               t1.wthrinfr_cnct_yn    AS wthrinfrCnctYn,
               t1.envrinfr_cnct_yn    AS envrinfrCnctYn,
               t1.vms_max_phse_num    AS vmsMaxPhseNum,
               t1.panl_on_time        AS panlOnTime,
               t1.panl_off_time       AS panlOffTime,
               t1.panl_pwer_mode      AS panlPwerMode,
               t1.brgh_mode           AS brghMode,
               t1.istl_lctn_nm        AS istlLctnNm,
               t1.trfc_strg_use_yn    AS trfcStrgUseYn,
               t1.vms_loc_ifsc_id     AS vmsLocIfscId,
               t1.fan_run_tmpr        AS fanRunTmpr,
               t1.hetr_run_tmpr       AS hetrRunTmpr,
               t1.fan_mode            AS fanMode,
               t1.hetr_mode           AS hetrMode,
               t1.brgh_curr_step      AS brghCurrStep,
               t1.istl_lctn_addr      AS istlLctnAddr,
               t1.protocol_ver        AS protocolVer,
               t1.del_yn              AS delYn,
               t2.vms_wdth            AS vmsWidth,
               t2.vms_hght            AS vmsHeight,
               t2.modl_row_num        AS powrRowNum,
               t2.modl_col_num        AS powrColNum,
               t2.powr_row_num        AS powrRowNum,
               t2.powr_col_num        AS powrColNum
        from tb_vms_ctlr t1
        inner join tb_vms_type t2
            on t1.del_yn = 'N'
            and t1.vms_type_cd = t2.vms_type_cd
        order by t1.vms_ctlr_nmbr
    ]]>
    </select>

    <select id="selectCtlrStts" resultType="com.its.vms.entity.TbVmsCtlrStts" fetchSize="100">
    <![CDATA[
        select t1.vms_ctlr_nmbr     AS vmsCtlrNmbr,
               t1.updt_dt           AS updtDt,
               t1.cmnc_stts_cd      AS cmncSttsCd,
               t1.pwer_stts_cd      AS pwerSttsCd,
               t1.modl_stts_cd      AS modlSttsCd,
               t1.cbox_door_stts_cd AS cboxDoorSttsCd,
               t1.fan_stts_cd       AS fanSttsCd,
               t1.hetr_stts_cd      AS hetrSttsCd,
               t1.cbox_tmpr         AS cboxTmpr,
               t1.brgh_val          AS brghVal,
               t1.comm_stts_cd      AS commSttsCd,
               t1.modl_stts         AS modlStts,
               t1.pwer_stts         AS pwerStts
        from tb_vms_ctlr_stts t1
        inner join tb_vms_ctlr t2
        on t1.vms_ctlr_nmbr = t2.vms_ctlr_nmbr
        ]]>
    </select>

    <update id="updateCtlrStts" parameterType="com.its.vms.entity.TbVmsCtlrStts">
    <![CDATA[
        merge into tb_vms_ctlr_stts L
            using (select
                       #{vmsCtlrNmbr}     AS vms_ctlr_nmbr,
                       #{updtDt}          AS updt_dt,
                       #{cmncSttsCd}      AS cmnc_stts_cd,
                       #{pwerSttsCd}      AS pwer_stts_cd,
                       #{modlSttsCd}      AS modl_stts_cd,
                       #{cboxDoorSttsCd}  AS cbox_door_stts_cd,
                       #{fanSttsCd}       AS fan_stts_cd,
                       #{hetrSttsCd}      AS hetr_stts_cd,
                       #{cboxTmpr}        AS cbox_tmpr,
                       #{brghVal}         AS brgh_val,
                       #{commSttsCd}      AS comm_stts_cd,
                       #{modlStts}        AS modl_stts,
                       #{pwerStts}        AS pwer_stts
                   from dual) M
            on (    L.vms_ctlr_nmbr = M.vms_ctlr_nmbr)
            when matched then
                update set
                    L.updt_dt           = M.updt_dt,
                    L.cmnc_stts_cd      = M.cmnc_stts_cd,
                    L.pwer_stts_cd      = M.pwer_stts_cd,
                    L.modl_stts_cd      = M.modl_stts_cd,
                    L.cbox_door_stts_cd = M.cbox_door_stts_cd,
                    L.fan_stts_cd       = M.fan_stts_cd,
                    L.hetr_stts_cd      = M.hetr_stts_cd,
                    L.cbox_tmpr         = M.cbox_tmpr,
                    L.brgh_val          = M.brgh_val,
                    L.comm_stts_cd      = M.comm_stts_cd,
                    L.modl_stts         = M.modl_stts,
                    L.pwer_stts         = M.pwer_stts
            when not matched then
                insert (
                        vms_ctlr_nmbr,
                        updt_dt,
                        cmnc_stts_cd,
                        pwer_stts_cd,
                        modl_stts_cd,
                        cbox_door_stts_cd,
                        fan_stts_cd,
                        hetr_stts_cd,
                        cbox_tmpr,
                        brgh_val,
                        comm_stts_cd,
                        modl_stts,
                        pwer_stts )
                    values (
                               M.vms_ctlr_nmbr,
                               M.updt_dt,
                               M.cmnc_stts_cd,
                               M.pwer_stts_cd,
                               M.modl_stts_cd,
                               M.cbox_door_stts_cd,
                               M.fan_stts_cd,
                               M.hetr_stts_cd,
                               M.cbox_tmpr,
                               M.brgh_val,
                               M.comm_stts_cd,
                               M.modl_stts,
                               M.pwer_stts )
        ]]>
    </update>

    <update id="batchUpdateCtlrStts" parameterType="java.util.Map">
    <![CDATA[
        merge into tb_vms_ctlr_stts L
            using (select
                       #{vmsCtlrNmbr}     AS vms_ctlr_nmbr,
                       #{updtDt}           AS updt_dt,
                       #{cmncSttsCd}      AS cmnc_stts_cd,
                       #{pwerSttsCd}      AS pwer_stts_cd,
                       #{modlSttsCd}      AS modl_stts_cd,
                       #{cboxDoorSttsCd} AS cbox_door_stts_cd,
                       #{fanSttsCd}       AS fan_stts_cd,
                       #{hetrSttsCd}      AS hetr_stts_cd,
                       #{cboxTmpr}         AS cbox_tmpr,
                       #{brghVal}          AS brgh_val,
                       #{commSttsCd}      AS comm_stts_cd,
                       #{modlStts}         AS modl_stts,
                       #{pwerStts}         AS pwer_stts
                   from dual) M
            on (    L.vms_ctlr_nmbr = M.vms_ctlr_nmbr)
            when matched then
                update set
                    L.updt_dt           = M.updt_dt,
                    L.cmnc_stts_cd      = M.cmnc_stts_cd,
                    L.pwer_stts_cd      = M.pwer_stts_cd,
                    L.modl_stts_cd      = M.modl_stts_cd,
                    L.cbox_door_stts_cd = M.cbox_door_stts_cd,
                    L.fan_stts_cd       = M.fan_stts_cd,
                    L.hetr_stts_cd      = M.hetr_stts_cd,
                    L.cbox_tmpr         = M.cbox_tmpr,
                    L.brgh_val          = M.brgh_val,
                    L.comm_stts_cd      = M.comm_stts_cd,
                    L.modl_stts         = M.modl_stts,
                    L.pwer_stts         = M.pwer_stts
            when not matched then
                insert (
                        vms_ctlr_nmbr,
                        updt_dt,
                        cmnc_stts_cd,
                        pwer_stts_cd,
                        modl_stts_cd,
                        cbox_door_stts_cd,
                        fan_stts_cd,
                        hetr_stts_cd,
                        cbox_tmpr,
                        brgh_val,
                        comm_stts_cd,
                        modl_stts,
                        pwer_stts )
                    values (
                               M.vms_ctlr_nmbr,
                               M.updt_dt,
                               M.cmnc_stts_cd,
                               M.pwer_stts_cd,
                               M.modl_stts_cd,
                               M.cbox_door_stts_cd,
                               M.fan_stts_cd,
                               M.hetr_stts_cd,
                               M.cbox_tmpr,
                               M.brgh_val,
                               M.comm_stts_cd,
                               M.modl_stts,
                               M.pwer_stts )
        ]]>
    </update>

    <insert id="insertCtlrSttsHs" parameterType="com.its.vms.entity.TbVmsCtlrStts">
    <![CDATA[
        insert into tb_vms_ctlr_stts_hs (
            crtn_dt,
            vms_ctlr_nmbr,
            cmnc_stts_cd,
            pwer_stts_cd,
            modl_stts_cd,
            cbox_door_stts_cd,
            fan_stts_cd,
            hetr_stts_cd,
            cbox_tmpr,
            brgh_val,
            comm_stts_cd,
            modl_stts,
            pwer_stts )
        values (
                   #{updtDt},
                   #{vmsCtlrNmbr},
                   #{cmncSttsCd},
                   #{pwerSttsCd},
                   #{modlSttsCd},
                   #{cboxDoorSttsCd},
                   #{fanSttsCd},
                   #{hetrSttsCd},
                   #{cboxTmpr},
                   #{brghVal},
                   #{commSttsCd},
                   #{modlStts},
                   #{pwerStts}
               )
        ]]>
    </insert>

    <insert id="batchInsertCtlrSttsHs" parameterType="java.util.Map">
    <![CDATA[
        insert into tb_vms_ctlr_stts_hs (
            crtn_dt,
            vms_ctlr_nmbr,
            cmnc_stts_cd,
            pwer_stts_cd,
            modl_stts_cd,
            cbox_door_stts_cd,
            fan_stts_cd,
            hetr_stts_cd,
            cbox_tmpr,
            brgh_val,
            comm_stts_cd,
            modl_stts,
            pwer_stts )
        values (
                   #{updtDt},
                   #{vmsCtlrNmbr},
                   #{cmncSttsCd},
                   #{pwerSttsCd},
                   #{modlSttsCd},
                   #{cboxDoorSttsCd},
                   #{fanSttsCd},
                   #{hetrSttsCd},
                   #{cboxTmpr},
                   #{brghVal},
                   #{commSttsCd},
                   #{modlStts},
                   #{pwerStts}
               )
        ]]>
    </insert>

    <insert id="insertVmsCtrlHs" parameterType="com.its.vms.entity.TbVmsCtrlHs">
    <![CDATA[
        insert into tb_vms_ctrl_hs (
            ctrl_dt,
            vms_ctlr_nmbr,
            ctrl_type,
            ctrl_val,
            ctrl_result,
            user_id )
        values (
                   #{obj.ctrlDt},
                   #{obj.vmsCtlrNmbr},
                   #{obj.ctrlType},
                   #{obj.ctrlVal},
                   #{obj.ctrlResult},
                   #{obj.userId}
               )
        ]]>
    </insert>

    <insert id="updateVmsCtrlHs" parameterType="com.its.vms.entity.TbVmsCtrlHs">
    <![CDATA[
        update tb_vms_ctrl_hs
            set ctrl_result = #{obj.ctrlResult}
        where ctrl_dt       = #{obj.ctrlDt}
          and vms_ctlr_nmbr = #{obj.obj.vmsCtlrNmbr}
          and ctrl_type     = #{obj.obj.ctrlType}
        ]]>
    </insert>

    <update id="updateVmsCtlrParam" parameterType="com.its.vms.entity.TbVmsCtlr">
    <![CDATA[
        update tb_vms_ctlr
        set
            panl_pwer_mode      = #{panlPwerMode},
            fan_mode            = #{fanMode},
            fan_run_tmpr        = #{fanRunTmpr},
            hetr_mode           = #{hetrMode},
            hetr_run_tmpr       = #{hetrRunTmpr},
            brgh_mode           = #{brghMode},
            brgh_curr_step      = #{brghCurrStep},
            brgh_week_step      = #{brghWeekStep},
            brgh_nght_step      = #{brghNghtStep}
        where vms_ctlr_nmbr = #{vmsCtlrNmbr}
        ]]>
    </update>

</mapper>
