<?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.fastbee.iot.mapper.UploadedPhotosMapper">
    
    <resultMap type="com.fastbee.iot.model.anfang.UploadedPhotos" id="UploadedPhotosResult">
        <result property="id"    column="id"    />
        <result property="photoPath"    column="photo_path"    />
        <result property="monitorPath"    column="monitor_path"    />
        <result property="imei"    column="imei"    />
        <result property="sn"    column="sn"    />
        <result property="lat"    column="lat"    />
        <result property="lng"    column="lng"    />
        <result property="temp"    column="temp"    />
        <result property="doorState"    column="door_state"    />
        <result property="shakeState"    column="shake_state"    />
        <result property="uploadTime"    column="upload_time"    />
        <result property="eventType"    column="event_type"    />
        <result property="cpuTemp"    column="cpu_temp"    />
        <result property="anFangStatus"    column="an_fang_status"    />
    </resultMap>

    <sql id="selectUploadedPhotosVo">
        select * from uploaded_photos
    </sql>

    <select id="selectUploadedPhotosList" parameterType="com.fastbee.iot.model.anfang.UploadedPhotos"
            resultMap="UploadedPhotosResult">
        <include refid="selectUploadedPhotosVo"/>
        <where>  
            <if test="photoPath != null  and photoPath != ''"> and photo_path = #{photoPath}</if>
            <if test="imei != null  and imei != ''"> and imei = #{imei}</if>
            <if test="sn != null  and sn != ''"> and sn like concat('%', #{sn}, '%')</if>
            <if test="lat != null "> and lat = #{lat}</if>
            <if test="lng != null "> and lng = #{lng}</if>
            <if test="params.beginTemp != null and params.beginTemp != '' and params.endTemp != null and params.endTemp != ''"> and temp between #{params.beginTemp} and #{params.endTemp}</if>
            <if test="doorState != null  and doorState != ''"> and door_state = #{doorState}</if>
            <if test="eventType != null  and eventType != ''"> and event_type = #{eventType}</if>
            <if test="cpuTemp != null  and cpuTemp != ''"> and cpu_temp = #{cpuTemp}</if>
            <if test="shakeState != null  and shakeState != ''"> and shake_state = #{shakeState}</if>
            <if test="anFangStatus != null  and anFangStatus != ''"> and an_fang_status = #{anFangStatus}</if>
            <if test="params.beginUploadTime != null and params.beginUploadTime != '' and params.endUploadTime != null and params.endUploadTime != ''"> and upload_time between #{params.beginUploadTime} and #{params.endUploadTime}</if>
        </where>
		order by id desc
    </select>
    
    <select id="selectUploadedPhotosById" parameterType="Long" resultMap="UploadedPhotosResult">
        <include refid="selectUploadedPhotosVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertUploadedPhotos" parameterType="com.fastbee.iot.model.anfang.UploadedPhotos" useGeneratedKeys="true" keyProperty="id">
        insert into uploaded_photos
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="photoPath != null and photoPath != ''">photo_path,</if>
            <if test="monitorPath != null and monitorPath != ''">monitor_path,</if>
            <if test="imei != null and imei != ''">imei,</if>
            <if test="sn != null">sn,</if>
            <if test="lat != null">lat,</if>
            <if test="lng != null">lng,</if>
            <if test="temp != null">temp,</if>
            <if test="doorState != null">door_state,</if>
            <if test="shakeState != null">shake_state,</if>
            <if test="uploadTime != null">upload_time,</if>
            <if test="eventType != null">event_type,</if>
            <if test="cpuTemp != null">cpu_temp,</if>
            <if test="anFangStatus != null">an_fang_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="photoPath != null and photoPath != ''">#{photoPath},</if>
            <if test="monitorPath != null and monitorPath != ''">#{monitorPath},</if>
            <if test="imei != null and imei != ''">#{imei},</if>
            <if test="sn != null">#{sn},</if>
            <if test="lat != null">#{lat},</if>
            <if test="lng != null">#{lng},</if>
            <if test="temp != null">#{temp},</if>
            <if test="doorState != null">#{doorState},</if>
            <if test="shakeState != null">#{shakeState},</if>
            <if test="uploadTime != null">#{uploadTime},</if>
            <if test="eventType != null">#{eventType},</if>
            <if test="cpuTemp != null">#{cpuTemp},</if>
            <if test="anFangStatus != null">#{anFangStatus},</if>
         </trim>
    </insert>

    <update id="updateUploadedPhotos" parameterType="com.fastbee.iot.model.anfang.UploadedPhotos">
        update uploaded_photos
        <trim prefix="SET" suffixOverrides=",">
            <if test="photoPath != null and photoPath != ''">photo_path = #{photoPath},</if>
            <if test="monitorPath != null and monitorPath != ''">monitor_path = #{monitorPath},</if>
            <if test="imei != null and imei != ''">imei = #{imei},</if>
            <if test="sn != null">sn = #{sn},</if>
            <if test="lat != null">lat = #{lat},</if>
            <if test="lng != null">lng = #{lng},</if>
            <if test="temp != null">temp = #{temp},</if>
            <if test="doorState != null">door_state = #{doorState},</if>
            <if test="shakeState != null">shake_state = #{shakeState},</if>
            <if test="uploadTime != null">upload_time = #{uploadTime},</if>
            <if test="eventType != null">event_type = #{eventType},</if>
            <if test="cpuTemp != null">cpu_temp = #{cpuTemp},</if>
            <if test="anFangStatus != null">an_fang_status = #{anFangStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateBySnAndTime" parameterType="com.fastbee.iot.model.anfang.UploadedPhotos">
        update uploaded_photos
        <trim prefix="SET" suffixOverrides=",">
            <if test="photoPath != null and photoPath != ''">photo_path = #{photoPath},</if>
            <if test="monitorPath != null and monitorPath != ''">monitor_path = #{monitorPath},</if>
            <if test="imei != null and imei != ''">imei = #{imei},</if>
            <if test="sn != null">sn = #{sn},</if>
            <if test="lat != null">lat = #{lat},</if>
            <if test="lng != null">lng = #{lng},</if>
            <if test="temp != null">temp = #{temp},</if>
            <if test="doorState != null">door_state = #{doorState},</if>
            <if test="shakeState != null">shake_state = #{shakeState},</if>
            <if test="uploadTime != null">upload_time = #{uploadTime},</if>
            <if test="eventType != null">event_type = #{eventType},</if>
            <if test="cpuTemp != null">cpu_temp = #{cpuTemp},</if>
            <if test="anFangStatus != null">an_fang_status = #{anFangStatus},</if>
        </trim>
        where sn = #{sn} and upload_time = #{uploadTime}
    </update>

    <delete id="deleteUploadedPhotosById" parameterType="Long">
        delete from uploaded_photos where id = #{id}
    </delete>

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