ZhuDongming
2020-04-28 8aa815dd9fca6dc914c0cba72ada9af5c795a44c
src/main/resources/mapper/HistoryMapper.xml
@@ -1,91 +1,91 @@
<?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.moral.mapper.HistoryMapper">
  <resultMap id="BaseResultMap" type="com.moral.entity.History">
    <result column="mac" jdbcType="VARCHAR" property="mac" />
    <result column="value" jdbcType="OTHER" property="value" />
    <result column="time" jdbcType="TIMESTAMP" property="time" />
    <result column="version" jdbcType="INTEGER" property="version" />
  </resultMap>
  <insert id="insert" parameterType="com.moral.entity.History">
    <resultMap id="BaseResultMap" type="com.moral.entity.History">
        <result column="mac" jdbcType="VARCHAR" property="mac"/>
        <result column="value" jdbcType="OTHER" property="value"/>
        <result column="time" jdbcType="TIMESTAMP" property="time"/>
        <result column="version" jdbcType="INTEGER" property="version"/>
    </resultMap>
    <insert id="insert" parameterType="com.moral.entity.History">
    insert into history (mac, value, time, 
      version)
    values (#{mac,jdbcType=VARCHAR}, #{value,jdbcType=OTHER}, #{time,jdbcType=TIMESTAMP}, 
      #{version,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.moral.entity.History">
    insert into history
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="mac != null">
        mac,
      </if>
      <if test="value != null">
        value,
      </if>
      <if test="time != null">
        time,
      </if>
      <if test="version != null">
        version,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="mac != null">
        #{mac,jdbcType=VARCHAR},
      </if>
      <if test="value != null">
        #{value,jdbcType=OTHER},
      </if>
      <if test="time != null">
        #{time,jdbcType=TIMESTAMP},
      </if>
      <if test="version != null">
        #{version,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
   <select id="getAvgData" resultType="java.util.Map">
      SELECT
         <foreach  collection="sensorKeys" separator="," item="sensorKey">
            AVG(value->'$.${sensorKey}') AS '${sensorKey}'
         </foreach>
      FROM
         history h
      WHERE
         h.time >= #{start}
      AND h.time <![CDATA[<]]> #{end}
      AND   h.mac = #{mac}
   </select>
   <select id="getMaxAndMinData" resultType="java.util.Map">
      SELECT
         <foreach  collection="sensorKeys" separator="," item="sensorKey">
            max(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'max${sensorKey}',
               MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'min${sensorKey}'
         </foreach>
      FROM
         history h
      WHERE
         h.time >= #{start}
      AND h.time <![CDATA[<]]> #{end}
      AND   h.mac = #{mac}
   </select>
   <select id="getMinData" resultType="java.util.Map">
      SELECT
         <foreach  collection="sensorKeys" separator="," item="sensorKey">
            MIN(value->'$.${sensorKey}') AS '${sensorKey}'
         </foreach>
      FROM
         history h
      WHERE
         h.time >= #{start}
      AND h.time <![CDATA[<]]> #{end}
      AND   h.mac = #{mac}
   </select>
    <insert id="insertSelective" parameterType="com.moral.entity.History">
        insert into history
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mac != null">
                mac,
            </if>
            <if test="value != null">
                value,
            </if>
            <if test="time != null">
                time,
            </if>
            <if test="version != null">
                version,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mac != null">
                #{mac,jdbcType=VARCHAR},
            </if>
            <if test="value != null">
                #{value,jdbcType=OTHER},
            </if>
            <if test="time != null">
                #{time,jdbcType=TIMESTAMP},
            </if>
            <if test="version != null">
                #{version,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>
    <select id="getAvgData" resultType="java.util.Map">
        SELECT
        <foreach collection="sensorKeys" separator="," item="sensorKey">
            AVG(value->'$.${sensorKey}') AS '${sensorKey}'
        </foreach>
        FROM
        history h
        WHERE
        h.time >= #{start}
        AND h.time <![CDATA[<]]> #{end}
        AND h.mac = #{mac}
    </select>
    <select id="getMaxAndMinData" resultType="java.util.Map">
        SELECT
        <foreach collection="sensorKeys" separator="," item="sensorKey">
            max(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'max${sensorKey}',
            MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'min${sensorKey}'
        </foreach>
        FROM
        history h
        WHERE
        h.time >= #{start}
        AND h.time <![CDATA[<]]> #{end}
        AND h.mac = #{mac}
    </select>
    <select id="getMinData" resultType="java.util.Map">
        SELECT
        <foreach collection="sensorKeys" separator="," item="sensorKey">
            MIN(value->'$.${sensorKey}') AS '${sensorKey}'
        </foreach>
        FROM
        history h
        WHERE
        h.time >= #{start}
        AND h.time <![CDATA[<]]> #{end}
        AND h.mac = #{mac}
    </select>
    <select id="getSensorData" resultType="java.util.LinkedHashMap">
        SELECT
        h.mac,
        <foreach  collection="sensorKeys" separator="," item="sensorKey">
        <foreach collection="sensorKeys" separator="," item="sensorKey">
            AVG(value->'$.${sensorKey}') AS '${sensorKey}',
            MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
            MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
@@ -102,7 +102,7 @@
    <select id="getSensorDataByMac" resultType="java.util.LinkedHashMap">
        SELECT
        h.mac,
        <foreach  collection="sensorKeys" separator="," item="sensorKey">
        <foreach collection="sensorKeys" separator="," item="sensorKey">
            AVG(value->'$.${sensorKey}') AS '${sensorKey}',
            MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
            MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
@@ -120,10 +120,31 @@
        h.mac
    </select>
    <select id="getSensorDataByMacOnce" resultType="java.util.LinkedHashMap">
        SELECT
        h.mac,DATE_FORMAT(time,'%Y-%m-%dT%H:%i') time,
        <foreach collection="sensorKeys" separator="," item="sensorKey">
            AVG(value->'$.${sensorKey}') AS '${sensorKey}',
            MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
            MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
        </foreach>
        FROM
        history h
        WHERE
        h.time >= #{start}
        AND h.time <![CDATA[<]]> #{end}
        and h.mac in
        <foreach collection="macList" index="index" item="mac" open="(" separator="," close=")">
            #{mac}
        </foreach>
        group by
        h.mac,DATE_FORMAT(time,'%Y-%m-%dT%H:%i')
    </select>
    <select id="getSensorDataOnce" resultType="java.util.LinkedHashMap">
        SELECT
        h.mac,DATE_FORMAT(time,'%Y-%m-%dT%H:%i') time,
        <foreach  collection="sensorKeys" separator="," item="sensorKey">
        <foreach collection="sensorKeys" separator="," item="sensorKey">
            AVG(value->'$.${sensorKey}') AS '${sensorKey}',
            MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MAX${sensorKey}',
            MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,3))) AS 'MIN${sensorKey}'
@@ -137,4 +158,22 @@
        h.mac,DATE_FORMAT(time,'%Y-%m-%dT%H:%i')
    </select>
    <select id="selectByMacAndTime" resultType="com.moral.entity.History">
        SELECT *
        from history
        WHERE time = #{time} and mac in
        <foreach collection="macList" index="index" item="mac" open="(" separator="," close=")">
            #{mac}
        </foreach>
    </select>
    <insert id="insertHistorySpecialTable">
        insert into
        history_special(mac, value, time,version)
        SELECT * from history WHERE time>=DATE_SUB(#{time}, INTERVAL 1 HOUR) and time<![CDATA[<]]>#{time} and mac in
        <foreach collection="macList" index="index" item="mac" open="(" separator="," close=")">
            #{mac}
        </foreach>
    </insert>
</mapper>