HomeController.java
4.58 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package cn.com.glsx.admin.modules.controller;
import cn.com.glsx.admin.modules.model.ActiveDeviceVo;
import cn.com.glsx.admin.modules.service.HomeService;
import cn.com.glsx.auth.utils.ShieldContextHolder;
import com.glsx.plat.core.web.R;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 首页统计展示 ontroller
*
* @author yangll
*/
@Slf4j
@RestController
@RequestMapping(value = "/home")
@Api(value = "首页统计展示", tags = {"首页统计展示"})
public class HomeController {
//查询对象+条件 做一个 用户ID ==> 上次查询时间
private static Map<String, Long> DEVICE_COUNT_LAST_REFRESH_TIME = new HashMap<>();
//查询对象+条件 做一个 用户ID ==> 总数
private static Map<String, Map<String, Object>> DEVICE_COUNT_CACHE = new HashMap<>();
@Autowired
private HomeService homeService;
@GetMapping("/authuserorg")
@ApiOperation(value = "用户组织鉴权")
public R<Map<String, Boolean>> authUserOrg() {
Map<String, Boolean> authUserOrgMap = homeService.authUserOrg();
return R.ok().data(authUserOrgMap);
}
@GetMapping("/getdevicenumber")
@ApiOperation(value = "查询首页统计数据")
public R<Map<String, Object>> getDeviceNumber() {
String uniqueValue = hashQueryBean("DeviceNumber");
Map<String, Object> deviceNumberMap;
if (needRefreshTotal(uniqueValue)) {
deviceNumberMap = homeService.getDeviceNumber();
DEVICE_COUNT_LAST_REFRESH_TIME.put(uniqueValue, System.currentTimeMillis());
DEVICE_COUNT_CACHE.put(uniqueValue, deviceNumberMap);
} else {
//从缓存获取数据
log.info("DeviceNumber===从缓存中获取首页展示数据===" + ShieldContextHolder.getUserId());
deviceNumberMap = DEVICE_COUNT_CACHE.get(uniqueValue);
}
return R.ok().data(deviceNumberMap);
}
@GetMapping("/getalarmnumber")
@ApiOperation(value = "查询警情统计数据")
public R<Map<String, Object>> getAlarmNumber() {
String uniqueValue = hashQueryBean("AlarmNumber");
Map<String, Object> alarmNumberMap;
if (needRefreshTotal(uniqueValue)) {
log.info("从数据库中查询获取首页数据===" + ShieldContextHolder.getUserId());
alarmNumberMap = homeService.getAlarmNumber();
DEVICE_COUNT_LAST_REFRESH_TIME.put(uniqueValue, System.currentTimeMillis());
DEVICE_COUNT_CACHE.put(uniqueValue, alarmNumberMap);
} else {
//从缓存获取数据
log.info("AlarmNumber===从缓存中获取首页展示数据===" + ShieldContextHolder.getUserId());
alarmNumberMap = DEVICE_COUNT_CACHE.get(uniqueValue);
}
return R.ok().data(alarmNumberMap);
}
/**
* 查询设备激活趋势
*
* @param period
* @return
*/
@GetMapping("getactivedeviceinfo")
@ApiOperation(value = "查询设备激活趋势")
public R<List<ActiveDeviceVo>> getActiveDeviceInfo(@RequestParam Integer period) {
List<ActiveDeviceVo> activeDeviceList = Lists.newArrayList();
try {
activeDeviceList = homeService.getActiveDeviceInfo(period);
} catch (Exception e) {
e.printStackTrace();
}
return R.ok().data(activeDeviceList);
}
/**
* 总数查询刷新间隔,判断是否需要重新刷新
*
* @param uniqueValue 唯一标识字符
* @return 是否需要刷新
*/
private boolean needRefreshTotal(String uniqueValue) {
Long lastRefreshTime = DEVICE_COUNT_LAST_REFRESH_TIME.get(uniqueValue);
return lastRefreshTime == null || (Math.abs(lastRefreshTime - System.currentTimeMillis()) >= 120000);
}
/**
* 加密生成唯一标识符
*
* @param methodType 方法类型
* @return 加密后的字符串
*/
private String hashQueryBean(String methodType) {
return DigestUtils.md5Hex((methodType + ShieldContextHolder.getUserId()).getBytes(StandardCharsets.UTF_8));
}
}