<?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="locationId"    column="location_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 location.location_id, location.user_id, location.longitude, location.latitude, location.create_time,(SELECT sysU.user_name FROM sys_user sysU WHERE sysU.user_id = location.user_id) AS userName from t_user_location 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 = #{longitude}</if>
            <if test="latitude != null "> and latitude = #{latitude}</if>
        </where>
    </select>

    <select id="selectTUserLocationListByMap" parameterType="java.util.Map" resultMap="TUserLocationResult">
        <include refid="selectTUserLocationVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="longitude != null "> and longitude = #{longitude}</if>
            <if test="latitude != null "> and latitude = #{latitude}</if>
            <if test="beginTime != null and endTime != null"> and create_time BETWEEN #{beginTime} AND #{endTime}</if>
        </where>
        ORDER BY create_time DESC
        <if test="initInspectors == 'initInspectors'">
            LIMIT 1
        </if>

    </select>
    
    <select id="selectTUserLocationById" parameterType="Long" resultMap="TUserLocationResult">
        <include refid="selectTUserLocationVo"/>
        where location_id = #{locationId}
    </select>
        
    <insert id="insertTUserLocation" parameterType="TUserLocation" useGeneratedKeys="true" keyProperty="locationId">
        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 location_id = #{locationId}
    </update>

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

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