<?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.devplatform.equipment.modules.alarms.dao.AlarmsInfoDao">
  <!-- Result Map -->
  <resultMap id="BaseResultMap" type="com.devplatform.equipment.modules.alarms.bean.AlarmsInfo">
    <result column="id" property="id"/>
    <result column="system_id" property="systemId"/>
    <result column="event_number" property="eventNumber"/>
    <result column="event_name" property="eventName"/>
    <result column="event_type" property="eventType"/>
    <result column="event_level" property="eventLevel"/>
    <result column="resource_id" property="resourceId"/>
    <result column="resource_name" property="resourceName"/>
    <result column="occurrence_time" property="occurrenceTime"/>
    <result column="occurrence_site" property="occurrenceSite"/>
    <result column="status" property="status"/>
    <result column="alarms_status" property="alarmsStatus"/>
    <result column="type" property="type"/>
    <result column="plan_id" property="planId"/>
    <result column="create_time" property="createTime"/>
    <result column="create_user_id" property="createUserId"/>
    <result column="update_time" property="updateTime"/>
    <result column="update_user_id" property="updateUserId"/>
    <result column="deleted" property="deleted"/>
    <result column="byx1" property="byx1"/>
    <result column="byx2" property="byx2"/>
    <result column="byx3" property="byx3"/>
    <result column="station_id" property="stationId"/>
    <result column="sys_sign" property="sysSign"/>
    <association column="{id=id,stationId=station_id}" property="fj" select="getFj"/>
  </resultMap>

  <!-- alarms_info table all fields -->
  <sql id="Base_Column_List">
		id,system_id,event_number,event_name,event_type,event_level,resource_id,resource_name,occurrence_time,occurrence_site,status,alarms_status,`type`,plan_id,create_time,create_user_id,update_time,update_user_id,deleted,byx1,byx2,byx3,sys_sign,station_id
	</sql>

  <!-- 公共查询条件 -->
  <sql id="Example_Where_Clause">
    where deleted=0
    <if test="id!=null and id!=''">and id = #{id}</if>
    <if test="systemId!=null and systemId!=''">and system_id = #{systemId}</if>
    <if test="eventNumber!=null and eventNumber!=''">and event_number = #{eventNumber}</if>
    <if test="eventName!=null and eventName!=''">and event_name = #{eventName}</if>
    <if test="eventType!=null and eventType!=''">and event_type = #{eventType}</if>
    <if test="eventLevel!=null ">and event_level = #{eventLevel}</if>
    <if test="resourceId!=null and resourceId!=''">and resource_id = #{resourceId}</if>
    <if test="resourceName!=null and resourceName!=''">and resource_name = #{resourceName}</if>
    <if test="occurrenceTime!=null ">and occurrence_time = #{occurrenceTime}</if>
    <if test="occurrenceSite!=null and occurrenceSite!=''">and occurrence_site = #{occurrenceSite}</if>
    <if test="status!=null ">and status = #{status}</if>
    <if test="createTime!=null ">and create_time = #{createTime}</if>
    <if test="createUserId!=null and createUserId!=''">and create_user_id = #{createUserId}</if>
    <if test="updateTime!=null ">and update_time = #{updateTime}</if>
    <if test="updateUserId!=null and updateUserId!=''">and update_user_id = #{updateUserId}</if>
    <if test="deleted!=null ">and deleted = #{deleted}</if>
    <if test="byx1!=null and byx1!=''">and byx1 = #{byx1}</if>
    <if test="byx2!=null and byx2!=''">and byx2 = #{byx2}</if>
    <if test="byx3!=null and byx3!=''">and byx3 = #{byx3}</if>
    <if test="sysSign!=null and sysSign!=''">and sys_sign = #{sysSign}</if>
    <if test="stationId!=null and stationId!=''">and station_id = #{stationId}</if>
  </sql>

  <!-- 分页查询条件 -->
  <sql id="queryPageByListClause">
    where deleted=0
    <if test="eventType != null and eventType !='' ">
      and event_type = #{eventType}
    </if>
    <if test="params !=null and params != '' ">
      and (INSTR(event_number, #{params})
      or INSTR(event_name, #{params})
      or INSTR(resource_name, #{params}))
    </if>
    <!--查询未处理的-->
    <if test="status != null and status == 0">
      and status IN (1,2)
    </if>
    <if test="status != null and status !=0">
      and status = #{status}
    </if>
    <if test="levelList != null">
      <if test="levelList.size() > 0">
        and event_level in
        <foreach close=")" collection="levelList" index="index" item="level" open="(" separator=",">
          #{level}
        </foreach>
      </if>
    </if>
    <if test='systemId != null and systemId != "" and systemId != "0"'>
      and system_id = #{systemId}
    </if>
    <if test="startTime !=null and startTime !=''">
      and occurrence_time &gt;= #{startTime}
    </if>
    <if test="endTime !=null and endTime !=''">
      and occurrence_time &lt;= #{endTime}
    </if>
    <if test="stationId!=null and stationId!=''">and station_id = #{stationId}</if>
  </sql>

  <select id="getFj" resultType="com.devplatform.equipment.modules.link.log.bean.LinkAttachment">
    SELECT fj.*
    FROM link_attachment fj
    left join link_logs_action lla on lla.id = fj.link_logs_action_id and lla.station_id = fj.station_id
    left join link_logs ll on ll.id = lla.logs_id and ll.station_id = fj.station_id
    left join alarms_info ai on ai.id = ll.alarms_info_id and ai.station_id = fj.station_id
    WHERE fj.deleted = 0 and lower(fj.file_type) = 'jpg' and ai.id = #{id}
    <if test='stationId != null and stationId != ""'>
      and fj.station_id = #{stationId}
    </if>
    order by fj.create_time limit 1
  </select>

  <!--根据id查询报警信息-->
  <select id="queryById" parameterType="java.lang.String" resultMap="BaseResultMap">
    select ai.*,ls.station_name stationName,lsm.name tierName,lr.code as resourceCode,le.event_name as eventTypeName from alarms_info ai
    left join li_resource lr on lr.id = ai.resource_id and ai.station_id = lr.station_id
    left join li_station ls on lr.station_id = ls.id and ai.station_id = ls.station_id
    left join li_station_map lsm on lsm.id = lr.tier_id and ai.station_id = lsm.station_id
    left join link_event le on le.id = ai.event_type
    where ai.deleted=0 and ai.id = #{id}
    <if test="stationId!=null and stationId!=''">and ai.station_id = #{stationId}</if>
  </select>

  <!--分页页数统计-->
  <select id="queryPageByCount" parameterType="java.lang.Object" resultType="java.lang.Integer">
    SELECT COUNT(1) FROM alarms_info
    <include refid="queryPageByListClause"></include>
  </select>

  <!--分页查询-->
  <select id="queryPageByList" parameterType="java.lang.Object" resultMap="BaseResultMap">
    select * from alarms_info
    <include refid="queryPageByListClause"></include>
    <if test="pager.orderCondition != null and pager.orderCondition != ''">
      ${pager.orderCondition}
    </if>
    <if test="pager.mysqlQueryCondition != null and pager.mysqlQueryCondition != ''">
      ${pager.mysqlQueryCondition}
    </if>
  </select>

  <select id="queryAll" parameterType="java.lang.Object" resultMap="BaseResultMap">
    select ai.*,ls.station_name stationName,lsm.name tierName from alarms_info ai
    left join li_resource lr on lr.id = ai.resource_id and ai.station_id = lr.station_id
    left join li_station ls on lr.station_id = ls.id and ai.station_id = ls.station_id
    left join li_station_map lsm on lsm.id = lr.tier_id and ai.station_id = lsm.station_id
    where ai.deleted=0
    <if test="params.status!=null ">and ai.status = #{params.status}</if>
    <if test="params.stationId!=null and params.stationId !=''">and lr.station_id = #{params.stationId}</if>
    order by ai.occurrence_time desc
  </select>

  <!--统计未处理报警信息-->
  <select id="queryGroupUntreated" parameterType="java.lang.Integer" resultMap="BaseResultMap">
		SELECT
				*
			FROM
				(
			SELECT
				'视频监控系统' AS systemName,
				count( CASE WHEN event_level = 1 AND `status` IN (1,2) AND system_id = '438c30d3d7f241b573f22a1a36c60485' THEN 0 END ) AS oneCount,
				count( CASE WHEN event_level = 2 AND `status` IN (1,2) AND system_id = '438c30d3d7f241b573f22a1a36c60485' THEN 0 END ) AS twoCount,
				count( CASE WHEN event_level = 3 AND `status` IN (1,2) AND system_id = '438c30d3d7f241b573f22a1a36c60485' THEN 0 END ) AS threeCount
			FROM
				alarms_info UNION
			SELECT
				'入侵报警系统' AS systemName,
				count( CASE WHEN event_level = 1 AND `status` IN (1,2) AND system_id = '75c7c11716713edfd2ce3581f5e7c193' THEN 0 END ) AS oneCount,
				count( CASE WHEN event_level = 2 AND `status` IN (1,2) AND system_id = '75c7c11716713edfd2ce3581f5e7c193' THEN 0 END ) AS twoCount,
				count( CASE WHEN event_level = 3 AND `status` IN (1,2) AND system_id = '75c7c11716713edfd2ce3581f5e7c193' THEN 0 END ) AS threeCount
			FROM
				alarms_info UNION
			SELECT
				'安全检查及探测系统' AS systemName,
				count( CASE WHEN event_level = 1 AND `status` IN (1,2) AND system_id = '3a64b86b1a5dead8ff9d2ad913744e50' THEN 0 END ) AS oneCount,
				count( CASE WHEN event_level = 2 AND `status` IN (1,2) AND system_id = '3a64b86b1a5dead8ff9d2ad913744e50' THEN 0 END ) AS twoCount,
				count( CASE WHEN event_level = 3 AND `status` IN (1,2) AND system_id = '3a64b86b1a5dead8ff9d2ad913744e50' THEN 0 END ) AS threeCount
			FROM
				alarms_info UNION
			SELECT
				'出入口控制系统' AS systemName,
				count( CASE WHEN event_level = 1 AND `status` IN (1,2) AND system_id = 'ed401f7d9291b508d411d8192b016c6e' THEN 0 END ) AS oneCount,
				count( CASE WHEN event_level = 2 AND `status` IN (1,2) AND system_id = 'ed401f7d9291b508d411d8192b016c6e' THEN 0 END ) AS twoCount,
				count( CASE WHEN event_level = 3 AND `status` IN (1,2) AND system_id = 'ed401f7d9291b508d411d8192b016c6e' THEN 0 END ) AS threeCount
			FROM
				alarms_info
				) a

	</select>

  <!--统计已处理报警信息-->
  <select id="queryGroupProcessed" parameterType="java.lang.Integer" resultMap="BaseResultMap">
		SELECT
				*
			FROM
				(
			SELECT
				'视频监控系统' AS systemName,
				count( CASE WHEN event_level = 1 AND `status` = 3 AND system_id = '438c30d3d7f241b573f22a1a36c60485' THEN 0 END ) AS oneCount,
				count( CASE WHEN event_level = 2 AND `status` = 3 AND system_id = '438c30d3d7f241b573f22a1a36c60485' THEN 0 END ) AS twoCount,
				count( CASE WHEN event_level = 3 AND `status` = 3 AND system_id = '438c30d3d7f241b573f22a1a36c60485' THEN 0 END ) AS threeCount
			FROM
				alarms_info UNION
			SELECT
				'入侵报警系统' AS systemName,
				count( CASE WHEN event_level = 1 AND `status` = 3 AND system_id = '75c7c11716713edfd2ce3581f5e7c193' THEN 0 END ) AS oneCount,
				count( CASE WHEN event_level = 2 AND `status` = 3 AND system_id = '75c7c11716713edfd2ce3581f5e7c193' THEN 0 END ) AS twoCount,
				count( CASE WHEN event_level = 3 AND `status` = 3 AND system_id = '75c7c11716713edfd2ce3581f5e7c193' THEN 0 END ) AS threeCount
			FROM
				alarms_info UNION
			SELECT
				'安全检查及探测系统' AS systemName,
				count( CASE WHEN event_level = 1 AND `status` = 3 AND system_id = '3a64b86b1a5dead8ff9d2ad913744e50' THEN 0 END ) AS oneCount,
				count( CASE WHEN event_level = 2 AND `status` = 3 AND system_id = '3a64b86b1a5dead8ff9d2ad913744e50' THEN 0 END ) AS twoCount,
				count( CASE WHEN event_level = 3 AND `status` = 3 AND system_id = '3a64b86b1a5dead8ff9d2ad913744e50' THEN 0 END ) AS threeCount
			FROM
				alarms_info UNION
			SELECT
				'出入口控制系统' AS systemName,
				count( CASE WHEN event_level = 1 AND `status` = 3 AND system_id = 'ed401f7d9291b508d411d8192b016c6e' THEN 0 END ) AS oneCount,
				count( CASE WHEN event_level = 2 AND `status` = 3 AND system_id = 'ed401f7d9291b508d411d8192b016c6e' THEN 0 END ) AS twoCount,
				count( CASE WHEN event_level = 3 AND `status` = 3 AND system_id = 'ed401f7d9291b508d411d8192b016c6e' THEN 0 END ) AS threeCount
			FROM
				alarms_info
				) a
	</select>

  <select id="queryByCameraList" parameterType="java.lang.Object" resultType="java.util.Map">
    select a.*
    from alarms_info a
    where a.id IN (
    select ai.id
    from link_attachment fj
    left join link_logs_action lla on lla.id = fj.link_logs_action_id and lla.station_id = fj.station_id
    left join link_logs ll on ll.id = lla.logs_id and ll.station_id = fj.station_id
    left join alarms_info ai on ai.id = ll.alarms_info_id and ai.station_id = fj.station_id
    WHERE fj.deleted = 0 and fj.file_type = 'mp4' and ai.deleted = 0
    <if test="params.systemId !=null and params.systemId != ''">and ai.system_id = #{params.systemId}</if>
    <if test="params.level !=null and params.level != ''">and ai.event_level = #{params.level}</if>
    <if test="params.name !=null and params.name !=''">and INSTR(ai.event_name, #{params.name})</if>
    <if test="params.staTime !=null and params.staTime != ''">and ai.occurrence_time &gt;= #{params.staTime}</if>
    <if test="params.endTime !=null and params.endTime != ''">and ai.occurrence_time &lt;= #{params.endTime}</if>
    <if test="params.stationId!=null and params.stationId!=''">and ai.station_id = #{params.stationId}</if>
    GROUP BY ai.id)
    order by a.occurrence_time desc
  </select>

  <select id="countNum" parameterType="java.lang.Object" resultType="java.lang.Integer">
    select
    count(1)
    from alarms_info ai
    left join li_resource lr on lr.id = ai.resource_id and lr.station_id = ai.station_id
    where ai.deleted=0
    <if test="params.status!=null ">and ai.status = #{params.status}</if>
    <if test="params.stationId!=null and params.stationId !=''">and lr.station_id = #{params.stationId}</if>
  </select>

  <select id="cNum" parameterType="java.lang.Object" resultType="java.lang.Integer">
    select
    count(1)
    from alarms_info ai
    left join li_resource lr on lr.id = ai.resource_id and lr.station_id = ai.station_id
    where ai.deleted=0
    <if test="params.status!=null ">and ai.status = #{params.status}</if>
    <if test="params.startTime !=null and params.startTime !=''">
      and ai.occurrence_time &gt;= #{params.startTime}
    </if>
    <if test="params.endTime !=null and params.endTime !=''">
      and ai.occurrence_time &lt;= #{params.endTime}
    </if>
    <if test="params.stationId!=null and params.stationId !=''">and lr.station_id = #{params.stationId}</if>
    <if test="params.resourceId!=null and params.resourceId !=''">and ai.resource_id = #{params.resourceId}</if>
  </select>

  <select id="getHistoryVideInfo" resultType="java.util.HashMap">
    SELECT la.*
    FROM alarms_info ai
    LEFT JOIN link_logs ll ON ai.id = ll.alarms_info_id and ll.station_id = ai.station_id
    LEFT JOIN link_logs_action lla ON lla.logs_id = ll.id and lla.station_id = ai.station_id
    RIGHT JOIN link_attachment la ON la.link_logs_action_id = la.id and lla.station_id = ai.station_id
    WHERE la.file_type = 'mp4'
    AND ai.id = #{id}
    <if test="stationId!=null and stationId!=''">and ai.station_id = #{stationId}</if>
  </select>

  <select id="queryByCameraVideoList" resultType="java.util.Map">
    select fj.*
    from link_attachment fj
    left join link_logs_action lla on lla.id = fj.link_logs_action_id and lla.station_id = fj.station_id
    left join link_logs ll on ll.id = lla.logs_id and ll.station_id = fj.station_id
    left join alarms_info ai on ai.id = ll.alarms_info_id and ai.station_id = fj.station_id
    WHERE fj.deleted = 0 and fj.file_type = 'mp4' AND ai.id = #{alarmsId}
    <if test="stationId!=null and stationId!=''">and ai.station_id = #{stationId}</if>
  </select>

  <select id="queryBsZt" parameterType="java.lang.Object" resultMap="BaseResultMap">
    select * from alarms_info
    <include refid="queryPageByListClause"></include>
    order by occurrence_time desc limit 1
  </select>
</mapper>


