<?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.StatisticsScadaMapper">

    <resultMap id="BaseResultMap" type="com.tofly.scada.entity.StatisticsScada">
            <id property="id" column="ID" jdbcType="DECIMAL"/>
            <result property="code" column="CODE" jdbcType="VARCHAR"/>
            <result property="value" column="VALUE" jdbcType="VARCHAR"/>
            <result property="statisticsType" column="STATISTICS_TYPE" jdbcType="DECIMAL"/>
            <result property="startTime" column="START_TIME" jdbcType="TIMESTAMP"/>
            <result property="endTime" column="END_TIME" jdbcType="TIMESTAMP"/>
            <result property="max" column="MAX" jdbcType="VARCHAR"/>
            <result property="min" column="MIN" jdbcType="VARCHAR"/>
            <result property="ave" column="AVE" jdbcType="VARCHAR"/>
            <result property="fold" column="FOLD" jdbcType="VARCHAR"/>
            <result property="dataType" column="DATA_TYPE" jdbcType="DECIMAL"/>
    </resultMap>

    <sql id="Base_Column_List">
        ID,CODE,VALUE,
        STATISTICS_TYPE,START_TIME,END_TIME,
        MAX,MIN,AVE,
        FOLD,DATA_TYPE
    </sql>

    <delete id="deleteByTime">
        DELETE FROM STATISTICS_SCADA
        WHERE STATISTICS_TYPE = #{dateType}
        <if test="codes!=null and codes.size()>0">
            and code in
            <foreach collection="codes" item="code" open="(" close=")" separator=",">
                #{code}
            </foreach>
        </if>
        AND START_TIME >= #{start}
        AND END_TIME &lt;= #{end}

    </delete>

    <select id="selectHistoryByMouthsTables" resultType="com.tofly.scada.entity.vo.ScadaVo">
        SELECT * FROM
        (
        <foreach collection="mouthTables" item="item" separator="union all">
            SELECT scada.*,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 ${item} scada
            INNER JOIN TF_INDEX_ALLOCATION allocation ON allocation.VARIABLE_CODE =  scada.CODE
            INNER JOIN TF_DEVICE_ARCHIVE_MANAGE manage ON manage.ID = allocation.DEVICE_ID
            ${ew.customSqlSegment}
        </foreach>
        ) DATA ORDER BY SCADA_TIME ASC
    </select>

    <select id="selectHistoryByMouthsTablesNoPage" resultType="com.tofly.scada.entity.vo.ScadaVo">
        SELECT * FROM
        (
        <foreach collection="mouthTables" item="item" separator="union all">
            SELECT scada.*,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 ${item} scada
            INNER JOIN TF_INDEX_ALLOCATION allocation ON allocation.VARIABLE_CODE =  scada.CODE
            INNER JOIN TF_DEVICE_ARCHIVE_MANAGE manage ON manage.ID = allocation.DEVICE_ID
            ${ew.customSqlSegment}
        </foreach>
        ) DATA ORDER BY SCADA_TIME ASC
    </select>

    <insert id="insertBacthHistoryTemp">
        begin
        <foreach collection="historyTempList" item="item" index="index" separator=";">
            insert /*+ IGNORE_ROW_ON_DUPKEY_INDEX(SCADA_HISTORY(CODE,SCADA_TIME)) */ into SCADA_HISTORY
            (
            ID,
            CODE,
            VALUE,
            SCADA_TIME,
            UPDATE_TIME
            ) values (
            #{item.id,jdbcType=NUMERIC},
            #{item.code,jdbcType=VARCHAR},
            #{item.value,jdbcType=VARCHAR},
            #{item.scadaTime},
            #{item.updateTime}
            )
        </foreach>
        ;end;
    </insert>

    <select id="selectScadaHistoryList" resultType="com.tofly.scada.entity.vo.ScadaDeviceCodeVo">
        SELECT t1.*, t3.CODE DEVICE_CODE, t2.TYPE INDEX_TYPE
        FROM SCADA_HISTORY t1
            LEFT JOIN TF_INDEX_ALLOCATION t2 ON t1.CODE = t2.VARIABLE_CODE
            LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE t3 ON t2.DEVICE_ID = t3.ID
        WHERE
            1 = 1
        <if test="startDate != null and endDate != null">
            AND to_char(t1.SCADA_TIME, 'yyyy-mm-dd hh24:mi:ss') BETWEEN #{startDate} AND #{endDate}
        </if>
            AND t3.CODE IN ('FT_A_17', 'FT_A_19', 'FT_A_21', 'FT_A_23', 'FT_A_6', 'FT_A_7', 'FT_B_22', 'FT_B_20', 'FT_B_4','FT_A_22', 'FT_B_32', 'FT_A_20')
            AND t2.TYPE IN ('13', '17', '19')
        ORDER BY t1.SCADA_TIME DESC
    </select>

    <update id="updateRealBatchTemp">
        begin
        <foreach collection="tempList" item="temp" index="index" separator=";">
            update SCADA_MONITOR
            SET
            CODE = #{temp.code},
            SCADA_TIME = #{temp.scadaTime},
            UPDATE_TIME = #{temp.updateTime}
            WHERE 1=1
            <if test="temp.code != null">
                AND CODE = #{temp.code}
            </if>
        </foreach>
        ;end;
    </update>

    <!-- 待更新的异常点位指标集(瞬时流量、正向累计流量、净累计流量)-->
    <select id="getAbnormalPointIndexList" resultType="map">
        SELECT
            a.VARIABLE_CODE INDEX_CODE, a.TYPE, b.CODE DEVICE_CODE
        FROM
            TF_INDEX_ALLOCATION a
                LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE b ON a.DEVICE_ID = b.ID
        WHERE
            1 = 1
          AND b.CODE IN ('FT_B_15', 'FT_A_3', 'FT_A_7', 'FT_A_8', 'FT_A_9', 'FT_A_10', 'FT_A_14', 'FT_A_16', 'FT_A_24', 'FT_A_25',
                         'FT_B_5', 'FT_B_20', 'FT_B_31', 'FT_A_11')
          AND a.TYPE IN ('13','17','19')
    </select>

<!--    <select id="getNormalPointIndexList" resultType="map">-->
<!--        SELECT-->
<!--            a.VARIABLE_CODE INDEX_CODE, a.TYPE, b.CODE DEVICE_CODE-->
<!--        FROM-->
<!--            TF_INDEX_ALLOCATION a-->
<!--                LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE b ON a.DEVICE_ID = b.ID-->
<!--        WHERE-->
<!--            1 = 1-->
<!--          AND (-->
<!--                b.CODE = 'FT-A-17'-->
<!--                OR b.CODE = 'FT-A-20'-->
<!--                OR b.CODE = 'FT-A-22'-->
<!--                OR b.CODE = 'FT-A-6'-->
<!--                OR b.CODE = 'FT-A-25'-->
<!--                OR b.CODE = 'FT-A-19'-->
<!--                OR b.CODE = 'FT-A-23'-->
<!--                OR b.CODE = 'FT-A-21'-->
<!--                OR b.CODE = 'FT-A-10'-->
<!--            )-->
<!--          AND (-->
<!--                    a.TYPE = '13'-->
<!--                OR a.TYPE = '17'-->
<!--                OR a.TYPE = '19')-->
<!--    </select>-->
</mapper>
