src/main/java/com/moral/controller/DemoConreoller.java | ●●●●● patch | view | raw | blame | history | |
src/main/resources/mapper/HistoryMapper.xml | ●●●●● patch | view | raw | blame | history |
src/main/java/com/moral/controller/DemoConreoller.java
@@ -3,6 +3,8 @@ import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalAdjusters; import java.util.ArrayList; import java.util.Arrays; @@ -17,6 +19,7 @@ import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.time.DateUtils; import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -26,6 +29,7 @@ import com.moral.common.util.WebUtils; import com.moral.mapper.AlarmDailyMapper; import com.moral.mapper.DemoMapper; import com.moral.mapper.HistoryMapper; @RestController @@ -37,6 +41,9 @@ @Resource private AlarmDailyMapper alarmDailyMapper; @Resource private HistoryMapper historyMapper; @GetMapping("list") public List<Map<String, Object>> getDatas(HttpServletRequest request){ @@ -113,4 +120,20 @@ public List<Map<String, Object>> getDayAqis(Date startTime,Date endTime) { return demoMapper.selectByTimeZones(startTime,endTime); } @GetMapping("avg") public Map<String, Object> getAvgDatas(HttpServletRequest request){ Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null); parameters.put("sensorKeys", Arrays.asList("e1", "e2", "e3", "e13")); LocalDateTime now = LocalDateTime.now(); parameters.put("start", now.minusHours(1).truncatedTo(ChronoUnit.HOURS)); parameters.put("end", now.truncatedTo(ChronoUnit.HOURS)); List<Map<String, Object>> avgs = historyMapper.getAreaAllDataByAccount(parameters); if (ObjectUtils.isEmpty(avgs)) { return new HashMap<String, Object>(); } else { return avgs.get(0); } } } src/main/resources/mapper/HistoryMapper.xml
@@ -6,34 +6,45 @@ 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> <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 mp.area_code = #{areaCode} AND h.time >= #{start} AND h.time < #{end} AND h.mac = d.mac AND d.monitor_point_id = mp.id <if test="orgIds != null and orgIds.size > 0"> AND mp.organization_id IN <foreach collection="orgIds" open="(" separator="," close=")" item="listItem"> #{listItem} </foreach> </if> 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 < #{end} </if> <if test="monitorPointId != null"> AND mp.id = #{monitorPointId} </if> <if test="orgIds != null and orgIds.size > 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> </if> </select> <select id="getRegionRankingData" resultType="java.util.Map">