<?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.yeejoin.equip.mapper.tdengine.IndicatorDataMapper">
    <!--创建数据库，指定压缩比-->
    <update id="createDB">
        create database if not exists iot_data vgroups 10 buffer 10 COMP 2 PRECISION 'ns';
    </update>

    <!--创建超级表-->
    <update id="createTable">
        create STABLE if not exists s_indicator_his
               (created_time timestamp,
               address binary(64),
               equipments_idx NCHAR(64),
               data_type NCHAR(12),
               is_alarm BIGINT,
               equipment_index_name VARCHAR(200) ,
               equipment_specific_name VARCHAR(200),
               `value` VARCHAR(12),
               `value_f` float,
                value_label VARCHAR(24),
                unit NCHAR(12))
        TAGS  (gateway_id binary(64));
    </update>

    <insert id="insertBatch" parameterType="java.util.List">
        insert into
        <foreach separator=" " collection="list" item="item" index="index">
            indicator_his_#{gatewayId,jdbcType=VARCHAR} USING s_indicator_his
            TAGS (#{item.gatewayId,jdbcType=VARCHAR})
            VALUES (NOW + #{index}a,
            #{item.address,jdbcType=VARCHAR},
            #{item.equipmentsIdx,jdbcType=VARCHAR},
            #{item.dataType,jdbcType=VARCHAR},
            #{item.isAlarm,jdbcType=VARCHAR},
            #{item.equipmentSpecificName,jdbcType=VARCHAR},
            #{item.equipmentIndexName,jdbcType=VARCHAR},
            #{item.value,jdbcType=VARCHAR},
            #{item.valueF,jdbcType=FLOAT},
            #{item.valueLabel,jdbcType=VARCHAR},
            #{item.unit,jdbcType=VARCHAR})
        </foreach>
    </insert>

<!--    <insert id="insertBatch" parameterType="java.util.List">-->
<!--        INSERT INTO indicator_#{gatewayId,jdbcType=VARCHAR} (created_time, `value`,`value_f`, value_label,unit,-->
<!--        address,gateway_id,equipments_idx,data_type,is_alarm,equipment_index_name,equipment_specific_name)-->
<!--        VALUES-->
<!--        <foreach collection="list" item="item" separator="UNION ALL" index="index">-->
<!--            SELECT NOW + #{index}a, #{item.value}, #{item.valueF}, #{item.valueLabel}, #{item.unit},-->
<!--            #{item.address}, #{item.gatewayId}, #{item.equipmentsIdx}, #{item.dataType}, #{item.isAlarm},-->
<!--            #{item.equipmentSpecificName},#{item.equipmentIndexName}-->
<!--        </foreach>-->
<!--    </insert>-->

    <insert id="insert" parameterType="com.yeejoin.equip.entity.IndicatorData">
        insert into indicator_#{gatewayId,jdbcType=VARCHAR} USING indicator
        TAGS (#{address,jdbcType=VARCHAR},
            #{gatewayId,jdbcType=VARCHAR},
            #{equipmentsIdx,jdbcType=VARCHAR},
            #{dataType,jdbcType=VARCHAR},
            #{isAlarm,jdbcType=VARCHAR},
            #{equipmentSpecificName,jdbcType=VARCHAR},
            #{equipmentIndexName,jdbcType=VARCHAR})
        VALUES (NOW, #{value,jdbcType=VARCHAR}, #{valueF,jdbcType=FLOAT}, #{valueLabel,jdbcType=VARCHAR}, #{unit,jdbcType=VARCHAR})
    </insert>
</mapper>