<?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.PrkPlceMapper">

    <select id="selectAll" resultType="com.its.pis.entity.TbPrkPlce">
    <![CDATA[
        SELECT A.PRK_PLCE_NMBR      AS PRK_PLCE_NMBR,
               A.PRK_PLCE_MANAGE_NO AS PRK_PLCE_MANAGE_NO,
               A.PRK_PLCE_NM        AS PRK_PLCE_NM,
               A.PIS_TYPE           AS PIS_TYPE
        FROM TB_PRK_PLCE A
        WHERE A.PIS_TYPE != 'N'
          AND A.DEL_YN    = 'N'
        ORDER BY A.PRK_PLCE_NMBR
    ]]>
    </select>

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

    <insert id="insertPrkPlceSttsHs" parameterType="com.its.pis.entity.TbPrkPlceStts">
    <![CDATA[
          INSERT INTO TB_PRK_PLCE_STTS_HS (
		          CRTN_DT,
                  PRK_PLCE_NMBR,
                  CMNC_STTS_CD
		         )
          VALUES (
				  #{stts.UPDT_DT},
				  #{stts.PRK_PLCE_NMBR},
				  #{stts.CMNC_STTS_CD}
                 )
    ]]>
    </insert>

    <update id="updatePrkPlceRlTime" parameterType="com.its.pis.entity.TbPrkPlceRt">
    <![CDATA[
        MERGE INTO TB_PRK_PLCE_RT L
            USING (SELECT
                       #{obj.prkPlceNmbr}           AS PRK_PLCE_NMBR,
                       #{obj.dataColctTime}         AS DATA_COLCT_TIME,
                       #{obj.parkingCgsSttus}       AS PARKING_CGS_STTUS,
                       #{obj.prkCmprtCo}            AS PRK_CMPRT_CO,
                       #{obj.remndrPrkCmprtCo}      AS REMNDR_PRK_CMPRT_CO,
                       #{obj.prkResveAvailCmprtCo}  AS PRK_RESVE_AVAIL_CMPRT_CO,
                       #{obj.partclrMatter}         AS PARTCLR_MATTER
                   FROM DUAL) M
            ON (L.PRK_PLCE_NMBR = M.PRK_PLCE_NMBR)
            WHEN MATCHED THEN
                UPDATE SET
                    L.DATA_COLCT_TIME          = M.DATA_COLCT_TIME,
                    L.PARKING_CGS_STTUS        = M.PARKING_CGS_STTUS,
                    L.PRK_CMPRT_CO             = M.PRK_CMPRT_CO,
                    L.REMNDR_PRK_CMPRT_CO      = M.REMNDR_PRK_CMPRT_CO,
                    L.PRK_RESVE_AVAIL_CMPRT_CO = M.PRK_RESVE_AVAIL_CMPRT_CO,
                    L.PARTCLR_MATTER           = M.PARTCLR_MATTER
            WHEN NOT MATCHED THEN
                INSERT (
                        PRK_PLCE_NMBR,
                        DATA_COLCT_TIME,
                        PARKING_CGS_STTUS,
                        PRK_CMPRT_CO,
                        REMNDR_PRK_CMPRT_CO,
                        PRK_RESVE_AVAIL_CMPRT_CO,
                        PARTCLR_MATTER )
                    VALUES (
                               M.PRK_PLCE_NMBR,
                               M.DATA_COLCT_TIME,
                               M.PARKING_CGS_STTUS,
                               M.PRK_CMPRT_CO,
                               M.REMNDR_PRK_CMPRT_CO,
                               M.PRK_RESVE_AVAIL_CMPRT_CO,
                               M.PARTCLR_MATTER )
        ]]>
    </update>

    <insert id="insertPrkPlceRlTimeHs" parameterType="com.its.pis.entity.TbPrkPlceRtHs">
    <![CDATA[
        INSERT INTO TB_PRK_PLCE_RT_HS (
            CRTN_DT,
            PRK_PLCE_NMBR,
            PARKING_CGS_STTUS,
            PRK_CMPRT_CO,
            REMNDR_PRK_CMPRT_CO
        )
        SELECT #{obj.crtnDt} AS CRTN_DT,
               A.PRK_PLCE_NMBR,
               C.PARKING_CGS_STTUS,
               C.PRK_CMPRT_CO,
               C.REMNDR_PRK_CMPRT_CO
          FROM TB_PRK_PLCE A, TB_PRK_PLCE_STTS B, TB_PRK_PLCE_RT C
         WHERE A.DEL_YN = 'N'
           AND A.PRK_PLCE_NMBR = B.PRK_PLCE_NMBR
           AND B.CMNC_STTS_CD = 'CMS0'
           AND B.PRK_PLCE_NMBR = C.PRK_PLCE_NMBR
        ]]>
    </insert>


</mapper>
