Commit 39ae0c1b512adc3008280def8a86017ca25f4e94

Authored by 云中非
2 parents 82865fa5 970a0e63

Merge branch 'master' into 20220322

@@ -10,16 +10,19 @@ import org.springframework.web.bind.annotation.RequestMapping; @@ -10,16 +10,19 @@ import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestParam; 10 import org.springframework.web.bind.annotation.RequestParam;
11 import org.springframework.web.bind.annotation.RestController; 11 import org.springframework.web.bind.annotation.RestController;
12 import org.springframework.web.context.request.async.DeferredResult; 12 import org.springframework.web.context.request.async.DeferredResult;
  13 +import org.thingsboard.server.common.data.exception.ThingsboardException;
13 import org.thingsboard.server.common.data.query.TsValue; 14 import org.thingsboard.server.common.data.query.TsValue;
  15 +import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
14 import org.thingsboard.server.common.data.yunteng.dto.*; 16 import org.thingsboard.server.common.data.yunteng.dto.*;
15 import org.thingsboard.server.common.data.yunteng.enums.TrendType; 17 import org.thingsboard.server.common.data.yunteng.enums.TrendType;
16 import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData; 18 import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
17 import org.thingsboard.server.controller.BaseController; 19 import org.thingsboard.server.controller.BaseController;
  20 +import org.thingsboard.server.dao.exception.DataValidationException;
18 import org.thingsboard.server.dao.yunteng.service.HomePageService; 21 import org.thingsboard.server.dao.yunteng.service.HomePageService;
19 -import org.thingsboard.server.dao.yunteng.service.YtTenantService;  
20 22
21 import java.util.HashMap; 23 import java.util.HashMap;
22 import java.util.List; 24 import java.util.List;
  25 +import java.util.concurrent.ExecutionException;
