<?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="statisticsScadaMap" 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}

    </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>
</mapper>
