<?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.MqttSensorEventMapper">

    <resultMap type="MqttSensorEvent" id="MqttSensorEventResult">
        <result property="id" column="id"/>
        <result property="deviceId" column="device_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="eventType" column="event_type"/>
        <result property="eventDesc" column="event_desc"/>
        <result property="level" column="level"/>
        <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="selectMqttSensorEventVo">
        select id, device_id, dept_id, event_type, event_desc, level, status, remark, create_by, create_time, update_by, update_time, is_delete
        from mqtt_sensor_event
    </sql>

    <select id="selectMqttSensorEventList" parameterType="MqttSensorEvent" resultMap="MqttSensorEventResult">
        <include refid="selectMqttSensorEventVo"/>
        <where>
            <if test="deviceId != null">and device_id = #{deviceId}</if>
            <if test="deptId != null">and dept_id = #{deptId}</if>

            <!-- 数据权限 -->
            <if test="deptIds != null and deptIds.size > 0">
                AND dept_id IN
                <foreach collection="deptIds" item="deptId" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>

            <if test="eventType != null and eventType != ''">and event_type = #{eventType}</if>
            <if test="eventDesc != null and eventDesc != ''">and event_desc = #{eventDesc}</if>
            <if test="level != null and level != ''">and level = #{level}</if>
            <if test="status != null and status != ''">and status = #{status}</if>
            <if test="isDelete != null and isDelete != ''">and is_delete = #{isDelete}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectMqttSensorEventById" parameterType="Long" resultMap="MqttSensorEventResult">
        <include refid="selectMqttSensorEventVo"/>
        where id = #{id}
    </select>

    <insert id="insertMqttSensorEvent" parameterType="MqttSensorEvent" useGeneratedKeys="true" keyProperty="id">
        insert into mqtt_sensor_event
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="eventType != null and eventType != ''">event_type,</if>
            <if test="eventDesc != null">event_desc,</if>
            <if test="level != null">level,</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="deviceId != null">#{deviceId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="eventType != null and eventType != ''">#{eventType},</if>
            <if test="eventDesc != null">#{eventDesc},</if>
            <if test="level != null">#{level},</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="updateMqttSensorEvent" parameterType="MqttSensorEvent">
        update mqtt_sensor_event
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="eventType != null and eventType != ''">event_type = #{eventType},</if>
            <if test="eventDesc != null">event_desc = #{eventDesc},</if>
            <if test="level != null">level = #{level},</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="deleteMqttSensorEventById" parameterType="Long">
        delete from mqtt_sensor_event where id = #{id}
    </delete>

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

</mapper>