<?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.ruoyi.ems.mapper.AdmOpAlarmMapper">

    <resultMap type="com.ruoyi.ems.domain.AdmOpAlarm" id="AdmOpAlarmResult">
        <result property="id" column="id"/>
        <result property="areaCode" column="area_code"/>
        <result property="objType" column="obj_type"/>
        <result property="objCode" column="obj_code"/>
        <result property="alarmDate" column="alarm_date"/>
        <result property="alarmTime" column="alarm_time"/>
        <result property="alarmCode" column="alarm_code"/>
        <result property="subSystemName" column="sub_system_name"/>
        <result property="systemCode" column="system_code"/>
        <result property="objName" column="obj_name"/>
        <result property="alarmMsg" column="alarm_msg"/>
        <result property="alarmType" column="alarm_type"/>
        <result property="alarmState" column="alarm_state"/>
    </resultMap>

    <sql id="selectAdmOpAlarmVo">
        select alarm.id,
               alarm.area_code,
               alarm.obj_type,
               alarm.obj_code,
               alarm.alarm_date,
               alarm.alarm_time,
               alarm.alarm_code,
               alarm.alarm_msg,
               alarm.alarm_type,
               alarm.alarm_state,
               alarm.obj_name,
               sub.system_name sub_system_name,
               alarm.system_code
        from adm_op_alarm alarm
                 left join adm_ems_subsystem sub on sub.system_code = alarm.system_code
    </sql>

    <select id="selectAdmOpAlarmList" parameterType="com.ruoyi.ems.domain.AdmOpAlarm" resultMap="AdmOpAlarmResult">
        <include refid="selectAdmOpAlarmVo"/>
        <where>
            <if test="areaCode != null  and areaCode != ''">and area_code = #{areaCode}</if>
            <if test="objType != null ">and obj_type = #{objType}</if>
            <if test="objCode != null  and objCode != ''">and obj_code = #{objCode}</if>
            <if test="alarmCode != null  and alarmCode != ''">and alarm_code = #{alarmCode}</if>
            <if test="objName !=null and objName!=''">and alarm.obj_name like concat('%', #{objName}, '%')</if>
            <if test="systemCode !=null and systemCode != ''">and alarm.system_code = #{systemCode}</if>
            <if test="alarmMsg != null  and alarmMsg != ''">and alarm_msg like concat('%', #{alarmMsg}, '%')</if>
            <if test="alarmType != null ">and alarm_type = #{alarmType}</if>
            <if test="alarmState != null ">and alarm_state = #{alarmState}</if>
            <if test="alarmStateList != null and alarmStateList.size() > 0">
                and alarm_state in
                <foreach collection="alarmStateList" item="alarmState" open="(" close=")" separator=",">
                    #{alarmState}
                </foreach>
            </if>
        </where>
        order by alarm.alarm_time desc
    </select>

    <select id="selectAdmOpAlarmById" parameterType="Long" resultMap="AdmOpAlarmResult">
        <include refid="selectAdmOpAlarmVo"/>
        where alarm.id = #{id}
    </select>

    <insert id="insertAdmOpAlarm" parameterType="com.ruoyi.ems.domain.AdmOpAlarm" useGeneratedKeys="true"
            keyProperty="id">
        insert into adm_op_alarm
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="objType != null">obj_type,</if>
            <if test="objCode != null and objCode != ''">obj_code,</if>
            <if test="objName != null and objName!=''">obj_name,</if>
            <if test="alarmDate != null">alarm_date,</if>
            <if test="alarmTime != null">alarm_time,</if>
            <if test="systemCode !=null">system_code,</if>
            <if test="alarmCode != null">alarm_code,</if>
            <if test="alarmMsg != null">alarm_msg,</if>
            <if test="alarmType != null">alarm_type,</if>
            <if test="alarmState != null">alarm_state,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="objType != null">#{objType},</if>
            <if test="objCode != null and objCode != ''">#{objCode},</if>
            <if test="objName != null and objName!=''">#{objName},</if>
            <if test="alarmDate != null">#{alarmDate},</if>
            <if test="alarmTime != null">#{alarmTime},</if>
            <if test="systemCode !=null">#{systemCode},</if>
            <if test="alarmCode != null">#{alarmCode},</if>
            <if test="alarmMsg != null">#{alarmMsg},</if>
            <if test="alarmType != null">#{alarmType},</if>
            <if test="alarmState != null">#{alarmState},</if>
        </trim>
    </insert>

    <update id="updateAdmOpAlarm" parameterType="com.ruoyi.ems.domain.AdmOpAlarm">
        update adm_op_alarm
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="objType != null">obj_type = #{objType},</if>
            <if test="objCode != null and objCode != ''">obj_code = #{objCode},</if>
            <if test="objName != null and objName!=''">obj_name = #{objName},</if>
            <if test="alarmDate != null">alarm_date = #{alarmDate},</if>
            <if test="alarmTime != null">alarm_time = #{alarmTime},</if>
            <if test="alarmCode != null">alarm_code = #{alarmCode},</if>
            <if test="systemCode !=null">system_code = #{systemCode},</if>
            <if test="alarmMsg != null">alarm_msg = #{alarmMsg},</if>
            <if test="alarmType != null">alarm_type = #{alarmType},</if>
            <if test="alarmState != null">alarm_state = #{alarmState},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAdmOpAlarmById" parameterType="Long">
        delete
        from adm_op_alarm
        where id = #{id}
    </delete>

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

    <select id="qryAlarmTypeIndex" resultType="java.util.Map">
        SELECT count(*)   cnt,
               alarm_type alarmType
        FROM adm_op_alarm
        WHERE area_code = #{areaCode}
        GROUP BY alarm_type
    </select>

    <select id="qryAlarmTypeIndexDay" resultType="java.util.Map">
        SELECT count(*)                         cnt,
               alarm_type                       alarmType,
               DATE_FORMAT(alarm_time, '%H:00') dateIndex
        FROM adm_op_alarm
        WHERE area_code = #{areaCode}
          and alarm_date = #{alarmDate}
        GROUP BY DATE_FORMAT(alarm_time, '%H:00'),
                 alarm_type
        ORDER BY alarm_type,
                 dateIndex;
    </select>

    <select id="qryAlarmTypeIndexMonth" resultType="java.util.Map">
        SELECT count(*)                               cnt,
               alarm_type                             alarmType,
               DATE_FORMAT(alarm_date, '%Y-%m-%d') AS dateIndex
        FROM adm_op_alarm
        WHERE area_code = #{areaCode}
          and alarm_date >= #{alarmDate}
        GROUP BY alarm_type,
                 alarm_date
        ORDER BY alarm_type,
                 dateIndex;
    </select>

    <select id="qryAlarmTypeIndexYear" resultType="java.util.Map">
        SELECT count(*)                            cnt,
               alarm_type                          alarmType,
               DATE_FORMAT(alarm_date, '%Y-%m') AS dateIndex
        FROM adm_op_alarm
        WHERE area_code = #{areaCode}
          and alarm_date >= #{alarmDate}
        GROUP BY alarm_type,
                 DATE_FORMAT(alarm_date, '%Y-%m')
        ORDER BY alarm_type,
                 dateIndex;
    </select>

    <select id="qrySubSysIndexDay" resultType="java.util.Map">
        SELECT count(*)                         cnt,
               alarm.system_code                systemCode,
               sub.system_name                  systemName,
               DATE_FORMAT(alarm_time, '%H:00') dateIndex
        FROM adm_op_alarm alarm
                 left join adm_ems_subsystem sub
                           on alarm.system_code = sub.system_code
        WHERE area_code = #{areaCode}
          and alarm_date = #{alarmDate}
        GROUP BY DATE_FORMAT(alarm_time, '%H:00'),
                 alarm.system_code
        ORDER BY alarm.system_code,
                 dateIndex;
    </select>

    <select id="qrySubSysIndexMonth" resultType="java.util.Map">
        SELECT count(*)                               cnt,
               alarm.system_code                      systemCode,
               sub.system_name                        systemName,
               DATE_FORMAT(alarm_date, '%Y-%m-%d') AS dateIndex
        FROM adm_op_alarm alarm
                 left join adm_ems_subsystem sub
                           on alarm.system_code = sub.system_code
        WHERE area_code = #{areaCode}
          and alarm_date >= #{alarmDate}
        GROUP BY alarm.system_code,
                 alarm_date
        ORDER BY alarm.system_code,
                 dateIndex;
    </select>

    <select id="qrySubSysIndexYear" resultType="java.util.Map">
        SELECT count(*)                            cnt,
               alarm.system_code                   systemCode,
               sub.system_name                     systemName,
               DATE_FORMAT(alarm_date, '%Y-%m') AS dateIndex
        FROM adm_op_alarm alarm
                 left join adm_ems_subsystem sub
                           on alarm.system_code = sub.system_code
        WHERE area_code = #{areaCode}
          and alarm_date >= #{alarmDate}
        GROUP BY alarm.system_code,
                 DATE_FORMAT(alarm_date, '%Y-%m')
        ORDER BY alarm.system_code,
                 dateIndex;
    </select>
</mapper>
