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

    <select id="findAllAsnCenter" resultType="com.utic.center.utic.traf.server.dto.AsnCenterDto">
        <![CDATA[
        SELECT CENTERID   AS centerId,
               CENTERINFO AS centerInfo,
               0          AS dataCount
        FROM SECTION.CENTER
        WHERE CENTERFLAG = 'L'
          AND EXEYN      = 'Y'
          AND RCVYN      = 'Y'
          AND TRAFFICYN  = 'Y'
        ]]>
    </select>

    <insert id="insertTrafficLoc" parameterType="com.utic.center.utic.traf.server.dto.TrafPrcsTimeDto" timeout="60">
        <![CDATA[
        INSERT /*+ APPEND */ INTO TRAFFIC_LOC (LOGDATE, CENTERID, LINKID, LOCALDATE, SPEED)
        WITH VALID_CENTER AS (
            SELECT CENTERID
            FROM SECTION.CENTER
            WHERE CENTERFLAG = 'L'
              AND EXEYN = 'Y'
              AND RCVYN = 'Y'
              AND TRAFFICYN = 'Y'
        ),
        LATEST_LOG AS (
             SELECT T.FROMCENTERID, T.LINKNUMBER, MAX(T.LOGDATE) AS MAX_LOGDATE
             FROM SECTION.RCV_LINK_TRAFFIC T
             JOIN VALID_CENTER C
               ON T.FROMCENTERID = C.CENTERID
             WHERE T.LOGDATE BETWEEN TO_DATE(#{obj.past10MinTime}, 'YYYYMMDDHH24MISS')
               AND TO_DATE(#{obj.prcsTime}, 'YYYYMMDDHH24MISS')
               AND T.LINKNUMBER BETWEEN '1000000000' AND '4210000000'
          GROUP BY T.FROMCENTERID, T.LINKNUMBER
        )
        SELECT
            TO_DATE(#{obj.prcsTime}, 'YYYYMMDDHH24MISS') AS LOGDATE,
            T.FROMCENTERID AS CENTERID,
            T.LINKNUMBER   AS LINKID,
            T.LOGDATE      AS LOCALDATE,
            T.SPEEDRATE    AS SPEED
        FROM SECTION.RCV_LINK_TRAFFIC T
        JOIN LATEST_LOG L
          ON T.FROMCENTERID = L.FROMCENTERID
         AND T.LINKNUMBER   = L.LINKNUMBER
         AND T.LOGDATE      = L.MAX_LOGDATE
        WHERE T.SPEEDRATE > 0
        ]]>
    </insert>

    <insert id="insertTrafficLocX" parameterType="com.utic.center.utic.traf.server.dto.TrafPrcsTimeDto">
        <![CDATA[
        INSERT /*+ APPEND */ INTO TRAFFIC_LOC (LOGDATE, CENTERID, LINKID, LOCALDATE, SPEED)
        SELECT
            TO_DATE(#{obj.prcsTime}, 'YYYYMMDDHH24MISS') AS LOGDATE,
            T.FROMCENTERID                               AS CENTERID,
            T.LINKNUMBER                                 AS LINKID,
            T.LOGDATE                                    AS LOCALDATE,
            T.SPEEDRATE                                  AS SPEED
        FROM (
                 SELECT T.FROMCENTERID, T.LINKNUMBER, MAX(T.LOGDATE) AS MAX_LOGDATE
                 FROM SECTION.RCV_LINK_TRAFFIC T
                          JOIN (
                     SELECT CENTERID
                     FROM SECTION.CENTER
                     WHERE CENTERFLAG = 'L'
                       AND EXEYN      = 'Y'
                       AND RCVYN      = 'Y'
                       AND TRAFFICYN  = 'Y'
                 ) C ON T.FROMCENTERID = C.CENTERID
                 WHERE T.LOGDATE BETWEEN TO_DATE(#{obj.past10MinTime}, 'YYYYMMDDHH24MISS')
                     AND TO_DATE(#{obj.prcsTime},      'YYYYMMDDHH24MISS')
                     AND T.LINKNUMBER BETWEEN '1000000000'
                     AND '4210000000'
                 GROUP BY T.FROMCENTERID, T.LINKNUMBER
             ) latest
                 JOIN SECTION.RCV_LINK_TRAFFIC T
                      ON T.FROMCENTERID = latest.FROMCENTERID
                          AND T.LINKNUMBER   = latest.LINKNUMBER
                          AND T.LOGDATE      = latest.MAX_LOGDATE
                          AND T.SPEEDRATE    > 0
        ]]>
    </insert>

    <select id="findAllTrafficLocCount" parameterType="com.utic.center.utic.traf.server.dto.TrafPrcsTimeDto" resultType="com.utic.center.utic.traf.server.dto.AsnCollectInfoDto">
        <![CDATA[
        SELECT CENTERID AS centerId,
               COUNT(1) AS dataCount
        FROM TRAFFIC_LOC
        WHERE LOGDATE = TO_DATE(#{obj.prcsTime}, 'YYYYMMDDHH24MISS')
        GROUP BY CENTERID
        ]]>
    </select>

    <insert id="insertIncidentCommon" parameterType="com.utic.center.utic.traf.server.dto.TrafPrcsTimeDto" timeout="60">
        <![CDATA[
        INSERT INTO PTAPP.INCIDENT_COMMON(INCIDENTID,LOGDATE,REPORTDATE,LINKID,CENTERID,STARTDATE,ENDDATE,TRAFFICGRADE,
                                              TROUBLEGRADE, INCIDENTTITLE, INCIDENTINFO, INCIDENTCODE, INCIDENTSUBCODE, LOCATION,
                                              COORDX, COORDY, DATARESID)
        SELECT NODE_NODEIDNUMBER, SYSDATE,
               TO_DATE(SUBSTR(UPDATETYPEOTHER, 1, 14),  'YYYYMMDDHH24MISS'),
               LINK_LINKIDNUMBER,
               SUBSTR(NODE_NODEIDNUMBER, 1,3),
               TO_DATE(SUBSTR(UPDATETYPEOTHER, 15, 14), 'YYYYMMDDHH24MISS'),
               TO_DATE(SUBSTR(UPDATETYPEOTHER, 29, 14), 'YYYYMMDDHH24MISS'),
               LTRIM(TO_CHAR(INCIDENTSTATUSCODE, '00')),
               UPDATETYPECODE,
               DESCRIPTIONTYPEINCIDENTOTHER,
               INCIDENTVEHICLESINVOLVEDOTHER,
               DESCRIPTIONTYPEINCIDENTCODE,
               INCIDENTVEHICLESINVOLVEDCODE,
               SUBSTR(INCIDENTSTATUSOTHER, 35, 120),
               TO_NUMBER(LTRIM(SUBSTR(INCIDENTSTATUSOTHER, 1, 17))),
               TO_NUMBER(SUBSTR(INCIDENTSTATUSOTHER, 19, 16)),
               SUBSTR(NODE_NODEIDNUMBER, 2,2)
        FROM SECTION.RCV_INCIDENT
        WHERE NODE_NODEIDNUMBER in
              (
                  SELECT NODE_NODEIDNUMBER INCIDENTID
                  FROM SECTION.RCV_INCIDENT
                  WHERE LOGDATE > TO_DATE(#{obj.incdTime}, 'YYYYMMDDHH24MISS')
            MINUS
        SELECT INCIDENTID
        FROM PTAPP.INCIDENT_COMMON
        WHERE LOGDATE > TO_DATE(#{obj.incdTime}, 'YYYYMMDDHH24MISS')
            )
        AND FROMCENTERID = 'L01'
        ]]>
    </insert>

</mapper>
