<?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.xrtygis.mapper.DmanightflowMapper">

    <resultMap id="dmanightflowMap" type="com.tofly.xrtygis.entity.Dmanightflow">
        <id property="lid" column="LID"/>
        <result property="tableId" column="TABLE_ID"/>
        <result property="tableName" column="TABLE_NAME"/>
        <result property="minval" column="MINVAL"/>
        <result property="mintime" column="MINTIME"/>
        <result property="minpval" column="MINPVAL"/>
        <result property="tval" column="TVAL"/>
        <result property="uday" column="UDAY"/>
        <result property="umonth" column="UMONTH"/>
        <result property="utime" column="UTIME"/>
        <result property="usfl" column="USFL"/>
        <result property="notes" column="NOTES"/>
    </resultMap>
    <select id="getMinFlowCollet" resultType="java.util.Map">
        SELECT to_char(UDAY, 'yyyy-MM-dd')                 UDAY,
               sum(case when USFL = '0' then 1 else 0 end) NORMAL,
               sum(case when USFL = '1' then 1 else 0 end) ABNORMAL
        FROM TF_YWPN_DMANIGHTFLOW_W
        WHERE TABLE_ID IN (${ids})
          and UDAY <![CDATA[ <= ]]> #{end}
          and UDAY <![CDATA[ >= ]]> #{start}
        GROUP BY UDAY
        ORDER BY UDAY desc
    </select>
    <select id="getMinFlowDetail" resultType="java.util.Map">
        SELECT TABLE_ID,
               to_char(UDAY, 'yyyy-MM-dd')          UDAY,
               decode(USFL, '0', '正常', '1', '超限异常') USFL,
               TABLE_NAME,
               MINVAL,
               MINTIME,
               MINPVAL
        FROM TF_YWPN_DMANIGHTFLOW_W
        WHERE TABLE_ID IN (${ids})
          and USFL in ('0', '1')
          and UDAY <![CDATA[ <= ]]> #{end}
          and UDAY <![CDATA[ >= ]]> #{start}
        ORDER BY UDAY desc
    </select>
    <select id="getMinFlowDetailPage" resultType="java.util.Map">
        SELECT a.TABLE_ID,
               to_char(a.UDAY, 'yyyy-MM-dd')          UDAY,
               decode(a.USFL, '0', '正常', '1', '超限异常') USFL,
               a.TABLE_NAME,
               a.MINVAL,
               a.MINTIME,
               a.MINPVAL,
               a.TVAL,
               b.MIN_FLOW
        FROM TF_YWPN_DMANIGHTFLOW_W a
                 LEFT JOIN TF_YWPN_DMATABLE_W b ON a.TABLE_ID = b.ID
        WHERE a.TABLE_ID IN (${ids})
          and a.USFL in ('0', '1')
          and a.UDAY between #{start} and #{end}
        ORDER BY a.UDAY desc
    </select>

    <select id="getNightFlow" resultType="map" fetchSize="1000">
        select FLOW_RATE flow, CREATE_TIME time
        from TF_YWPN_DMATABLEFLOW_W_VIEW
        where TABLE_ID = #{table}
          and to_char(create_time, 'yyyy-mm-dd hh24:mi:ss') between #{start} and #{end}
        order by time
    </select>

    <select id="getNightPress" resultType="map" fetchSize="1000">
        select PRESS, CREATE_TIME time
        from TF_YWPN_DMATABLEPRESS_W_VIEW
        where TABLE_ID = #{table}
          and to_char(create_time, 'yyyy-mm-dd hh24:mi:ss') between #{start} and #{end}
        order by time
    </select>

    <select id="getMinFlow" resultType="double">
        select MIN_FLOW from TF_YWPN_DMATABLE_W where ID = #{table}
    </select>

    <select id="getFlowList" resultMap="dmanightflowMap">
        SELECT t1.NAME table_Name,t4.id table_Id,nvl(t4.MIN_FLOW,0) tval,nvl(t3.min,0) minval,t3.START_TIME mintime,
               (CASE WHEN nvl(t3.min,0) > nvl(t4.MIN_FLOW,0) THEN 1 else 0 END) usfl
        FROM TF_DEVICE_ARCHIVE_MANAGE t1 LEFT JOIN TF_INDEX_ALLOCATION t2
                                                   ON t1.id = t2.DEVICE_ID
                                         LEFT JOIN STATISTICS_SCADA t3
                                                   on t2.VARIABLE_CODE = t3.CODE
                                         LEFT JOIN TF_YWPN_DMATABLE_W t4
                                                   on t1.id = t4.DEVICE_ID
        WHERE
            t3.STATISTICS_TYPE = '0'
          and  t4.isflow='1'
          and t3.START_TIME between #{startTime} and #{endTime}
        ORDER BY t3.START_TIME DESC
    </select>

    <select id="getPressureList" resultType="com.tofly.xrtygis.entity.Dmanightflow">
        SELECT t1.NAME tableName,t4.id tableId,nvl(t3.value,0) minpval,t3.START_TIME mintime
        FROM TF_DEVICE_ARCHIVE_MANAGE t1 LEFT JOIN TF_INDEX_ALLOCATION t2
                                                   ON t1.PRESSURE_DEVICE_ID = t2.DEVICE_ID
                                         LEFT JOIN STATISTICS_SCADA t3
                                                   on t2.VARIABLE_CODE = t3.CODE
                                         LEFT JOIN TF_YWPN_DMATABLE_W t4
                                                   on t1.id = t4.DEVICE_ID
        WHERE
            t3.STATISTICS_TYPE = '0'
          and t3.DATA_TYPE = '0'
          and t3.START_TIME >= #{startTime}
          and t3.START_TIME &lt;= #{endTime}
        ORDER BY t3.START_TIME DESC
    </select>

</mapper>
