<?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.ozs.mapper.MonitorSystemMapper">

    <resultMap type="com.ozs.entity.MonitorSystem" id="MonitorSystemResult">
        <id property="id" column="id"/>
        <result property="clientId" column="client_id"/>
        <result property="monitorSystemName" column="monitor_system_name"/>
        <result property="systemIntroduce" column="system_introduce"/>
        <result property="alarmContent" column="alarm_content"/>
        <result property="alarmGenerate" column="alarm_generate"/>
        <result property="alarmRelease" column="alarm_release"/>
        <result property="treateMeasure" column="treate_measure"/>
        <result property="status" column="status"/>
        <result property="remark" column="remark"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectMonitorSystemVo">
        select id, client_id, monitor_system_name, system_introduce,alarm_content,alarm_generate,alarm_release,treate_measure,status, create_by, create_time, remark
		from monitor_system
    </sql>

    <insert id="insertMonitorSystem" parameterType="com.ozs.entity.MonitorSystem">
        insert into monitor_system(
        <if test="clientId != null and clientId != ''">client_id,</if>
        <if test="monitorSystemName != null and monitorSystemName != ''">monitor_system_name,</if>
        <if test="systemIntroduce != null and systemIntroduce != ''">system_introduce,</if>
        <if test="alarmContent != null and alarmContent != ''">alarm_content,</if>
        <if test="alarmGenerate != null and alarmGenerate != ''">alarm_generate,</if>
        <if test="alarmRelease != null and alarmRelease != ''">alarm_release,</if>
        <if test="treateMeasure != null and treateMeasure != ''">treate_measure,</if>
        status,
        <if test="remark != null and remark != ''">remark,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )values(
        <if test="clientId != null and clientId != ''">#{clientId},</if>
        <if test="monitorSystemName != null and monitorSystemName != ''">#{monitorSystemName},</if>
        <if test="systemIntroduce != null and systemIntroduce != ''">#{systemIntroduce},</if>
        <if test="alarmContent != null and alarmContent != ''">#{alarmContent},</if>
        <if test="alarmGenerate != null and alarmGenerate != ''">#{alarmGenerate},</if>
        <if test="alarmRelease != null and alarmRelease != ''">#{alarmRelease},</if>
        <if test="treateMeasure != null and treateMeasure != ''">#{treateMeasure},</if>
        1,
        <if test="remark != null and remark != ''">#{remark},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        sysdate()
        )
    </insert>

    <delete id="deleteById" parameterType="Long">
 		delete from monitor_system where id = #{id}
 	</delete>

    <select id="checkClientIdUnique" parameterType="String" resultMap="MonitorSystemResult">
        <include refid="selectMonitorSystemVo"/>
        where client_id = #{clientId} limit 1
    </select>

    <select id="checkSystemOnline" parameterType="Long" resultMap="MonitorSystemResult">
        <include refid="selectMonitorSystemVo"/>
        where id = #{id}
    </select>

    <select id="monitorSystemCount" resultType="java.util.Map">
        select count(id) AS countAllMonitor,
               SUM(CASE status WHEN '2' THEN 1 ELSE 0 end) AS offlineMonitor,
               SUM(CASE status WHEN '1' THEN 1 ELSE 0 end) AS onlineMonitor
        from monitor_system
    </select>

    <select id="monitorSystemList" resultType="com.ozs.entity.MonitorSystem">
        SELECT a.*, b.countAlarm, b.SOURCE FROM (
            SELECT * FROM monitor_system
        ) a LEFT JOIN (
            SELECT count(id) countAlarm, SOURCE FROM msg_alarm where IS_RELEASE = 2 GROUP BY SOURCE
        ) b ON a.CLIENT_ID = b.SOURCE
    </select>

    <select id="selectAlarmHeartbeatLogByClientId" resultType="com.ozs.entity.vo.AlarmHeartbeatLog"
            parameterType="java.lang.String">
        select /*+ GROUP_OPT_FLAG(1)*/ id, client_id, max(create_time) as createTime from alarm_heartbeat_log where client_id = #{clientId}
    </select>
    <select id="monitorSystemPage" resultType="com.ozs.entity.MonitorSystem" parameterType="com.ozs.entity.vo.MonitorSystemVo">
        select mon.CLIENT_ID,max(create_time) as turnOnTime from  alarm_heartbeat_log  log right join 
(select CLIENT_ID,status,MONITOR_SYSTEM_NAME from monitor_system) mon  on log.CLIENT_ID=mon.CLIENT_ID
        <where>
            <if test="monitorSystemName != null and monitorSystemName != ''">
                and mon.MONITOR_SYSTEM_NAME like concat('%',#{monitorSystemName},'%')
            </if>
            <if test="status != null and status != 0">
                and mon.status = #{status}
            </if>
        </where>
  GROUP BY mon.CLIENT_ID
    </select>
</mapper>
