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 10 import org.springframework.web.bind.annotation.RequestParam;
11 11 import org.springframework.web.bind.annotation.RestController;
12 12 import org.springframework.web.context.request.async.DeferredResult;
  13 +import org.thingsboard.server.common.data.exception.ThingsboardException;
13 14 import org.thingsboard.server.common.data.query.TsValue;
  15 +import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
14 16 import org.thingsboard.server.common.data.yunteng.dto.*;
15 17 import org.thingsboard.server.common.data.yunteng.enums.TrendType;
16 18 import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
17 19 import org.thingsboard.server.controller.BaseController;
  20 +import org.thingsboard.server.dao.exception.DataValidationException;
18 21 import org.thingsboard.server.dao.yunteng.service.HomePageService;
19   -import org.thingsboard.server.dao.yunteng.service.YtTenantService;
20 22
21 23 import java.util.HashMap;
22 24 import java.util.List;
  25 +import java.util.concurrent.ExecutionException;
23 26
24 27 import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.PAGE;
25 28 import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.PAGE_SIZE;
... ... @@ -32,22 +35,16 @@ public class HomePageController extends BaseController {
32 35
33 36 private final HomePageService homePageService;
34 37
35   - private final YtTenantService tenantService;
36   -
37 38 @GetMapping("left/top")
38 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 50 @GetMapping("right/overdue")
... ... @@ -70,12 +67,25 @@ public class HomePageController extends BaseController {
70 67
71 68 @GetMapping("left/bottom")
72 69 @ApiOperation(value = "获取左侧底部信息")
73   - @PreAuthorize("hasAnyAuthority('SYS_ADMIN','PLATFORM_ADMIN')")
  70 + @PreAuthorize("hasAnyAuthority('SYS_ADMIN','PLATFORM_ADMIN','CUSTOMER_USER')")
74 71 public DeferredResult<List<TsValue>> getLeftBottomInfo(
75 72 @RequestParam(value = "startTs") long startTs,
76 73 @RequestParam("endTs") long endTs,
77 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 9 * 客户趋势
10 10 */
11 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 126
127 127 @Override
128 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 135 List<CompletableFuture<TsValue>> futures = new ArrayList<>();
131 136 interval = interval < 7200000 ? 7200000 : interval;
132 137 long stepTs = startTs;
... ... @@ -139,10 +144,25 @@ public class HomePageServiceImpl implements HomePageService {
139 144 LocalDateTime endTime =
140 145 LocalDateTime.ofEpochSecond(tempEndTs / 1000, 0, ZoneOffset.ofHours(8));
141 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 167 futures.add(tsValueCompletableFuture);
148 168 stepTs = tempEndTs;
... ... @@ -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 4 import com.baomidou.mybatisplus.core.metadata.IPage;
5 5 import org.apache.ibatis.annotations.Mapper;
6 6 import org.apache.ibatis.annotations.Param;
7   -import org.thingsboard.server.common.data.Device;
8 7 import org.thingsboard.server.common.data.yunteng.dto.BaseHomePageTop;
9 8 import org.thingsboard.server.common.data.yunteng.dto.DeviceDTO;
10   -import org.thingsboard.server.common.data.yunteng.dto.HomePageTopMessage;
11 9 import org.thingsboard.server.common.data.yunteng.dto.RelationDeviceDTO;
12 10 import org.thingsboard.server.dao.yunteng.entities.YtDevice;
13 11
... ... @@ -69,6 +67,15 @@ public interface DeviceMapper extends BaseMapper<YtDevice> {
69 67 @Param("todayTime") Long todayTime, @Param("customerId") String customerId);
70 68
71 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 2
3 3 import org.springframework.web.context.request.async.DeferredResult;
4 4 import org.thingsboard.server.common.data.id.TenantId;
5   -import org.thingsboard.server.common.data.query.EntityKey;
6 5 import org.thingsboard.server.common.data.query.TsValue;
7 6 import org.thingsboard.server.common.data.yunteng.dto.HomePageLeftTopDTO;
8   -import org.thingsboard.server.common.data.yunteng.dto.HomePageRightDTO;
9 7 import org.thingsboard.server.common.data.yunteng.dto.TenantDTO;
10 8 import org.thingsboard.server.common.data.yunteng.dto.TenantTransportMessageDTO;
11 9 import org.thingsboard.server.common.data.yunteng.enums.TrendType;
... ... @@ -52,19 +50,23 @@ public interface HomePageService {
52 50 * @param trend 趋势类型
53 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 57 * 获取首页TOP10
  58 + *
59 59 * @return top10
60 60 */
61 61 DeferredResult<List<TenantTransportMessageDTO>> getTop10();
62 62
63 63 /**
64 64 * 获取租户的传输信息
  65 + *
65 66 * @param tenantId 租户ID
66 67 * @param tenantName 租户姓名
67 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 237 FROM alarm
238 238 WHERE customer_id ::TEXT = #{customerId} AND originator_type = 5;
239 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 271 </mapper>
... ...