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

    <resultMap type="MqttSensorThreshold" id="MqttSensorThresholdResult">
        <result property="id" column="id"/>
        <result property="deviceId" column="device_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="metricType" column="metric_type"/>
        <result property="warnMin" column="warn_min"/>
        <result property="warnMax" column="warn_max"/>
        <result property="alarmMin" column="alarm_min"/>
        <result property="alarmMax" column="alarm_max"/>
        <result property="unit" column="unit"/>
        <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="selectMqttSensorThresholdVo">
        select id, device_id, dept_id, metric_type, warn_min, warn_max, alarm_min, alarm_max,
               unit, status, remark, create_by, create_time, update_by, update_time, is_delete
        from mqtt_sensor_threshold
    </sql>

    <select id="selectMqttSensorThresholdList" parameterType="MqttSensorThreshold" resultMap="MqttSensorThresholdResult">
        <include refid="selectMqttSensorThresholdVo"/>
        <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="metricType != null and metricType != ''">and metric_type = #{metricType}</if>
            <if test="warnMin != null">and warn_min = #{warnMin}</if>
            <if test="warnMax != null">and warn_max = #{warnMax}</if>
            <if test="alarmMin != null">and alarm_min = #{alarmMin}</if>
            <if test="alarmMax != null">and alarm_max = #{alarmMax}</if>
            <if test="unit != null and unit != ''">and unit = #{unit}</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="selectMqttSensorThresholdById" parameterType="Long" resultMap="MqttSensorThresholdResult">
        <include refid="selectMqttSensorThresholdVo"/>
        where id = #{id}
    </select>

    <insert id="insertMqttSensorThreshold" parameterType="MqttSensorThreshold" useGeneratedKeys="true" keyProperty="id">
        insert into mqtt_sensor_threshold
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="metricType != null and metricType != ''">metric_type,</if>
            <if test="warnMin != null">warn_min,</if>
            <if test="warnMax != null">warn_max,</if>
            <if test="alarmMin != null">alarm_min,</if>
            <if test="alarmMax != null">alarm_max,</if>
            <if test="unit != null">unit,</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="metricType != null and metricType != ''">#{metricType},</if>
            <if test="warnMin != null">#{warnMin},</if>
            <if test="warnMax != null">#{warnMax},</if>
            <if test="alarmMin != null">#{alarmMin},</if>
            <if test="alarmMax != null">#{alarmMax},</if>
            <if test="unit != null">#{unit},</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="updateMqttSensorThreshold" parameterType="MqttSensorThreshold">
        update mqtt_sensor_threshold
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="metricType != null and metricType != ''">metric_type = #{metricType},</if>
            <if test="warnMin != null">warn_min = #{warnMin},</if>
            <if test="warnMax != null">warn_max = #{warnMax},</if>
            <if test="alarmMin != null">alarm_min = #{alarmMin},</if>
            <if test="alarmMax != null">alarm_max = #{alarmMax},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</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="deleteMqttSensorThresholdById" parameterType="Long">
        delete from mqtt_sensor_threshold where id = #{id}
    </delete>

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

    <!-- 按设备+指标查询 -->
    <select id="selectByDeviceAndMetric" resultMap="MqttSensorThresholdResult">
        select *
        from mqtt_sensor_threshold
        where device_id = #{deviceId}
          and metric_type = #{metricType}
          and status = '1'
          and is_delete = '0'
            limit 1
    </select>

    <!-- 按部门+指标查询 -->
    <select id="selectByDeptAndMetric" resultMap="MqttSensorThresholdResult">
        select *
        from mqtt_sensor_threshold
        where dept_id = #{deptId}
          and metric_type = #{metricType}
          and status = '1'
          and is_delete = '0'
            limit 1
    </select>

    <select id="selectByPriority" resultMap="MqttSensorThresholdResult">
        select *
        from mqtt_sensor_threshold
        where metric_type = #{metricType}
          and status = '1'
          and is_delete = '0'
          and (
            device_id = #{deviceId}
                or dept_id = #{deptId}
            )
        order by case when device_id = #{deviceId} then 1 else 2 end
            limit 1
    </select>

</mapper>