<?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.njcn.device.mapper.AlarmMapper">

    <select id="getAlarmInfo" resultType="TerminalAlarmVO">
        SELECT
	*
FROM
	((
		SELECT
			d.NAME type,
			d.id typeId,
			d.`Level`,
			max( a.Occurred_Time ) occurredTime,
			c.NAME subName,
			b.NAME devLineName,
			b.id devLineId,
			a.Remark,
			a.flag,
			a.process_time,
		    i.name updateBy,
			h.NAME lineGrade,
			h.Algo_Describe algoDescribe
		FROM
			cld_alarm a
			INNER JOIN pq_line b ON a.Line_Id = b.id
			INNER JOIN pq_line c ON b.pid = c.id
			LEFT JOIN sys_dict_data d ON a.Type = d.id
			INNER JOIN pq_line e ON b.id = e.pid
			INNER JOIN pq_line f ON e.id = f.pid
			INNER JOIN pq_line_detail g ON f.id = g.id
			LEFT JOIN sys_dict_data h ON g.Line_Grade = h.id
			Left join sys_user i on a.update_by = i.id
		WHERE
			a.dev_line_type = 1
        <if test="flag!=null">
            and a.flag = #{flag}
        </if>
        <if test="alarmLevel!=null">
            and d.level = #{alarmLevel}
        </if>
        <if test="lineGrade!=null  and lineGrade !=''">
            and g.line_Grade = #{lineGrade}
        </if>
        <if test="type!=null and type!=''">
            and a.type = #{type}
        </if>
		GROUP BY
			type,
		    typeId,
			subName,
			devLineName,
		    devLineId,
			flag,
			Remark,
			process_time,
		    updateBy,
			lineGrade,
			algoDescribe
			)
			UNION
		(
		SELECT
			f.NAME type,
		    f.id typeId,
			f.`Level`,
			max( a.Occurred_Time ) occurredTime,
			e.NAME subName,
			b.NAME devLineName,
			b.id devLineId,
			a.Remark,
			a.flag,
			a.process_time,
		    i.name updateBy,
			h.NAME lineGrade,
			h.Algo_Describe algoDescribe
		FROM
			cld_alarm a
			INNER JOIN pq_line b ON a.Line_Id = b.id
			INNER JOIN pq_line c ON c.id = b.pid
			INNER JOIN pq_line d ON d.id = c.pid
			INNER JOIN pq_line e ON e.id = d.pid
			LEFT JOIN sys_dict_data f ON a.Type = f.id
			INNER JOIN pq_line_detail g ON g.id = b.id
			LEFT JOIN sys_dict_data h ON g.Line_Grade = h.id
		    Left join sys_user i on a.update_by = i.id
		WHERE
			a.dev_line_type = 2
        <if test="flag!=null">
            and a.flag = #{flag}
        </if>
        <if test="alarmLevel!=null">
            and f.level = #{alarmLevel}
        </if>
        <if test="lineGrade!=null and lineGrade !=''">
            and g.line_Grade = #{lineGrade}
        </if>
        <if test="type!=null and type!=''">
            and a.type = #{type}
        </if>
		GROUP BY
			type,
		    typeId,
			subName,
			devLineName,
		    devLineId,
			flag,
			Remark,
			process_time,
		    updateBy,
			lineGrade,
			algoDescribe
		)) tt
WHERE
	tt.occurredTime BETWEEN #{startTime} AND #{endTime}
ORDER BY
	algoDescribe ASC,
	LEVEL ASC,
	flag ASC,
	occurredTime DESC
    </select>


	<select id="getAlarmDetail" resultType="TerminalAlarmVO">
		SELECT
		a.id,
		a.occurred_time,
		b.`level`,
		a.Remark,
		a.flag,
		b.name type,
		a.process_time,

		c.NAME AS updateBy
		FROM
		cld_alarm a
		LEFT JOIN sys_dict_data b ON b.id = a.type
		LEFT JOIN sys_user c ON a.update_by = c.id
		WHERE
		a.occurred_Time BETWEEN #{startTime} AND #{endTime}
		AND a.line_id = #{devLineId}
		AND a.Flag = #{flag}
		AND a.type = #{type}
		<if test="processTime!=null">
			and a.process_time = #{processTime}
		</if>
	</select>

	<update id="updateAlarmInfo">
        update cld_alarm set
        process_time=#{processTime},
        flag = 1,
        update_by=#{userIndex}
        where type=#{type}
        and line_id=#{devLineId}
        and state = 1
        and flag = 0
    </update>



</mapper>
