<?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="alarmMsg"    column="alarm_msg"    />
        <result property="alarmType"    column="alarm_type"    />
        <result property="alarmState"    column="alarm_state"    />
    </resultMap>

    <sql id="selectAdmOpAlarmVo">
        select id, area_code, obj_type, obj_code, alarm_date, alarm_time, alarm_code, alarm_msg, alarm_type, alarm_state from adm_op_alarm
    </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="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>
        </where>
    </select>

    <select id="selectAdmOpAlarmById" parameterType="Long" resultMap="AdmOpAlarmResult">
        <include refid="selectAdmOpAlarmVo"/>
        where 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="alarmDate != null">alarm_date,</if>
            <if test="alarmTime != null">alarm_time,</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="alarmDate != null">#{alarmDate},</if>
            <if test="alarmTime != null">#{alarmTime},</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="alarmDate != null">alarm_date = #{alarmDate},</if>
            <if test="alarmTime != null">alarm_time = #{alarmTime},</if>
            <if test="alarmCode != null">alarm_code = #{alarmCode},</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>
</mapper>
