<?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.zehong.system.mapper.TDeviceAlarmInfoMapper">
    
    <resultMap type="TDeviceAlarmInfo" id="TDeviceAlarmInfoResult">
        <result property="alarmId"    column="alarm_id"    />
        <result property="relationDeviceId"    column="relation_device_code"    />
        <result property="deviceName"    column="device_name"    />
        <result property="alarmContent"    column="alarm_content"    />
        <result property="alarmLevel"    column="alarm_level"    />
        <result property="alarmBeginTime"    column="alarm_begin_time"    />
        <result property="alarmEndTime"    column="alarm_end_time"    />
        <result property="alarmStatus"    column="alarm_status"    />
        <result property="alarmImageUrl"    column="alarm_image_url"    />
        <result property="alarmVaule"    column="alarm_vaule"    />
        <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="isDel"    column="is_del"    />
        <result property="remarks"    column="remarks"    />
    </resultMap>

    <sql id="selectTDeviceAlarmInfoVo">
        select alarm_id, relation_device_id, alarm_content, alarm_level, alarm_begin_time, alarm_end_time, alarm_status, alarm_image_url, alarm_vaule, create_by, create_time, update_by, update_time, is_del, remarks from t_device_alarm_info
    </sql>

    <select id="selectTDeviceAlarmInfoList" parameterType="TDeviceAlarmInfo" resultMap="TDeviceAlarmInfoResult">
        select a.alarm_id, a.relation_device_code, a.alarm_content, a.alarm_level, a.alarm_begin_time, a.alarm_end_time,
        a.alarm_status, a.alarm_image_url, a.alarm_vaule, a.create_by, a.create_time, a.update_by, a.update_time, a.is_del,
        d.device_name ,a.remarks from t_device_alarm_info a
        LEFT JOIN t_device_info d on d.device_code  = a.relation_device_code
        <where>  
            <if test="alarmContent != null "> and alarm_content = #{alarmContent}</if>
            <if test="alarmLevel != null "> and alarm_level = #{alarmLevel}</if>
            <if test="alarmBeginTime != null "> and alarm_begin_time = #{alarmBeginTime}</if>
            <if test="alarmEndTime != null "> and alarm_end_time = #{alarmEndTime}</if>
            <if test="alarmStatus != null  and alarmStatus != ''"> and alarm_status = #{alarmStatus}</if>
        </where>
        ORDER BY create_time DESC
    </select>
    
    <select id="selectTDeviceAlarmInfoById" parameterType="Long" resultMap="TDeviceAlarmInfoResult">
        <include refid="selectTDeviceAlarmInfoVo"/>
        where alarm_id = #{alarmId}
    </select>
        
    <insert id="insertTDeviceAlarmInfo" parameterType="TDeviceAlarmInfo" useGeneratedKeys="true" keyProperty="alarmId">
        insert into t_device_alarm_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="relationDeviceId != null">relation_device_id,</if>
            <if test="alarmContent != null">alarm_content,</if>
            <if test="alarmLevel != null">alarm_level,</if>
            <if test="alarmBeginTime != null">alarm_begin_time,</if>
            <if test="alarmEndTime != null">alarm_end_time,</if>
            <if test="alarmStatus != null">alarm_status,</if>
            <if test="alarmImageUrl != null">alarm_image_url,</if>
            <if test="alarmVaule != null">alarm_vaule,</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="isDel != null">is_del,</if>
            <if test="remarks != null">remarks,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="relationDeviceId != null">#{relationDeviceId},</if>
            <if test="alarmContent != null">#{alarmContent},</if>
            <if test="alarmLevel != null">#{alarmLevel},</if>
            <if test="alarmBeginTime != null">#{alarmBeginTime},</if>
            <if test="alarmEndTime != null">#{alarmEndTime},</if>
            <if test="alarmStatus != null">#{alarmStatus},</if>
            <if test="alarmImageUrl != null">#{alarmImageUrl},</if>
            <if test="alarmVaule != null">#{alarmVaule},</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="isDel != null">#{isDel},</if>
            <if test="remarks != null">#{remarks},</if>
         </trim>
    </insert>

    <update id="updateTDeviceAlarmInfo" parameterType="TDeviceAlarmInfo">
        update t_device_alarm_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="relationDeviceId != null">relation_device_id = #{relationDeviceId},</if>
            <if test="alarmContent != null">alarm_content = #{alarmContent},</if>
            <if test="alarmLevel != null">alarm_level = #{alarmLevel},</if>
            <if test="alarmBeginTime != null">alarm_begin_time = #{alarmBeginTime},</if>
            <if test="alarmEndTime != null">alarm_end_time = #{alarmEndTime},</if>
            <if test="alarmStatus != null">alarm_status = #{alarmStatus},</if>
            <if test="alarmImageUrl != null">alarm_image_url = #{alarmImageUrl},</if>
            <if test="alarmVaule != null">alarm_vaule = #{alarmVaule},</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="isDel != null">is_del = #{isDel},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
        </trim>
        where alarm_id = #{alarmId}
    </update>

    <delete id="deleteTDeviceAlarmInfoById" parameterType="Long">
        delete from t_device_alarm_info where alarm_id = #{alarmId}
    </delete>

    <delete id="deleteTDeviceAlarmInfoByIds" parameterType="String">
        delete from t_device_alarm_info where alarm_id in 
        <foreach item="alarmId" collection="array" open="(" separator="," close=")">
            #{alarmId}
        </foreach>
    </delete>
    <select id="selectByType" resultType="java.util.HashMap">
        SELECT COUNT(a.alarm_id) AS num,d.dict_label AS label  FROM sys_dict_data d
         LEFT JOIN t_device_alarm_info a ON a.`alarm_content` = d.dict_sort AND
         a.alarm_begin_time > (SELECT DATE_SUB(CURDATE(), INTERVAL '29 0:0:0' DAY_SECOND))
         WHERE d.dict_type = "t_alarm_content"
         GROUP BY a.alarm_content
    </select>
    <select id="selectByLevel" resultType="java.util.HashMap">
        SELECT COUNT(a.alarm_id) AS `value`,d.dict_label AS `name` FROM sys_dict_data d
        LEFT JOIN t_device_alarm_info a ON a.alarm_level= d.dict_sort
        WHERE d.dict_type = "t_alarm_level"
        GROUP BY a.alarm_level
        ORDER BY d.`dict_sort`
    </select>
    <select id="selectByStatus" resultType="java.util.HashMap">
        SELECT COUNT(alarm_id) AS num,alarm_status AS label FROM t_device_alarm_info
        GROUP BY alarm_status
    </select>
    <select id="selectEveryDay" resultType="java.util.HashMap">
        SELECT alarm_id,DATE_FORMAT(alarm_begin_time , '%m-%d') AS beginTime, COUNT(alarm_id) AS num FROM t_device_alarm_info WHERE
        alarm_begin_time > (SELECT DATE_SUB(CURDATE(), INTERVAL '29 0:0:0' DAY_SECOND))
        GROUP BY beginTime
    </select>

    <select id="sumAlarmByYear" resultType="java.lang.Integer">
        SELECT
            COUNT(alarm_id)
        FROM
            t_device_alarm_info alarm
        WHERE year(create_time) = year(now())
    </select>

    <select id="sumAlarmByMonth" resultType="java.lang.Integer">
        SELECT
          COUNT(alarm_id)
        FROM
          t_device_alarm_info alarm
        WHERE month(create_time) = month(now())
    </select>

    <select id="sumAlarmByDay" resultType="java.lang.Integer">
        SELECT
          COUNT(alarm_id)
        FROM
          t_device_alarm_info alarm
        WHERE day(create_time) = day(now())
    </select>

</mapper>