ZhuDongming
2020-05-19 e51e2d053b498e236097bcb4012c8446d672be86
src/main/resources/mapper/HistoryMapper.xml
@@ -1,376 +1,258 @@
<?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>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    mac, value, time, version
  </sql>
  <select id="selectByExample" parameterType="com.moral.entity.HistoryExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from history
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <delete id="deleteByExample" parameterType="com.moral.entity.HistoryExample">
    delete from history
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <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="countByExample" parameterType="com.moral.entity.HistoryExample" resultType="java.lang.Integer">
    select count(*) from history
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update history
    <set>
      <if test="record.mac != null">
        mac = #{record.mac,jdbcType=VARCHAR},
      </if>
      <if test="record.value != null">
        value = #{record.value,jdbcType=OTHER},
      </if>
      <if test="record.time != null">
        time = #{record.time,jdbcType=TIMESTAMP},
      </if>
      <if test="record.version != null">
        version = #{record.version,jdbcType=INTEGER},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update history
    set mac = #{record.mac,jdbcType=VARCHAR},
      value = #{record.value,jdbcType=OTHER},
      time = #{record.time,jdbcType=TIMESTAMP},
      version = #{record.version,jdbcType=INTEGER}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <resultMap id="BaseResultMap" type="com.moral.entity.History">
    <result column="device_mac" jdbcType="VARCHAR" property="deviceMac" />
    <result column="value" jdbcType="OTHER" property="value" />
    <result column="time" jdbcType="TIMESTAMP" property="time" />
    <result column="version" jdbcType="VARCHAR" property="version" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    device_mac, value, time, version
  </sql>
  <select id="selectByExample" parameterType="com.moral.entity.HistoryExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from history
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <delete id="deleteByExample" parameterType="com.moral.entity.HistoryExample">
    delete from history
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.moral.entity.History">
    insert into history (device_mac, value, time,
      version)
    values (#{deviceMac,jdbcType=VARCHAR}, #{value,jdbcType=OTHER}, #{time,jdbcType=TIMESTAMP},
      #{version,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.moral.entity.History">
    insert into history
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="deviceMac != null">
        device_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="deviceMac != null">
        #{deviceMac,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=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.moral.entity.HistoryExample" resultType="java.lang.Integer">
    select count(*) from history
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update history
    <set>
      <if test="record.deviceMac != null">
        device_mac = #{record.deviceMac,jdbcType=VARCHAR},
      </if>
      <if test="record.value != null">
        value = #{record.value,jdbcType=OTHER},
      </if>
      <if test="record.time != null">
        time = #{record.time,jdbcType=TIMESTAMP},
      </if>
      <if test="record.version != null">
        version = #{record.version,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update history
    set device_mac = #{record.deviceMac,jdbcType=VARCHAR},
      value = #{record.value,jdbcType=OTHER},
      time = #{record.time,jdbcType=TIMESTAMP},
      version = #{record.version,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <select id="getDayAQIBySensor" resultType="map">
      SELECT
         AVG(value -&gt; '$.e1') e1,
         AVG(value -&gt; '$.e2') e2,
         AVG(value -&gt; '$.e10') e10,
         AVG(value -&gt; '$.e11') e11,
         AVG(value -&gt; '$.e15') e15,
         AVG(value -&gt; '$.e16') e16
      FROM
         history
      WHERE
         device_mac = #{mac}
      AND time &gt; #{start}
      AND time &lt; #{end}
  </select>
   <select id="getAverageByAll" resultType="map">
      SELECT
         <if test="macKey != null and macKey != 'all'">
            e.name,
         </if>
         <if test="macKey == 'all'">
            h.mac_key,
         </if>
         AVG(h.mac_value) avg
      FROM
         history h,
         monitorpoint m,
         equipment e
      WHERE
         m.areacode = #{areaCode}
      AND m.id = epoint
      AND e.mac = h.mac
      AND h.time &gt; #{start}
      AND h.time &lt; #{end}
      <if test="orgIds != null and orgIds.size &gt; 0">
         <!-- JOIN org_equ oe ON e.id = oe.equid -->
         AND e.owner_id IN
         <foreach close=")" collection="orgIds" item="listItem" open="(" separator=",">
            #{listItem}
         </foreach>
      </if>
      <if test="macKey != null and macKey != 'all'">
         AND   h.mac_key = #{macKey}
         GROUP BY e.id
         ORDER BY avg
      </if>
      <if test="macKey == 'all'">
         GROUP BY h.mac_key
      </if>
   </select>
<mapper namespace="com.moral.mapper.HistoryMapper">
    <select id="selectValueByMacAndTime" resultType="java.lang.String">
      select `value` from history
      where  time = #{time} and mac = #{mac}
      limit 0,1
   </select>
    <select id="getAreaAllDataByAccount" resultType="java.util.LinkedHashMap">
        SELECT
        <if test="macKey != null">
            d.`name`,
        </if>
        <foreach collection="sensorKeys" separator="," item="sensorKey">
            AVG(h.`value` ->'$.${sensorKey}[0]') AS '${sensorKey}'
        </foreach>
        FROM
        history h,
        device d,
        monitor_point mp
        WHERE
        h.mac = d.mac
        AND d.monitor_point_id = mp.id
        <if test="areaCode != null">
            AND mp.area_code = #{areaCode}
        </if>
        <if test="start != null">
            AND h.time >= #{start}
        </if>
        <if test="end != null">
            AND h.time &lt; #{end}
        </if>
        <if test="monitorPointId != null">
            AND mp.id = #{monitorPointId}
        </if>
        <if test="orgIds != null and orgIds.size &gt; 0">
            AND mp.organization_id IN
            <foreach collection="orgIds" open="(" separator="," close=")"
                     item="listItem">
                #{listItem}
            </foreach>
        </if>
        <if test="macKey != null">
            GROUP BY d.id
            ORDER BY ${macKey} desc
        </if>
    </select>
    <select id="getRegionRankingData" resultType="java.util.Map">
        SELECT
        AVG( h.${column} -> '$.${sensorKey}[0]' ) avg ,
        <if test="dimension == 'profession'">
            p.`name`
        </if>
        <if test="dimension == 'monitorPoint'">
            mp.`name`
        </if>
        <if test="dimension == 'region'">
            r.${nextLevel}_name AS 'name'
        </if>
        FROM
        <if test="dimension == 'profession'">
            profession p ,
        </if>
        <if test="dimension == 'region'">
            ${nextLevel} r,
        </if>
        device d,
        ${table} h,
        monitor_point mp
        WHERE
        d.mac = h.mac
        AND d.is_delete = '0'
        AND mp.is_delete = '0'
        AND d.monitor_point_id = mp.id
        AND h.time >= #{start}
        AND mp.${regionType}_code = #{regionCode}
        <if test="dimension == 'profession'">
            AND d.profession_id = p.id
        </if>
        <if test="dimension == 'region'">
            AND r.${nextLevel}_code = mp.${nextLevel}_code
        </if>
        <if test="orgIds != null and orgIds.size &gt; 0">
            AND mp.organization_id IN
            <foreach collection="orgIds" open="(" separator="," close=")" item="listItem">
                #{listItem}
            </foreach>
        </if>
        GROUP BY
        <if test="dimension == 'profession'">
            p.id
        </if>
        <if test="dimension == 'monitorPoint'">
            mp.id
        </if>
        <if test="dimension == 'region'">
            mp.${nextLevel}_code
        </if>
        ORDER BY
        avg DESC
        <if test="dimension == 'monitorPoint'">
            LIMIT 0,50
        </if>
    </select>
    <select id="selectNextLevelRegion" resultType="string">
      SELECT
         ${nextLevel}_name AS 'name'
      FROM
         ${nextLevel}
      WHERE
         ${regionType}_code = #{regionCode}
   </select>
    <select id="getDischargeByRegion" resultType="java.util.Map">
      SELECT
         mp.`name`,
         dis.`value` -> '$.${sensorKey}' 'value'
      FROM
         monitor_point mp,
         discharge dis
      WHERE
         dis.monitor_point_id = mp.id
         AND mp.${regionType}_code = #{regionCode}
         AND dis.`year` = #{year}
   </select>
    <select id="getValueByMacAndSize" resultType="java.util.Map">
      SELECT
         value,
         time
      FROM
         history
      WHERE
         mac = #{mac}
      ORDER BY
         time DESC
      LIMIT #{size}
   </select>
    <select id="getActualDataByRegion" resultType="java.util.Map">
        SELECT
        <foreach collection="sensorKeys" separator="," item="sensorKey">
            MAX( `value` ->> '$.${sensorKey}' ) * 1 AS '${sensorKey}'
        </foreach>
        FROM
        history h ,
        device d,
        monitor_point mp
        <where>
            h.mac = d.mac
            AND d.monitor_point_id = mp.id
            <if test="orgIds != null and orgIds.size &gt; 0">
                AND mp.organization_id IN
                <foreach collection="orgIds" open="(" separator="," close=")" item="listItem">
                    #{listItem}
                </foreach>
            </if>
            AND h.time >= #{start}
            AND h.time &lt; #{end}
        </where>
    </select>
    <select id="getAVGValueByMacAndTimeslot" resultType="java.util.Map">
      SELECT AVG(JSON_EXTRACT(value,'$.e1[0]')) e1,AVG(JSON_EXTRACT(value,'$.e2[0]')) e2,AVG(JSON_EXTRACT(value,'$.e10[0]')) e10,AVG(JSON_EXTRACT(value,'$.e11[0]')) e11,AVG(JSON_EXTRACT(value,'$.e16[0]')) e16,AVG(JSON_EXTRACT(value,'$.e15[0]')) e15
      FROM history
      WHERE mac = #{mac}
      AND time BETWEEN #{starttime} AND #{endtime}
   </select>
    <!-- 无人机sensor当天查询 -->
    <select id="getSensorDataToday" resultType="java.util.LinkedHashMap">
        SELECT
        DATE_FORMAT(time,'%Y-%m-%d %H:%i') time,
        <foreach collection="sensorKeys" separator="," item="sensorKey">
            AVG(h.value ->'$.${sensorKey}') AS '${sensorKey}',
            MIN(cast(h.value ->'$.${sensorKey}' as decimal(11,5))) AS 'min${sensorKey}',
            MAX(cast(h.value ->'$.${sensorKey}' as decimal(11,5))) AS 'max${sensorKey}'
        </foreach>
        FROM
        history h
        <where>
            <if test="mac!=null">
                and h.mac = #{mac}
            </if>
            and h.time >= #{startTime}
            and h.time <![CDATA[<=]]> #{endTime}
        </where>
        GROUP BY
        DATE_FORMAT(time,'%Y-%m-%d %H:%i')
        ORDER BY
        time
    </select>
    <select id="getAVGSensorRankByMonitorPointIdList" resultType="java.util.Map">
        SELECT mp.name, AVG(JSON_EXTRACT(h.value,'$.${sensor}[0]')) sensor
        FROM device d, history h, monitor_point mp
        WHERE d.mac=h.mac
        And mp.id=d.monitor_point_id
        AND d.monitor_point_id IN
        <foreach collection="monitorPointIdList" index="index" item="monitorPointId" open="(" separator="," close=")">
            #{monitorPointId}
        </foreach>
        AND h.time BETWEEN #{before5Time} AND #{endTime}
        GROUP BY d.monitor_point_id
        ORDER BY AVG(JSON_EXTRACT(h.value,'$.${sensor}[0]')) DESC
    </select>
    <select id="getMonitorPointAVGValueByMonitorPointIdAndTimeslot" resultType="java.util.Map">
       SELECT AVG(JSON_EXTRACT(value,'$.e1[0]')) e1,AVG(JSON_EXTRACT(value,'$.e2[0]')) e2,AVG(JSON_EXTRACT(value,'$.e10[0]')) e10,AVG(JSON_EXTRACT(value,'$.e11[0]')) e11,AVG(JSON_EXTRACT(value,'$.e16[0]')) e16,AVG(JSON_EXTRACT(value,'$.e15[0]')) e15
      FROM device d,history h
      WHERE d.monitor_point_id = #{monitor_point_id}
      AND d.mac = h.mac
      AND h.time BETWEEN #{starttime} AND #{endtime}
    </select>
    <!-- 走航车sensor查询 -->
    <select id="getCarSensorData" resultType="java.util.LinkedHashMap">
        SELECT
        DATE_FORMAT(h.time,'%Y-%m-%d %H:%i:%s') time,
        <foreach collection="sensorKeys" item="sensorKey" separator=",">
            h.value->'$.${sensorKey}' as '${sensorKey}'
        </foreach>
        FROM
        history_special as h
        <where>
            <if test="mac!=null">
                and h.mac = #{mac}
            </if>
            and h.time >= #{startTime}
            and h.time <![CDATA[<]]> #{endTime}
        </where>
        ORDER BY
        h.time
    </select>
    <select id="listGetSensorData" resultType="java.util.LinkedHashMap">
        SELECT
        DATE_FORMAT(h.time,'%Y-%m-%d %H:%i:%s') time,
        <foreach collection="sensorKeys" item="sensorKey" separator=",">
            h.value->'$.${sensorKey}' as '${sensorKey}'
        </foreach>
        FROM
        history as h
        <where>
            <if test="mac!=null">
                and h.mac = #{mac}
            </if>
            and h.time >= #{startTime}
            and h.time <![CDATA[<]]> #{endTime}
        </where>
        ORDER BY
        h.time
    </select>
</mapper>