<?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.utic.center.utic.traf.server.dao.mapper.utic.UticTrafficMapper">

    <select id="findNextMonth" resultType="com.utic.center.utic.traf.server.dto.ViewTextDto">
        <![CDATA[
        SELECT TO_CHAR(ADD_MONTHS(SYSDATE, 1), 'MM') AS viewText FROM DUAL
        ]]>
    </select>

    <select id="findTrafficCenterViewDdl" resultType="com.utic.center.utic.traf.server.dto.ViewTextDto">
        <![CDATA[
        SELECT TEXT AS viewText FROM USER_VIEWS WHERE VIEW_NAME = 'TRAFFIC_CENTER'
        ]]>
    </select>
<!--    <select id="findTrafficCenterViewDdl2" resultType="com.utic.center.utic.traf.server.dto.ViewTextDto">-->
<!--        <![CDATA[-->
<!--        SELECT DBMS_METADATA.GET_DDL('VIEW', 'TRAFFIC_CENTER', 'UTIADMIN') AS viewText FROM DUAL-->
<!--        ]]>-->
<!--    </select>-->

    <update id="swapTrafficCenterView">
        CREATE OR REPLACE VIEW TRAFFIC_CENTER AS
        SELECT * FROM ${sourceTable}
    </update>

    <select id="findOneTrafPrcsTime" resultType="com.utic.center.utic.traf.server.dto.TrafPrcsTimeDto">
        <![CDATA[
        SELECT TO_CHAR(SYSDATE,            'YYYYMMDDHH24MISS')     AS currTime,
               TO_CHAR(SYSDATE,            'YYYYMMDDHH24MI')||'00' AS prcsTime,
               TO_CHAR(SYSDATE +  15/1440, 'YYYYMMDDHH24MI')||'00' AS quarterTime,
               TO_CHAR(SYSDATE -   5/1440, 'YYYYMMDDHH24MI')||'00' AS past05MinTime,
               TO_CHAR(SYSDATE -   6/1440, 'YYYYMMDDHH24MI')||'00' AS past10MinTime,
               TO_CHAR(SYSDATE - 720/1440, 'YYYYMMDDHH24MI')||'00' AS incdTime
        FROM DUAL
        ]]>
    </select>

    <update id="truncateTrafficLoc">
        <![CDATA[
        TRUNCATE TABLE TRAFFIC_LOC
        ]]>
    </update>

    <insert id="insertCenterRecv">
        <![CDATA[
        INSERT INTO CENTER_RECEIVE(LOGDATE, CENTERID, INFOTYPE, SEQ, DATACNT)
        VALUES(SYSDATE, #{centerId}, #{infoType}, #{seq}, #{dataCnt})
        ]]>
    </insert>

    <update id="truncateTrafficCenter">
        <![CDATA[
        TRUNCATE TABLE ${tableName}
        ]]>
    </update>

    <!-- 1. Disable Indexes -->
    <update id="disableIndexes">
        ALTER INDEX ${tableName}_IDX${index} UNUSABLE
    </update>

    <!-- 2. Disable Primary Key Constraint -->
    <update id="disablePrimaryKey">
        ALTER TABLE ${tableName} DISABLE CONSTRAINT PK_${tableName}
    </update>

    <!-- 4. Rebuild Indexes -->
    <update id="rebuildIndexes">
        ALTER INDEX ${tableName}_IDX${index} REBUILD PARALLEL 4 NOLOGGING
    </update>

    <!-- 5. Enable Primary Key Constraint -->
    <update id="enablePrimaryKey">
        ALTER TABLE ${tableName} ENABLE CONSTRAINT PK_${tableName}
    </update>

    <insert id="insertTrafficCenter" timeout="60">
        <![CDATA[
        INSERT /*+ APPEND PARALLEL(${tableName}, 2) */ INTO ${tableName} NOLOGGING (
            LINKID,
            MISSVALUEYN,
            REGDATE,
            LINKLEVEL,
            SPEED,
            TRAVELTIME,
            TRAFFICGRADE,
            DATARESTYPE,
            CENTERID,
            PSDMISSVALUEYN ,
            PSDSPEED
        )
        VALUES(
            #{obj.linkId},
            #{obj.missValueYn},
            TO_DATE(#{obj.regDate}, 'YYYYMMDDHH24MISS'),
            #{obj.linkLevel},
            #{obj.speed},
            #{obj.travelTime},
            #{obj.trafficGrade},
            #{obj.dataResType},
            #{obj.centerId},
            #{obj.psdMissValueYn, jdbcType=NULL},
            DECODE(#{obj.psdSpeed}, 0, NULL, #{obj.psdSpeed})
            )
        ]]>
    </insert>

</mapper>
