<?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.deviceData.mapper.DeviceRealtimedataWormsMapper">

    <resultMap type="DeviceRealtimedataWorms" id="DeviceRealtimedataWormsResult">
        <result property="id"    column="id"    />
        <result property="deviceAddr"    column="device_addr"    />
        <result property="rain"    column="rain"    />
        <result property="wormFlap"    column="worm_flap"    />
        <result property="insecticideTem"    column="insecticide_tem"    />
        <result property="shake"    column="shake"    />
        <result property="lng"    column="lng"    />
        <result property="dryingFlap"    column="drying_flap"    />
        <result property="insecticide"    column="insecticide"    />
        <result property="moveWorm"    column="move_worm"    />
        <result property="mode"    column="mode"    />
        <result property="drying"    column="drying"    />
        <result property="rainFlap"    column="rain_flap"    />
        <result property="attractWorm"    column="attract_worm"    />
        <result property="illum"    column="illum"    />
        <result property="dryingTem"    column="drying_tem"    />
        <result property="lat"    column="lat"    />
        <result property="fillLight"    column="fill_light"    />
        <result property="status"    column="status"    />
        <result property="saveTime"    column="save_time"    />
        <result property="realTime"    column="real_time"    />
        <result property="camera"    column="camera"    />
        <result property="pestPhotos"    column="pest_photos"    />
    </resultMap>

    <sql id="selectDeviceRealtimedataWormsVo">
        select id, device_addr, rain, worm_flap, insecticide_tem, shake, lng, drying_flap, insecticide, move_worm, mode, drying, rain_flap, attract_worm, illum, drying_tem, lat, fill_light, status, save_time, real_time, camera, pest_photos from iot_device_realtimedata_worms
    </sql>

    <select id="selectDeviceRealtimedataWormsList" parameterType="DeviceRealtimedataWorms" resultMap="DeviceRealtimedataWormsResult">
        <include refid="selectDeviceRealtimedataWormsVo"/>
        <where>
            <if test="deviceAddr != null  and deviceAddr != ''"> and device_addr = #{deviceAddr}</if>
            <if test="rain != null  and rain != ''"> and rain = #{rain}</if>
            <if test="wormFlap != null  and wormFlap != ''"> and worm_flap = #{wormFlap}</if>
            <if test="insecticideTem != null  and insecticideTem != ''"> and insecticide_tem = #{insecticideTem}</if>
            <if test="shake != null  and shake != ''"> and shake = #{shake}</if>
            <if test="lng != null  and lng != ''"> and lng = #{lng}</if>
            <if test="dryingFlap != null  and dryingFlap != ''"> and drying_flap = #{dryingFlap}</if>
            <if test="insecticide != null  and insecticide != ''"> and insecticide = #{insecticide}</if>
            <if test="moveWorm != null  and moveWorm != ''"> and move_worm = #{moveWorm}</if>
            <if test="mode != null  and mode != ''"> and mode = #{mode}</if>
            <if test="drying != null  and drying != ''"> and drying = #{drying}</if>
            <if test="rainFlap != null  and rainFlap != ''"> and rain_flap = #{rainFlap}</if>
            <if test="attractWorm != null  and attractWorm != ''"> and attract_worm = #{attractWorm}</if>
            <if test="illum != null  and illum != ''"> and illum = #{illum}</if>
            <if test="dryingTem != null  and dryingTem != ''"> and drying_tem = #{dryingTem}</if>
            <if test="lat != null  and lat != ''"> and lat = #{lat}</if>
            <if test="fillLight != null  and fillLight != ''"> and fill_light = #{fillLight}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="saveTime != null "> and save_time = #{saveTime}</if>
            <if test="realTime != null "> and real_time = #{realTime}</if>
            <if test="camera != null  and camera != ''"> and camera = #{camera}</if>
            <if test="pestPhotos != null  and pestPhotos != ''"> and pest_photos = #{pestPhotos}</if>
        </where>
    </select>

    <select id="selectDeviceRealtimedataWormsById" parameterType="Long" resultMap="DeviceRealtimedataWormsResult">
        <include refid="selectDeviceRealtimedataWormsVo"/>
        where id = #{id}
    </select>

    <insert id="insertDeviceRealtimedataWorms" parameterType="DeviceRealtimedataWorms" useGeneratedKeys="true" keyProperty="id">
        insert into iot_device_realtimedata_worms
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceAddr != null">device_addr,</if>
            <if test="rain != null">rain,</if>
            <if test="wormFlap != null">worm_flap,</if>
            <if test="insecticideTem != null">insecticide_tem,</if>
            <if test="shake != null">shake,</if>
            <if test="lng != null">lng,</if>
            <if test="dryingFlap != null">drying_flap,</if>
            <if test="insecticide != null">insecticide,</if>
            <if test="moveWorm != null">move_worm,</if>
            <if test="mode != null">mode,</if>
            <if test="drying != null">drying,</if>
            <if test="rainFlap != null">rain_flap,</if>
            <if test="attractWorm != null">attract_worm,</if>
            <if test="illum != null">illum,</if>
            <if test="dryingTem != null">drying_tem,</if>
            <if test="lat != null">lat,</if>
            <if test="fillLight != null">fill_light,</if>
            <if test="status != null">status,</if>
            <if test="saveTime != null">save_time,</if>
            <if test="realTime != null">real_time,</if>
            <if test="camera != null">camera,</if>
            <if test="pestPhotos != null">pest_photos,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceAddr != null">#{deviceAddr},</if>
            <if test="rain != null">#{rain},</if>
            <if test="wormFlap != null">#{wormFlap},</if>
            <if test="insecticideTem != null">#{insecticideTem},</if>
            <if test="shake != null">#{shake},</if>
            <if test="lng != null">#{lng},</if>
            <if test="dryingFlap != null">#{dryingFlap},</if>
            <if test="insecticide != null">#{insecticide},</if>
            <if test="moveWorm != null">#{moveWorm},</if>
            <if test="mode != null">#{mode},</if>
            <if test="drying != null">#{drying},</if>
            <if test="rainFlap != null">#{rainFlap},</if>
            <if test="attractWorm != null">#{attractWorm},</if>
            <if test="illum != null">#{illum},</if>
            <if test="dryingTem != null">#{dryingTem},</if>
            <if test="lat != null">#{lat},</if>
            <if test="fillLight != null">#{fillLight},</if>
            <if test="status != null">#{status},</if>
            <if test="saveTime != null">#{saveTime},</if>
            <if test="realTime != null">#{realTime},</if>
            <if test="camera != null">#{camera},</if>
            <if test="pestPhotos != null">#{pestPhotos},</if>
        </trim>
    </insert>

    <update id="updateDeviceRealtimedataWorms" parameterType="DeviceRealtimedataWorms">
        update iot_device_realtimedata_worms
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceAddr != null">device_addr = #{deviceAddr},</if>
            <if test="rain != null">rain = #{rain},</if>
            <if test="wormFlap != null">worm_flap = #{wormFlap},</if>
            <if test="insecticideTem != null">insecticide_tem = #{insecticideTem},</if>
            <if test="shake != null">shake = #{shake},</if>
            <if test="lng != null">lng = #{lng},</if>
            <if test="dryingFlap != null">drying_flap = #{dryingFlap},</if>
            <if test="insecticide != null">insecticide = #{insecticide},</if>
            <if test="moveWorm != null">move_worm = #{moveWorm},</if>
            <if test="mode != null">mode = #{mode},</if>
            <if test="drying != null">drying = #{drying},</if>
            <if test="rainFlap != null">rain_flap = #{rainFlap},</if>
            <if test="attractWorm != null">attract_worm = #{attractWorm},</if>
            <if test="illum != null">illum = #{illum},</if>
            <if test="dryingTem != null">drying_tem = #{dryingTem},</if>
            <if test="lat != null">lat = #{lat},</if>
            <if test="fillLight != null">fill_light = #{fillLight},</if>
            <if test="status != null">status = #{status},</if>
            <if test="saveTime != null">save_time = #{saveTime},</if>
            <if test="realTime != null">real_time = #{realTime},</if>
            <if test="camera != null">camera = #{camera},</if>
            <if test="pestPhotos != null">pest_photos = #{pestPhotos},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDeviceRealtimedataWormsById" parameterType="Long">
        delete from iot_device_realtimedata_worms where id = #{id}
    </delete>

    <delete id="deleteDeviceRealtimedataWormsByIds" parameterType="String">
        delete from iot_device_realtimedata_worms where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="getWormsRealtimedata" resultType="DeviceRealtimedataWorms">
        SELECT t1.*
        FROM iot_device_realtimedata_worms t1
                 JOIN (
            SELECT device_addr, MAX(save_time) AS max_save_time
            FROM iot_device_realtimedata_worms
            GROUP BY device_addr
        ) t2 ON t1.device_addr = t2.device_addr AND t1.save_time = t2.max_save_time
    </select>
</mapper>