<?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.zehong.system.mapper.TUserLocationMapper">
    
    <resultMap type="TUserLocation" id="TUserLocationResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectTUserLocationVo">
        select id, user_id, longitude, latitude, create_time from t_user_location
    </sql>

    <select id="selectTUserLocationList" parameterType="TUserLocation" resultMap="TUserLocationResult">
        <include refid="selectTUserLocationVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="longitude != null  and longitude != ''"> and longitude = #{longitude}</if>
            <if test="latitude != null  and latitude != ''"> and latitude = #{latitude}</if>
            <if test="beginTime != null  and endTime == null"> and create_time > #{beginTime}</if>
            <if test="beginTime != null  and endTime != null"> and create_time BETWEEN #{beginTime} and #{endTime}</if>
        </where>
        order by create_time
    </select>

    <select id="selectTUserLocationListByGroup" parameterType="TUserLocation" resultMap="TUserLocationResult">
        SELECT
            id,
            user_id,
            longitude,
            latitude,
            max(create_time)
        FROM
            t_user_location
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="longitude != null  and longitude != ''"> and longitude = #{longitude}</if>
            <if test="latitude != null  and latitude != ''"> and latitude = #{latitude}</if>
            <if test="beginTime != null  and endTime == null"> and create_time > #{beginTime}</if>
            <if test="beginTime != null  and endTime != null"> and create_time BETWEEN #{beginTime} and #{endTime}</if>
        </where>
        GROUP BY latitude,longitude
        ORDER BY create_time asc
    </select>
    
    <select id="selectTUserLocationById" parameterType="Long" resultMap="TUserLocationResult">
        <include refid="selectTUserLocationVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTUserLocation" parameterType="TUserLocation" useGeneratedKeys="true" keyProperty="id">
        insert into t_user_location
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateTUserLocation" parameterType="TUserLocation">
        update t_user_location
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTUserLocationById" parameterType="Long">
        delete from t_user_location where id = #{id}
    </delete>

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