时间范围查询统计
Showing
1 changed file
with
57 additions
and
18 deletions
... | @@ -9,10 +9,9 @@ import org.springframework.web.bind.annotation.RequestParam; | ... | @@ -9,10 +9,9 @@ import org.springframework.web.bind.annotation.RequestParam; |
9 | import org.springframework.web.bind.annotation.RestController; | 9 | import org.springframework.web.bind.annotation.RestController; |
10 | 10 | ||
11 | import javax.servlet.http.HttpServletRequest; | 11 | import javax.servlet.http.HttpServletRequest; |
12 | import java.text.ParseException; | ||
12 | import java.text.SimpleDateFormat; | 13 | import java.text.SimpleDateFormat; |
13 | import java.util.Date; | 14 | import java.util.*; |
14 | import java.util.HashMap; | ||
15 | import java.util.Map; | ||
16 | 15 | ||
17 | /** | 16 | /** |
18 | * Created by JA on 17/7/3. | 17 | * Created by JA on 17/7/3. |
... | @@ -27,24 +26,64 @@ public class StatisticsController { | ... | @@ -27,24 +26,64 @@ public class StatisticsController { |
27 | private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | 26 | private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
28 | 27 | ||
29 | @RequestMapping | 28 | @RequestMapping |
30 | public MessageEntity statistics(HttpServletRequest request,@RequestParam(required = false)String time) { | 29 | public MessageEntity statistics(HttpServletRequest request,@RequestParam(required = false) String startTime,@RequestParam(required = false) String endTime) { |
31 | MessageEntity.Builder builder = new MessageEntity.Builder(request); | 30 | MessageEntity.Builder builder = new MessageEntity.Builder(request); |
32 | if(StringUtils.isBlank(time)){ | 31 | Map<String,Object> returnMap = new HashMap<>(); |
32 | if(StringUtils.isBlank(startTime)){ | ||
33 | Date date = new Date(); | 33 | Date date = new Date(); |
34 | time = this.sdf.format(date); | 34 | startTime = this.sdf.format(date); |
35 | } | 35 | } |
36 | String startDate = time + " 00:00:00"; | 36 | if(StringUtils.isBlank(endTime)){ |
37 | String endDate = time + " 23:59:59"; | 37 | Date date = new Date(); |
38 | int pv = this.shareLogMapper.findPVByDay(startDate, endDate); | 38 | endTime = this.sdf.format(date); |
39 | int uv = this.shareLogMapper.findUVByDay(startDate, endDate); | 39 | } |
40 | int share = this.shareLogMapper.findShareByDay(startDate, endDate); | 40 | List<Date> dateList = getBetweenDates(startTime, endTime); |
41 | int img = this.shareLogMapper.findImgByDay(startDate, endDate); | 41 | if(dateList != null && dateList.size() > 0){ |
42 | Map<String,Object> map = new HashMap<>(); | 42 | for(Date date:dateList){ |
43 | map.put("pv",pv); | 43 | String time = this.sdf.format(date); |
44 | map.put("uv",uv); | 44 | String startDate = time + " 00:00:00"; |
45 | map.put("share",share); | 45 | String endDate = time + " 23:59:59"; |
46 | map.put("img",img); | 46 | int pv = this.shareLogMapper.findPVByDay(startDate, endDate); |
47 | return builder.success(true).code("2000").content(map).create(); | 47 | int uv = this.shareLogMapper.findUVByDay(startDate, endDate); |
48 | int share = this.shareLogMapper.findShareByDay(startDate, endDate); | ||
49 | int img = this.shareLogMapper.findImgByDay(startDate, endDate); | ||
50 | Map<String,Object> map = new HashMap<>(); | ||
51 | map.put("pv",pv); | ||
52 | map.put("uv",uv); | ||
53 | map.put("share",share); | ||
54 | map.put("img",img); | ||
55 | returnMap.put(time,map); | ||
56 | } | ||
57 | } | ||
58 | return builder.success(true).code("2000").content(returnMap).create(); | ||
59 | } | ||
60 | |||
61 | |||
62 | private List<Date> getBetweenDates(String startString, String endString) { | ||
63 | List<Date> result = new ArrayList<>(); | ||
64 | Calendar tempStart = Calendar.getInstance(); | ||
65 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
66 | Date start = null; | ||
67 | Date end = null; | ||
68 | try { | ||
69 | start = sdf.parse(startString); | ||
70 | end = sdf.parse(endString); | ||
71 | } catch (ParseException e) { | ||
72 | e.printStackTrace(); | ||
73 | } | ||
74 | tempStart.setTime(start); | ||
75 | result.add(tempStart.getTime()); | ||
76 | tempStart.add(Calendar.DAY_OF_YEAR, 1); | ||
77 | |||
78 | Calendar tempEnd = Calendar.getInstance(); | ||
79 | tempEnd.setTime(end); | ||
80 | while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) { | ||
81 | result.add(tempStart.getTime()); | ||
82 | Date time = tempStart.getTime(); | ||
83 | tempStart.add(Calendar.DAY_OF_YEAR, 1); | ||
84 | } | ||
85 | return result; | ||
48 | } | 86 | } |
49 | 87 | ||
88 | |||
50 | } | 89 | } | ... | ... |
-
Please register or sign in to post a comment