23 26
24 import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.PAGE; 27 import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.PAGE;
25 import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.PAGE_SIZE; 28 import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.PAGE_SIZE;
@@ -32,22 +35,16 @@ public class HomePageController extends BaseController { @@ -32,22 +35,16 @@ public class HomePageController extends BaseController {
32 35
33 private final HomePageService homePageService; 36 private final HomePageService homePageService;
34 37
35 - private final YtTenantService tenantService;  
36 -  
37 @GetMapping("left/top") 38 @GetMapping("left/top")
38 @ApiOperation(value = "获取左侧顶部信息") 39 @ApiOperation(value = "获取左侧顶部信息")
39 - public HomePageLeftTopDTO getLeftTopInfo() {  
40 - try {  
41 - return homePageService.getHomePageLeftTopInfo(  
42 - getCurrentUser().isPtSysadmin(),  
43 - getCurrentUser().isPtAdmin(),  
44 - getCurrentUser().isPtTenantAdmin(),  
45 - getCurrentUser().getCurrentTenantId(),  
46 - getCurrentUser().getCurrentUserId());  
47 - } catch (Exception e) {  
48 - handleException(e);  
49 - }  
50 - return null; 40 + public HomePageLeftTopDTO getLeftTopInfo()
  41 + throws ThingsboardException, ExecutionException, InterruptedException {
  42 + return homePageService.getHomePageLeftTopInfo(
  43 + getCurrentUser().isPtSysadmin(),
  44 + getCurrentUser().isPtAdmin(),
  45 + getCurrentUser().isPtTenantAdmin(),
  46 + getCurrentUser().getCurrentTenantId(),
  47 + getCurrentUser().getCurrentUserId());
51 } 48 }
52 49
53 @GetMapping("right/overdue") 50 @GetMapping("right/overdue")
@@ -70,12 +67,25 @@ public class HomePageController extends BaseController { @@ -70,12 +67,25 @@ public class HomePageController extends BaseController {
70 67
71 @GetMapping("left/bottom") 68 @GetMapping("left/bottom")
72 @ApiOperation(value = "获取左侧底部信息") 69 @ApiOperation(value = "获取左侧底部信息")
73 - @PreAuthorize("hasAnyAuthority('SYS_ADMIN','PLATFORM_ADMIN')") 70 + @PreAuthorize("hasAnyAuthority('SYS_ADMIN','PLATFORM_ADMIN','CUSTOMER_USER')")
74 public DeferredResult<List<TsValue>> getLeftBottomInfo( 71 public DeferredResult<List<TsValue>> getLeftBottomInfo(
75 @RequestParam(value = "startTs") long startTs, 72 @RequestParam(value = "startTs") long startTs,
76 @RequestParam("endTs") long endTs, 73 @RequestParam("endTs") long endTs,
77 @RequestParam("interval") long interval, 74 @RequestParam("interval") long interval,
78 - @RequestParam("trend") TrendType trend) {  
79 - return homePageService.getHomePageLeftBottomInfo(startTs, endTs, interval, trend); 75 + @RequestParam("trend") TrendType trend)
  76 + throws ThingsboardException {
  77 + String customerId = getCurrentUser().getCustomerId().getId().toString();
  78 + boolean isCustomer = getCurrentUser().isCustomerUser();
  79 + if(TrendType.CUSTOMER_MESSAGE_STATISTICAL == trend || TrendType.CUSTOMER_ALARM_STATISTICAL == trend){
  80 + if(!isCustomer){
  81 + throw new DataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
  82 + }
  83 + }
  84 + if(TrendType.TENANT_TREND == trend || TrendType.CUSTOMER_TREND == trend){
  85 + if(isCustomer){
  86 + throw new DataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
  87 + }
  88 + }
  89 + return homePageService.getHomePageLeftBottomInfo(customerId, startTs, endTs, interval, trend,isCustomer);
80 } 90 }
81 } 91 }
@@ -9,4 +9,14 @@ public enum TrendType { @@ -9,4 +9,14 @@ public enum TrendType {
9 * 客户趋势 9 * 客户趋势
10 */ 10 */
11 CUSTOMER_TREND, 11 CUSTOMER_TREND,
  12 +
  13 + /**
  14 + * 客户告警统计
  15 + */
  16 + CUSTOMER_ALARM_STATISTICAL,
  17 +
  18 + /**
  19 + * 客户消息统计
  20 + */
  21 + CUSTOMER_MESSAGE_STATISTICAL
12 } 22 }
@@ -126,7 +126,12 @@ public class HomePageServiceImpl implements HomePageService { @@ -126,7 +126,12 @@ public class HomePageServiceImpl implements HomePageService {
126 126
127 @Override 127 @Override
128 public DeferredResult<List<TsValue>> getHomePageLeftBottomInfo( 128 public DeferredResult<List<TsValue>> getHomePageLeftBottomInfo(
129 - long startTs, long endTs, long interval, TrendType trend) { 129 + String customerId,
  130 + long startTs,
  131 + long endTs,
  132 + long interval,
  133 + TrendType trend,
  134 + boolean isCustomer) {
130 List<CompletableFuture<TsValue>> futures = new ArrayList<>(); 135 List<CompletableFuture<TsValue>> futures = new ArrayList<>();
131 interval = interval < 7200000 ? 7200000 : interval; 136 interval = interval < 7200000 ? 7200000 : interval;
132 long stepTs = startTs; 137 long stepTs = startTs;
@@ -139,10 +144,25 @@ public class HomePageServiceImpl implements HomePageService { @@ -139,10 +144,25 @@ public class HomePageServiceImpl implements HomePageService {
139 LocalDateTime endTime = 144 LocalDateTime endTime =
140 LocalDateTime.ofEpochSecond(tempEndTs / 1000, 0, ZoneOffset.ofHours(8)); 145 LocalDateTime.ofEpochSecond(tempEndTs / 1000, 0, ZoneOffset.ofHours(8));
141 CompletableFuture<TsValue> tsValueCompletableFuture = null; 146 CompletableFuture<TsValue> tsValueCompletableFuture = null;
142 - if (trend.equals(TrendType.TENANT_TREND)) {  
143 - tsValueCompletableFuture = tenantService.findTenantsByTs(startTime, endTime, ts);  
144 - } else if (trend.equals(TrendType.CUSTOMER_TREND)) {  
145 - tsValueCompletableFuture = ytUserService.findUsersAsyncByTs(startTime, endTime, ts); 147 + //客户查询的是告警统计 消息统计
  148 + if (isCustomer) {
  149 + if (trend == TrendType.CUSTOMER_ALARM_STATISTICAL) {
  150 + tsValueCompletableFuture =
  151 + findDeviceInfoByTs(
  152 + customerId, tempStartTs, tempEndTs, ts, TrendType.CUSTOMER_ALARM_STATISTICAL);
  153 + }
  154 + if (trend == TrendType.CUSTOMER_MESSAGE_STATISTICAL) {
  155 + tsValueCompletableFuture =
  156 + findDeviceInfoByTs(
  157 + customerId, tempStartTs, tempEndTs, ts, TrendType.CUSTOMER_MESSAGE_STATISTICAL);
  158 + }
  159 + } else {
  160 + if (trend == TrendType.TENANT_TREND) {
  161 + tsValueCompletableFuture = tenantService.findTenantsByTs(startTime, endTime, ts);
  162 + }
  163 + if (trend == TrendType.CUSTOMER_TREND) {
  164 + tsValueCompletableFuture = ytUserService.findUsersAsyncByTs(startTime, endTime, ts);
  165 + }
146 } 166 }
147 futures.add(tsValueCompletableFuture); 167 futures.add(tsValueCompletableFuture);
148 stepTs = tempEndTs; 168 stepTs = tempEndTs;
@@ -533,4 +553,15 @@ public class HomePageServiceImpl implements HomePageService { @@ -533,4 +553,15 @@ public class HomePageServiceImpl implements HomePageService {
533 } 553 }
534 }; 554 };
535 } 555 }
  556 +
  557 + private CompletableFuture<TsValue> findDeviceInfoByTs(
  558 + String customerId, Long startTs, Long endTs, long ts, TrendType trend) {
  559 + Integer value;
  560 + if (trend == TrendType.CUSTOMER_MESSAGE_STATISTICAL) {
  561 + value = deviceMapper.findDeviceMessageInfoByTs(customerId, startTs, endTs);
  562 + } else {
  563 + value = deviceMapper.findDeviceAlarmInfoByCreatedTime(customerId, startTs, endTs);
  564 + }
  565 + return CompletableFuture.supplyAsync(() -> new TsValue(ts, String.valueOf(value)));
  566 + }
536 } 567 }
@@ -4,10 +4,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; @@ -4,10 +4,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 import com.baomidou.mybatisplus.core.metadata.IPage; 4 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;  
8 import org.thingsboard.server.common.data.yunteng.dto.BaseHomePageTop; 7 import org.thingsboard.server.common.data.yunteng.dto.BaseHomePageTop;
9 import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO; 8 import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO;
10 -import org.thingsboard.server.common.data.yunteng.dto.HomePageTopMessage;  
11 import org.thingsboard.server.common.data.yunteng.dto.RelationDeviceDTO; 9 import org.thingsboard.server.common.data.yunteng.dto.RelationDeviceDTO;
12 import org.thingsboard.server.dao.yunteng.entities.YtDevice; 10 import org.thingsboard.server.dao.yunteng.entities.YtDevice;
13 11
@@ -69,6 +67,15 @@ public interface DeviceMapper extends BaseMapper<YtDevice> { @@ -69,6 +67,15 @@ public interface DeviceMapper extends BaseMapper<YtDevice> {
69 @Param("todayTime") Long todayTime, @Param("customerId") String customerId); 67 @Param("todayTime") Long todayTime, @Param("customerId") String customerId);
70 68
71 List<BaseHomePageTop> findDeviceAlarmInfoByCustomer( 69 List<BaseHomePageTop> findDeviceAlarmInfoByCustomer(
72 - @Param("todayTime") Long todayTime, @Param("customerId") String customerId); 70 + @Param("todayTime") Long todayTime, @Param("customerId") String customerId);
  71 +
  72 + Integer findDeviceMessageInfoByTs(
  73 + @Param("customerId") String customerId,
  74 + @Param("startTime") Long startTime,
  75 + @Param("endTime") Long endTime);
73 76
  77 + Integer findDeviceAlarmInfoByCreatedTime(
  78 + @Param("customerId") String customerId,
  79 + @Param("startTime") Long startTime,
  80 + @Param("endTime") Long endTime);
74 } 81 }
@@ -2,10 +2,8 @@ package org.thingsboard.server.dao.yunteng.service; @@ -2,10 +2,8 @@ package org.thingsboard.server.dao.yunteng.service;
2 2
3 import org.springframework.web.context.request.async.DeferredResult; 3 import org.springframework.web.context.request.async.DeferredResult;
4 import org.thingsboard.server.common.data.id.TenantId; 4 import org.thingsboard.server.common.data.id.TenantId;
5 -import org.thingsboard.server.common.data.query.EntityKey;  
6 import org.thingsboard.server.common.data.query.TsValue; 5 import org.thingsboard.server.common.data.query.TsValue;
7 import org.thingsboard.server.common.data.yunteng.dto.HomePageLeftTopDTO; 6 import org.thingsboard.server.common.data.yunteng.dto.HomePageLeftTopDTO;
8 -import org.thingsboard.server.common.data.yunteng.dto.HomePageRightDTO;  
9 import org.thingsboard.server.common.data.yunteng.dto.TenantDTO; 7 import org.thingsboard.server.common.data.yunteng.dto.TenantDTO;
10 import org.thingsboard.server.common.data.yunteng.dto.TenantTransportMessageDTO; 8 import org.thingsboard.server.common.data.yunteng.dto.TenantTransportMessageDTO;
11 import org.thingsboard.server.common.data.yunteng.enums.TrendType; 9 import org.thingsboard.server.common.data.yunteng.enums.TrendType;
@@ -52,19 +50,23 @@ public interface HomePageService { @@ -52,19 +50,23 @@ public interface HomePageService {
52 * @param trend 趋势类型 50 * @param trend 趋势类型
53 * @return 左侧底部信息 51 * @return 左侧底部信息
54 */ 52 */
55 - DeferredResult<List<TsValue>> getHomePageLeftBottomInfo(long startTs, long endTs, long interval, TrendType trend); 53 + DeferredResult<List<TsValue>> getHomePageLeftBottomInfo(
  54 + String customerId, long startTs, long endTs, long interval, TrendType trend,boolean isCustomer);
56 55
57 /** 56 /**
58 * 获取首页TOP10 57 * 获取首页TOP10
  58 + *
59 * @return top10 59 * @return top10
60 */ 60 */
61 DeferredResult<List<TenantTransportMessageDTO>> getTop10(); 61 DeferredResult<List<TenantTransportMessageDTO>> getTop10();
62 62
63 /** 63 /**
64 * 获取租户的传输信息 64 * 获取租户的传输信息
  65 + *
65 * @param tenantId 租户ID 66 * @param tenantId 租户ID
66 * @param tenantName 租户姓名 67 * @param tenantName 租户姓名
67 * @return 传输信息 68 * @return 传输信息
68 */ 69 */
69 - CompletableFuture<TenantTransportMessageDTO> getTransportMessageByTenantId(TenantId tenantId,String tenantName); 70 + CompletableFuture<TenantTransportMessageDTO> getTransportMessageByTenantId(
  71 + TenantId tenantId, String tenantName);
70 } 72 }
@@ -237,4 +237,35 @@ @@ -237,4 +237,35 @@
237 FROM alarm 237 FROM alarm
238 WHERE customer_id ::TEXT = #{customerId} AND originator_type = 5; 238 WHERE customer_id ::TEXT = #{customerId} AND originator_type = 5;
239 </select> 239 </select>
  240 +
  241 + <select id="findDeviceMessageInfoByTs" resultType="java.lang.Integer">
  242 + SELECT COUNT
  243 + ( ts.ts ) AS sum_count
  244 + FROM
  245 + (
  246 + SELECT
  247 + tk.ts
  248 + FROM
  249 + ts_kv tk
  250 + LEFT JOIN device d ON tk.entity_id = d.ID
  251 + WHERE
  252 + d.customer_id ::TEXT = #{customerId}
  253 + AND tk.ts &gt;= #{startTime}
  254 + AND tk.ts &lt; #{endTime}
  255 + GROUP BY
  256 + tk.ts
  257 + ) ts;
  258 + </select>
  259 +
  260 + <select id="findDeviceAlarmInfoByCreatedTime" resultType="java.lang.Integer">
  261 + SELECT COUNT
  262 + ( ID ) AS sum_count
  263 + FROM
  264 + alarm
  265 + WHERE
  266 + customer_id ::TEXT = #{customerId}
  267 + AND created_time &gt;= #{startTime}
  268 + AND created_time &lt; #{endTime}
  269 + AND originator_type = 5;
  270 + </select>
240 </mapper> 271 </mapper>