<?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.dsrc.dao.mapper.RseCtlrMapper">

    <select id="selectAll" resultType="com.its.dsrc.entity.TbRseCtlr">
    <![CDATA[
        SELECT A.RSE_CTLR_NMBR,
               A.RSE_CTLR_ID,
               A.RSE_NM,
               A.RSE_CTLR_IP,
               A.RSE_CTLR_PORT,
               A.RSE_CTLR_LOCAL_NO,
               A.RSE_TYPE_CD,
               A.RSE_MCU_ID,
               A.LOG_CNNC_ID,
               A.LOG_CNNC_PW,
               A.MNFC_CMPY_CD,
               A.CLCT_ABNR_BASI,
               A.DEL_YN
        FROM TB_RSE_CTLR A
       WHERE A.DEL_YN = 'N'
       ORDER BY A.RSE_CTLR_NMBR
    ]]>
    </select>

    <select id="selectRseCtlrStts" resultType="com.its.dsrc.entity.TbRseCtlrStts">
    <![CDATA[
        SELECT A.RSE_CTLR_NMBR AS RSE_CTLR_NMBR,
               A.UPDT_DT,
               A.CMNC_STTS_CD,
               A.CNTL_DEVC_STTS,
               A.ATN_1_STTS,
               A.ATN_2_STTS,
               A.ATN_3_STTS,
               A.ATN_4_STTS,
               A.ATN_1_MTNS,
               A.ATN_2_MTNS,
               A.ATN_3_MTNS,
               A.ATN_4_MTNS
        FROM TB_RSE_CTLR_STTS A,
             TB_RSE_CTLR      B
        WHERE B.DEL_YN = 'N'
          AND A.RSE_CTLR_NMBR = B.RSE_CTLR_NMBR
        ]]>
    </select>

    <select id="selectRseObuNonCryptList" resultType="com.its.dsrc.entity.TbRseObuNonCrypt">
    <![CDATA[
	    SELECT TRIM(A.OBU_ID) AS OBU_ID,
	           TRIM(A.OBU_IDNT_NMBR) AS OBU_IDNT_NMBR
	      FROM TB_RSE_OBU_NON_CRPT A
	     ORDER BY A.OBU_ID
    ]]>
    </select>

    <update id="updateRseCtlrStts" parameterType="com.its.dsrc.entity.TbRseCtlrStts">
    <![CDATA[
        MERGE INTO TB_RSE_CTLR_STTS L
        USING (
               SELECT #{obj.RSE_CTLR_NMBR} 	 AS RSE_CTLR_NMBR,
                      #{obj.UPDT_DT} 	     AS UPDT_DT,
                      #{obj.CMNC_STTS_CD} 	 AS CMNC_STTS_CD,
                      #{obj.CNTL_DEVC_STTS}  AS CNTL_DEVC_STTS,
                      #{obj.ATN_1_STTS} 	 AS ATN_1_STTS,
                      #{obj.ATN_2_STTS} 	 AS ATN_2_STTS,
                      #{obj.ATN_3_STTS} 	 AS ATN_3_STTS,
                      #{obj.ATN_4_STTS} 	 AS ATN_4_STTS,
                      #{obj.ATN_1_MTNS} 	 AS ATN_1_MTNS,
                      #{obj.ATN_2_MTNS} 	 AS ATN_2_MTNS,
                      #{obj.ATN_3_MTNS} 	 AS ATN_3_MTNS,
                      #{obj.ATN_4_MTNS} 	 AS ATN_4_MTNS
                 FROM DUAL
           ) M
        ON ( L.RSE_CTLR_NMBR = M.RSE_CTLR_NMBR )
        WHEN MATCHED THEN
          UPDATE
                 SET
                     L.UPDT_DT        = M.UPDT_DT,
                     L.CMNC_STTS_CD   = M.CMNC_STTS_CD,
                     L.CNTL_DEVC_STTS = M.CNTL_DEVC_STTS,
                     L.ATN_1_STTS     = M.ATN_1_STTS,
                     L.ATN_2_STTS     = M.ATN_2_STTS,
                     L.ATN_3_STTS     = M.ATN_3_STTS,
                     L.ATN_4_STTS     = M.ATN_4_STTS,
                     L.ATN_1_MTNS     = M.ATN_1_MTNS,
                     L.ATN_2_MTNS     = M.ATN_2_MTNS,
                     L.ATN_3_MTNS     = M.ATN_3_MTNS,
                     L.ATN_4_MTNS     = M.ATN_4_MTNS
        WHEN NOT MATCHED THEN
          INSERT (
	              L.RSE_CTLR_NMBR,
	              L.UPDT_DT,
                  L.CMNC_STTS_CD,
                  L.CNTL_DEVC_STTS,
                  L.ATN_1_STTS,
                  L.ATN_2_STTS,
                  L.ATN_3_STTS,
                  L.ATN_4_STTS,
                  L.ATN_1_MTNS,
                  L.ATN_2_MTNS,
                  L.ATN_3_MTNS,
                  L.ATN_4_MTNS
                 )
          VALUES (
	              M.RSE_CTLR_NMBR,
	              M.UPDT_DT,
                  M.CMNC_STTS_CD,
                  M.CNTL_DEVC_STTS,
                  M.ATN_1_STTS,
                  M.ATN_2_STTS,
                  M.ATN_3_STTS,
                  M.ATN_4_STTS,
                  M.ATN_1_MTNS,
                  M.ATN_2_MTNS,
                  M.ATN_3_MTNS,
                  M.ATN_4_MTNS
                 )
    ]]>
    </update>

    <update id="batchUpdateRseCtlrStts" parameterType="java.util.Map">
    <![CDATA[
        MERGE INTO TB_RSE_CTLR_STTS L
            USING (
                SELECT #{RSE_CTLR_NMBR}  AS RSE_CTLR_NMBR,
                       #{UPDT_DT} 	     AS UPDT_DT,
                       #{CMNC_STTS_CD} 	 AS CMNC_STTS_CD,
                       #{CNTL_DEVC_STTS} AS CNTL_DEVC_STTS,
                       #{ATN_1_STTS} 	 AS ATN_1_STTS,
                       #{ATN_2_STTS} 	 AS ATN_2_STTS,
                       #{ATN_3_STTS} 	 AS ATN_3_STTS,
                       #{ATN_4_STTS} 	 AS ATN_4_STTS,
                       #{ATN_1_MTNS} 	 AS ATN_1_MTNS,
                       #{ATN_2_MTNS} 	 AS ATN_2_MTNS,
                       #{ATN_3_MTNS} 	 AS ATN_3_MTNS,
                       #{ATN_4_MTNS} 	 AS ATN_4_MTNS
                FROM DUAL
            ) M
            ON ( L.RSE_CTLR_NMBR = M.RSE_CTLR_NMBR )
            WHEN MATCHED THEN
                UPDATE
                    SET
                        L.UPDT_DT        = M.UPDT_DT,
                        L.CMNC_STTS_CD   = M.CMNC_STTS_CD,
                        L.CNTL_DEVC_STTS = M.CNTL_DEVC_STTS,
                        L.ATN_1_STTS     = M.ATN_1_STTS,
                        L.ATN_2_STTS     = M.ATN_2_STTS,
                        L.ATN_3_STTS     = M.ATN_3_STTS,
                        L.ATN_4_STTS     = M.ATN_4_STTS,
                        L.ATN_1_MTNS     = M.ATN_1_MTNS,
                        L.ATN_2_MTNS     = M.ATN_2_MTNS,
                        L.ATN_3_MTNS     = M.ATN_3_MTNS,
                        L.ATN_4_MTNS     = M.ATN_4_MTNS
            WHEN NOT MATCHED THEN
                INSERT (
                        L.RSE_CTLR_NMBR,
                        L.UPDT_DT,
                        L.CMNC_STTS_CD,
                        L.CNTL_DEVC_STTS,
                        L.ATN_1_STTS,
                        L.ATN_2_STTS,
                        L.ATN_3_STTS,
                        L.ATN_4_STTS,
                        L.ATN_1_MTNS,
                        L.ATN_2_MTNS,
                        L.ATN_3_MTNS,
                        L.ATN_4_MTNS
                    )
                    VALUES (
                       M.RSE_CTLR_NMBR,
                       M.UPDT_DT,
                       M.CMNC_STTS_CD,
                       M.CNTL_DEVC_STTS,
                       M.ATN_1_STTS,
                       M.ATN_2_STTS,
                       M.ATN_3_STTS,
                       M.ATN_4_STTS,
                       M.ATN_1_MTNS,
                       M.ATN_2_MTNS,
                       M.ATN_3_MTNS,
                       M.ATN_4_MTNS
                   )
        ]]>
    </update>

    <update id="updateRseCtlrSttsList" parameterType="java.util.List">
    <![CDATA[
        MERGE INTO TB_RSE_CTLR_STTS L
        USING (
               <foreach collection="list" item="obj" open="" close="" separator="UNION">
               SELECT #{obj.RSE_CTLR_NMBR} 	 AS RSE_CTLR_NMBR,
                      #{obj.UPDT_DT} 	     AS UPDT_DT,
                      #{obj.CMNC_STTS_CD} 	 AS CMNC_STTS_CD,
                      #{obj.CNTL_DEVC_STTS}  AS CNTL_DEVC_STTS,
                      #{obj.ATN_1_STTS} 	 AS ATN_1_STTS,
                      #{obj.ATN_2_STTS} 	 AS ATN_2_STTS,
                      #{obj.ATN_3_STTS} 	 AS ATN_3_STTS,
                      #{obj.ATN_4_STTS} 	 AS ATN_4_STTS,
                      #{obj.ATN_1_MTNS} 	 AS ATN_1_MTNS,
                      #{obj.ATN_2_MTNS} 	 AS ATN_2_MTNS,
                      #{obj.ATN_3_MTNS} 	 AS ATN_3_MTNS,
                      #{obj.ATN_4_MTNS} 	 AS ATN_4_MTNS
                 FROM DUAL
               </foreach>
           ) M
        ON ( L.RSE_CTLR_NMBR = M.RSE_CTLR_NMBR )
        WHEN MATCHED THEN
          UPDATE
                 <set>
                     L.UPDT_DT        = M.UPDT_DT,
                     L.CMNC_STTS_CD   = M.CMNC_STTS_CD,
                     L.CNTL_DEVC_STTS = M.CNTL_DEVC_STTS,
                     L.ATN_1_STTS     = M.ATN_1_STTS,
                     L.ATN_2_STTS     = M.ATN_2_STTS,
                     L.ATN_3_STTS     = M.ATN_3_STTS,
                     L.ATN_4_STTS     = M.ATN_4_STTS,
                     L.ATN_1_MTNS     = M.ATN_1_MTNS,
                     L.ATN_2_MTNS     = M.ATN_2_MTNS,
                     L.ATN_3_MTNS     = M.ATN_3_MTNS,
                     L.ATN_4_MTNS     = M.ATN_4_MTNS
                 </set>
        WHEN NOT MATCHED THEN
          INSERT (
	              L.RSE_CTLR_NMBR,
	              L.UPDT_DT,
                  L.CMNC_STTS_CD,
                  L.CNTL_DEVC_STTS,
                  L.ATN_1_STTS,
                  L.ATN_2_STTS,
                  L.ATN_3_STTS,
                  L.ATN_4_STTS,
                  L.ATN_1_MTNS,
                  L.ATN_2_MTNS,
                  L.ATN_3_MTNS,
                  L.ATN_4_MTNS
                 )
          VALUES (
	              M.RSE_CTLR_NMBR,
	              M.UPDT_DT,
                  M.CMNC_STTS_CD,
                  M.CNTL_DEVC_STTS,
                  M.ATN_1_STTS,
                  M.ATN_2_STTS,
                  M.ATN_3_STTS,
                  M.ATN_4_STTS,
                  M.ATN_1_MTNS,
                  M.ATN_2_MTNS,
                  M.ATN_3_MTNS,
                  M.ATN_4_MTNS
                 )
    ]]>
    </update>

    <insert id="insertRseCtlrSttsHs" parameterType="com.its.dsrc.entity.TbRseCtlrStts">
    <![CDATA[
          INSERT INTO TB_RSE_CTLR_STTS_HS (
                  CRTN_DT,
                  RSE_CTLR_NMBR,
                  CMNC_STTS_CD,
                  CNTL_DEVC_STTS,
                  ATN_1_STTS,
                  ATN_2_STTS,
                  ATN_3_STTS,
                  ATN_4_STTS,
                  ATN_1_MTNS,
                  ATN_2_MTNS,
                  ATN_3_MTNS,
                  ATN_4_MTNS
                 )
          VALUES (
                  #{obj.UPDT_DT},
                  #{obj.RSE_CTLR_NMBR},
                  #{obj.CMNC_STTS_CD},
                  #{obj.CNTL_DEVC_STTS},
                  #{obj.ATN_1_STTS},
                  #{obj.ATN_2_STTS},
                  #{obj.ATN_3_STTS},
                  #{obj.ATN_4_STTS},
                  #{obj.ATN_1_MTNS},
                  #{obj.ATN_2_MTNS},
                  #{obj.ATN_3_MTNS},
                  #{obj.ATN_4_MTNS}
                 )
    ]]>
    </insert>

    <insert id="batchInsertRseCtlrSttsHs" parameterType="java.util.Map">
    <![CDATA[
        INSERT INTO TB_RSE_CTLR_STTS_HS (
            CRTN_DT,
            RSE_CTLR_NMBR,
            CMNC_STTS_CD,
            CNTL_DEVC_STTS,
            ATN_1_STTS,
            ATN_2_STTS,
            ATN_3_STTS,
            ATN_4_STTS,
            ATN_1_MTNS,
            ATN_2_MTNS,
            ATN_3_MTNS,
            ATN_4_MTNS
        )
        VALUES (
                   #{UPDT_DT},
                   #{RSE_CTLR_NMBR},
                   #{CMNC_STTS_CD},
                   #{CNTL_DEVC_STTS},
                   #{ATN_1_STTS},
                   #{ATN_2_STTS},
                   #{ATN_3_STTS},
                   #{ATN_4_STTS},
                   #{ATN_1_MTNS},
                   #{ATN_2_MTNS},
                   #{ATN_3_MTNS},
                   #{ATN_4_MTNS}
               )
        ]]>
    </insert>

    <insert id="insertRseCtlrCnncHs" parameterType="com.its.dsrc.entity.TbRseCtlrCnncHs">
    <![CDATA[
          INSERT INTO TB_RSE_CTLR_CNNC_HS (
                   RSE_CTLR_NMBR,
                   CLCT_DT,
                   LOG_TYPE,
                   LOG_ID,
                   LOG_ADDRESS )
          VALUES (
                  #{obj.RSE_CTLR_NMBR},
                  #{obj.CLCT_DT},
                  #{obj.LOG_TYPE},
                  #{obj.LOG_ID},
                  #{obj.LOG_ADDRESS}
                 )
    ]]>
    </insert>

    <insert id="insertRseCtrlHs" parameterType="com.its.dsrc.entity.TbRseCtrlHs">
    <![CDATA[
        INSERT INTO TB_RSE_CTRL_HS (
            RSE_CTLR_NMBR,
            CNTL_DT,
            DEVC_TYPE,
            CNTL_TYPE,
            RSPS_TYPE )
        VALUES (
                   #{obj.RSE_CTLR_NMBR},
                   #{obj.CNTL_DT},
                   #{obj.DEVC_TYPE},
                   #{obj.CNTL_TYPE},
                   #{obj.RSPS_TYPE}
               )
        ]]>
    </insert>

</mapper>
