<?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.songhua.system.mapper.CruiseShipMappingMapper">
    
    <resultMap type="CruiseShipMapping" id="CruiseShipMappingResult">
        <result property="id"    column="id"    />
        <result property="shipId"    column="ship_id"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="shipTime"    column="ship_time"    />
    </resultMap>

    <sql id="selectCruiseShipMappingVo">
        select id, ship_id, longitude, latitude, ship_time from cruise_ship_mapping
    </sql>

    <select id="selectCruiseShipMappingList" parameterType="CruiseShipMapping" resultMap="CruiseShipMappingResult">
        SELECT
        c.id,
        c.ship_id,
        b.ship_name,
        c.longitude,
        c.latitude,
        c.ship_time
        FROM
        cruise_ship_mapping c
        left join basic_cruise_ship b ON c.ship_id = b.id
        <where>  
            <if test="shipId != null  and shipId != ''"> and ship_id = #{shipId}</if>
            <if test="longitude != null  and longitude != ''"> and longitude = #{longitude}</if>
            <if test="latitude != null  and latitude != ''"> and latitude = #{latitude}</if>
            <if test="shipTime != null "> and ship_time = #{shipTime}</if>
        </where>
        ORDER BY ship_time DESC
    </select>
    
    <select id="selectCruiseShipMappingById" parameterType="String" resultMap="CruiseShipMappingResult">
        <include refid="selectCruiseShipMappingVo"/>
        where id = #{id}
    </select>

        
    <insert id="insertCruiseShipMapping" parameterType="CruiseShipMapping">
        insert into cruise_ship_mapping
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="shipId != null">ship_id,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="shipTime != null">ship_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="shipId != null">#{shipId},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="shipTime != null">#{shipTime},</if>
         </trim>
    </insert>

    <update id="updateCruiseShipMapping" parameterType="CruiseShipMapping">
        update cruise_ship_mapping
        <trim prefix="SET" suffixOverrides=",">
            <if test="shipId != null">ship_id = #{shipId},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="shipTime != null">ship_time = #{shipTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCruiseShipMappingById" parameterType="String">
        delete from cruise_ship_mapping where id = #{id}
    </delete>

    <delete id="deleteCruiseShipMappingByIds" parameterType="String">
        delete from cruise_ship_mapping where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="getShipRouteData" parameterType="CruiseShipMapping" resultMap="CruiseShipMappingResult">
        SELECT b.ship_name shipName,
               GROUP_CONCAT(a.longitude) longitude,
               GROUP_CONCAT(a.latitude)  latitude
        FROM basic_cruise_ship b
                 left join cruise_ship_mapping a on b.id = a.ship_id
        GROUP BY b.ship_name
        ORDER BY a.ship_time DESC
    </select>
</mapper>