Showing
5 changed files
with
110 additions
and
56 deletions
@@ -29,6 +29,13 @@ public class HomePageTopMessage { | @@ -29,6 +29,13 @@ public class HomePageTopMessage { | ||
29 | this.todayDataPointsAdd = todayDataPointsAdd; | 29 | this.todayDataPointsAdd = todayDataPointsAdd; |
30 | } | 30 | } |
31 | 31 | ||
32 | + public HomePageTopMessage(Integer messageCount, Integer todayMessageAdd) { | ||
33 | + this.messageCount = messageCount; | ||
34 | + this.todayMessageAdd = todayMessageAdd; | ||
35 | + this.dataPointsCount = 0; | ||
36 | + this.todayDataPointsAdd = 0; | ||
37 | + } | ||
38 | + | ||
32 | public HomePageTopMessage(Integer defaultValue) { | 39 | public HomePageTopMessage(Integer defaultValue) { |
33 | this.messageCount = defaultValue; | 40 | this.messageCount = defaultValue; |
34 | this.dataPointsCount = defaultValue; | 41 | this.dataPointsCount = defaultValue; |
1 | package org.thingsboard.server.dao.yunteng.impl; | 1 | package org.thingsboard.server.dao.yunteng.impl; |
2 | 2 | ||
3 | -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||
4 | import com.google.common.util.concurrent.*; | 3 | import com.google.common.util.concurrent.*; |
5 | import lombok.RequiredArgsConstructor; | 4 | import lombok.RequiredArgsConstructor; |
6 | import org.apache.commons.lang3.StringUtils; | 5 | import org.apache.commons.lang3.StringUtils; |
@@ -22,13 +21,10 @@ import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants; | @@ -22,13 +21,10 @@ import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants; | ||
22 | import org.thingsboard.server.common.data.yunteng.dto.*; | 21 | import org.thingsboard.server.common.data.yunteng.dto.*; |
23 | import org.thingsboard.server.common.data.yunteng.enums.RoleEnum; | 22 | import org.thingsboard.server.common.data.yunteng.enums.RoleEnum; |
24 | import org.thingsboard.server.common.data.yunteng.enums.TrendType; | 23 | import org.thingsboard.server.common.data.yunteng.enums.TrendType; |
25 | -import org.thingsboard.server.common.data.yunteng.utils.ReflectUtils; | ||
26 | import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData; | 24 | import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData; |
27 | import org.thingsboard.server.dao.entity.EntityService; | 25 | import org.thingsboard.server.dao.entity.EntityService; |
28 | import org.thingsboard.server.dao.timeseries.TimeseriesService; | 26 | import org.thingsboard.server.dao.timeseries.TimeseriesService; |
29 | import org.thingsboard.server.dao.usagerecord.ApiUsageStateService; | 27 | import org.thingsboard.server.dao.usagerecord.ApiUsageStateService; |
30 | -import org.thingsboard.server.dao.yunteng.entities.User; | ||
31 | -import org.thingsboard.server.dao.yunteng.entities.YtDevice; | ||
32 | import org.thingsboard.server.dao.yunteng.mapper.DeviceMapper; | 28 | import org.thingsboard.server.dao.yunteng.mapper.DeviceMapper; |
33 | import org.thingsboard.server.dao.yunteng.mapper.UserMapper; | 29 | import org.thingsboard.server.dao.yunteng.mapper.UserMapper; |
34 | import org.thingsboard.server.dao.yunteng.service.HomePageService; | 30 | import org.thingsboard.server.dao.yunteng.service.HomePageService; |
@@ -91,8 +87,26 @@ public class HomePageServiceImpl implements HomePageService { | @@ -91,8 +87,26 @@ public class HomePageServiceImpl implements HomePageService { | ||
91 | 87 | ||
92 | } else { | 88 | } else { |
93 | String customerId = userMapper.findCustomerIdByUserId(currentUserId); | 89 | String customerId = userMapper.findCustomerIdByUserId(currentUserId); |
94 | - // 查询customerId | ||
95 | - queryMap.put("customerId", customerId); | 90 | + if (StringUtils.isNotEmpty(customerId)) { |
91 | + // 查询customerId | ||
92 | + queryMap.put("customerId", customerId); | ||
93 | + long startTs = | ||
94 | + LocalDateTime.of(LocalDateTime.now().toLocalDate(), LocalTime.MIN) | ||
95 | + .toInstant(ZoneOffset.of("+8")) | ||
96 | + .toEpochMilli(); | ||
97 | + List<BaseHomePageTop> baseHomePageTopList = | ||
98 | + deviceMapper.findDeviceMessageInfo(startTs, customerId); | ||
99 | + if (baseHomePageTopList.size() > FastIotConstants.MagicNumber.ZERO) { | ||
100 | + messageInfo.setMessageCount(baseHomePageTopList.get(0).getSumCount()); | ||
101 | + messageInfo.setTodayMessageAdd(baseHomePageTopList.get(0).getTodayAdd()); | ||
102 | + } | ||
103 | + List<BaseHomePageTop> alarmList = | ||
104 | + deviceMapper.findDeviceAlarmInfoByCustomer(startTs, customerId); | ||
105 | + if (alarmList.size() > FastIotConstants.MagicNumber.ZERO) { | ||
106 | + alarm.setSumCount(alarmList.get(0).getSumCount()); | ||
107 | + alarm.setTodayAdd(alarmList.get(0).getTodayAdd()); | ||
108 | + } | ||
109 | + } | ||
96 | } | 110 | } |
97 | deviceList = deviceMapper.findDevices(queryMap); | 111 | deviceList = deviceMapper.findDevices(queryMap); |
98 | homeDeviceInfo = new HomeDeviceInfoDTO(FastIotConstants.MagicNumber.ZERO); | 112 | homeDeviceInfo = new HomeDeviceInfoDTO(FastIotConstants.MagicNumber.ZERO); |
@@ -178,8 +192,8 @@ public class HomePageServiceImpl implements HomePageService { | @@ -178,8 +192,8 @@ public class HomePageServiceImpl implements HomePageService { | ||
178 | listenableFuture, | 192 | listenableFuture, |
179 | getTransportMessageCallback(deferredResult), | 193 | getTransportMessageCallback(deferredResult), |
180 | MoreExecutors.directExecutor()); | 194 | MoreExecutors.directExecutor()); |
181 | - }else{ | ||
182 | - List<TenantTransportMessageDTO> result =new ArrayList<>(); | 195 | + } else { |
196 | + List<TenantTransportMessageDTO> result = new ArrayList<>(); | ||
183 | deferredResult.setResult(result); | 197 | deferredResult.setResult(result); |
184 | } | 198 | } |
185 | return deferredResult; | 199 | return deferredResult; |
@@ -312,7 +326,7 @@ public class HomePageServiceImpl implements HomePageService { | @@ -312,7 +326,7 @@ public class HomePageServiceImpl implements HomePageService { | ||
312 | Integer value = | 326 | Integer value = |
313 | StringUtils.isEmpty(tsValue) | 327 | StringUtils.isEmpty(tsValue) |
314 | ? FastIotConstants.MagicNumber.ZERO | 328 | ? FastIotConstants.MagicNumber.ZERO |
315 | - : Integer.valueOf(tsValue); | 329 | + : Integer.parseInt(tsValue); |
316 | switch (mapKey) { | 330 | switch (mapKey) { |
317 | case "createdAlarmsCount": | 331 | case "createdAlarmsCount": |
318 | alarm.setSumCount(value); | 332 | alarm.setSumCount(value); |
@@ -5,7 +5,9 @@ import com.baomidou.mybatisplus.core.metadata.IPage; | @@ -5,7 +5,9 @@ import com.baomidou.mybatisplus.core.metadata.IPage; | ||
5 | import org.apache.ibatis.annotations.Mapper; | 5 | import org.apache.ibatis.annotations.Mapper; |
6 | import org.apache.ibatis.annotations.Param; | 6 | import org.apache.ibatis.annotations.Param; |
7 | import org.thingsboard.server.common.data.Device; | 7 | import org.thingsboard.server.common.data.Device; |
8 | +import org.thingsboard.server.common.data.yunteng.dto.BaseHomePageTop; | ||
8 | import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO; | 9 | import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO; |
10 | +import org.thingsboard.server.common.data.yunteng.dto.HomePageTopMessage; | ||
9 | import org.thingsboard.server.common.data.yunteng.dto.RelationDeviceDTO; | 11 | import org.thingsboard.server.common.data.yunteng.dto.RelationDeviceDTO; |
10 | import org.thingsboard.server.dao.yunteng.entities.YtDevice; | 12 | import org.thingsboard.server.dao.yunteng.entities.YtDevice; |
11 | 13 | ||
@@ -15,49 +17,58 @@ import java.util.Map; | @@ -15,49 +17,58 @@ import java.util.Map; | ||
15 | @Mapper | 17 | @Mapper |
16 | public interface DeviceMapper extends BaseMapper<YtDevice> { | 18 | public interface DeviceMapper extends BaseMapper<YtDevice> { |
17 | 19 | ||
18 | - IPage<DeviceDTO> getDevicePage( | ||
19 | - IPage<?> page, @Param("queryMap") Map<String, Object> queryMap); | ||
20 | - | ||
21 | - DeviceDTO selectDetail(@Param("tenantId") String tenantId, @Param("id") String id); | ||
22 | - | ||
23 | - /** | ||
24 | - * 通过网关子设备的TB设备ID查询网关设备信息 | ||
25 | - * | ||
26 | - * @param tbDeviceId 网关子设备的TB设备ID | ||
27 | - * @return 网关设备信息 | ||
28 | - */ | ||
29 | - List<DeviceDTO> findGateWayDeviceByTbDeviceId(@Param("tbDeviceId") String tbDeviceId); | ||
30 | - | ||
31 | - /** | ||
32 | - * 更新设备告警状态 | ||
33 | - * | ||
34 | - * @param tbDeviceId TB设备主键 | ||
35 | - * @param created 告警状态:0正常,1告警 | ||
36 | - * @return true or false | ||
37 | - */ | ||
38 | - boolean freshAlarmStatus(@Param("tbDeviceId") String tbDeviceId, @Param("created") Integer created); | ||
39 | - | ||
40 | - /** | ||
41 | - * 用于统计设备相关信息 | ||
42 | - * | ||
43 | - * @param queryMap 查询条件 | ||
44 | - * @return 查询的设备信息 | ||
45 | - */ | ||
46 | - List<DeviceDTO> findDevices(@Param("queryMap") Map<String, Object> queryMap); | ||
47 | - | ||
48 | - /** | ||
49 | - * 获取关联设备的子设备分页列表 | ||
50 | - * | ||
51 | - * @param page 分页 | ||
52 | - * @param queryMap 查询条件 | ||
53 | - * @return 分页数据 | ||
54 | - */ | ||
55 | - IPage<RelationDeviceDTO> getRelationDevicePage(IPage<?> page, @Param("queryMap") Map<String, Object> queryMap); | ||
56 | - | ||
57 | - /** | ||
58 | - * 通过用户ID查询分配的所有设备ID | ||
59 | - * @param customerId 用户ID | ||
60 | - * @return 所有的设备ids | ||
61 | - */ | ||
62 | - List<String> findDeviceIdsByCustomerId(@Param("customerId")String customerId); | 20 | + IPage<DeviceDTO> getDevicePage(IPage<?> page, @Param("queryMap") Map<String, Object> queryMap); |
21 | + | ||
22 | + DeviceDTO selectDetail(@Param("tenantId") String tenantId, @Param("id") String id); | ||
23 | + | ||
24 | + /** | ||
25 | + * 通过网关子设备的TB设备ID查询网关设备信息 | ||
26 | + * | ||
27 | + * @param tbDeviceId 网关子设备的TB设备ID | ||
28 | + * @return 网关设备信息 | ||
29 | + */ | ||
30 | + List<DeviceDTO> findGateWayDeviceByTbDeviceId(@Param("tbDeviceId") String tbDeviceId); | ||
31 | + | ||
32 | + /** | ||
33 | + * 更新设备告警状态 | ||
34 | + * | ||
35 | + * @param tbDeviceId TB设备主键 | ||
36 | + * @param created 告警状态:0正常,1告警 | ||
37 | + * @return true or false | ||
38 | + */ | ||
39 | + boolean freshAlarmStatus( | ||
40 | + @Param("tbDeviceId") String tbDeviceId, @Param("created") Integer created); | ||
41 | + | ||
42 | + /** | ||
43 | + * 用于统计设备相关信息 | ||
44 | + * | ||
45 | + * @param queryMap 查询条件 | ||
46 | + * @return 查询的设备信息 | ||
47 | + */ | ||
48 | + List<DeviceDTO> findDevices(@Param("queryMap") Map<String, Object> queryMap); | ||
49 | + | ||
50 | + /** | ||
51 | + * 获取关联设备的子设备分页列表 | ||
52 | + * | ||
53 | + * @param page 分页 | ||
54 | + * @param queryMap 查询条件 | ||
55 | + * @return 分页数据 | ||
56 | + */ | ||
57 | + IPage<RelationDeviceDTO> getRelationDevicePage( | ||
58 | + IPage<?> page, @Param("queryMap") Map<String, Object> queryMap); | ||
59 | + | ||
60 | + /** | ||
61 | + * 通过用户ID查询分配的所有设备ID | ||
62 | + * | ||
63 | + * @param customerId 用户ID | ||
64 | + * @return 所有的设备ids | ||
65 | + */ | ||
66 | + List<String> findDeviceIdsByCustomerId(@Param("customerId") String customerId); | ||
67 | + | ||
68 | + List<BaseHomePageTop> findDeviceMessageInfo( | ||
69 | + @Param("todayTime") Long todayTime, @Param("customerId") String customerId); | ||
70 | + | ||
71 | + List<BaseHomePageTop> findDeviceAlarmInfoByCustomer( | ||
72 | + @Param("todayTime") Long todayTime, @Param("customerId") String customerId); | ||
73 | + | ||
63 | } | 74 | } |
@@ -44,6 +44,11 @@ | @@ -44,6 +44,11 @@ | ||
44 | <result property="lastOnlineTime" column="last_online_time"/> | 44 | <result property="lastOnlineTime" column="last_online_time"/> |
45 | </resultMap> | 45 | </resultMap> |
46 | 46 | ||
47 | + <resultMap id="baseHomePageTop" type="org.thingsboard.server.common.data.yunteng.dto.BaseHomePageTop"> | ||
48 | + <result property="sumCount" column="sum_count"/> | ||
49 | + <result property="todayAdd" column="today_add"/> | ||
50 | + </resultMap> | ||
51 | + | ||
47 | <sql id="basicColumns"> | 52 | <sql id="basicColumns"> |
48 | ifd.id,ifd.name,ifd.device_info,ifd.profile_id,ifd.active_time,ifd.tenant_id,ifd.description | 53 | ifd.id,ifd.name,ifd.device_info,ifd.profile_id,ifd.active_time,ifd.tenant_id,ifd.description |
49 | ,ifd.tb_device_id,ifd.label,ifd.last_connect_time,ifd.device_type,ifd.device_state,ifd.create_time,ifd.update_time,ifd.creator, | 54 | ,ifd.tb_device_id,ifd.label,ifd.last_connect_time,ifd.device_type,ifd.device_state,ifd.create_time,ifd.update_time,ifd.creator, |
@@ -172,7 +177,7 @@ | @@ -172,7 +177,7 @@ | ||
172 | AND ifd.tenant_id = #{queryMap.tenantId} | 177 | AND ifd.tenant_id = #{queryMap.tenantId} |
173 | </if> | 178 | </if> |
174 | <if test="queryMap.customerId !=null and queryMap.customerId !=''"> | 179 | <if test="queryMap.customerId !=null and queryMap.customerId !=''"> |
175 | - AND d.customer_id = #{queryMap.customerId} | 180 | + AND d.customer_id :: TEXT = #{queryMap.customerId} |
176 | </if> | 181 | </if> |
177 | </where> | 182 | </where> |
178 | </select> | 183 | </select> |
@@ -215,4 +220,21 @@ | @@ -215,4 +220,21 @@ | ||
215 | SELECT idi.tb_device_id FROM iotfs_device idi LEFT JOIN device d ON d.id::TEXT = idi.tb_device_id | 220 | SELECT idi.tb_device_id FROM iotfs_device idi LEFT JOIN device d ON d.id::TEXT = idi.tb_device_id |
216 | WHERE customer_id::TEXT = #{customerId} | 221 | WHERE customer_id::TEXT = #{customerId} |
217 | </select> | 222 | </select> |
223 | + | ||
224 | + <select id="findDeviceMessageInfo" resultMap="baseHomePageTop"> | ||
225 | + SELECT COUNT( ts.ts ) AS sum_count, | ||
226 | + SUM ( CASE WHEN ts.ts >= #{todayTime} THEN 1 ELSE 0 END ) AS today_add | ||
227 | + FROM (SELECT tk.ts FROM ts_kv tk | ||
228 | + LEFT JOIN device d ON tk.entity_id = d.ID | ||
229 | + WHERE | ||
230 | + d.customer_id::TEXT = #{customerId} | ||
231 | + GROUP BY tk.ts) ts; | ||
232 | + </select> | ||
233 | + | ||
234 | + <select id="findDeviceAlarmInfoByCustomer" resultMap="baseHomePageTop"> | ||
235 | + SELECT COUNT( ID ) AS sum_count, | ||
236 | + SUM ( CASE WHEN created_time >= #{todayTime} THEN 1 ELSE 0 END ) AS today_add | ||
237 | + FROM alarm | ||
238 | + WHERE customer_id ::TEXT = #{customerId} AND originator_type = 5; | ||
239 | + </select> | ||
218 | </mapper> | 240 | </mapper> |
@@ -205,6 +205,6 @@ | @@ -205,6 +205,6 @@ | ||
205 | </select> | 205 | </select> |
206 | 206 | ||
207 | <select id="findCustomerIdByUserId" resultType="java.lang.String"> | 207 | <select id="findCustomerIdByUserId" resultType="java.lang.String"> |
208 | - SELECT tu.customer_id FROM sys_user su LEFT JOIN tb_user tu ON su.tb_user = tu.id WHERE su.id = #{id} | 208 | + SELECT tu.customer_id FROM sys_user su LEFT JOIN tb_user tu ON su.tb_user = tu.id::TEXT WHERE su.id = #{id} |
209 | </select> | 209 | </select> |
210 | </mapper> | 210 | </mapper> |