<?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.bis.dao.mapper.NodeLinkMapper">

    <select id="selectNodeAll" resultType="com.its.bis.entity.TbNode" fetchSize="1000">
    <![CDATA[
        SELECT A.NODE_ID,
               A.NODE_TYPE,
               A.NODE_NAME,
               A.TURN_P,
               A.RMRK,
               A.X_CRDN,
               A.Y_CRDN
        FROM TB_NODE A
        ]]>
    </select>

    <select id="selectLinkAll" resultType="com.its.bis.entity.TbLink" fetchSize="1000">
    <![CDATA[
        SELECT A.LINK_ID,
               A.F_NODE_ID,
               A.T_NODE_ID,
               A.LANES,
               A.ROAD_RANK,
               A.ROAD_TYPE,
               A.ROAD_NO,
               A.ROAD_NAME,
               A.MAX_SPD,
               A.LINK_LENG,
               A.DEL_YN
        FROM TB_LINK A
       WHERE A.LINK_LENG > 1
        ]]>
    </select>

    <update id="createBisLinkTraf" parameterType="com.its.bis.entity.MakeTrafParam">
    <![CDATA[
        MERGE INTO TB_BIS_LINK_TRAF L
        USING (SELECT LINK_ID                AS LINK_ID,
                      #{obj.PRCN_DT}         AS PRCN_DT,
                      COUNT(1)               AS TFVL,
                      ROUND(AVG(SPED), 0)    AS SPED,
                      ROUND(AVG(TRVL_HH), 0) AS TRVL_HH,
                      COUNT(1)               AS DATA_NUM
               FROM TB_BIS_LINK_TRAF_CLCT
               WHERE 1=1
                 AND CLCT_DT >= TO_CHAR(SYSDATE - #{obj.FROM_MIN}/1440, 'YYYYMMDDHH24MISS')
                 AND SPED >= #{obj.MIN_SPEED}
                 AND SPED <= #{obj.MAX_SPEED}
               GROUP BY LINK_ID) M
        ON (L.LINK_ID = M.LINK_ID)
        WHEN MATCHED THEN
            UPDATE SET
                L.PRCN_DT  = M.PRCN_DT,
                L.TFVL     = M.TFVL,
                L.SPED     = M.SPED,
                L.TRVL_HH  = M.TRVL_HH,
                L.DATA_NUM = M.DATA_NUM
        WHEN NOT MATCHED THEN
            INSERT (LINK_ID,
                    PRCN_DT,
                    TFVL,
                    SPED,
                    TRVL_HH,
                    DATA_NUM )
            VALUES (M.LINK_ID,
                    M.PRCN_DT,
                    M.TFVL,
                    M.SPED,
                    M.TRVL_HH,
                    M.DATA_NUM )
    ]]>
    </update>

    <update id="updateBisVehLoc" parameterType="com.its.bis.entity.TbBisVehLoc">
    <![CDATA[
        MERGE INTO TB_BIS_VEH_LOC L
        USING (SELECT #{obj.DEVICE_ID}    AS DEVICE_ID,
                      #{obj.CLCT_DT}      AS CLCT_DT,
                      #{obj.LOC_TYPE}     AS LOC_TYPE,
                      #{obj.SPEED}        AS SPEED,
                      #{obj.HEGT}         AS HEGT,
                      #{obj.ANGLE}        AS ANGLE,
                      #{obj.LNG}          AS LNG,
                      #{obj.LAT}          AS LAT,
                      #{obj.PASS_NODE_ID} AS PASS_NODE_ID
                 FROM DUAL) M
        ON (L.DEVICE_ID = M.DEVICE_ID)
        WHEN MATCHED THEN
            UPDATE SET L.CLCT_DT      = M.CLCT_DT,
                       L.LOC_TYPE     = M.LOC_TYPE,
                       L.SPEED        = M.SPEED,
                       L.HEGT         = M.HEGT,
                       L.ANGLE        = M.ANGLE,
                       L.LNG          = M.LNG,
                       L.LAT          = M.LAT,
                       L.PASS_NODE_ID = M.PASS_NODE_ID
        WHEN NOT MATCHED THEN
            INSERT (DEVICE_ID,
                    CLCT_DT,
                    LOC_TYPE,
                    SPEED,
                    HEGT,
                    ANGLE,
                    LNG,
                    LAT,
                    PASS_NODE_ID )
            VALUES (M.DEVICE_ID,
                    M.CLCT_DT,
                    M.LOC_TYPE,
                    M.SPEED,
                    M.HEGT,
                    M.ANGLE,
                    M.LNG,
                    M.LAT,
                    M.PASS_NODE_ID )
        ]]>
    </update>

    <insert id="insertBisLinkTrafClct" parameterType="com.its.bis.entity.TbBisLinkTrafClct">
    <![CDATA[
        INSERT INTO TB_BIS_LINK_TRAF_CLCT (
            CLCT_DT,
            LINK_ID,
            DEVICE_ID,
            SPED,
            TRVL_HH )
        VALUES (
           #{obj.CLCT_DT},
           #{obj.LINK_ID},
           #{obj.DEVICE_ID},
           #{obj.SPED},
           #{obj.TRVL_HH}
        )
        ]]>
    </insert>

</mapper>
