9f2622b9 by ja

时间范围查询统计

1 parent db58f432
...@@ -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,12 +26,21 @@ public class StatisticsController { ...@@ -27,12 +26,21 @@ 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 if(StringUtils.isBlank(endTime)){
37 Date date = new Date();
38 endTime = this.sdf.format(date);
39 }
40 List<Date> dateList = getBetweenDates(startTime, endTime);
41 if(dateList != null && dateList.size() > 0){
42 for(Date date:dateList){
43 String time = this.sdf.format(date);
36 String startDate = time + " 00:00:00"; 44 String startDate = time + " 00:00:00";
37 String endDate = time + " 23:59:59"; 45 String endDate = time + " 23:59:59";
38 int pv = this.shareLogMapper.findPVByDay(startDate, endDate); 46 int pv = this.shareLogMapper.findPVByDay(startDate, endDate);
...@@ -44,7 +52,38 @@ public class StatisticsController { ...@@ -44,7 +52,38 @@ public class StatisticsController {
44 map.put("uv",uv); 52 map.put("uv",uv);
45 map.put("share",share); 53 map.put("share",share);
46 map.put("img",img); 54 map.put("img",img);
47 return builder.success(true).code("2000").content(map).create(); 55 returnMap.put(time,map);
56 }
48 } 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;
86 }
87
49 88
50 } 89 }
......