<?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.spire.mapper.SpireBigGridMapper">

    <resultMap type="com.tofly.spire.entity.SpireBigGrid" id="SpireBigGridMap">
        <result property="id" column="ID" jdbcType="INTEGER"/>
        <result property="accFlow" column="ACC_FLOW" jdbcType="VARCHAR"/>
        <result property="decAccFlow" column="DEC_ACC_FLOW" jdbcType="VARCHAR"/>
        <result property="flow" column="FLOW" jdbcType="VARCHAR"/>
        <result property="inTemp" column="IN_TEMP" jdbcType="VARCHAR"/>
        <result property="latitude" column="LATITUDE" jdbcType="VARCHAR"/>
        <result property="longitude" column="LONGITUDE" jdbcType="VARCHAR"/>
        <result property="meterName" column="METER_NAME" jdbcType="VARCHAR"/>
        <result property="meterStatus" column="METER_STATUS" jdbcType="VARCHAR"/>
        <result property="meterType" column="METER_TYPE" jdbcType="VARCHAR"/>
        <result property="pressure" column="PRESSURE" jdbcType="VARCHAR"/>
        <result property="readTime" column="READ_TIME" jdbcType="TIMESTAMP"/>
        <result property="sn" column="SN" jdbcType="VARCHAR"/>
        <result property="temp" column="TEMP" jdbcType="VARCHAR"/>
        <result property="uploadTime" column="UPLOAD_TIME" jdbcType="TIMESTAMP"/>
        <result property="createTime" column="CREATE_TIME" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="UPDATE_TIME" jdbcType="TIMESTAMP"/>
        <result property="status" column="STATUS" jdbcType="VARCHAR"/>
    </resultMap>

    <!-- 批量插入 -->
    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into SPIRE_BIG_GRID(ACC_FLOW, DEC_ACC_FLOW, FLOW, IN_TEMP, LATITUDE, LONGITUDE, METER_NAME,
        METER_STATUS, METER_TYPE, PRESSURE, READ_TIME, SN, TEMP, UPLOAD_TIME, CREATE_TIME, UPDATE_TIME)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.accFlow}, #{entity.decAccFlow}, #{entity.flow}, #{entity.inTemp}, #{entity.latitude},
            #{entity.longitude}, #{entity.meterName}, #{entity.meterStatus}, #{entity.meterType}, #{entity.pressure},
            #{entity.readTime}, #{entity.sn}, #{entity.temp}, #{entity.uploadTime}, #{entity.createTime},
            #{entity.updateTime})
        </foreach>
    </insert>
    <!-- 批量插入或按主键更新 -->
    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        <foreach collection='entities' item='temp' separator=';'> MERGE INTO SPIRE_BIG_GRID T1 USING
            (SELECT
            #{temp.readTime, jdbcType=VARCHAR} AS readTime,
            #{temp.sn, jdbcType=VARCHAR} AS sn,
            #{temp.uploadTime, jdbcType=VARCHAR} AS uploadTime FROM dual) T2
            ON
            (
            and T1.readTime = T2.readTime
            and T1.sn = T2.sn
            and T1.uploadTime = T2.uploadTime)
            WHEN NOT MATCHED THEN
            INSERT INTO (ACC_FLOW, DEC_ACC_FLOW, FLOW, IN_TEMP, LATITUDE, LONGITUDE, METER_NAME,
            METER_STATUS, METER_TYPE, PRESSURE, READ_TIME, SN, TEMP, UPLOAD_TIME, CREATE_TIME, UPDATE_TIME)
            VALUES (T2.accFlow, T2.decAccFlow, T2.flow, T2.inTemp, T2.latitude,
                    T2.longitude, T2.meterName, T2.meterStatus, T2.meterType, T2.pressure, T2.readTime,
                    T2.sn, T2.temp, T2.uploadTime,T2.createTime,T2.updateTime)
        </foreach>
    </insert>

    <delete id="removeAllData">
        delete from SPIRE_BIG_GRID
    </delete>

</mapper>

