<?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="org.dromara.mapper.TblSensorRecordMapper">

    <select id="createTable" parameterType="java.util.Map">
        create table if not exists tbl_sensor_record_${datetime} (
                                                                     id bigint not null auto_increment,
                                                                     sensor_id bigint not null,
                                                                     point_name varchar(255) not null,
            point_value varchar(255) not null,
            create_dept varchar(64) default null,
            create_by varchar(64) default null,
            create_time datetime default null,
            update_by varchar(64) default null,
            update_time datetime default null,
            PRIMARY KEY (id)
            )
    </select>

    <insert id="insertData" parameterType="org.dromara.domain.TblSensorRecord" useGeneratedKeys="true" keyProperty="id">
        insert into tbl_sensor_record_${params.datetime} (id,sensor_id,
                                                          point_name,
                                                          point_value,create_time) values (#{id}, #{sensorId},
                                                                               #{pointName},
                                                                               #{pointValue},
                                                                                           #{createTime}

                                                                              )
    </insert>

    <select id="selectData" parameterType="org.dromara.domain.TblSensorRecord"
            resultType="org.dromara.domain.vo.TblSensorRecordVo">
       select * from (
        <foreach collection="params.datetimes" item="datav" separator=" UNION ALL ">
            select id,sensor_id,
            point_name,
            point_value,
            create_dept,
            create_by,
            create_time,
            update_by,
            update_time from tbl_sensor_record_${datav} where 1=1
            <if test="id!=null">
                and id=#{id}
            </if>
            <if test="sensorId!=null">
                and sensor_id=#{sensorId}
            </if>
            <if test="pointName!=null">
                and point_name=#{pointName}
            </if>
            <if test="pointValue!=null">
                and point_value=#{pointValue}
            </if>
            <if test="createDept!=null">
                and create_dept=#{createDept}
            </if>
            <if test="createBy!=null">
                and create_by=#{createBy}
            </if>
            <if test="params!=null and params.startTime!=null and params.endTime!=null">
              <![CDATA[
               and create_time > #{params.startTime} and create_time < #{params.endTime}
              ]]>
            </if>

        </foreach>
        ) AS t ORDER BY
        create_time DESC;
    </select>

    <select id="selectDataPage"
            resultType="org.dromara.domain.vo.TblSensorRecordVo">
        select * from (
        <foreach collection="t.params.datetimes" item="datav" separator=" UNION ALL ">
            select id,sensor_id,
            point_name,
            point_value,
            create_dept,
            create_by,
            create_time,
            update_by,
            update_time from tbl_sensor_record_${datav} where 1=1
            <if test="t.id!=null">
                and id=#{t.id}
            </if>
            <if test="t.sensorId!=null">
                and sensor_id=#{t.sensorId}
            </if>
            <if test="t.pointName!=null">
                and point_name=#{t.pointName}
            </if>
            <if test="t.pointValue!=null">
                and point_value=#{t.pointValue}
            </if>
            <if test="t.createDept!=null">
                and create_dept=#{t.createDept}
            </if>
            <if test="t.createBy!=null">
                and create_by=#{t.createBy}
            </if>
            <if test="t.params!=null and t.params.startTime!=null and t.params.endTime!=null">
                <![CDATA[
               and create_time > #{t.params.startTime} and create_time < #{t.params.endTime}
              ]]>
            </if>

        </foreach>
        ) AS t ORDER BY
        create_time DESC
    </select>


</mapper>
