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

    <select id="selectRseOffrSectAll" resultType="com.its.vms.entity.TbRseOffrSect">
    <![CDATA[
	    SELECT B.OFFR_SECT_ID,
               B.OFFR_DRCT_NM,
               B.CNGS_BASI_SPED,
               B.DELY_BASI_SPED,
               B.RSE_CTLR_NMBR,
               B.OBU_ENTR_DRCT_NMBR
          FROM TB_RSE_CTLR                A,
               TB_RSE_OFFR_SECT           B,
               TB_RSE_OFFR_DRCT           C,
               TB_RSE_OFFR_SECT_RLTN_IFSC D
         WHERE A.RSE_CTLR_NMBR = B.RSE_CTLR_NMBR
           AND B.RSE_CTLR_NMBR = C.RSE_CTLR_NMBR
           AND B.OBU_ENTR_DRCT_NMBR = C.OBU_ENTR_DRCT_NMBR
           AND B.OFFR_SECT_ID       = D.OFFR_SECT_ID
           AND A.DEL_YN = 'N'
    ]]>
    </select>

    <select id="selectRseOffrSectAll_Temp" resultType="com.its.vms.entity.TbRseOffrSect">
    <![CDATA[
        SELECT A.OFFR_SECT_ID,
               A.OFFR_DRCT_NM,
               A.CNGS_BASI_SPED,
               A.DELY_BASI_SPED,
               A.RSE_CTLR_NMBR,
               A.OBU_ENTR_DRCT_NMBR
        FROM TB_RSE_OFFR_SECT A
        ]]>
    </select>

    <select id="selectRseOffrDrctAll" resultType="com.its.vms.entity.TbRseOffrDrct">
    <![CDATA[
	    SELECT A.RSE_CTLR_NMBR,
               A.PRE_RSE_CTLR_NMBR,
               A.OBU_ENTR_DRCT_NMBR,
               A.IXR_DRCT_NUM
          FROM TB_RSE_OFFR_DRCT A,
               TB_RSE_CTLR      B,
               TB_RSE_CTLR      C
         WHERE B.DEL_YN = 'N'
           AND C.DEL_YN = 'N'
           AND A.RSE_CTLR_NMBR     = B.RSE_CTLR_NMBR
           AND A.PRE_RSE_CTLR_NMBR = C.RSE_CTLR_NMBR
    ]]>
    </select>

     <select id="selectRseOffrSectTraf" resultType="com.its.vms.dto.voDsrcOffrSectTraf">
    <![CDATA[
        SELECT OFFR_SECT_ID, OFFR_DRCT_NM, OBU_ENTR_DRCT_NMBR,
               IFSC_ID, CMTR_GRAD_CD,
               (SELECT CMMN_CD_KOR_NM
                FROM TB_CMMN_CD
                WHERE CMMN_CLSF_CD = 'LTC'
                  AND CMMN_CD = CMTR_GRAD_CD) AS CMTR_GRAD_NM,
               SPED, TRVL_HH
        FROM (SELECT A.OFFR_SECT_ID, B.OFFR_DRCT_NM, B.OBU_ENTR_DRCT_NMBR,
                     'x' AS IFSC_ID,
                     CASE WHEN AVG_SPED >  B.DELY_BASI_SPED THEN 'LTC1'
                          WHEN AVG_SPED <= B.CNGS_BASI_SPED THEN 'LTC3'
                          ELSE 'LTC2'
                         END AS CMTR_GRAD_CD,
                     AVG_SPED AS SPED,
                     TRVL_HH
              FROM (SELECT A.OFFR_SECT_ID,
                           ROUND(AVG(B.SPED), 0) AS SPED,
                           ROUND(AVG(B.TRVL_HH), 0) AS TRVL_HH,
                           ROUND(((SUM(C.SECT_LNGT) * 3.6) / SUM(B.TRVL_HH)), 0) AS AVG_SPED,
                           COUNT(1)              AS CNT
                    FROM TB_RSE_OFFR_SECT_RLTN_IFSC A,
                         TB_IFSC_TRAF               B,
                         TB_IFSC                    C
                    WHERE 1=1
                      AND A.IFSC_ID = B.IFSC_ID
                      AND B.IFSC_ID = C.IFSC_ID
                      AND B.CMTR_GRAD_CD <> 'LTC0'
                      AND B.TRVL_HH > 0
                    GROUP BY A.OFFR_SECT_ID
                   ) A,
                   TB_RSE_OFFR_SECT B
              WHERE 1 = 1
                AND A.OFFR_SECT_ID = B.OFFR_SECT_ID
             )
        ]]>
    </select>

    <select id="selectRseOffrSectTrafMulti" resultType="com.its.vms.dto.voDsrcOffrSectTraf">
    <![CDATA[
        SELECT A.OFFR_SECT_ID,
               B.IFSC_ID,
               B.CMTR_GRAD_CD,
               C.CMMN_CD_KOR_NM         AS CMTR_GRAD_NM,
               ROUND(AVG(B.SPED),    0) AS SPED,
               ROUND(AVG(B.TRVL_HH), 0) AS TRVL_HH
        FROM TB_RSE_OFFR_SECT_RLTN_IFSC A,
             TB_IFSC_TRAF               B,
             (SELECT CMMN_CD, CMMN_CD_KOR_NM
              FROM TB_CMMN_CD
              WHERE CMMN_CLSF_CD = 'LTC') C
        WHERE 1 = 1
          AND A.IFSC_ID = B.IFSC_ID
          AND B.CMTR_GRAD_CD = C.CMMN_CD
        GROUP BY A.OFFR_SECT_ID, B.IFSC_ID, B.CMTR_GRAD_CD, C.CMMN_CD_KOR_NM
        ]]>
    </select>

    <update id="updateRseOffrSectTraf" parameterType="com.its.vms.entity.TbRseOffrSectTraf">
    <![CDATA[
        MERGE INTO TB_RSE_OFFR_SECT_TRAF L
            USING (SELECT
                       #{obj.OFFR_SECT_ID} AS OFFR_SECT_ID,
                       #{obj.PRCN_DT}      AS PRCN_DT,
                       #{obj.OFFR_CONT}    AS OFFR_CONT
                   FROM DUAL) M
            ON (L.OFFR_SECT_ID = M.OFFR_SECT_ID)
            WHEN MATCHED THEN
                UPDATE SET
                    L.PRCN_DT   = M.PRCN_DT,
                    L.OFFR_CONT = M.OFFR_CONT
            WHEN NOT MATCHED THEN
                INSERT (
                        OFFR_SECT_ID,
                        PRCN_DT,
                        OFFR_CONT )
                    VALUES (
                               M.OFFR_SECT_ID,
                               M.PRCN_DT,
                               M.OFFR_CONT )
        ]]>
    </update>

    <insert id="insertRseOffrSectTrafHs" parameterType="com.its.vms.entity.TbRseOffrSectTraf">
    <![CDATA[
        INSERT INTO TB_RSE_OFFR_SECT_TRAF_HS (
            OFFR_SECT_ID,
            PRCN_DT,
            OFFR_CONT
        )
        VALUES (
                   #{obj.OFFR_SECT_ID},
                   #{obj.PRCN_DT},
                   #{obj.OFFR_CONT}
               )
        ]]>
    </insert>

</mapper>
