<?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="cn.edu.whu.boot.allocation.mapper.RouteLineMapper">

    <resultMap id="routeLineResultMap" type="cn.edu.whu.boot.allocation.entities.RouteLine">
        <id column="id" property="id"/>
        <result column="collector" property="name"/>
        <result column="route" property="polyline"/>
        <result column="begin_time" property="begindate"/>
        <result column="stop_time" property="stopdate"/>
    </resultMap>

    <insert id="insert">
        INSERT INTO ${databaseName}."route_line"(collector_id, collector, route, begin_time, stop_time)
        VALUES(#{routeLine.collectorId}, #{routeLine.name}, #{routeLine.polyline}, to_timestamp(#{routeLine.begindate},'YYYY-MM-DD HH24:MI:SS'),
        to_timestamp(#{routeLine.stopdate},'YYYY-MM-DD HH24:MI:SS'))
    </insert>

    <select id="findByName" resultMap="routeLineResultMap">
        SELECT * FROM ${databaseName}."route_line" WHERE collector = #{name}
    </select>

    <select id="findByNameAndTime" resultMap="routeLineResultMap">
        SELECT * FROM ${databaseName}."route_line"
        WHERE collector = #{name} AND ( (to_timestamp(#{stopTime},'YYYY-MM-DD HH24:MI:SS') BETWEEN begin_time AND stop_time)
        OR (stop_time BETWEEN to_timestamp(#{beginTime},'YYYY-MM-DD HH24:MI:SS') AND to_timestamp(#{stopTime},'YYYY-MM-DD HH24:MI:SS')))
    </select>
</mapper>