|
|
1
|
+package org.thingsboard.server.dao.yunteng.impl;
|
|
|
2
|
+
|
|
|
3
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
4
|
+import lombok.RequiredArgsConstructor;
|
|
|
5
|
+import org.springframework.stereotype.Service;
|
|
|
6
|
+import org.thingsboard.server.common.data.alarm.Alarm;
|
|
|
7
|
+import org.thingsboard.server.common.data.alarm.AlarmInfo;
|
|
|
8
|
+import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
|
|
|
9
|
+import org.thingsboard.server.common.data.yunteng.dto.HomeDeviceInfoDTO;
|
|
|
10
|
+import org.thingsboard.server.common.data.yunteng.dto.HomePageLeftTopDTO;
|
|
|
11
|
+import org.thingsboard.server.common.data.yunteng.utils.ReflectUtils;
|
|
|
12
|
+import org.thingsboard.server.dao.yunteng.entities.YtDevice;
|
|
|
13
|
+import org.thingsboard.server.dao.yunteng.mapper.DeviceMapper;
|
|
|
14
|
+import org.thingsboard.server.dao.yunteng.service.HomePageService;
|
|
|
15
|
+
|
|
|
16
|
+import java.time.LocalDate;
|
|
|
17
|
+import java.time.LocalDateTime;
|
|
|
18
|
+import java.time.LocalTime;
|
|
|
19
|
+import java.time.ZoneOffset;
|
|
|
20
|
+import java.util.List;
|
|
|
21
|
+
|
|
|
22
|
+@Service
|
|
|
23
|
+@RequiredArgsConstructor
|
|
|
24
|
+public class HomePageServiceImpl implements HomePageService {
|
|
|
25
|
+
|
|
|
26
|
+ private final DeviceMapper deviceMapper;
|
|
|
27
|
+
|
|
|
28
|
+ @Override
|
|
|
29
|
+ public HomePageLeftTopDTO getHomePageLeftTopInfo(
|
|
|
30
|
+ boolean isPtSysAdmin,
|
|
|
31
|
+ boolean isPtAdmin,
|
|
|
32
|
+ boolean isTenantAdmin,
|
|
|
33
|
+ String tenantId,
|
|
|
34
|
+ String currentUserId) {
|
|
|
35
|
+ // 如果当前用户是超级管理员或平台管理员,查询所有的设备、告警、消息量
|
|
|
36
|
+ // 如果当前用户是租户管理员,查询租户下的所有设备、告警、消息量
|
|
|
37
|
+ // 如果当前用户是客户,查询客户可以查看的设备、告警、消息量
|
|
|
38
|
+ HomePageLeftTopDTO homePageLeftTopDTO = new HomePageLeftTopDTO();
|
|
|
39
|
+ HomeDeviceInfoDTO homeDeviceInfo;
|
|
|
40
|
+ List<YtDevice> deviceList;
|
|
|
41
|
+ if (isPtSysAdmin || isPtAdmin) {
|
|
|
42
|
+ deviceList = deviceMapper.selectList(new LambdaQueryWrapper<>());
|
|
|
43
|
+ } else if (isTenantAdmin) {
|
|
|
44
|
+ deviceList =
|
|
|
45
|
+ deviceMapper.selectList(
|
|
|
46
|
+ new LambdaQueryWrapper<YtDevice>().eq(YtDevice::getTenantId, tenantId));
|
|
|
47
|
+ } else {
|
|
|
48
|
+ deviceList =
|
|
|
49
|
+ ReflectUtils.sourceToTarget(
|
|
|
50
|
+ deviceMapper.getDeviceByCustomerUserId(currentUserId), YtDevice.class);
|
|
|
51
|
+ }
|
|
|
52
|
+ homeDeviceInfo = new HomeDeviceInfoDTO(Integer.valueOf(FastIotConstants.MagicNumber.ZERO));
|
|
|
53
|
+ if (null != deviceList) {
|
|
|
54
|
+ homeDeviceInfo = setDeviceInfoData(deviceList, homeDeviceInfo);
|
|
|
55
|
+ }
|
|
|
56
|
+ homePageLeftTopDTO.setDeviceInfo(homeDeviceInfo);
|
|
|
57
|
+ return homePageLeftTopDTO;
|
|
|
58
|
+ }
|
|
|
59
|
+
|
|
|
60
|
+ /**
|
|
|
61
|
+ * 设置设备相关的数据
|
|
|
62
|
+ *
|
|
|
63
|
+ * @param deviceList 拥有的设备
|
|
|
64
|
+ * @param homeDeviceInfoDTO 首页信息
|
|
|
65
|
+ * @return 设置后的首页信息
|
|
|
66
|
+ */
|
|
|
67
|
+ private HomeDeviceInfoDTO setDeviceInfoData(
|
|
|
68
|
+ List<YtDevice> deviceList, HomeDeviceInfoDTO homeDeviceInfoDTO) {
|
|
|
69
|
+ homeDeviceInfoDTO.setSumCount(deviceList.size());
|
|
|
70
|
+ deviceList.forEach(
|
|
|
71
|
+ ytDevice -> {
|
|
|
72
|
+ int basicNum = FastIotConstants.MagicNumber.ONE;
|
|
|
73
|
+ ZoneOffset zoneOffset = ZoneOffset.of("+8");
|
|
|
74
|
+ LocalDateTime todayZeroTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
|
|
|
75
|
+ if (ytDevice.getCreateTime().atZone(zoneOffset).toInstant().toEpochMilli()
|
|
|
76
|
+ >= todayZeroTime.toInstant(zoneOffset).toEpochMilli()) {
|
|
|
77
|
+ homeDeviceInfoDTO.setTodayAdd(homeDeviceInfoDTO.getTodayAdd() + basicNum);
|
|
|
78
|
+ }
|
|
|
79
|
+ switch (ytDevice.getDeviceState()) {
|
|
|
80
|
+ case OFFLINE:
|
|
|
81
|
+ homeDeviceInfoDTO.setOffLine(homeDeviceInfoDTO.getOffLine() + basicNum);
|
|
|
82
|
+ break;
|
|
|
83
|
+ case ONLINE:
|
|
|
84
|
+ homeDeviceInfoDTO.setOnLine(homeDeviceInfoDTO.getOnLine() + basicNum);
|
|
|
85
|
+ break;
|
|
|
86
|
+ case INACTIVE:
|
|
|
87
|
+ homeDeviceInfoDTO.setInActive(homeDeviceInfoDTO.getInActive() + basicNum);
|
|
|
88
|
+ break;
|
|
|
89
|
+ default:
|
|
|
90
|
+ break;
|
|
|
91
|
+ }
|
|
|
92
|
+ });
|
|
|
93
|
+ return homeDeviceInfoDTO;
|
|
|
94
|
+ }
|
|
|
95
|
+
|
|
|
96
|
+ /**
|
|
|
97
|
+ * 设置告警相关的数据
|
|
|
98
|
+ * @param alarmInfoList 告警数据
|
|
|
99
|
+ * @param homeDeviceInfoDTO 首页数据
|
|
|
100
|
+ * @return 设置后的首页数据
|
|
|
101
|
+ */
|
|
|
102
|
+ private HomeDeviceInfoDTO setAlarmInfoData(List<AlarmInfo> alarmInfoList,HomeDeviceInfoDTO homeDeviceInfoDTO){
|
|
|
103
|
+ return homeDeviceInfoDTO;
|
|
|
104
|
+ }
|
|
|
105
|
+
|
|
|
106
|
+ private HomeDeviceInfoDTO setMessageInfoData(HomeDeviceInfoDTO homeDeviceInfoDTO){
|
|
|
107
|
+ return homeDeviceInfoDTO;
|
|
|
108
|
+ }
|
|
|
109
|
+} |