<?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.aucma.base.mapper.ProcessAlertMapper">

    <resultMap type="ProcessAlert" id="ProcessAlertResult">
        <result property="alertId" column="alert_id"/>
        <result property="alertCode" column="alert_code"/>
        <result property="alertType" column="alert_type"/>
        <result property="alertLevel" column="alert_level"/>
        <result property="deviceCode" column="device_code"/>
        <result property="deviceName" column="device_name"/>
        <result property="paramCode" column="param_code"/>
        <result property="paramName" column="param_name"/>
        <result property="alertContent" column="alert_content"/>
        <result property="alertValue" column="alert_value"/>
        <result property="thresholdValue" column="threshold_value"/>
        <result property="alertTime" column="alert_time"/>
        <result property="alertStatus" column="alert_status"/>
        <result property="handleUser" column="handle_user"/>
        <result property="handleTime" column="handle_time"/>
        <result property="handleResult" column="handle_result"/>
        <result property="isFlag" column="is_flag"/>
        <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"/>
    </resultMap>

    <sql id="selectProcessAlertVo">
        select alert_id, alert_code, alert_type, alert_level, device_code, device_name, param_code, param_name, 
               alert_content, alert_value, threshold_value, alert_time, alert_status, handle_user, handle_time, 
               handle_result, is_flag, remark, create_by, create_time, update_by, update_time
        from process_alert
    </sql>

    <select id="selectProcessAlertList" parameterType="ProcessAlert" resultMap="ProcessAlertResult">
        <include refid="selectProcessAlertVo"/>
        <where>
            <if test="alertCode != null and alertCode != ''">and alert_code like concat(concat('%', #{alertCode}), '%')</if>
            <if test="alertType != null and alertType != ''">and alert_type = #{alertType}</if>
            <if test="alertLevel != null and alertLevel != ''">and alert_level = #{alertLevel}</if>
            <if test="deviceCode != null and deviceCode != ''">and device_code = #{deviceCode}</if>
            <if test="paramCode != null and paramCode != ''">and param_code = #{paramCode}</if>
            <if test="alertStatus != null and alertStatus != ''">and alert_status = #{alertStatus}</if>
            <if test="isFlag != null and isFlag != ''">and is_flag = #{isFlag}</if>
            <if test="params != null and params.beginTime != null and params.endTime != null">
                and alert_time between #{params.beginTime} and #{params.endTime}
            </if>
        </where>
        order by alert_time desc
    </select>

    <select id="selectProcessAlertByAlertId" parameterType="Long" resultMap="ProcessAlertResult">
        <include refid="selectProcessAlertVo"/>
        where alert_id = #{alertId}
    </select>

    <insert id="insertProcessAlert" parameterType="ProcessAlert">
        <selectKey keyProperty="alertId" resultType="long" order="BEFORE">
            SELECT PROCESS_ALERT_SEQ.NEXTVAL as alertId FROM DUAL
        </selectKey>
        insert into process_alert
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="alertId != null">alert_id,</if>
            <if test="alertCode != null and alertCode != ''">alert_code,</if>
            <if test="alertType != null">alert_type,</if>
            <if test="alertLevel != null">alert_level,</if>
            <if test="deviceCode != null">device_code,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="paramCode != null">param_code,</if>
            <if test="paramName != null">param_name,</if>
            <if test="alertContent != null">alert_content,</if>
            <if test="alertValue != null">alert_value,</if>
            <if test="thresholdValue != null">threshold_value,</if>
            <if test="alertTime != null">alert_time,</if>
            <if test="alertStatus != null">alert_status,</if>
            <if test="handleUser != null">handle_user,</if>
            <if test="handleTime != null">handle_time,</if>
            <if test="handleResult != null">handle_result,</if>
            <if test="isFlag != null">is_flag,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="alertId != null">#{alertId},</if>
            <if test="alertCode != null and alertCode != ''">#{alertCode},</if>
            <if test="alertType != null">#{alertType},</if>
            <if test="alertLevel != null">#{alertLevel},</if>
            <if test="deviceCode != null">#{deviceCode},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="paramCode != null">#{paramCode},</if>
            <if test="paramName != null">#{paramName},</if>
            <if test="alertContent != null">#{alertContent},</if>
            <if test="alertValue != null">#{alertValue},</if>
            <if test="thresholdValue != null">#{thresholdValue},</if>
            <if test="alertTime != null">#{alertTime},</if>
            <if test="alertStatus != null">#{alertStatus},</if>
            <if test="handleUser != null">#{handleUser},</if>
            <if test="handleTime != null">#{handleTime},</if>
            <if test="handleResult != null">#{handleResult},</if>
            <if test="isFlag != null">#{isFlag},</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>
        </trim>
    </insert>

    <update id="updateProcessAlert" parameterType="ProcessAlert">
        update process_alert
        <trim prefix="SET" suffixOverrides=",">
            <if test="alertCode != null and alertCode != ''">alert_code = #{alertCode},</if>
            <if test="alertType != null">alert_type = #{alertType},</if>
            <if test="alertLevel != null">alert_level = #{alertLevel},</if>
            <if test="deviceCode != null">device_code = #{deviceCode},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="paramCode != null">param_code = #{paramCode},</if>
            <if test="paramName != null">param_name = #{paramName},</if>
            <if test="alertContent != null">alert_content = #{alertContent},</if>
            <if test="alertValue != null">alert_value = #{alertValue},</if>
            <if test="thresholdValue != null">threshold_value = #{thresholdValue},</if>
            <if test="alertTime != null">alert_time = #{alertTime},</if>
            <if test="alertStatus != null">alert_status = #{alertStatus},</if>
            <if test="handleUser != null">handle_user = #{handleUser},</if>
            <if test="handleTime != null">handle_time = #{handleTime},</if>
            <if test="handleResult != null">handle_result = #{handleResult},</if>
            <if test="isFlag != null">is_flag = #{isFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where alert_id = #{alertId}
    </update>

    <delete id="deleteProcessAlertByAlertId" parameterType="Long">
        delete from process_alert where alert_id = #{alertId}
    </delete>

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

    <!-- 查询指定设备和参数的未处理预警 -->
    <select id="selectUnhandledAlert" resultMap="ProcessAlertResult">
        <include refid="selectProcessAlertVo"/>
        where device_code = #{deviceCode}
          and param_code = #{paramCode}
          and alert_status = '0'
          and is_flag = '1'
        order by alert_time desc
        fetch first 1 rows only
    </select>

    <!-- 查询指定设备和参数的最新预警 -->
    <select id="selectLatestAlert" resultMap="ProcessAlertResult">
        <include refid="selectProcessAlertVo"/>
        where device_code = #{deviceCode}
          and param_code = #{paramCode}
          and is_flag = '1'
        order by alert_time desc
        fetch first 1 rows only
    </select>
</mapper>
