<?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.tofly.water.mapper.OverviewMapper">

    <resultMap id="waterProductInfoMap" type="com.tofly.water.entity.vo.WaterProductionInfoVo">
       <result property="waterType" column="WATER_TYPE"/>
        <result property="designProcessingCapacity" column="DESIGN_PROCESSING_CAPACITY"/>
        <result property="waterCount" column="WATER_COUNT"/>
        <association property="waterInfoVoList" column="WATER_TYPE" select="getWaterInfoVoList"/>

    </resultMap>


    <select id="getDevicesByPage" resultType="com.tofly.water.entity.vo.DeviceBusPumpsVo">
        SELECT
        pg.name pumpGroupName,
        p.name pumpName,
        d.*
        FROM TF_DEVICE_ARCHIVE_MANAGE d
        INNER JOIN TF_DEVICE_BUSINESS b ON (b.ID = d.WATER_ID)
        LEFT JOIN TF_BASE_PUMP_GROUP pg ON pg.ID = b.BUSINESS_ID
        LEFT JOIN TF_BASE_PUMP p ON p.ID = b.BUSINESS_ID
        <where>
            1=1
            <if test="queryWrapper.deviceIds != null and queryWrapper.deviceIds.size() != 0">
                and d.ID in (#{queryWrapper.deviceIds})
            </if>
            <if test="queryWrapper.pumpName != null and queryWrapper.pumpName != ''">
                and p.NAME like '%'||#{queryWrapper.pumpName}||'%'
            </if>
            <if test="queryWrapper.pumpOrGroupIds != null and queryWrapper.pumpOrGroupIds.size() != 0">
                and (p.ID in
                <foreach collection="queryWrapper.pumpOrGroupIds" index="index" item="item" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
                or pg.ID in
                <foreach collection="queryWrapper.pumpOrGroupIds" index="index" item="item" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
                )
            </if>
        </where>
    </select>

    <select id="getDevices" resultType="com.tofly.water.entity.vo.DeviceBusPumpsVo">
        SELECT
        pg.name pumpGroupName,
        p.name pumpName,
        d.*
        FROM TF_DEVICE_ARCHIVE_MANAGE d
        LEFT JOIN TF_DEVICE_BUSINESS b ON (b.ID = d.WATER_ID)
        LEFT JOIN TF_BASE_PUMP_GROUP pg ON pg.ID = b.BUSINESS_ID
        LEFT JOIN TF_BASE_PUMP p ON p.ID = b.BUSINESS_ID
        <where>
            1=1
            <if test="queryWrapper.deviceIds != null and queryWrapper.deviceIds.size() != 0">
                and d.ID in (#{queryWrapper.deviceIds})
            </if>
            <if test="queryWrapper.pumpName != null and queryWrapper.pumpName != ''">
                and p.NAME like '%'||#{queryWrapper.pumpName}||'%'
            </if>
            <if test="queryWrapper.pumpOrGroupIds != null and queryWrapper.pumpOrGroupIds.size() != 0">
                and (p.ID in
                <foreach collection="queryWrapper.pumpOrGroupIds" index="index" item="item" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
                or pg.ID in
                <foreach collection="queryWrapper.pumpOrGroupIds" index="index" item="item" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
                )
            </if>
        </where>
    </select>

    <select id="monitor" resultType="com.tofly.water.entity.vo.MonitorPumpVo">
        <include refid="monitorSql"></include>
    </select>

    <select id="pageMonitor" resultType="com.tofly.water.entity.vo.MonitorPumpVo">
        <include refid="monitorSql"></include>
    </select>
    <sql id="monitorSql">
        SELECT * FROM
        (
        SELECT p.NAME pName,pg.NAME pgName,ia.variable_code ,ia.DISPLAY_NAME
        iaName,ia.MODEL iaModel,ia.TYPE iaType,dic.cname iaTypeName,sm.value,ia.UNIT,sr.REPORT_LEVEL,sr.REPORT_UPPER,sm.SCADA_TIME,
        sr.REPORT_LOWER,sr.REPORT_UPPER || '~' || sr.REPORT_LOWER as rangeVal,
        case when ia.IS_ALARM = '1' and TO_NUMBER(sm.value) NOT BETWEEN TO_NUMBER(sr.REPORT_LOWER) AND TO_NUMBER(sr.REPORT_UPPER) then '报警'
        else '正常' end as status
        FROM TF_DEVICE_BUSINESS d
        LEFT JOIN TF_BASE_PUMP p ON (p.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP')
        LEFT JOIN TF_BASE_PUMP_GROUP pg ON (pg.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP_GROUP')
        LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE dm ON dm.WATER_ID = d.ID
        LEFT JOIN TF_INDEX_ALLOCATION ia ON ia.DEVICE_ID = dm.ID
        LEFT JOIN ${tableName} sm ON sm.code = ia.VARIABLE_CODE
        LEFT JOIN TF_SCADA_REPORT sr ON (sr.ID = to_number(ia.REPORT_ID) AND ia.IS_ALARM = '1')
        left join TF_YWPN_SYSCHILDCODE_D dic
        on dic.ccode=ia.type and dic.pcid=60000
        where 1=1
        <if test="overviewDto!=null">
            <if test="overviewDto.searchStr!=null and overviewDto.searchStr!=''">
                and ia.DISPLAY_NAME like '%' || #{overviewDto.searchStr} || '%'
            </if>
            <choose>
                <when test="overviewDto.pumpId!=null and overviewDto.pumpId > 0">
                    and p.id = #{overviewDto.pumpId} and d.ID in (SELECT d1.WATER_ID FROM TF_DEVICE_ARCHIVE_MANAGE d1,TF_DEVICE_BUSINESS b1,TF_BASE_PUMP p1
                    WHERE d1.WATER_ID = b1.ID and b1.BUSINESS_ID = p1.ID and b1.OUTSIDE_TABLE = 'TF_BASE_PUMP' and
                    p1.id = #{overviewDto.pumpId} )
                </when>
                <otherwise>
                    and d.ID in (SELECT d1.WATER_ID FROM TF_DEVICE_ARCHIVE_MANAGE d1,TF_DEVICE_BUSINESS b1,TF_BASE_PUMP p1
                    WHERE d1.WATER_ID = b1.ID and b1.BUSINESS_ID = p1.ID and b1.OUTSIDE_TABLE = 'TF_BASE_PUMP')
                </otherwise>
            </choose>
            <if test="overviewDto.codes!=null and overviewDto.codes!=''">
                and variable_code in
                <foreach collection="overviewDto.codes.split(',')" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>

            <if test="overviewDto.start!=null and overviewDto.end!=null">
                and sm.SCADA_TIME BETWEEN #{overviewDto.start} AND #{overviewDto.end}
            </if>
            <if test="overviewDto.pumpIds != null and overviewDto.pumpIds.size() != 0">
                and p.ID in
                <foreach collection="overviewDto.pumpIds" index="index" item="item" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            <if test="overviewDto.reportLevel!=null and overviewDto.reportLevel!='' ">
                and nvl(sr.REPORT_LEVEL,'')=#{overviewDto.reportLevel}
            </if>
        </if>


        ) DATA where 1=1
        <if test="overviewDto.status!=null">
            <choose>
                <when test="overviewDto.status== 1">
                    and status='正常'
                </when>
                <otherwise>
                    and status='报警'
                </otherwise>
            </choose>
        </if>

        ORDER BY SCADA_TIME ASC
    </sql>
    <select id="pageMonitorNew" resultType="com.tofly.water.entity.vo.MonitorPumpVo">
        <include refid="monitorSqlNew"></include>
    </select>
    <sql id="monitorSqlNew">
select  * from
              (
        select  p.NAME pName,pg.NAME pgName,ia.variable_code ,ia.DISPLAY_NAME
        iaName,ia.MODEL iaModel,ia.TYPE iaType,sr.value,nvl(ia.UNIT,'') UNIT,sr.REPORT_LEVEL,sr.REPORT_UPPER,sr.SCADA_TIME,
        sr.REPORT_LOWER,sr.REPORT_UPPER || '~' || sr.REPORT_LOWER as rangeVal,
        '报警' status from
        Tf_Scada_Report_List sr
        inner join TF_INDEX_ALLOCATION ia
        on sr.code=ia.variable_code
        inner join TF_DEVICE_ARCHIVE_MANAGE  dm
        ON ia.DEVICE_ID = dm.ID
        inner join
        TF_DEVICE_BUSINESS d
        ON dm.WATER_ID = d.ID
        inner JOIN TF_BASE_PUMP p ON (p.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP')
        LEFT JOIN TF_BASE_PUMP_GROUP pg ON (pg.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP_GROUP')
        union all
        select  p.NAME pName,'' pgName ,ia.variable_code ,ia.DISPLAY_NAME
        iaName,ia.MODEL iaModel,ia.TYPE iaType,sr.value,nvl(ia.UNIT,'') UNIT,sr.REPORT_LEVEL,sr.REPORT_UPPER,sr.SCADA_TIME,
        sr.REPORT_LOWER,sr.REPORT_UPPER || '~' || sr.REPORT_LOWER as rangeVal,
        '报警' status from
        Tf_Scada_Report_List sr
        inner join TF_INDEX_ALLOCATION ia
        on sr.code=ia.variable_code
        inner join TF_DEVICE_ARCHIVE_MANAGE  dm
        ON ia.DEVICE_ID = dm.ID
        inner join TF_WATER_INFO p ON dm.WATER_ID = p.ID

        ) t
        where 1=1
        <if test="overviewDto.start!=null and overviewDto.end!=null">
            and SCADA_TIME BETWEEN #{overviewDto.start} AND #{overviewDto.end}
        </if>

        <if test="overviewDto.reportLevel!=null and overviewDto.reportLevel!='' ">
            and nvl(REPORT_LEVEL,'')=#{overviewDto.reportLevel}
        </if>

        ORDER BY SCADA_TIME ASC
    </sql>



    <select id="monitorWater" resultType="com.tofly.water.entity.vo.MonitorWaterVo">
        <include refid="monitorWaterSql"></include>
    </select>

    <select id="pageMonitorWater" resultType="com.tofly.water.entity.vo.MonitorWaterVo">
        <include refid="monitorWaterSql"></include>
    </select>

    <sql id="monitorWaterSql">
        SELECT * FROM
        (
        SELECT w.NAME as wName,dm.NAME dName,ia.variable_code ,ia.DISPLAY_NAME
        iaName,ia.MODEL iaModel,ia.TYPE iaType,dic.cname iaTypeName,sm.value,ia.UNIT,sr.REPORT_LEVEL,sr.REPORT_UPPER,sm.SCADA_TIME,
        sr.REPORT_LOWER,sr.REPORT_UPPER || '~' || sr.REPORT_LOWER as rangeVal,
        case when ia.IS_ALARM = '1' and TO_NUMBER(sm.value) NOT BETWEEN TO_NUMBER(sr.REPORT_LOWER) AND TO_NUMBER(sr.REPORT_UPPER) then '报警'

        else '正常' end as status
        FROM TF_INDEX_ALLOCATION ia
        INNER JOIN TF_DEVICE_ARCHIVE_MANAGE dm ON ia.DEVICE_ID = dm.ID
        INNER JOIN TF_WATER_INFO w ON dm.WATER_ID = w.ID
        LEFT JOIN ${tableName} sm ON sm.code = ia.VARIABLE_CODE
        LEFT JOIN TF_SCADA_REPORT sr ON (sr.ID = to_number(ia.REPORT_ID) AND ia.IS_ALARM = '1')
        left join TF_YWPN_SYSCHILDCODE_D dic
        on dic.ccode=ia.type and dic.pcid=60000
        where 1=1
        <if test="overviewDto!=null">
            <if test="overviewDto.searchStr!=null and overviewDto.searchStr!=''">
                and ia.DISPLAY_NAME like '%' || #{overviewDto.searchStr} || '%'
            </if>
            <if test="overviewDto.waterId!=null">
                and w.ID = #{overviewDto.waterId}
            </if>
            <if test="overviewDto.codes!=null and overviewDto.codes!=''">
            and variable_code in
                <foreach collection="overviewDto.codes.split(',')" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>


            <if test="overviewDto.start!=null and overviewDto.end!=null">
                and sm.SCADA_TIME BETWEEN #{overviewDto.start} AND #{overviewDto.end}
            </if>
            <if test="overviewDto.reportLevel!=null and overviewDto.reportLevel!='' ">
                and nvl(sr.REPORT_LEVEL,'')=#{overviewDto.reportLevel}
            </if>
        </if>
        ) DATA where 1=1
        <if test="overviewDto.status!=null">
            <choose>
                <when test="overviewDto.status== 1">
                    and status='正常'
                </when>
                <otherwise>
                    and status='报警'
                </otherwise>
            </choose>
        </if>

    ORDER BY SCADA_TIME ASC
    </sql>


    <select id="reportRealPage" resultType="com.tofly.water.entity.vo.ReportPumpVo">
        <include refid="reportRealSql"></include>
    </select>

    <select id="reportReal" resultType="com.tofly.water.entity.vo.ReportPumpVo">
    <include refid="reportRealSql"></include>
    </select>

    <sql id="reportRealSql">
        SELECT * FROM
        (
        SELECT p.NAME pName,pg.NAME pgName,dm.NAME dName,
        ia.UNIT,ia.DISPLAY_NAME iaName,ia.MODEL iaModel,ia.TYPE iaType,sm.*
        FROM TF_DEVICE_BUSINESS d
        LEFT JOIN TF_BASE_PUMP p ON (p.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP')
        LEFT JOIN TF_BASE_PUMP_GROUP pg ON (pg.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP_GROUP')
        LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE dm ON dm.WATER_ID = d.ID
        LEFT JOIN TF_INDEX_ALLOCATION ia ON ia.DEVICE_ID = dm.ID
        inner join TF_SCADA_REPORT_LIST sm ON sm.code = ia.VARIABLE_CODE
        where 1=1
        <if test="overviewDto!=null">
            <if test="overviewDto.searchStr!=null and overviewDto.searchStr!=''">
                and dm.NAME  || sm.DISPLAY_NAME like '%' || #{overviewDto.searchStr} || '%'
            </if>
            <choose>
                <when test="overviewDto.pumpId!=null and overviewDto.pumpId > 0">
                    and p.id = #{overviewDto.pumpId}
                </when>
                <otherwise>
                    and d.ID in (SELECT d1.WATER_ID FROM TF_DEVICE_ARCHIVE_MANAGE d1,TF_DEVICE_BUSINESS b1,TF_BASE_PUMP p1
                    WHERE d1.WATER_ID = b1.ID and b1.BUSINESS_ID = p1.ID and b1.OUTSIDE_TABLE = 'TF_BASE_PUMP')
                </otherwise>
            </choose>
            <if test="overviewDto.start!=null and overviewDto.end!=null">
                and sm.SCADA_TIME BETWEEN #{overviewDto.start} AND #{overviewDto.end}
            </if>
            <if test="overviewDto.levels and overviewDto.levels !=null and overviewDto.levels !=''">
                sm.REPORT_LEVEL in (#{overviewDto.levels})
            </if>
            <if test="overviewDto.pumpIds != null and overviewDto.pumpIds.size() != 0">
                and p.ID in
                <foreach collection="overviewDto.pumpIds" index="index" item="item" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
        </if>
        ) DATA ORDER BY SCADA_TIME ASC
    </sql>


    <select id="reportRealWaterPage" resultType="com.tofly.water.entity.vo.ReportWaterVo">
        <include refid="reportWaterRealSql"></include>
    </select>

    <select id="reportWaterReal" resultType="com.tofly.water.entity.vo.ReportWaterVo">
        <include refid="reportWaterRealSql"></include>
    </select>

    <sql id="reportWaterRealSql">
        SELECT * FROM
        (
        SELECT dm.NAME dName,w.NAME wName,
        ia.UNIT,ia.DISPLAY_NAME iaName,ia.MODEL iaModel,ia.TYPE iaType,sm.*
        FROM TF_INDEX_ALLOCATION ia
        INNER JOIN TF_DEVICE_ARCHIVE_MANAGE dm ON ia.DEVICE_ID = dm.ID
        INNER JOIN TF_WATER_INFO w ON dm.WATER_ID = w.ID
        INNER JOIN  TF_SCADA_REPORT_LIST sm ON sm.code = ia.VARIABLE_CODE
        where 1=1
        <if test="overviewDto!=null">
            <if test="overviewDto.searchStr!=null and overviewDto.searchStr!=''">
                and sm.DISPLAY_NAME || dm.NAME ||  w.NAME like '%' || #{overviewDto.searchStr} || '%'
            </if>
            <if test="overviewDto.start!=null and overviewDto.end!=null">
                and sm.SCADA_TIME BETWEEN #{overviewDto.start} AND #{overviewDto.end}
            </if>
            <if test="overviewDto.waterIds != null and overviewDto.waterIds.size() != 0">
                and w.ID in
                <foreach collection="overviewDto.waterIds" index="index" item="item" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
        </if>
        ) DATA ORDER BY SCADA_TIME ASC
    </sql>


    <select id="statisticsPumpReal" resultType="com.tofly.water.entity.vo.StatisticsPumpRealVo">
        <include refid="statisticsPumpRealSql"></include>
    </select>

    <select id="statisticsPumpRealPage" resultType="com.tofly.water.entity.vo.StatisticsPumpRealVo">
      <include refid="statisticsPumpRealSql"></include>
    </select>

    <sql id="statisticsPumpRealSql">
        SELECT * FROM
        (
        SELECT (select name from TF_BASE_PUMP where id = pg.PUMP_ID) pName,
               pg.NAME pgName,ia.DISPLAY_NAME iaName,ia.MODEL iaModel,ia.TYPE iaType,dm.NAME dName,
                sm.*,ia.UNIT
        FROM TF_DEVICE_BUSINESS d
        LEFT JOIN TF_BASE_PUMP p ON (p.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP')
        LEFT JOIN TF_BASE_PUMP_GROUP pg ON (pg.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP_GROUP')
        LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE dm ON dm.WATER_ID = d.ID
        LEFT JOIN TF_INDEX_ALLOCATION ia ON ia.DEVICE_ID = dm.ID
        LEFT JOIN STATISTICS_SCADA sm ON sm.code = ia.VARIABLE_CODE
        where 1=1
        <if test="overviewDto!=null">
            <if test="overviewDto.searchStr!=null and overviewDto.searchStr!=''">
                and (ia.DISPLAY_NAME like '%' || #{overviewDto.searchStr} || '%'
                         or p.NAME like '%' || #{overviewDto.searchStr} || '%' )
            </if>
            <if test="overviewDto.start!=null and overviewDto.end!=null">
                and sm.START_TIME BETWEEN #{overviewDto.start} AND #{overviewDto.end}
            </if>
            <if test="overviewDto.codeStr!=null and overviewDto.codeStr!=''">
                and sm.CODE in (${overviewDto.codeStr})
            </if>
            <if test="overviewDto.statisticsType!=null">
                and sm.STATISTICS_TYPE = #{overviewDto.statisticsType}
            </if>
            <if test="overviewDto.codes!=null and overviewDto.codes.size()!=0">
                and sm.CODE in
                <foreach item="item" collection="overviewDto.codes" separator="," open="(" close=")" index="">
                    #{item}
                </foreach>
            </if>
            <if test="overviewDto.pumpIds!=null and overviewDto.pumpIds.size()!=0">
                and (
                    d.OUTSIDE_TABLE = 'TF_BASE_PUMP' AND d.BUSINESS_ID in
                <foreach item="item" collection="overviewDto.pumpIds" separator="," open="(" close=")" index="">
                    #{item}
                </foreach>
                or d.OUTSIDE_TABLE = 'TF_BASE_PUMP_GROUP' AND d.BUSINESS_ID in
                    (
                    select ID from TF_BASE_PUMP_GROUP where PUMP_ID in
                    <foreach item="item" collection="overviewDto.pumpIds" separator="," open="(" close=")" index="">
                        #{item}
                    </foreach>
                    )
                )
            </if>
            <if test="overviewDto.pumpGroupIds!=null and overviewDto.pumpGroupIds.size()!=0">
                and  d.OUTSIDE_TABLE = 'TF_BASE_PUMP_GROUP' AND d.BUSINESS_ID in
                <foreach item="item" collection="overviewDto.pumpGroupIds" separator="," open="(" close=")" index="">
                    #{item}
                </foreach>
            </if>
            <if test="overviewDto.pumpId!=null">
                and  (
                    (d.OUTSIDE_TABLE = 'TF_BASE_PUMP' AND d.BUSINESS_ID = #{overviewDto.pumpId})
                    or
                    (d.OUTSIDE_TABLE = 'TF_BASE_PUMP_GROUP' AND d.BUSINESS_ID in
                        (
                        select ID from TF_BASE_PUMP_GROUP where PUMP_ID = #{overviewDto.pumpId}
                        )
                    )
                    )
            </if>
        </if>
        ) DATA ORDER BY START_TIME ASC
    </sql>

    <select id="statisticsPumpWater" resultType="com.tofly.water.entity.vo.StatisticsWaterRealVo">
        <include refid="statisticsPumpWaterSql"></include>
    </select>

    <select id="statisticsPumpWaterPage" resultType="com.tofly.water.entity.vo.StatisticsWaterRealVo">
        <include refid="statisticsPumpWaterSql"></include>
    </select>

    <sql id="statisticsPumpWaterSql">
        SELECT * FROM
        (
        SELECT dm.NAME dName,w.NAME wName,ia.MODEL iaModel,ia.TYPE iaType,
        ia.UNIT,ia.DISPLAY_NAME iaName,sm.*
        FROM TF_INDEX_ALLOCATION ia
        INNER JOIN TF_DEVICE_ARCHIVE_MANAGE dm ON ia.DEVICE_ID = dm.ID
        INNER JOIN TF_WATER_INFO w ON dm.WATER_ID = w.ID
        LEFT JOIN STATISTICS_SCADA sm ON sm.code = ia.VARIABLE_CODE
        where 1=1
        <if test="overviewDto!=null">
            <if test="overviewDto.searchStr!=null and overviewDto.searchStr!=''">
                and ia.DISPLAY_NAME like '%' || #{overviewDto.searchStr} || '%'
            </if>
            <if test="overviewDto.waterId!=null">
                and w.ID = #{overviewDto.waterId}
            </if>
            <if test="overviewDto.start!=null and overviewDto.end!=null">
                and sm.START_TIME BETWEEN #{overviewDto.start} AND #{overviewDto.end}
            </if>
            <if test="overviewDto.codeStr!=null and overviewDto.codeStr!=''">
                and ia.VARIABLE_CODE in (#{overviewDto.codeStr})
            </if>
            <if test="overviewDto.statisticsType!=null">
                and sm.STATISTICS_TYPE = #{overviewDto.statisticsType}
            </if>
            <if test="overviewDto.codes!=null and overviewDto.codes.size()!=0">
                and sm.CODE in
                <foreach item="item" collection="overviewDto.codes" separator="," open="(" close=")" index="">
                    #{item}
                </foreach>
            </if>
            <if test="overviewDto.waterIds!=null and overviewDto.waterIds.size()!=0">
                and w.ID in
                <foreach item="item" collection="overviewDto.waterIds" separator="," open="(" close=")" index="">
                    #{item}
                </foreach>
            </if>
        </if>
        ) DATA ORDER BY START_TIME ASC
    </sql>
    
    <select id="waterSupplyDay" resultType="com.tofly.water.entity.vo.WaterSupplyVo">
        select t.water_id,
               t.water_name,
               t.water_type,
               t.water_Supply_Day,
               t.design_processing_capacity,
               nvl(to_char(start_time, 'HH24:mi'), '00:00') || '~' ||
               nvl(to_char(end_time, 'HH24:mi'), '00:00') as last_Time_Range,
            nvl(t1.alarm_Status,0) alarm_Status
        from (select w.id as water_id,
                     w.NAME water_name,
                     w.design_processing_capacity,
                     w.water_Type,
                     nvl(sum(fold), 0) as water_Supply_Day,
                     max(sm.start_time) start_time,
                     max(sm.end_time) end_time

              from TF_WATER_INFO w
                       left JOIN TF_DEVICE_ARCHIVE_MANAGE dm
                                 ON dm.WATER_ID = w.ID
                       left join TF_INDEX_ALLOCATION ia
                                 ON ia.DEVICE_ID = dm.ID
                       left JOIN STATISTICS_SCADA sm
                                 ON sm.code = ia.VARIABLE_CODE
                                     and sm.start_time between trunc(sysdate) and trunc(sysdate + 1)
            and sm.CODE in
            <foreach item="item" collection="codes" separator="," open="(" close=")" index="">
                #{item}
            </foreach>
              and sm.statistics_type = 1
              group by w.water_Type,w.NAME, w.id,w.design_processing_capacity) t
          left join(
            SELECT w.id,1 as alarm_Status
            FROM TF_INDEX_ALLOCATION ia
                     INNER JOIN TF_DEVICE_ARCHIVE_MANAGE dm ON ia.DEVICE_ID = dm.ID
                     INNER JOIN TF_WATER_INFO w ON dm.WATER_ID = w.ID
                     INNER JOIN tf_scada_report_list sm ON sm.code = ia.VARIABLE_CODE
            where sm.scada_time between trunc(sysdate) and trunc(sysdate+1)
            group by w.id

        ) t1  on t.water_id=t1.id

    </select>


    <select id="waterSupplyYear" resultType="com.tofly.water.entity.vo.WaterSupplyVo">
        select nvl(t1.water_id, t2.water_id) water_id,nvl(t1.design_processing_capacity,t2.design_processing_capacity) design_processing_capacity,
               nvl(t1.water_name, t2.water_name) water_name,
               t1.water_Supply_Year,
               t2.water_Supply_Year as water_Supply_Last_Year
        from (select w.id as water_id,w.design_processing_capacity,
                     w.NAME water_name,
                     nvl(sum(fold), 0) as water_Supply_Year

              from TF_WATER_INFO w
                       left JOIN TF_DEVICE_ARCHIVE_MANAGE dm
                                 ON dm.WATER_ID = w.ID
                       left join TF_INDEX_ALLOCATION ia
                                 ON ia.DEVICE_ID = dm.ID
                       left JOIN STATISTICS_SCADA sm
                                 ON sm.code = ia.VARIABLE_CODE
                                     and sm.statistics_type =3
                                     and sm.start_time between #{yearStart} and #{yearEnd}
        and sm.CODE in
        <foreach item="item" collection="codes" separator="," open="(" close=")" index="">
            #{item}
        </foreach>

              group by w.NAME, w.id,w.design_processing_capacity) t1
                 left join (select w.id as water_id,
                                   w.NAME water_name,w.design_processing_capacity,
                                   nvl(sum(fold), 0) as water_Supply_Year

                            from TF_WATER_INFO w
                                     left JOIN TF_DEVICE_ARCHIVE_MANAGE dm
                                               ON dm.WATER_ID = w.ID
                                     left join TF_INDEX_ALLOCATION ia
                                               ON ia.DEVICE_ID = dm.ID
                                     left JOIN STATISTICS_SCADA sm
                                               ON sm.code = ia.VARIABLE_CODE
                                                   and sm.statistics_type = 3
                                                   and sm.start_time between ADD_MONTHS(#{yearStart},-12) and ADD_MONTHS(#{yearEnd},-12)
                and sm.CODE in
                <foreach item="item" collection="codes" separator="," open="(" close=")" index="">
                    #{item}
                </foreach>
                                                   and sm.statistics_type = 3
                            group by w.NAME, w.id,w.design_processing_capacity) t2
                           on t1.water_id = t2.water_id



    </select>


    <select id="pumpSupplyDay" resultType="com.tofly.water.entity.vo.PupmSupplyVo">
        select t1.*,nvl(t2.alarm_Status,0) alarm_Status from
            (select p.id as   PUMP_ID,p.name as PUMP_NAME
             FROM TF_DEVICE_BUSINESS d
                      LEFT JOIN TF_BASE_PUMP p ON (p.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP')
             where p.DELETED=0
            ) t1
                left join(
                select p.id as   PUMP_ID,1 as alarm_Status
                FROM TF_DEVICE_BUSINESS d
                         inner JOIN TF_BASE_PUMP p ON (p.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP')
                         inner JOIN TF_DEVICE_ARCHIVE_MANAGE dm ON dm.WATER_ID = d.ID
                         inner JOIN TF_INDEX_ALLOCATION ia ON ia.DEVICE_ID = dm.ID
                         inner join tf_scada_report_list sm on sm.code=ia.VARIABLE_CODE
                    and sm.scada_time between trunc(sysdate) and  trunc(sysdate+1)
                where p.DELETED=0
                group by p.id
            ) t2
                         on t1.PUMP_ID=t2.PUMP_ID
</select>

    <select id="pumpSupplyYear" resultType="com.tofly.water.entity.vo.PupmSupplyVo">

            select p.id as PUMP_ID,p.name as PUMP_NAME
             FROM TF_DEVICE_BUSINESS d
                      LEFT JOIN TF_BASE_PUMP p ON (p.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP')
             where p.DELETED=0

    </select>

    <select id="reportCount" resultType="com.tofly.water.entity.vo.ScadaReportVo">
        SELECT REPORT_LEVEL reportLevel, COUNT(REPORT_LEVEL) as quantity
        FROM TF_SCADA_REPORT_LIST
        GROUP BY REPORT_LEVEL
    </select>

    <select id="selectValueByCode" resultType="java.lang.String">
        SELECT * FROM
            (
                SELECT sm.VALUE
                FROM TF_DEVICE_BUSINESS d
                         LEFT JOIN TF_BASE_PUMP p ON (p.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP')
                         LEFT JOIN TF_BASE_PUMP_GROUP pg ON (pg.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP_GROUP')
                         LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE dm ON dm.WATER_ID = d.ID
                         LEFT JOIN TF_INDEX_ALLOCATION ia ON ia.DEVICE_ID = dm.ID
                         LEFT JOIN SCADA_MONITOR sm ON sm.code = ia.VARIABLE_CODE
                where 1=1
                and  d.OUTSIDE_TABLE = 'TF_BASE_PUMP_GROUP' AND d.BUSINESS_ID in (#{pumpGroupId})
                and sm.CODE = #{code}
                ORDER BY sm.SCADA_TIME DESC
            ) DATA
    </select>

    <select id="selectPumpByIndex" resultType="com.tofly.scada.entity.Allocation">
        SELECT ia.*
        FROM TF_DEVICE_BUSINESS d
                INNER JOIN TF_DEVICE_ARCHIVE_MANAGE dm ON dm.WATER_ID = d.ID
                INNER JOIN TF_INDEX_ALLOCATION ia ON ia.DEVICE_ID = dm.ID
        where 1=1
        <if test="ids!=null and ids.size()!=0">
           <choose>
               <when test="typeQuery == 0">
                   and d.OUTSIDE_TABLE = 'TF_BASE_PUMP' AND d.BUSINESS_ID in
                   <foreach item="item" collection="ids" separator="," open="(" close=")" index="">
                       #{item}
                   </foreach>
               </when>
                <otherwise>
                    and d.OUTSIDE_TABLE = 'TF_BASE_PUMP_GROUP' AND d.BUSINESS_ID in
                    <foreach item="item" collection="ids" separator="," open="(" close=")" index="">
                        #{item}
                    </foreach>
                </otherwise>
           </choose>
        </if>
    </select>


    <select id="waterSupplyOneMonth" resultType="Map">
       select w.water_type,w.NAME water_name, nvl(sum(fold), 0) as fold
        from TF_WATER_INFO w
                 left JOIN TF_DEVICE_ARCHIVE_MANAGE dm
                           ON dm.WATER_ID = w.ID
                 left join TF_INDEX_ALLOCATION ia
                           ON ia.DEVICE_ID = dm.ID
                 LEFT JOIN STATISTICS_SCADA sm
                           ON sm.code = ia.VARIABLE_CODE
                               and sm.statistics_type =2
                               and to_char(sm.start_time, 'yyyyMM') = #{yearAndMonth}
        and sm.CODE in
        <foreach item="item" collection="codes" separator="," open="(" close=")" index="">
            #{item}
        </foreach>
        <if test="waterType!=null and waterType!=''">
            and w.water_Type=#{waterType}
        </if>
        group by w.water_Type,w.NAME

    </select>

    <select id="waterSupplYearReport" resultType="Map">
        select  t1.fold as water_Jin_Total, t2.fold as water_Chu_total from
        (select sum(fold) as fold from STATISTICS_SCADA t1
        where t1.code in
        <foreach item="item" collection="waterSupplyJin.split(',')" separator="," open="(" close=")" index="">
            #{item}
        </foreach>
        and t1.statistics_type=3
        and to_char(t1.start_time,'yyyy')=#{yearAndMonth}
        ) t1, (select sum(fold) as fold from STATISTICS_SCADA t1
        where t1.code in
        <foreach item="item" collection="waterSupplyChu.split(',')" separator="," open="(" close=")" index="">
            #{item}
        </foreach>
        and t1.statistics_type=3
        and to_char(t1.start_time,'yyyy')=#{yearAndMonth}
        ) t2
    </select>
    <select id="waterSupplyMonthReport" resultType="Map">
       select  t1.fold as water_Jin_Total, t2.fold as water_Chu_total from
        (select sum(fold) as fold from STATISTICS_SCADA t1
        where t1.code in
        <foreach item="item" collection="waterSupplyJin.split(',')" separator="," open="(" close=")" index="">
            #{item}
        </foreach>
        and t1.statistics_type=2
        and to_char(t1.start_time,'yyyyMM')=#{yearAndMonth}
        ) t1, (select sum(fold) as fold from STATISTICS_SCADA t1
        where t1.code in
        <foreach item="item" collection="waterSupplyChu.split(',')" separator="," open="(" close=")" index="">
            #{item}
        </foreach>
        and t1.statistics_type=2
        and to_char(t1.start_time,'yyyyMM')=#{yearAndMonth}
        ) t2
    </select>



    <select id="waterSupplyDayReport" resultType="Map">
        select  abs(t1.fold) as water_Jin_Total, abs(t2.fold) as water_Chu_total from
        (select sum(t1.max - t1.min) as fold from STATISTICS_SCADA t1
        where t1.code in
        <foreach item="item" collection="waterSupplyJin.split(',')" separator="," open="(" close=")" index="">
            #{item}
        </foreach>
        and t1.statistics_type=0
        <if test="startHour != null and endHour != null">
            and to_char(t1.start_time,'yyyymmdd hh24') between #{startHour} and #{endHour}
        </if>
        <if test="yearAndMonth != null and yearAndMonth != ''">
            and to_char(t1.start_time,'yyyymmdd') = #{yearAndMonth}
        </if>
        ) t1, (select sum(t1.max - t1.min) as fold from STATISTICS_SCADA t1
        where t1.code in
        <foreach item="item" collection="waterSupplyChu.split(',')" separator="," open="(" close=")" index="">
            #{item}
        </foreach>
        and t1.statistics_type=0
        <if test="startHour != null and endHour != null">
            and to_char(t1.start_time,'yyyymmdd hh24') between #{startHour} and #{endHour}
        </if>
        <if test="yearAndMonth != null and yearAndMonth != ''">
            and to_char(t1.start_time,'yyyymmdd') = #{yearAndMonth}
        </if>
        ) t2
    </select>


    <select id="waterSupplyCommReport" resultType="Map">
        select  t1.fold as water_Jin_Total, t2.fold as water_Chu_total from
        (select sum(fold) as fold from STATISTICS_SCADA t1
        where t1.statistics_type=#{dateType}
        and to_char(t1.start_time,'${formatterStr}')=#{yearAndMonth}
        ) t1, (select sum(fold) as fold from STATISTICS_SCADA t1
        where t1.statistics_type=#{dateType}
        and to_char(t1.start_time,'${formatterStr}')=#{yearAndMonth}
        ) t2
    </select>

    <select id="getWaterAllocationCount" resultType="BigDecimal">
        select count(1) as count from  TF_WATER_INFO w
            inner JOIN TF_DEVICE_ARCHIVE_MANAGE dm
        ON dm.WATER_ID = w.ID
            inner join TF_INDEX_ALLOCATION ia
            ON ia.DEVICE_ID = dm.ID
        where ia.is_display=1 and w.id=#{waterId}
    </select>


    <select id="getWaterAlarmAllocationCount" resultType="BigDecimal">
    select count(distinct sm.code) as count from  TF_WATER_INFO w
    inner JOIN TF_DEVICE_ARCHIVE_MANAGE dm
    ON dm.WATER_ID = w.ID
    inner join TF_INDEX_ALLOCATION ia
    ON ia.DEVICE_ID = dm.ID
    INNER JOIN tf_scada_report_list sm ON sm.code = ia.VARIABLE_CODE
    where sm.scada_time between trunc(sysdate) and trunc(sysdate+1)
    and ia.is_display=1
    and w.id=#{waterId}
    </select>
    <select id="getWaterAlarmCount" resultType="BigDecimal">
        select count(1) as count from  TF_WATER_INFO w
            inner JOIN TF_DEVICE_ARCHIVE_MANAGE dm
        ON dm.WATER_ID = w.ID
            inner join TF_INDEX_ALLOCATION ia
            ON ia.DEVICE_ID = dm.ID
            INNER JOIN tf_scada_report_list sm ON sm.code = ia.VARIABLE_CODE
        where sm.scada_time between trunc(sysdate) and trunc(sysdate+1)
          and ia.is_display=1
          and w.id=#{waterId}
    </select>

    <select id="getWaterAllocation" resultType="String">
    SELECT t1.VARIABLE_CODE
    FROM TF_INDEX_ALLOCATION t1
        LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE t2 ON t1.DEVICE_ID = t2.ID
    WHERE t2.WATER_ID = #{waterId}
      and t1.TYPE = '4'
        <if test="isOut != null">
            AND t2.IS_OUT = #{isOut}
        </if>
    </select>


    <select id="getWaterProductInfo" resultMap="waterProductInfoMap">
        select water_type,count(1) water_count, sum(t.design_processing_capacity) design_processing_capacity from tf_water_info t
        where t.water_type=#{waterType}
        group by water_type
 </select>
    <select id="getWaterInfoVoList" resultType="com.tofly.water.entity.vo.WaterInfoVo">
        select t.id as water_id,t.name as water_name,t.design_processing_capacity from tf_water_info t
        where t.water_type=#{waterType}
    </select>

</mapper>