<?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.scada.mapper.ScadaMapper">
    <resultMap id="scadaHisMap" type="com.tofly.scada.entity.Scada">
        <id column="ID" property="id"></id>
        <result column="CODE" property="code"></result>
        <result column="VALUE" property="value"></result>
        <result column="SCADA_TIME" property="scadaTime"></result>
        <result column="UPDATE_TIME" property="updateTime"></result>
    </resultMap>
    <insert id="insertMouth">
        insert into ${tableName}
        (ID,CODE,VALUE,SCADA_TIME,UPDATE_TIME)
        <foreach collection="scadas" item="item" separator="union all" open="(" close=")">
            select
            #{item.id,jdbcType=NUMERIC},#{item.code,jdbcType=VARCHAR},#{item.value,jdbcType=VARCHAR},
            #{item.scadaTime,jdbcType=TIMESTAMP},#{item.updateTime,jdbcType=TIMESTAMP}
            from dual
        </foreach>
    </insert>

    <insert id="insertByOne">
        INSERT INTO ${tableName} (ID, CODE, VALUE, SCADA_TIME, UPDATE_TIME)
        VALUES (#{item.id,jdbcType=NUMERIC}, #{item.code,jdbcType=VARCHAR}, #{item.value,jdbcType=VARCHAR},
                #{item.scadaTime,jdbcType=TIMESTAMP}, #{item.updateTime,jdbcType=TIMESTAMP})
    </insert>

    <insert id="insertMonitoring">
        INSERT INTO SCADA_MONITORING (ID, CODE, VALUE, SCADA_TIME, UPDATE_TIME)
        VALUES (#{scada.id,jdbcType=VARCHAR}, #{scada.code,jdbcType=VARCHAR}, #{scada.value,jdbcType=VARCHAR},
                #{scada.scadaTime,jdbcType=TIMESTAMP}, #{scada.updateTime,jdbcType=TIMESTAMP})
    </insert>


    <update id="createTableMonths">
        CREATE TABLE ${tableName}
        (
            ID         NUMBER NOT NULL,
            CODE        VARCHAR2(500 BYTE) NOT NULL,
            VALUE       VARCHAR2(100 BYTE),
            SCADA_TIME  DATE,
            UPDATE_TIME DATE
        ) LOGGING
            NOCOMPRESS
            PCTFREE 10
            INITRANS 1
            STORAGE (
              BUFFER_POOL DEFAULT
            )
            PARALLEL 1
            NOCACHE
            DISABLE ROW MOVEMENT
    </update>

    <update id="updateMonitoring">
        UPDATE SCADA_MONITOR
        SET CODE       = #{scada.code,jdbcType=VARCHAR},
            VALUE       = #{scada.value,jdbcType=VARCHAR},
            SCADA_TIME  = #{scada.scadaTime,jdbcType=TIMESTAMP},
            UPDATE_TIME = #{scada.updateTime,jdbcType=TIMESTAMP}
        WHERE CODE = #{scada.code,jdbcType=VARCHAR}
    </update>

    <select id="verifyTableMonthsIsExits" resultType="java.lang.Integer">
        select count(*)
        from user_tables
        where table_name = upper('${tableName}')
    </select>

    <select id="selectDataByMonitoring" resultType="com.tofly.scada.entity.Scada">
        select *
        from SCADA_MONITORING
        where CODE = #{code}
    </select>

    <select id="selectHistoryByMouthsTablePage" resultType="com.tofly.scada.entity.vo.StatisticsScadaVo">
        SELECT scada.ID,
        scada.CODE,
        case WHEN trim(translate(scada.VALUE,'-0123456789.',' ')) is NULL THEN
       /* to_char(ROUND(to_number(scada.VALUE),nvl(allocation.accuracy,2)))*/
        decode(SUBSTR(to_char(ROUND(to_number(scada.VALUE),nvl(allocation.accuracy,2))),1,1),'.','0'||to_char(ROUND(to_number(scada.VALUE),nvl(allocation.accuracy,2))),to_char(ROUND(to_number(scada.VALUE),nvl(allocation.accuracy,2))))
        ELSE scada.VALUE END VALUE,
        scada.SCADA_TIME,
        scada.UPDATE_TIME,
        manage.NAME name,
        manage.MAKE_DATE makeDate,
        manage.TYPE type,
        manage.MANUFACTURER manufacturer,
        manage.LEADER leader,
        manage.PHONE phone,
        allocation.DISPLAY_NAME displayName,
        allocation.VARIABLE_NAME variableName,
        allocation.UNIT unit
        FROM ${mouthTable} scada
        LEFT JOIN TF_INDEX_ALLOCATION allocation ON allocation.VARIABLE_CODE = scada.CODE
        LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE manage ON manage.ID = allocation.DEVICE_ID
        WHERE 1 = 1
        <if test="scadaQuery.codes != null">
            AND allocation.VARIABLE_CODE in
            <foreach item="item" index="index" collection="scadaQuery.codes" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="scadaQuery.dataTypeIds != null">
            AND allocation.TYPE in
            <foreach item="item" index="index" collection="scadaQuery.dataTypeIds" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="scadaQuery.code != null">
            AND scada.CODE = #{scadaQuery.code}
        </if>
        <if test="scadaQuery.deviceIds != null and scadaQuery.deviceIds.size()>0 ">
            AND manage.ID in
            <foreach collection="scadaQuery.deviceIds" item="index" open="(" close=")" separator=",">
                #{index}
            </foreach>
        </if>
        <if test="scadaQuery.start != null and scadaQuery.end != null">
            AND scada.SCADA_TIME between #{scadaQuery.start} and #{scadaQuery.end}
        </if>
        <if test="scadaQuery.startDate != null and scadaQuery.endDate != null">
            AND scada.SCADA_TIME between to_date( #{scadaQuery.startDate}, 'yyyy-MM-dd HH24:mi:ss' ) and to_date( #{scadaQuery.endDate}, 'yyyy-MM-dd HH24:mi:ss' )
        </if>
        <if test="scadaQuery.startDay != null and scadaQuery.endDay != null">
            AND to_char(scada.SCADA_TIME,'yyyy-MM-dd') between #{scadaQuery.startDay} and #{scadaQuery.endDay}
        </if>
        <if test="scadaQuery.startDayTime != null and scadaQuery.endDayTime != null">
            AND to_char(scada.SCADA_TIME,'HH24:mi:ss') between #{scadaQuery.startDayTime} and #{scadaQuery.endDayTime}
        </if>
        ORDER BY scada.SCADA_TIME ASC
    </select>

    <select id="selectHistoryByMouthsTableList" resultType="com.tofly.scada.entity.vo.StatisticsScadaVo">
        SELECT scada.ID,
        scada.CODE,
        case WHEN trim(translate(scada.VALUE,'-0123456789.',' ')) is NULL THEN
        decode(SUBSTR(to_char(ROUND(to_number(scada.VALUE),nvl(allocation.accuracy,2))),1,1),'.','0'||to_char(ROUND(to_number(scada.VALUE),nvl(allocation.accuracy,2))),to_char(ROUND(to_number(scada.VALUE),nvl(allocation.accuracy,2))))
        ELSE scada.VALUE END VALUE,
        scada.SCADA_TIME,
        scada.UPDATE_TIME,
        manage.NAME name,
        manage.MAKE_DATE makeDate,
        manage.TYPE type,
        manage.MANUFACTURER manufacturer,
        manage.LEADER leader,
        manage.PHONE phone,
        allocation.DISPLAY_NAME displayName,
        allocation.VARIABLE_NAME variableName,
        allocation.UNIT unit
        FROM ${mouthTable} scada
        LEFT JOIN TF_INDEX_ALLOCATION allocation ON allocation.VARIABLE_CODE = scada.CODE
        LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE manage ON manage.ID = allocation.DEVICE_ID
        WHERE 1 = 1
        <if test="scadaQuery.codes != null">
            AND allocation.VARIABLE_CODE in
            <foreach item="item" index="index" collection="scadaQuery.codes" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="scadaQuery.dataTypeIds != null">
            AND allocation.TYPE in
            <foreach item="item" index="index" collection="scadaQuery.dataTypeIds" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="scadaQuery.code != null">
            AND scada.CODE = #{scadaQuery.code}
        </if>
        <if test="scadaQuery.deviceIds != null and scadaQuery.deviceIds.size()>0 ">
            AND manage.ID in
            <foreach collection="scadaQuery.deviceIds" item="index" open="(" close=")" separator=",">
                #{index}
            </foreach>
        </if>
        <if test="scadaQuery.start != null and scadaQuery.end != null">
            AND scada.SCADA_TIME between #{scadaQuery.start} and #{scadaQuery.end}
        </if>
        <if test="scadaQuery.startDate != null and scadaQuery.endDate != null">
            AND scada.SCADA_TIME between to_date( #{scadaQuery.startDate}, 'yyyy-MM-dd HH24:mi:ss' ) and to_date( #{scadaQuery.endDate}, 'yyyy-MM-dd HH24:mi:ss' )
        </if>
        <if test="scadaQuery.startDay != null and scadaQuery.endDay != null">
            AND to_char(scada.SCADA_TIME,'yyyy-MM-dd') between #{scadaQuery.startDay} and #{scadaQuery.endDay}
        </if>
        <if test="scadaQuery.startDayTime != null and scadaQuery.endDayTime != null">
            AND to_char(scada.SCADA_TIME,'HH24:mi:ss') between #{scadaQuery.startDayTime} and #{scadaQuery.endDayTime}
        </if>

        ORDER BY scada.SCADA_TIME ASC
    </select>

    <!--    <select id="selectHistoryByTime" resultType="com.tofly.scada.entity.Scada">-->
    <!--        select  * from ${tableName}-->
    <!--        <if test="start!=null and start!='' and end!=null and end!=''">-->
    <!--            where SCADA_TIME between to_date(#{start},'yyyy-mm-dd hh24:mi:ss') and to_date(#{end},'yyyy-mm-dd hh24:mi:ss')-->
    <!--        </if>-->
    <!--    </select>-->
    <select id="selectHistoryMaxAndMinByTime" resultType="com.tofly.scada.entity.Scada">
        select CODE,
        <choose>
            <when test="dataType = null or dataType = 0">
                to_char(SCADA_TIME,'yyyy-mm-dd hh24') as scadaTimeStr
            </when>
            <when test="dataType = 1">
                to_char(SCADA_TIME,'yyyy-mm-dd') as scadaTimeStr
            </when>
            <otherwise>
                to_char(SCADA_TIME,'yyyy-mm') as scadaTimeStr
            </otherwise>
        </choose>
        from ${tableName} where 1=1
        <if test="codes!=null and codes.size()>0">
            and code in
            <foreach collection="codes" item="index" open="(" close=")" separator=",">
                #{index}
            </foreach>
        </if>
        <if test="start!=null and start!='' and end!=null and end!=''">
            and UPDATE_TIME between to_date(#{start},'yyyy-mm-dd hh24:mi:ss') and to_date(#{end},'yyyy-mm-dd
            hh24:mi:ss')
        </if>
        <choose>
            <when test="dataType = null or dataType = 0">
                group by CODE,to_char(SCADA_TIME,'yyyy-mm-dd hh24')
            </when>
            <when test="dataType = 1">
                group by CODE,to_char(SCADA_TIME,'yyyy-mm-dd')
            </when>
            <otherwise>
                group by CODE,to_char(SCADA_TIME,'yyyy-mm')
            </otherwise>
        </choose>
    </select>

    <select id="selectCodesComm" resultType="com.tofly.scada.entity.vo.StatisticsScadaCalVo">
        select t1.code, to_number(t1.value) as minValue, to_number(t2.value) as maxValue,
        <choose>
            <when test="dataType == null or dataType == 0">
                to_char(t1.SCADA_TIME,'yyyy-mm-dd hh24')
            </when>
            <when test="dataType == 1">
                to_char(t1.SCADA_TIME,'yyyy-mm-dd')
            </when>
            <when test="dataType == 2">
                to_char(t1.SCADA_TIME,'yyyy-mm')
            </when>
            <otherwise>
                to_char(t1.SCADA_TIME,'yyyy')
            </otherwise>
        </choose> dateTimeStr,
        t3.type allocationType
        from (select *
              from (select t.*,
                           row_number() over(partition by t.code order by t.scada_time) num
                    from scada_history t
                    where t.code in
        <foreach collection="codes" item="index" open="(" close=")" separator=",">
            #{index}
        </foreach>
                      and t.scada_time between #{start} and #{end}
                      and value != '0') t
              where num = 1) t1 inner join
             (select *
              from (select t.*,
                           row_number() over(partition by t.code order by t.scada_time desc) num
                    from scada_history t
                    where t.code in
        <foreach collection="codes" item="index" open="(" close=")" separator=",">
            #{index}
        </foreach>
        and t.scada_time between #{start} and #{end}
                      and value != '0') t
              where num = 1) t2
        on t1.code = t2.code
left join TF_INDEX_ALLOCATION t3 on t1.code = t3.VARIABLE_CODE

        <choose>
            <when test="dataType == null or dataType == 0">
                group by t1.code, t1.value, t2.value, t3.type, to_char(t1.SCADA_TIME,'yyyy-mm-dd hh24')
            </when>
            <when test="dataType == 1">
                group by t1.code, t1.value, t2.value,  t3.type, to_char(t1.SCADA_TIME,'yyyy-mm-dd')
            </when>
            <when test="dataType == 2">
                group by t1.code, t1.value, t2.value, t3.type, to_char(t1.SCADA_TIME,'yyyy-mm')
            </when>

            <otherwise>
                group by t1.code, t1.value, t2.value, t3.type, to_char(t1.SCADA_TIME,'yyyy')
            </otherwise>
        </choose>
    </select>

<select id="getHistoryAvgValueByScadaTime" resultType="BigDecimal">
    SELECT
        avg(t1.value) avgValue
    FROM SCADA_HISTORY t1 RIGHT JOIN TF_INDEX_ALLOCATION t2 on t1.code = t2.VARIABLE_CODE
    WHERE t1.SCADA_TIME between #{start} and #{end}
    and t1.code=#{code}

</select>

    <select id="selectHistoryMaxAndMinByScadaTime" resultType="com.tofly.scada.entity.vo.StatisticsScadaCalVo">
        SELECT t1.code,
        <choose>
            <when test="dataType == null or dataType == 0">
                to_char(t1.SCADA_TIME,'yyyy-mm-dd hh24')
            </when>
            <when test="dataType == 1">
                to_char(t1.SCADA_TIME,'yyyy-mm-dd')
            </when>
            <otherwise>
                to_char(t1.SCADA_TIME,'yyyy-mm')
            </otherwise>
        </choose>
        dateTimeStr,
        regexp_replace(to_char(max(t1.value),'FM99999990.9999'), '\.$', '') maxValue ,
        regexp_replace(to_char(min(t1.value),'FM99999990.9999'), '\.$', '') minValue,
        regexp_replace(to_char(avg(t1.value),'FM99999990.9999'), '\.$', '') avgValue,
        regexp_replace(to_char((max(t1.value)-min(t1.value)) ,'FM99999990.9999'), '\.$', '') differenceValue,
        t2.type allocationType
        FROM SCADA_HISTORY t1 RIGHT JOIN TF_INDEX_ALLOCATION t2 on t1.code = t2.VARIABLE_CODE
        WHERE t1.SCADA_TIME between #{start} and #{end}
        <if test="codes!=null and codes.size()>0">
            and t1.code in
            <foreach collection="codes" item="index" open="(" close=")" separator=",">
                #{index}
            </foreach>
        </if>
        GROUP BY t1.code ,t2.type,
        <choose>
            <when test="dataType == null or dataType == 0">
                to_char(t1.SCADA_TIME,'yyyy-mm-dd hh24')
            </when>
            <when test="dataType == 1">
                to_char(t1.SCADA_TIME,'yyyy-mm-dd')
            </when>
            <otherwise>
                to_char(t1.SCADA_TIME,'yyyy-mm')
            </otherwise>
        </choose>
    </select>




    <select id="selectHistoryMaxAndMinByScadaTimeToOne" resultType="com.tofly.scada.entity.Scada">
        SELECT * FROM (
        select * from ${tableName} where 1=1
        <if test="codes!=null and codes.size()>0">
            and code in
            <foreach collection="codes" item="index" open="(" close=")" separator=",">
                #{index}
            </foreach>
        </if>
        <if test="time!=null and time != ''">
            <choose>
                <when test="dataType = null or dataType = 0">
                    and to_char(SCADA_TIME,'yyyy-mm-dd hh24') = #{time}
                </when>
                <when test="dataType = 1">
                    and to_char(SCADA_TIME,'yyyy-mm-dd') = #{time}
                </when>
                <otherwise>
                    and to_char(SCADA_TIME,'yyyy-mm') = #{time}
                </otherwise>
            </choose>
        </if>
        order by value asc
        )
        WHERE ROWNUM = 1
        UNION ALL
        SELECT * FROM (
        select * from SCADA_HISTORY where 1=1
        <if test="codes!=null and codes.size()>0">
            and code in
            <foreach collection="codes" item="index" open="(" close=")" separator=",">
                #{index}
            </foreach>
        </if>
        <if test="time!=null and time != ''">
            <choose>
                <when test="dataType = null or dataType == 0">
                    and to_char(SCADA_TIME,'yyyy-mm-dd hh24') = #{time}
                </when>
                <when test="dataType == 1">
                    and to_char(SCADA_TIME,'yyyy-mm-dd') = #{time}
                </when>
                <otherwise>
                    and to_char(SCADA_TIME,'yyyy-mm') = #{time}
                </otherwise>
            </choose>
        </if>
        order by value desc
        )
        WHERE ROWNUM = 1
    </select>

    <delete id="deleteFromTableName">
        delete from ${tableName}
        <if test="start!=null and end!=null">
            where SCADA_TIME between to_date(#{start},'yyyy-mm-dd hh24:mi:ss') and to_date(#{end},'yyyy-mm-dd
            hh24:mi:ss')
        </if>
    </delete>

    <select id="getNetCumulativeFlow" resultType="com.tofly.scada.entity.vo.NetCumulativeFlow">
        SELECT m3.ID DEVICE_ID,
               m1.CODE,
               m1.VALUE,
               TO_CHAR(m1.SCADA_TIME,'yyyy-mm-dd hh24:mi') SCADA_TIME,
               m2.TYPE INDEX_TYPE
        FROM SCADA_HISTORY m1
                 LEFT JOIN TF_INDEX_ALLOCATION m2 ON m1.CODE = m2.VARIABLE_CODE
                 LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE m3 ON m2.DEVICE_ID = m3.ID
        WHERE
            TO_CHAR(m1.SCADA_TIME,'yyyy-mm-dd hh24:mi:ss') BETWEEN #{startTime} AND #{endTime}
--           AND m3.DEVICE_TYPE IN ('2', '3')
          AND m2.TYPE IN ('2', '3')
    </select>

    <select id="selectHistoryRunTimeByScadaTime" resultType="com.tofly.scada.entity.vo.StatisticsScadaCalVo">
        WITH temp AS (
        SELECT
        CODE,
        LEAD ( CODE ) over ( ORDER BY CODE, SCADA_TIME ) LATER_CODE,
        LAG ( VALUE ) over ( ORDER BY CODE, SCADA_TIME ) FRONT_VALUE,
        VALUE,
        LEAD ( VALUE ) over ( ORDER BY CODE, SCADA_TIME ) LATER_VALUE,
        LAG ( SCADA_TIME ) over ( ORDER BY CODE, SCADA_TIME ) FRONT_SCADA_TIME,
        SCADA_TIME,
        LEAD ( SCADA_TIME ) over ( ORDER BY CODE, SCADA_TIME ) LATER_SCDA_TIME
        FROM
        SCADA_HISTORY
        WHERE SCADA_TIME between #{start} and #{end}
        <if test="codes!=null and codes.size()>0">
            and code in
            <foreach collection="codes" item="index" open="(" close=")" separator=",">
                #{index}
            </foreach>
        </if>
        ),
        temp2 AS (
        SELECT
        CODE,
        FRONT_VALUE,
        VALUE,
        LATER_VALUE,
        FRONT_SCADA_TIME,
        SCADA_TIME,
        LATER_SCDA_TIME,
        (LATER_SCDA_TIME - SCADA_TIME) *24*60*60 AS difference
        FROM temp
        WHERE CODE = LATER_CODE
        ),
        temp3 as (
        SELECT
        CODE,
        FRONT_VALUE,
        VALUE,
        LATER_VALUE,
        FRONT_SCADA_TIME,
        SCADA_TIME,
        LATER_SCDA_TIME,
        difference
        FROM temp2
        )

        SELECT CODE,VALUE,floor(SUM(difference)) as runTime FROM temp3 GROUP BY CODE,VALUE
    </select>


    <select id="viewScadaData" resultType="com.tofly.scada.entity.vo.StationIndexVo">

        SELECT  to_char(a.start_time,'yyyy-MM-dd HH:mm:ss') scada_time,
        fold index_value,
        c.id device_id
        FROM statistics_scada a
        LEFT JOIN tf_index_allocation b ON b.variable_code = a.code
        LEFT JOIN tf_device_archive_manage c ON b.device_id = c.id
        WHERE
        1=1 AND b.type = '19'
        <if test="startDate != null and endDate != null">
            AND to_char(a.start_time,'yyyy-MM-dd HH:mm:ss') between #{startDate} and #{endDate}
        </if>
        <if test="statisticsType != null">
            AND a.statistics_type = #{statisticsType}
        </if>
        <if test="indexCodeList != null">
            AND b.variable_code IN
            <foreach item="item" index="index" collection="indexCodeList" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="deviceIdList != null">
            AND c.id IN
            <foreach item="item" index="index" collection="deviceIdList" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="deviceName != null and deviceName.trim() != ''">
            AND c.name LIKE '%'||#{deviceName}||'%'
        </if>
        <if test="stationIds != null and stationIds != ''">
            AND to_char(c.WATER_ID) in
            <foreach item="item" collection="stationIds.split(',')" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ORDER BY scada_time DESC
    </select>

    <select id="viewScadaDataNew" resultType="com.tofly.scada.entity.vo.WaterIndexInfoVo">

        select b.device_id, c.name device_name, b.variable_code, b.display_name
        from tf_device_archive_manage c
                 inner join tf_index_allocation b
                            on c.id = b.device_id
        where b.type = 19
        <if test="dto.indexCodeList != null">
            AND b.variable_code IN
            <foreach item="item" index="index" collection="dto.indexCodeList" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.deviceIdList != null">
            AND c.id IN
            <foreach item="item" index="index" collection="dto.deviceIdList" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>

        <if test="dto.deviceTypeList != null">
            AND c.type IN
            <foreach item="item" index="index" collection="dto.deviceTypeList" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>

        <if test="dto.deviceName != null and dto.deviceName.trim() != ''">
            AND c.name LIKE '%'||#{dto.deviceName}||'%'
        </if>
        order by b.device_id, b.sort_number

    </select>
    <select id="getWaterIndexInfo" resultType="com.tofly.scada.entity.vo.WaterIndexValueVo">
        SELECT
        <choose>
            <when test="statisticsType != null and statisticsType == 0">
                 to_char(start_time,'yyyy-mm-dd hh24')
            </when>
            <when test="statisticsType == 1">
                 to_char(start_time,'yyyy-mm-dd')
            </when>
            <otherwise>
                 to_char(start_time,'yyyy-mm')
            </otherwise>
        </choose>
        as scada_time,
        fold index_value
        FROM statistics_scada a
        LEFT JOIN tf_index_allocation b ON b.variable_code = a.code
        LEFT JOIN tf_device_archive_manage c ON b.device_id = c.id
        WHERE
        b.variable_code=#{variableCode}
        AND a.statistics_type = #{statisticsType}
        AND a.start_time between #{startDate} and #{endDate}
        ORDER BY a.start_time
    </select>


    <select id="selectLastOne" resultMap="scadaHisMap">
        select * from(
                         select * from scada_history t
                         where t.code=#{code}
        <![CDATA[and t.scada_time<#{scadaTime} ]]>
         order by scada_time desc
                     ) t where rownum=1

    </select>


    <select id="selectMonitoringNew" resultType="com.tofly.scada.entity.vo.ScadaVo">
        select t1.code,
        t1.value,
        t1.scada_time,
        t2.unit,
        t2.display_name,
        t2.type,
        t3.name,
        t3.water_id,
        t2.index_max
        from SCADA_MONITORING t1
        inner join tf_index_allocation t2
        on t1.code = t2.variable_code
        inner join tf_device_archive_manage t3
        on t2.device_id = t3.id
        inner join tf_water_info info
        on t3.water_id=info.id
        <if test="codes!=null and codes.size()>0">
            and t1.code in
            <foreach collection="codes" item="index" open="(" close=")" separator=",">
                #{index}
            </foreach>
        </if>

    </select>

    <select id="viewScadaDataShort" resultType="com.tofly.scada.entity.vo.ScadaIndexVo">
        SELECT
        <choose>
            <when test="tableType == 2">
                to_char(a.start_time,'yyyy-mm-dd hh24:mi:ss') scada_time,
                (a.max - a.min) index_value,
                a.max index_value_max,
                a.min index_value_min,
                a.ave index_value_avg,
                a.fold index_value_fold,
                a.all_count,
                a.pass pass_rate,
                a.error_count,
            </when>
            <when test="tableType == 1 or tableType == 3">
                to_char(a.scada_time,'yyyy-mm-dd hh24:mi:ss') scada_time,
                a.value index_value,
            </when>
        </choose>
        a.code index_code,
        b.VARIABLE_CODE index_name,
        b.type index_type_id,
        b.unit index_unit,
        b.REPORT_ID report_ids,
        c.id device_id,
        c.code device_code,
        c.name device_name
        FROM ${tableName} a
        LEFT JOIN tf_index_allocation b ON b.variable_code = a.code
        LEFT JOIN tf_device_archive_manage c ON b.device_id = c.id
        WHERE
        1=1
        <choose>
            <when test="tableType == 2">
                <if test="startDate != null and endDate != null">
                    AND to_char(a.start_time,'yyyy-mm-dd hh24:mi:ss') between #{startDate} and #{endDate}
                </if>
            </when>
            <when test="tableType == 1 or tableType == 3">
                <if test="startDate != null and endDate != null">
                    AND to_char(a.scada_time,'yyyy-mm-dd hh24:mi:ss') between #{startDate} and #{endDate}
                </if>
            </when>
        </choose>
        <if test="tableType == 2 and statisticsType != null">
            AND a.statistics_type = #{statisticsType}
        </if>
        <if test="indexCodeList != null">
            AND b.variable_code IN
            <foreach item="item" index="index" collection="indexCodeList" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="indexType != null">
            AND b.type = #{indexType}
        </if>
        <if test="indexTypeList != null">
            AND b.type IN
            <foreach item="item" index="index" collection="indexTypeList" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="deviceIdList != null">
            AND c.id IN
            <foreach item="item" index="index" collection="deviceIdList" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        ORDER BY scada_time DESC
    </select>

</mapper>