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

    <select id="findLinkCount" resultType="com.utic.center.utic.traf.server.dto.LinkCountDto">
        <![CDATA[
        SELECT SUM(DECODE(LINKLEVEL, '1', 1, 0)) AS level1,
               SUM(DECODE(LINKLEVEL, '2', 1, 0)) AS level2,
               SUM(DECODE(LINKLEVEL, '3', 1, 0)) AS level3,
               SUM(DECODE(LINKLEVEL, '4', 1, 0)) AS level4
        FROM LINK
        WHERE LINKLEVEL IN ('1', '2', '3', '4')
        ]]>
    </select>

    <select id="findLinkAll" parameterType="java.lang.String" resultType="com.utic.center.utic.traf.server.dto.LinkDto" fetchSize="2000">
        <![CDATA[
        SELECT A.LINKID                     AS linkId,
               DECODE(A.ROADUSE, '0', 1, 0) AS roadUse,
               DECODE(A.ROADRANK, '101', 0,
                      '102', 1,
                      '103', 2,
                      '104', 3,
                      '105', 4,
                      '106', 5,
                      '107', 6,
                      '108', 7,
                      8) AS roadRank,
               NVL(A.LINKLEVEL, 0)          AS linkLevel,
               NVL(A.LENGTH,   10)          AS length,
               NVL(A.LANES,     1)          AS lanes,
               NVL(A.MAXSPEED, 100)         AS maxSpeed,
               5                            AS minSpeed
        FROM LINK A
        WHERE LINKLEVEL = #{linkLevel}
--      WHERE LINKLEVEL IN ('1', '2', '3', '4')
        ]]>
    </select>

    <resultMap id="result-logicalLink" type="com.utic.center.utic.traf.server.dto.LogicalLinkDto">
        <id column="logicalLinkId" property="logicalLinkId"/>
        <collection property="linkIds" ofType="java.lang.String" javaType="java.util.ArrayList">
            <result column="linkId"/>
        </collection>
    </resultMap>

    <select id="findLogicalLinkAll" parameterType="java.lang.String" resultMap="result-logicalLink" fetchSize="2000">
        <![CDATA[
        SELECT LOL.LOGICALLINKID AS logicalLinkId, LOL.SEQUENCE AS linkSeq, LOL.LINKID AS linkId
        FROM LOGICALLINK LOL,
             (SELECT LINKID AS LOGICALLINKID, LINKLEVEL
              FROM LINK
              WHERE LINKLEVEL = #{linkLevel}) LVL,
             (SELECT LINKID
              FROM LINK
              WHERE LINKLEVEL = '1') LNK
        WHERE LOL.LINKLEVEL = '1'
          AND LOL.LOGICALLINKID = LVL.LOGICALLINKID
          AND LOL.LINKID = LNK.LINKID
        ORDER BY LOL.LOGICALLINKID, LOL.SEQUENCE
        ]]>
    </select>

</mapper>
