xufenglei
2019-03-13 f9640a02ea2abd1369c179f3ec88a7f9d91dea62
src/main/resources/mapper/HistoryDailyMapper.xml
@@ -33,4 +33,65 @@
      </where>
      GROUP BY mp.`name`, vdi.mac
   </select>
   <select id="getOverproofData" resultType="java.util.Map">
      SELECT
         AVG( hd.`json` -> '$.${sensorKey}[0]' ) avg,
         ( SELECT DISTINCT limit_val ->> '$.${sensorKey}' FROM view_device_density WHERE monitor_point_id = vdd.monitor_point_id AND device_tech = vdd.device_tech ) `limit`,
         ( SELECT mp.`name` FROM monitor_point mp WHERE mp.id = vdd.monitor_point_id ) `name`,
         vdd.device_tech
      FROM
         history_daily hd,
         view_device_density vdd,
         (
            SELECT
               vdd.monitor_point_id
            FROM
               history_daily hd,
               device vdd,
               monitor_point mp
            <where>
               hd.mac = vdd.mac
               AND mp.id = vdd.monitor_point_id
               <if test="start != null">
                  AND hd.time >= #{start}
               </if>
               <if test="end != null">
                  AND hd.time &lt; #{end}
               </if>
               <if test="regionCode != null">
                  AND mp.${regionType}_code = #{regionCode}
               </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>
            </where>
            GROUP BY
               vdd.mac,
               vdd.monitor_point_id
            HAVING
               AVG( hd.`json` -> '$.${sensorKey}[0]' ) >= ( SELECT limit_val ->> '$.${sensorKey}' FROM view_device_density WHERE mac = vdd.mac ) * (1 + #{overproofRatio} * 1)
               <if test="overproofRatio != 0 and overproofRatio != 3">
               AND AVG( hd.`json` -> '$.${sensorKey}[0]' ) &lt; ( SELECT limit_val ->> '$.${sensorKey}' FROM view_device_density WHERE mac = vdd.mac ) * (2 + #{overproofRatio} * 1)
               </if>
         ) ta
      <where>
         hd.mac = vdd.mac
         AND vdd.monitor_point_id = ta.monitor_point_id
         <if test="start != null">
            AND hd.time >= #{start}
         </if>
         <if test="end != null">
            AND hd.time &lt; #{end}
         </if>
      </where>
      GROUP BY
         vdd.monitor_point_id,
         vdd.device_tech
      </select>
</mapper>