src/main/java/com/moral/controller/AlarmController.java | ●●●●● patch | view | raw | blame | history | |
src/main/java/com/moral/mapper/AlarmMapper.java | ●●●●● patch | view | raw | blame | history | |
src/main/java/com/moral/service/AlarmService.java | ●●●●● patch | view | raw | blame | history | |
src/main/java/com/moral/service/impl/AlarmServiceImpl.java | ●●●●● patch | view | raw | blame | history | |
src/main/resources/mapper/AlarmMapper.xml | ●●●●● patch | view | raw | blame | history |
src/main/java/com/moral/controller/AlarmController.java
@@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import java.text.ParseException; import java.util.*; @RestController @@ -35,7 +36,7 @@ @Resource AlarmService alarmService; @RequestMapping(value = "/count-by-times", method = RequestMethod.GET) public ResultBean<List<Map>> countByTimes(Date start, Date end,@RequestParam(value = "timeUnits")Optional<TimeUnits> timeUnits){ return new ResultBean<>(alarmService.countByTimes(start,end,timeUnits.isPresent()?timeUnits.get():null)); public ResultBean<List<Map>> countByTimes(Date start, Date end,@RequestParam(value = "timeUnits")Optional<TimeUnits> timeUnits) throws ParseException { return new ResultBean<>(alarmService.countByTimes(start,end,timeUnits.isPresent()?timeUnits.get():null)); } } src/main/java/com/moral/mapper/AlarmMapper.java
@@ -10,5 +10,5 @@ public interface AlarmMapper extends BaseMapper<Alarm> { List<Map> countByTimes(@Param("start")Date start, @Param("end")Date end, @Param("format")String format); List<Map> countByTimes(@Param("start")Date start, @Param("end")Date end, @Param("format")String format,@Param("list") List<String> list); } src/main/java/com/moral/service/AlarmService.java
@@ -1,5 +1,6 @@ package com.moral.service; import java.text.ParseException; import java.util.Date; import java.util.List; import java.util.Map; @@ -9,6 +10,6 @@ @SuppressWarnings("rawtypes") public interface AlarmService { List<Map> countByTimes(Date start, Date end, TimeUnits timeUnits); List<Map> countByTimes(Date start, Date end, TimeUnits timeUnits) throws ParseException; } src/main/java/com/moral/service/impl/AlarmServiceImpl.java
@@ -1,5 +1,9 @@ package com.moral.service.impl; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map; @@ -14,25 +18,57 @@ import com.moral.service.HistoryMinutelyService; @Service @SuppressWarnings({ "rawtypes" }) @SuppressWarnings({"rawtypes"}) public class AlarmServiceImpl implements AlarmService { @Resource private AlarmMapper alarmMapper; @Resource private AlarmMapper alarmMapper; @Resource private HistoryMinutelyService historyMinutelyService; @Resource private HistoryMinutelyService historyMinutelyService; @Override public List<Map> countByTimes(Date start, Date end, TimeUnits timeUnits){ String format = null; if(timeUnits!=null){ switch (timeUnits){ case MONTH: format = "%Y-%m"; case DAY: format = "%Y-%m-%d"; } } public List<Map> countByTimes(Date start, Date end, TimeUnits timeUnits) throws ParseException { String format = null; if (timeUnits != null) { switch (timeUnits) { case MONTH: format = "%Y-%m"; break; case DAY: format = "%Y-%m-%d"; break; default: break; } } if (start == null) { Calendar cal = Calendar.getInstance(); String s = cal.get(Calendar.YEAR) + ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); start = sdf.parse(s); } return alarmMapper.countByTimes(start, end, format); } if (end == null) { end = new Date(); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMM"); String[] starts = sdf.format(start).split("-"); String[] ends = sdf.format(end).split("-"); Date startTime = sdf1.parse(starts[0] + "" + starts[1]); Date endTime = sdf1.parse(ends[0] + "" + ends[1]); Calendar cal = Calendar.getInstance(); cal.setTime(startTime); List<String> list = new ArrayList<>(); for (long d = cal.getTimeInMillis(); d <= endTime.getTime(); cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1), d = cal.getTimeInMillis()) { list.add(sdf1.format(d)); } return alarmMapper.countByTimes(start, end, format, list); } } src/main/resources/mapper/AlarmMapper.xml
@@ -1,25 +1,46 @@ <?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.AlarmMapper"> <select id="countByTimes" resultType="java.util.Map"> select <if test="format !=null"> DATE_FORMAT(time,#{format}) as time, </if> COUNT(*) as count from alarm <where> <if test="start != null"> time >= #{start} </if> <if test="end != null"> and time <![CDATA[ <= ]]> #{end} </if> </where> <if test="format !=null"> GROUP BY DATE_FORMAT(time,#{format}) ; </if> </select> <select id="countByTimes" resultType="java.util.Map"> select <if test="format !=null"> DATE_FORMAT(a.time,#{format}) as time, </if> count(*) as count from (<foreach collection="list" item="item" separator="union"> select * from alarm_${item} </foreach>) as a where a.time >= #{start} and a.time <![CDATA[<=]]> #{end} <if test="format !=null"> GROUP BY DATE_FORMAT(a.time,#{format}) ; </if> </select> <!-- <select id="countByTimes" resultType="java.util.Map"> select <if test="format !=null"> DATE_FORMAT(time,#{format}) as time, </if> COUNT(*) as count from alarm <where> <if test="start != null"> time >= #{start} </if> <if test="end != null"> and time <![CDATA[ <= ]]> #{end} </if> </where> <if test="format !=null"> GROUP BY DATE_FORMAT(time,#{format}) ; </if> </select>--> </mapper>