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

    <resultMap type="MqttSensorData" id="MqttSensorDataResult">
        <result property="id" column="id"/>
        <result property="deviceId" column="device_id"/>
        <result property="deptId" column="dept_id"/>

        <result property="topic" column="topic"/>
        <result property="project" column="project"/>
        <result property="warehouse" column="warehouse"/>
        <result property="payload" column="payload"/>
        <result property="dataJson" column="data_json"/>
        <result property="battery" column="battery"/>
        <result property="temperature" column="temperature"/>
        <result property="humidity" column="humidity"/>
        <result property="nh3" column="nh3"/>
        <result property="h2s" column="h2s"/>
        <result property="concentration" column="concentration"/>
        <result property="waterStatus" column="water_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="selectMqttSensorDataVo">
        select
            id,
            device_id,
            dept_id,
            topic,
            project,
            warehouse,
            payload,
            data_json,
            battery,
            temperature,
            humidity,
            nh3,
            h2s,
            concentration,
            water_status,
            remark,
            create_by,
            create_time,
            update_by,
            update_time,
            is_delete
        from mqtt_sensor_data
    </sql>

    <select id="selectMqttSensorDataList" parameterType="MqttSensorData" resultMap="MqttSensorDataResult">
        <include refid="selectMqttSensorDataVo"/>
        <where>
            <if test="deviceId != null"> and device_id = #{deviceId}</if>

            <if test="deptIds != null and deptIds.size > 0">
                and dept_id in
                <foreach collection="deptIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>

            <if test="topic != null and topic != ''"> and topic = #{topic}</if>
            <if test="project != null and project != ''"> and project = #{project}</if>
            <if test="warehouse != null and warehouse != ''"> and warehouse = #{warehouse}</if>
            <if test="payload != null and payload != ''"> and payload = #{payload}</if>
            <if test="dataJson != null and dataJson != ''"> and data_json = #{dataJson}</if>
            <if test="battery != null"> and battery = #{battery}</if>
            <if test="temperature != null"> and temperature = #{temperature}</if>
            <if test="humidity != null"> and humidity = #{humidity}</if>
            <if test="nh3 != null"> and nh3 = #{nh3}</if>
            <if test="h2s != null"> and h2s = #{h2s}</if>
            <if test="concentration != null"> and concentration = #{concentration}</if>
            <if test="waterStatus != null"> and water_status = #{waterStatus}</if>
            <if test="isDelete != null and isDelete != ''"> and is_delete = #{isDelete}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectMqttSensorDataById" parameterType="Long" resultMap="MqttSensorDataResult">
        <include refid="selectMqttSensorDataVo"/>
        where id = #{id}
    </select>

    <insert id="insertMqttSensorData" parameterType="MqttSensorData" useGeneratedKeys="true" keyProperty="id">
        insert into mqtt_sensor_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="deptId != null">dept_id,</if>

            <if test="topic != null">topic,</if>
            <if test="project != null">project,</if>
            <if test="warehouse != null">warehouse,</if>
            <if test="payload != null">payload,</if>
            <if test="dataJson != null">data_json,</if>
            <if test="battery != null">battery,</if>
            <if test="temperature != null">temperature,</if>
            <if test="humidity != null">humidity,</if>
            <if test="nh3 != null">nh3,</if>
            <if test="h2s != null">h2s,</if>
            <if test="concentration != null">concentration,</if>
            <if test="waterStatus != null">water_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="topic != null">#{topic},</if>
            <if test="project != null">#{project},</if>
            <if test="warehouse != null">#{warehouse},</if>
            <if test="payload != null">#{payload},</if>
            <if test="dataJson != null">#{dataJson},</if>
            <if test="battery != null">#{battery},</if>
            <if test="temperature != null">#{temperature},</if>
            <if test="humidity != null">#{humidity},</if>
            <if test="nh3 != null">#{nh3},</if>
            <if test="h2s != null">#{h2s},</if>
            <if test="concentration != null">#{concentration},</if>
            <if test="waterStatus != null">#{waterStatus},</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="updateMqttSensorData" parameterType="MqttSensorData">
        update mqtt_sensor_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>

            <if test="topic != null">topic = #{topic},</if>
            <if test="project != null">project = #{project},</if>
            <if test="warehouse != null">warehouse = #{warehouse},</if>
            <if test="payload != null">payload = #{payload},</if>
            <if test="dataJson != null">data_json = #{dataJson},</if>
            <if test="battery != null">battery = #{battery},</if>
            <if test="temperature != null">temperature = #{temperature},</if>
            <if test="humidity != null">humidity = #{humidity},</if>
            <if test="nh3 != null">nh3 = #{nh3},</if>
            <if test="h2s != null">h2s = #{h2s},</if>
            <if test="concentration != null">concentration = #{concentration},</if>
            <if test="waterStatus != null">water_status = #{waterStatus},</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="deleteMqttSensorDataById" parameterType="Long">
        delete from mqtt_sensor_data where id = #{id}
    </delete>

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

</mapper>