<?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.pms.mapper.distribution.RStatPwAlarmCountWMapper">

    <resultMap id="BaseResultMap" type="com.njcn.device.pms.pojo.po.RStatPwAlarmCountW">
            <id property="orgNo" column="org_no" jdbcType="VARCHAR"/>
            <id property="dataDate" column="data_date" jdbcType="DATE"/>
            <result property="onlineMonitorCount" column="online_monitor_count" jdbcType="INTEGER"/>
            <result property="alarmMonitorCount" column="alarm_monitor_count" jdbcType="INTEGER"/>
    </resultMap>

    <!--orgNo在外面单独处理-->
    <resultMap id="BaseResultMapVO" type="com.njcn.device.pms.pojo.vo.RStatPwAlarmCountWVO">
        <id property="orgNo" column="org_no" jdbcType="VARCHAR"/>
        <id property="dataDate" column="data_date" jdbcType="DATE"/>
        <result property="onlineMonitorCount" column="online_monitor_count" jdbcType="INTEGER"/>
        <result property="alarmMonitorCount" column="alarm_monitor_count" jdbcType="INTEGER"/>
        <result property="alarmMonitorRate" column="alarm_monitor_rate" jdbcType="DOUBLE"/>
        <result property="alarmMonitorRateYoy" column="alarm_monitor_rate_yoy" jdbcType="DOUBLE"/>
    </resultMap>

    <sql id="Base_Column_List">
        org_no,data_date,online_monitor_count,
        alarm_monitor_count
    </sql>

    <!--查询各单位告警监测点总数、占比、占比同比信息-->
    <select id="getRStatPwAlarmCountVOList" resultType="com.njcn.device.pms.pojo.vo.RStatPwAlarmCountWVO" parameterType="map">
        select
               org_no,data_date,online_monitor_count,alarm_monitor_count,
                TRUNCATE(alarm_monitor_count / online_monitor_count * 100, 2) as alarm_monitor_rate,
               (
                   select TRUNCATE(alarm_monitor_count / online_monitor_count * 100, 2)
                   from r_stat_pw_alarm_count_w rspw2
                   where rspw1.org_no = rspw2.org_no
                   <if test="startTime != null and startTime != ''">
                       and data_date >= DATE(DATE_SUB(#{startTime}, INTERVAL 1 YEAR))
                   </if>
                   <if test="endTime != null and endTime != ''">
                       and data_date &lt;= DATE(DATE_SUB(#{endTime}, INTERVAL 1 YEAR))
                   </if>
               ) as alarm_monitor_rate_yoy
        from r_stat_pw_alarm_count_w rspw1
        where 1 = 1
        and org_no in
        <foreach collection="orgIdList" item="orgId" separator="," open="(" close=")">
            #{orgId}
        </foreach>
        <if test="startTime != null and startTime != ''">
            and data_date >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and data_date &lt;= #{endTime}
        </if>
    </select>
</mapper>
