<?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.inspect.metadata.mqtt.mapper.HrNestMapper">

    <resultMap id="HrNestResult" type="com.inspect.metadata.mqtt.domain.HrNest">
        <id property="nestId" column="nest_id"/>
        <result property="nestCode" column="nest_code"/>
        <result property="nestStateConstant" column="nest_state_constant"/>
        <result property="droneStateConstant" column="drone_state_constant"/>
        <result property="isDroneConnected" column="is_drone_connected"/>
        <result property="isRemoteConnected" column="is_remote_connected"/>
        <result property="nestBatteryAvailable" column="nest_battery_available"/>
        <result property="chargingState" column="charging_state"/>
        <result property="nestPower" column="nest_power"/>
        <result property="nestEasyLockStus" column="nest_easy_lock_stus"/>
        <result property="nestFireWarn" column="nest_fire_warn"/>
        <result property="nestAutoCheck" column="nest_auto_check"/>
        <result property="nestAutoCheckErr" column="nest_auto_check_err"/>
        <result property="nestPowerMode" column="nest_power_mode"/>
        <result property="nestFireBombNum" column="nest_fire_bomb_num"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectHrNestVo">
        select nest_id,
               nest_code,
               nest_state_constant,
               drone_state_constant,
               is_drone_connected,
               is_remote_connected,
               nest_battery_available,
               charging_state,
               nest_power,
               nest_easy_lock_stus,
               nest_fire_warn,
               nest_auto_check,
               nest_auto_check_err,
               nest_power_mode,
               nest_fire_bomb_num,
               create_time,
               update_time
        from basedata_hr_nest
    </sql>

    <select id="selectNestList" parameterType="com.inspect.metadata.mqtt.domain.HrNest"
            resultMap="HrNestResult">
        <include refid="selectHrNestVo"/>
        <where>
            <if test="nestCode != null ">and nest_code = #{nestCode}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectNestByCode" parameterType="com.inspect.metadata.mqtt.domain.HrNest"
            resultMap="HrNestResult">
        <include refid="selectHrNestVo"/>
        WHERE nest_code = #{nestCode}
        ORDER BY create_time DESC
        LIMIT 1
    </select>

    <insert id="batchInsert" parameterType="java.util.List">
        insert into basedata_hr_nest (nest_code, nest_state_constant, drone_state_constant, is_drone_connected,
        is_remote_connected, nest_battery_available, charging_state, nest_power, nest_easy_lock_stus, nest_fire_warn,
        nest_auto_check, nest_auto_check_err, nest_power_mode, nest_fire_bomb_num, create_time) values
        <foreach collection="list" item="item" separator=",">
            (#{item.nestCode}, #{item.nestStateConstant}, #{item.droneStateConstant}, #{item.isDroneConnected},
            #{item.isRemoteConnected},
            #{item.nestBatteryAvailable}, #{item.chargingState}, #{item.nestPower}, #{item.nestEasyLockStus},
            #{item.nestFireWarn},
            #{item.nestAutoCheck}, #{item.nestAutoCheckErr}, #{item.nestPowerMode}, #{item.nestFireBombNum}, sysdate())
        </foreach>
    </insert>

    <delete id="deleteBeforeDaysAgo" parameterType="java.lang.Integer">
        delete from basedata_hr_nest where <![CDATA[ create_time < date_sub(now(), INTERVAL #{days} DAY) ]]>
    </delete>
</mapper>