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

    <select id="findLinkTrafAsnTime" resultType="com.utic.center.utic.traf.server.dto.TrafPrcsTimeDto">
        <![CDATA[
        SELECT TO_CHAR(SYSDATE,            'YYYYMMDDHH24MI')||'00' AS currTime,
               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>

    <insert id="insertTrafficLoc" parameterType="com.utic.center.utic.traf.server.dto.LinkTrafAsnTimeDto">
        INSERT INTO TRAFFIC_LOC(LOGDATE, CENTERID, LINKID, LOCALDATE, SPEED)
        SELECT TO_DATE(#{obj.currTime}, 'YYYYMMDDHH24MISS')         AS LOGDATE,
               FROMCENTERID                                         AS CENTERID,
               SUBSTR(MAXDATA, 18, 10)                              AS LINKID,
               TO_DATE(SUBSTR(MAXDATA, 28, 14), 'YYYYMMDDHH24MISS') AS LOCALDATE,
               TO_NUMBER(SUBSTR(MAXDATA, 43, 4))                    AS SPEED
          FROM (
                SELECT FROMCENTERID,
                       MAX(TO_CHAR(LOGDATE, 'YYYYMMDDHH24MISS')||FROMCENTERID||TO_CHAR(LINKNUMBER)||TO_CHAR(REGDATE, 'YYYYMMDDHH24MISS')||TO_CHAR(SPEEDRATE, '0000')) MAXDATA
                  FROM SECTION.RCV_LINK_TRAFFIC
                 WHERE LOGDATE <![CDATA[ > ]]> TO_DATE(#{obj.past10MinTime}, 'YYYYMMDDHH24MISS')
                   AND LOGDATE <![CDATA[<= ]]> TO_DATE(#{obj.currTime}, 'YYYYMMDDHH24MISS')
                   AND FROMCENTERID IN
                     <foreach item="centerId" collection="obj.inCenterIds" open="("  close=")" separator=",">
                         #{centerId}
                     </foreach>
                   AND LINKNUMBER <![CDATA[ > ]]> '1000000000'
                   AND LINKNUMBER <![CDATA[ < ]]> '4210000000'
                 GROUP BY FROMCENTERID, LINKNUMBER
               )
    </insert>

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

    <insert id="insertIncidentCommon" parameterType="com.utic.center.utic.traf.server.dto.LinkTrafAsnTimeDto">
        <![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>
