StatisticsController.java
2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.example.controller;
import com.example.mapper.ShareLogMapper;
import com.example.support.model.MessageEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Created by JA on 17/7/3.
*/
@RequestMapping("/kmr/statistics")
@RestController
public class StatisticsController {
@Autowired
private ShareLogMapper shareLogMapper;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@RequestMapping
public MessageEntity statistics(HttpServletRequest request,@RequestParam(required = false) String startTime,@RequestParam(required = false) String endTime) {
MessageEntity.Builder builder = new MessageEntity.Builder(request);
Map<String,Object> returnMap = new HashMap<>();
if(StringUtils.isBlank(startTime)){
Date date = new Date();
startTime = this.sdf.format(date);
}
if(StringUtils.isBlank(endTime)){
Date date = new Date();
endTime = this.sdf.format(date);
}
List<Date> dateList = getBetweenDates(startTime, endTime);
if(dateList != null && dateList.size() > 0){
for(Date date:dateList){
String time = this.sdf.format(date);
String startDate = time + " 00:00:00";
String endDate = time + " 23:59:59";
int pv = this.shareLogMapper.findPVByDay(startDate, endDate);
int uv = this.shareLogMapper.findUVByDay(startDate, endDate);
int share = this.shareLogMapper.findShareByDay(startDate, endDate);
int img = this.shareLogMapper.findImgByDay(startDate, endDate);
Map<String,Object> map = new HashMap<>();
map.put("pv",pv);
map.put("uv",uv);
map.put("share",share);
map.put("img",img);
returnMap.put(time,map);
}
}
return builder.success(true).code("2000").content(returnMap).create();
}
private List<Date> getBetweenDates(String startString, String endString) {
List<Date> result = new ArrayList<>();
Calendar tempStart = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date start = null;
Date end = null;
try {
start = sdf.parse(startString);
end = sdf.parse(endString);
} catch (ParseException e) {
e.printStackTrace();
}
tempStart.setTime(start);
result.add(tempStart.getTime());
tempStart.add(Calendar.DAY_OF_YEAR, 1);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
result.add(tempStart.getTime());
Date time = tempStart.getTime();
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
return result;
}
}