<?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.ruoyi.ss.locationLog.mapper.LocationLogMapper">

    <resultMap type="LocationLog" id="EtLocationLogResult">
        <result property="locationId"    column="location_id"    />
        <result property="mac"    column="mac"    />
        <result property="onenetMsg"    column="onenet_msg"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="createTime"    column="create_time"    />
        <result property="at"    column="at"    />
        <result property="status"    column="status"    />
        <result property="lockStatus"    column="lock_status"    />
    </resultMap>

    <sql id="selectEtLocationLogVo">
        select location_id, mac, onenet_msg, longitude, latitude, create_time, at, status, lock_status from e_location_log
    </sql>

    <sql id="selectEtLocationLogVoNoMsg">
        select location_id, mac, longitude, latitude, create_time, at, status, lock_status from e_location_log
    </sql>

    <select id="selectEtLocationLogList" parameterType="LocationLog" resultMap="EtLocationLogResult">
        <include refid="selectEtLocationLogVo"/>
        <where>
            <if test="mac != null  and mac != ''"> and mac = #{mac}</if>
            <if test="onenetMsg != null  and onenetMsg != ''"> and onenet_msg = #{onenetMsg}</if>
            <if test="longitude != null  and longitude != ''"> and longitude = #{longitude}</if>
            <if test="latitude != null  and latitude != ''"> and latitude = #{latitude}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="lockStatus != null  and lockStatus != ''"> and lock_status = #{lockStatus}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectEtLocationLogListByCreateTime" parameterType="LocationLog" resultMap="EtLocationLogResult">
        <include refid="selectEtLocationLogVoNoMsg"/>
        where  longitude != '0E-8' and latitude != '0E-8'
            <if test="mac != null  and mac != ''"> and mac = #{mac}</if>
            <if test="startTime != null  and startTime != ''">
                AND date_format(`AT`,'%Y%m%d%H%i%s') &gt;= date_format(#{startTime},'%Y%m%d%H%i%s')
            </if>
            <if test="endTime != null  and endTime != ''">
                AND date_format(`AT`,'%Y%m%d%H%i%s') &lt;= date_format(#{endTime},'%Y%m%d%H%i%s')
            </if>
        order by `AT`
    </select>

    <select id="selectEtLocationLogByLocationId" parameterType="Long" resultMap="EtLocationLogResult">
        <include refid="selectEtLocationLogVo"/>
        where location_id = #{locationId}
    </select>

    <select id="getLastMsg" resultType="java.lang.String">
        SELECT onenet_msg
        FROM e_location_log
        WHERE mac = #{mac}
        ORDER BY create_time DESC
            LIMIT 1
    </select>

    <insert id="insertEtLocationLog" parameterType="LocationLog" useGeneratedKeys="true" keyProperty="locationId">
        insert into e_location_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mac != null">mac,</if>
            <if test="onenetMsg != null">onenet_msg,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="createTime != null">create_time,</if>
            <if test="at != null">at,</if>
            <if test="status != null">status,</if>
            <if test="lockStatus != null">lock_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mac != null">#{mac},</if>
            <if test="onenetMsg != null">#{onenetMsg},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="at != null">#{at},</if>
            <if test="status != null">#{status},</if>
            <if test="lockStatus != null">#{lockStatus},</if>
         </trim>
    </insert>

    <update id="updateEtLocationLog" parameterType="LocationLog">
        update e_location_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="mac != null">mac = #{mac},</if>
            <if test="onenetMsg != null">onenet_msg = #{onenetMsg},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="at != null">at = #{at},</if>
            <if test="status != null">status = #{status},</if>
            <if test="lockStatus != null">lock_status = #{lockStatus},</if>
        </trim>
        where location_id = #{locationId}
    </update>

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

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

    <delete id="deleteLocationLogByCreateTime">
        delete from e_location_log
        where create_time &lt;= now() - INTERVAL 7 DAY
    </delete>

</mapper>
