<?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.pis.dao.mapper.PisInfrMapper">

    <select id="selectAll" resultType="com.its.pis.entity.TbPisInfr">
    <![CDATA[
		SELECT A.PIS_NMBR AS PIS_NMBR,
		       A.PIS_NM AS PIS_NM,
		       A.PIS_TP AS PIS_TP,
		       A.PIS_IP AS PIS_IP,
		       A.PIS_PORT AS PIS_PORT,
		       A.PIS_END_POINT AS PIS_END_POINT,
		       A.PIS_ID AS PIS_ID,
		       A.PIS_TOKEN AS PIS_TOKEN,
		       A.PIS_CYCLE AS PIS_CYCLE,
		       A.DEL_YN AS DEL_YN
		  FROM TB_PIS_INFR A
		 WHERE A.DEL_YN = 'N'
		 ORDER BY A.PIS_NMBR
    ]]>
    </select>

    <select id="selectPisInfrStts" resultType="com.its.pis.entity.TbPisInfrStts">
    <![CDATA[
        SELECT A.PIS_NMBR AS PIS_NMBR,
               NVL(B.UPDT_DT,      '19700101000000') AS UPDT_DT,
               NVL(B.CMNC_STTS_CD, 'CMS1')           AS CMNC_STTS_CD,
               NVL(B.CONN_DT,      '19700101000000') AS CONN_DT,
               NVL(B.DIS_CONN_DT,  '19700101000000') AS DIS_CONN_DT
        FROM TB_PIS_INFR A
        LEFT OUTER JOIN TB_PIS_INFR_STTS B
                     ON A.DEL_YN = 'N'
                    AND A.PIS_NMBR = B.PIS_NMBR
    ]]>
    </select>

    <select id="selectPisRltnPrkPlce" resultType="com.its.pis.entity.TbPisRltnPrlt">
    <![CDATA[
        SELECT A.PIS_NMBR       AS PIS_NMBR,
               A.PRK_PLCE_NMBR  AS PRK_PLCE_NMBR,
               A.ORD            AS ORD
        FROM TB_PIS_RLTN_PRK_PLCE A,
             TB_PIS_INFR          B,
             TB_PRK_PLCE          C
        WHERE A.PIS_NMBR = B.PIS_NMBR
          AND A.PRK_PLCE_NMBR = C.PRK_PLCE_NMBR
          AND B.DEL_YN = 'N'
        ]]>
    </select>

    <update id="updatePisInfrStts" parameterType="com.its.pis.entity.TbPisInfrStts">
    <![CDATA[
        MERGE INTO TB_PIS_INFR_STTS
            USING (SELECT #{stts.PIS_NMBR}      AS PIS_NMBR,
                          #{stts.UPDT_DT}       AS UPDT_DT,
                          #{stts.CMNC_STTS_CD}  AS CMNC_STTS_CD,
                          #{stts.CONN_DT}       AS CONN_DT,
                          #{stts.DIS_CONN_DT}   AS DIS_CONN_DT
                   FROM DUAL) M
            ON (L.PIS_NMBR = M.PIS_NMBR)
            WHEN MATCHED THEN
                UPDATE SET L.UPDT_DT      = M.UPDT_DT,
                    L.CMNC_STTS_CD = M.CMNC_STTS_CD,
                    L.CONN_DT      = M.CONN_DT,
                    L.DIS_CONN_DT  = M.DIS_CONN_DT
            WHEN NOT MATCHED THEN
                INSERT (
                        PIS_NMBR,
                        UPDT_DT,
                        CMNC_STTS_CD,
                        CONN_DT,
                        DIS_CONN_DT
                    )
                    VALUES (
                               M.PIS_NMBR,
                               M.UPDT_DT,
                               M.CMNC_STTS_CD,
                               M.CONN_DT,
                               NULL
                           )
        ]]>
    </update>

    <insert id="insertPisInfrSttsHs" parameterType="com.its.pis.entity.TbPisInfrStts">
    <![CDATA[
          INSERT INTO TB_PIS_INFR_STTS_HS (
                  CRTN_DT,
                  PIS_NMBR,
		          CMNC_STTS_CD
		         )           
          VALUES (
				  #{stts.UPDT_DT},
				  #{stts.PIS_NMBR},
				  #{stts.CMNC_STTS_CD}
                 )
    ]]>
    </insert>

    <update id="batchUpdatePisInfrStts" parameterType="java.util.Map">
    <![CDATA[
        MERGE INTO TB_PIS_INFR_STTS L
            USING (SELECT #{PIS_NMBR}     AS PIS_NMBR,
                          #{UPDT_DT}      AS UPDT_DT,
                          #{CMNC_STTS_CD} AS CMNC_STTS_CD,
                          #{CONN_DT}      AS CONN_DT,
                          #{DIS_CONN_DT}  AS DIS_CONN_DT
                   FROM DUAL) M
            ON (L.PIS_NMBR = M.PIS_NMBR)
            WHEN MATCHED THEN
                UPDATE SET
                    L.UPDT_DT      = M.UPDT_DT,
                    L.CMNC_STTS_CD = M.CMNC_STTS_CD,
                    L.CONN_DT      = M.CONN_DT,
                    L.DIS_CONN_DT  = M.DIS_CONN_DT
            WHEN NOT MATCHED THEN
                INSERT (
                        PIS_NMBR,
                        UPDT_DT,
                        CMNC_STTS_CD,
                        CONN_DT,
                        DIS_CONN_DT
                    )
                    VALUES (
                               M.PIS_NMBR,
                               M.UPDT_DT,
                               M.CMNC_STTS_CD,
                               M.CONN_DT,
                               NULL
                           )
        ]]>
    </update>

    <insert id="batchInsertPisInfrSttsHs" parameterType="java.util.Map">
    <![CDATA[
        INSERT INTO TB_PIS_INFR_STTS_HS (
            CRTN_DT,
            PIS_NMBR,
            CMNC_STTS_CD
        )
        VALUES (
                   #{UPDT_DT},
                   #{PIS_NMBR},
                   #{CMNC_STTS_CD}
               )
        ]]>
    </insert>

</mapper>
