| | |
| | | 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 |
| | |
| | | @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){ |
| | | 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)); |
| | | } |
| | | } |
| | |
| | |
|
| | | 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);
|
| | | } |
| | |
| | | package com.moral.service;
|
| | |
|
| | | import java.text.ParseException;
|
| | | import java.util.Date;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | |
| | | @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;
|
| | |
|
| | | }
|
| | |
| | | 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;
|
| | |
| | | @Resource
|
| | | private HistoryMinutelyService historyMinutelyService;
|
| | | @Override
|
| | | public List<Map> countByTimes(Date start, Date end, TimeUnits timeUnits){
|
| | | 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";
|
| | | case DAY: format = "%Y-%m-%d";
|
| | | 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);
|
| | |
|
| | | }
|
| | |
|
| | | }
|
| | |
| | | <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
|
| | |
| | | <if test="format !=null">
|
| | | GROUP BY DATE_FORMAT(time,#{format}) ;
|
| | | </if>
|
| | | </select>
|
| | | </select>-->
|
| | | </mapper> |