From 409851cc3f33d3fd016e20518f90a2c093e9e1db Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Tue, 19 May 2020 16:58:54 +0800
Subject: [PATCH] alarm相关接口更新
---
src/main/java/com/moral/controller/AlarmController.java | 5 +
src/main/resources/mapper/AlarmMapper.xml | 63 ++++++++++++++-------
src/main/java/com/moral/mapper/AlarmMapper.java | 2
src/main/java/com/moral/service/impl/AlarmServiceImpl.java | 66 +++++++++++++++++-----
src/main/java/com/moral/service/AlarmService.java | 3
5 files changed, 99 insertions(+), 40 deletions(-)
diff --git a/src/main/java/com/moral/controller/AlarmController.java b/src/main/java/com/moral/controller/AlarmController.java
index a76d137..bb5a793 100644
--- a/src/main/java/com/moral/controller/AlarmController.java
+++ b/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));
}
}
diff --git a/src/main/java/com/moral/mapper/AlarmMapper.java b/src/main/java/com/moral/mapper/AlarmMapper.java
index aac5662..bbb4f18 100644
--- a/src/main/java/com/moral/mapper/AlarmMapper.java
+++ b/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);
}
\ No newline at end of file
diff --git a/src/main/java/com/moral/service/AlarmService.java b/src/main/java/com/moral/service/AlarmService.java
index dcad103..4d14b00 100644
--- a/src/main/java/com/moral/service/AlarmService.java
+++ b/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;
}
diff --git a/src/main/java/com/moral/service/impl/AlarmServiceImpl.java b/src/main/java/com/moral/service/impl/AlarmServiceImpl.java
index 3386740..fb3e494 100644
--- a/src/main/java/com/moral/service/impl/AlarmServiceImpl.java
+++ b/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);
+
+ }
}
diff --git a/src/main/resources/mapper/AlarmMapper.xml b/src/main/resources/mapper/AlarmMapper.xml
index e2a7007..f0cbc24 100644
--- a/src/main/resources/mapper/AlarmMapper.xml
+++ b/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>
\ No newline at end of file
--
Gitblit v1.8.0