<?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="ink.wgink.module.map.dao.userlocation.IUserLocationDao">

    <cache/>

    <resultMap id="userLocationDTO" type="ink.wgink.module.map.pojo.dtos.userlocation.UserLocationDTO">
        <result column="user_location_id" property="userLocationId"/>
        <result column="user_lng" property="userLng"/>
        <result column="user_lat" property="userLat"/>
        <result column="is_overstep" property="isOverstep"/>
        <result column="user_username" property="userUsername"/>
        <result column="user_name" property="userName"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <resultMap id="userLocationBO" type="ink.wgink.module.map.pojo.bos.userlocation.UserLocationBO">
        <result column="user_location_id" property="userLocationId"/>
        <result column="user_lng" property="userLng"/>
        <result column="user_lat" property="userLat"/>
        <result column="is_overstep" property="isOverstep"/>
        <result column="user_username" property="userUsername"/>
        <result column="user_name" property="userName"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <resultMap id="userLocationPO" type="ink.wgink.module.map.pojo.pos.userlocation.UserLocationPO">
        <result column="user_location_id" property="userLocationId"/>
        <result column="user_lng" property="userLng"/>
        <result column="user_lat" property="userLat"/>
        <result column="is_overstep" property="isOverstep"/>
        <result column="user_username" property="userUsername"/>
        <result column="user_name" property="userName"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable" parameterType="map">
        CREATE TABLE IF NOT EXISTS `map_user_location` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `user_location_id` char(36) DEFAULT NULL COMMENT '主键',
            `user_lng` varchar(255) DEFAULT NULL COMMENT '用户经度',
            `user_lat` varchar(255) DEFAULT NULL COMMENT '用户精度',
            `is_overstep` int(1) DEFAULT '0' COMMENT '是否越界',
            `user_username` varchar(255) DEFAULT NULL COMMENT '用户名',
            `user_name` varchar(255) DEFAULT NULL COMMENT '昵称',
            `creator` char(36) DEFAULT NULL COMMENT '创建人',
            `gmt_create` datetime DEFAULT NULL COMMENT '创建时间',
            `is_delete` int(1) DEFAULT '0' COMMENT '是否删除',
            PRIMARY KEY (`id`) USING BTREE,
            UNIQUE KEY `user_location_id` (`user_location_id`) USING BTREE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户定位';
    </update>

    <!-- 新增用户定位 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO map_user_location(
            user_location_id,
            user_lng,
            user_lat,
            is_overstep,
            creator,
            user_username,
            user_name,
            gmt_create,
            is_delete
        ) VALUES(
            #{userLocationId},
            #{userLng},
            #{userLat},
            #{isOverstep},
            #{creator},
            #{userUsername},
            #{userName},
            #{gmtCreate},
            #{isDelete}
        )
    </insert>

    <!-- 删除用户定位 -->
    <update id="remove" parameterType="map" flushCache="true">
        UPDATE
            map_user_location
        SET
            is_delete = 1
        WHERE
            user-location_id IN
        <foreach collection="userLocationIds" index="index" open="(" separator="," close=")">
            #{userLocationIds[${index}]}
        </foreach>
    </update>

    <!-- 删除用户定位（物理） -->
    <update id="delete" parameterType="map" flushCache="true">
        DELETE FROM
            map_user_location
        WHERE
            user_location_id IN
        <foreach collection="userLocationIds" index="index" open="(" separator="," close=")">
            #{userLocationIds[${index}]}
        </foreach>
    </update>

    <!-- 修改用户定位 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            map_user_location
        SET
        <if test="userLng != null and userLng != ''">
            user_lng = #{userLng},
        </if>
        <if test="userLat != null and userLat != ''">
            user_lat = #{userLat},
        </if>
        <if test="isOverstep != null">
            is_overstep = #{isOverstep},
        </if>
            user_location_id = user_location_id
        WHERE
            user_location_id = #{userLocationId}
    </update>

    <!-- 用户定位详情 -->
    <select id="get" parameterType="map" resultMap="userLocationDTO" useCache="false">
        SELECT
            t1.user_lng,
            t1.user_lat,
            t1.is_overstep,
            t1.creator,
            t1.user_username,
            t1.user_name,
            t1.user_location_id
        FROM
            map_user_location t1
        WHERE
            t1.is_delete = 0
            <if test="userLocationId != null and userLocationId != ''">
        AND
            t1.user_location_id = #{userLocationId}
            </if>
    </select>

    <!-- 用户定位详情 -->
    <select id="getBO" parameterType="map" resultMap="userLocationBO" useCache="false">
        SELECT
            t1.user_location_id,
            t1.user_lng,
            t1.user_lat,
            t1.is_overstep,
            t1.creator,
            t1.user_username,
            t1.user_name,
            t1.gmt_create,
            t1.is_delete
        FROM
            map_user_location t1
        WHERE
            t1.is_delete = 0
            <if test="userLocationId != null and userLocationId != ''">
        AND
            t1.user_location_id = #{userLocationId}
            </if>
    </select>

    <!-- 用户定位详情 -->
    <select id="getPO" parameterType="map" resultMap="userLocationPO" useCache="false">
        SELECT
            t1.user_location_id,
            t1.user_lng,
            t1.user_lat,
            t1.is_overstep,
            t1.creator,
            t1.user_username,
            t1.user_name,
            t1.gmt_create,
            t1.is_delete
        FROM
            map_user_location t1
        WHERE
            t1.is_delete = 0
            <if test="userLocationId != null and userLocationId != ''">
        AND
            t1.user_location_id = #{userLocationId}
            </if>
    </select>

    <!-- 用户定位列表 -->
    <select id="list" parameterType="map" resultMap="userLocationDTO" useCache="true">
        SELECT
            t1.user_location_id,
            t1.user_lng,
            t1.user_lat,
            t1.is_overstep,
            t1.creator,
            t1.user_username,
            t1.user_name,
            t1.gmt_create
        FROM
            map_user_location t1
        WHERE
            t1.is_delete = 0
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="userId != null and userId != ''">
        AND
            t1.creator = #{userId}
        </if>
        <if test="userIds != null and userIds.size > 0">
        AND
            t1.creator IN
            <foreach collection="userIds" index="index" open="(" separator="," close=")">
                #{userIds[${index}]}
            </foreach>
        </if>
        <if test="userLocationIds != null and userLocationIds.size > 0">
        AND
            t1.user_location_id IN
            <foreach collection="userLocationIds" index="index" open="(" separator="," close=")">
                #{userLocationIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 用户定位列表 -->
    <select id="listBO" parameterType="map" resultMap="userLocationBO" useCache="true">
        SELECT
            t1.user_location_id,
            t1.user_lng,
            t1.user_lat,
            t1.is_overstep,
            t1.creator,
            t1.user_username,
            t1.user_name,
            t1.gmt_create,
            t1.is_delete
        FROM
            map_user_location t1
        WHERE
            t1.is_delete = 0
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="userId != null and userId != ''">
        AND
            t1.creator = #{userId}
        </if>
        <if test="userIds != null and userIds.size > 0">
        AND
            t1.creator IN
            <foreach collection="userIds" index="index" open="(" separator="," close=")">
                #{userIds[${index}]}
            </foreach>
        </if>
        <if test="userLocationIds != null and userLocationIds.size > 0">
        AND
            t1.user_location_id IN
            <foreach collection="userLocationIds" index="index" open="(" separator="," close=")">
                #{userLocationIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 用户定位列表 -->
    <select id="listPO" parameterType="map" resultMap="userLocationPO" useCache="true">
        SELECT
            t1.user_location_id,
            t1.user_lng,
            t1.user_lat,
            t1.is_overstep,
            t1.creator,
            t1.user_username,
            t1.user_name,
            t1.gmt_create,
            t1.is_delete
        FROM
            map_user_location t1
        WHERE
            t1.is_delete = 0
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="userId != null and userId != ''">
        AND
            t1.creator = #{userId}
        </if>
        <if test="userIds != null and userIds.size > 0">
        AND
            t1.creator IN
            <foreach collection="userIds" index="index" open="(" separator="," close=")">
                #{userIds[${index}]}
            </foreach>
        </if>
        <if test="userLocationIds != null and userLocationIds.size > 0">
        AND
            t1.user_location_id IN
            <foreach collection="userLocationIds" index="index" open="(" separator="," close=")">
                #{userLocationIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 用户定位统计  -->
    <select id="count" parameterType="map" resultType="Integer" useCache="true">
        SELECT
            COUNT(*)
        FROM
            map_user_location t1
        WHERE
            t1.is_delete = 0
    </select>

</mapper>