<?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.shzg.project.worn.mapper.MqttSensorDeviceMapper">

    <resultMap type="MqttSensorDevice" id="MqttSensorDeviceResult">
        <result property="id"    column="id"    />
        <result property="devEui"    column="dev_eui"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceType"    column="device_type"    />
        <result property="deptId"    column="dept_id"    />
        <result property="status"    column="status"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDelete"    column="is_delete"    />
    </resultMap>

    <sql id="selectMqttSensorDeviceVo">
        select id, dev_eui, device_name, device_type, dept_id, status, remark, create_by, create_time, update_by, update_time, is_delete from mqtt_sensor_device
    </sql>

    <select id="selectMqttSensorDeviceList" parameterType="MqttSensorDevice" resultMap="MqttSensorDeviceResult">
        <include refid="selectMqttSensorDeviceVo"/>
        <where>
            <if test="devEui != null  and devEui != ''"> and dev_eui = #{devEui}</if>
            <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="deviceType != null  and deviceType != ''"> and device_type = #{deviceType}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="isDelete != null  and isDelete != ''"> and is_delete = #{isDelete}</if>
        </where>
    </select>

    <select id="selectMqttSensorDeviceById" parameterType="Long" resultMap="MqttSensorDeviceResult">
        <include refid="selectMqttSensorDeviceVo"/>
        where id = #{id}
    </select>

    <insert id="insertMqttSensorDevice" parameterType="MqttSensorDevice" useGeneratedKeys="true" keyProperty="id">
        insert into mqtt_sensor_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="devEui != null and devEui != ''">dev_eui,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDelete != null">is_delete,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="devEui != null and devEui != ''">#{devEui},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDelete != null">#{isDelete},</if>
         </trim>
    </insert>

    <update id="updateMqttSensorDevice" parameterType="MqttSensorDevice">
        update mqtt_sensor_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="devEui != null and devEui != ''">dev_eui = #{devEui},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMqttSensorDeviceById" parameterType="Long">
        delete from mqtt_sensor_device where id = #{id}
    </delete>

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

    <select id="selectByDevEui" parameterType="String" resultMap="MqttSensorDeviceResult">
        <include refid="selectMqttSensorDeviceVo"/>
        WHERE dev_eui = #{devEui}
        AND is_delete = '0'
        LIMIT 1
    </select>
</mapper>