Showing
5 changed files
with
110 additions
and
56 deletions
... | ... | @@ -29,6 +29,13 @@ public class HomePageTopMessage { |
29 | 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 | 39 | public HomePageTopMessage(Integer defaultValue) { |
33 | 40 | this.messageCount = defaultValue; |
34 | 41 | this.dataPointsCount = defaultValue; | ... | ... |
1 | 1 | package org.thingsboard.server.dao.yunteng.impl; |
2 | 2 | |
3 | -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | |
4 | 3 | import com.google.common.util.concurrent.*; |
5 | 4 | import lombok.RequiredArgsConstructor; |
6 | 5 | import org.apache.commons.lang3.StringUtils; |
... | ... | @@ -22,13 +21,10 @@ import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants; |
22 | 21 | import org.thingsboard.server.common.data.yunteng.dto.*; |
23 | 22 | import org.thingsboard.server.common.data.yunteng.enums.RoleEnum; |
24 | 23 | import org.thingsboard.server.common.data.yunteng.enums.TrendType; |
25 | -import org.thingsboard.server.common.data.yunteng.utils.ReflectUtils; | |
26 | 24 | import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData; |
27 | 25 | import org.thingsboard.server.dao.entity.EntityService; |
28 | 26 | import org.thingsboard.server.dao.timeseries.TimeseriesService; |
29 | 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 | 28 | import org.thingsboard.server.dao.yunteng.mapper.DeviceMapper; |
33 | 29 | import org.thingsboard.server.dao.yunteng.mapper.UserMapper; |
34 | 30 | import org.thingsboard.server.dao.yunteng.service.HomePageService; |
... | ... | @@ -91,8 +87,26 @@ public class HomePageServiceImpl implements HomePageService { |
91 | 87 | |
92 | 88 | } else { |
93 | 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 | 111 | deviceList = deviceMapper.findDevices(queryMap); |
98 | 112 | homeDeviceInfo = new HomeDeviceInfoDTO(FastIotConstants.MagicNumber.ZERO); |
... | ... | @@ -178,8 +192,8 @@ public class HomePageServiceImpl implements HomePageService { |
178 | 192 | listenableFuture, |
179 | 193 | getTransportMessageCallback(deferredResult), |
180 | 194 | MoreExecutors.directExecutor()); |
181 | - }else{ | |
182 | - List<TenantTransportMessageDTO> result =new ArrayList<>(); | |
195 | + } else { | |
196 | + List<TenantTransportMessageDTO> result = new ArrayList<>(); | |
183 | 197 | deferredResult.setResult(result); |
184 | 198 | } |
185 | 199 | return deferredResult; |
... | ... | @@ -312,7 +326,7 @@ public class HomePageServiceImpl implements HomePageService { |
312 | 326 | Integer value = |
313 | 327 | StringUtils.isEmpty(tsValue) |
314 | 328 | ? FastIotConstants.MagicNumber.ZERO |
315 | - : Integer.valueOf(tsValue); | |
329 | + : Integer.parseInt(tsValue); | |
316 | 330 | switch (mapKey) { |
317 | 331 | case "createdAlarmsCount": |
318 | 332 | alarm.setSumCount(value); | ... | ... |
... | ... | @@ -5,7 +5,9 @@ import com.baomidou.mybatisplus.core.metadata.IPage; |
5 | 5 | import org.apache.ibatis.annotations.Mapper; |
6 | 6 | import org.apache.ibatis.annotations.Param; |
7 | 7 | import org.thingsboard.server.common.data.Device; |
8 | +import org.thingsboard.server.common.data.yunteng.dto.BaseHomePageTop; | |
8 | 9 | import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO; |
10 | +import org.thingsboard.server.common.data.yunteng.dto.HomePageTopMessage; | |
9 | 11 | import org.thingsboard.server.common.data.yunteng.dto.RelationDeviceDTO; |
10 | 12 | import org.thingsboard.server.dao.yunteng.entities.YtDevice; |
11 | 13 | |
... | ... | @@ -15,49 +17,58 @@ import java.util.Map; |
15 | 17 | @Mapper |
16 | 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 | 44 | <result property="lastOnlineTime" column="last_online_time"/> |
45 | 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 | 52 | <sql id="basicColumns"> |
48 | 53 | ifd.id,ifd.name,ifd.device_info,ifd.profile_id,ifd.active_time,ifd.tenant_id,ifd.description |
49 | 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 | 177 | AND ifd.tenant_id = #{queryMap.tenantId} |
173 | 178 | </if> |
174 | 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 | 181 | </if> |
177 | 182 | </where> |
178 | 183 | </select> |
... | ... | @@ -215,4 +220,21 @@ |
215 | 220 | SELECT idi.tb_device_id FROM iotfs_device idi LEFT JOIN device d ON d.id::TEXT = idi.tb_device_id |
216 | 221 | WHERE customer_id::TEXT = #{customerId} |
217 | 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 | 240 | </mapper> | ... | ... |
... | ... | @@ -205,6 +205,6 @@ |
205 | 205 | </select> |
206 | 206 | |
207 | 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 | 209 | </select> |
210 | 210 | </mapper> | ... | ... |