<?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 "TF_HNLS"."${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 "TF_HNLS"."SCADA_MONITOR" ("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 "TF_HNLS"."${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 "TF_HNLS"."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 "TF_HNLS"."SCADA_MONITOR"
        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
        ROUND(to_number(scada.VALUE),nvl(allocation.accuracy,0)) ELSE 0 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 allocation.VARIABLE_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>
        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
        ROUND(to_number(scada.VALUE),nvl(allocation.accuracy,0)) ELSE 0 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 allocation.VARIABLE_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>
        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="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 ('17', '18')
    </select>

    <insert id="insertBatchTempHistory">
        begin
        <foreach collection="tempList" item="item" index="index" separator=";">
            insert 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>

    <insert id="insertBatchTempMonitor">
        begin
        <foreach collection="tempList" item="item" index="index" separator=";">
            insert into SCADA_MONITOR
            (
                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>
</mapper>