Commit edd98d8e586e2eb72d64a12ec0d7a49bd419d9de
1 parent
60255ce2
feat: 告警中心
1、添加UUID数据处理函数 2、日期时间格式化工具 3、告警接口基于Mybatis实现
Showing
11 changed files
with
381 additions
and
213 deletions
... | ... | @@ -15,6 +15,7 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.controller; |
17 | 17 | |
18 | +import io.swagger.annotations.Api; | |
18 | 19 | import io.swagger.annotations.ApiOperation; |
19 | 20 | import io.swagger.annotations.ApiParam; |
20 | 21 | import org.apache.commons.lang3.StringUtils; |
... | ... | @@ -71,6 +72,7 @@ import static org.thingsboard.server.controller.ControllerConstants.UUID_WIKI_LI |
71 | 72 | @RestController |
72 | 73 | @TbCoreComponent |
73 | 74 | @RequestMapping("/api") |
75 | +@Api(tags = {"告警"}) | |
74 | 76 | public class AlarmController extends BaseController { |
75 | 77 | |
76 | 78 | public static final String ALARM_ID = "alarmId"; |
... | ... | @@ -102,7 +104,7 @@ public class AlarmController extends BaseController { |
102 | 104 | } |
103 | 105 | } |
104 | 106 | |
105 | - @ApiOperation(value = "Get Alarm Info (getAlarmInfoById)", | |
107 | + @ApiOperation(value = "详情", | |
106 | 108 | notes = "Fetch the Alarm Info object based on the provided Alarm Id. " + |
107 | 109 | ALARM_SECURITY_CHECK + ALARM_INFO_DESCRIPTION + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE) |
108 | 110 | @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") |
... | ... | @@ -178,7 +180,7 @@ public class AlarmController extends BaseController { |
178 | 180 | } |
179 | 181 | } |
180 | 182 | |
181 | - @ApiOperation(value = "Acknowledge Alarm (ackAlarm)", | |
183 | + @ApiOperation(value = "处理", | |
182 | 184 | notes = "Acknowledge the Alarm. " + |
183 | 185 | "Once acknowledged, the 'ack_ts' field will be set to current timestamp and special rule chain event 'ALARM_ACK' will be generated. " + |
184 | 186 | "Referencing non-existing Alarm Id will cause an error." + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE) |
... | ... | @@ -202,7 +204,7 @@ public class AlarmController extends BaseController { |
202 | 204 | } |
203 | 205 | } |
204 | 206 | |
205 | - @ApiOperation(value = "Clear Alarm (clearAlarm)", | |
207 | + @ApiOperation(value = "清除", | |
206 | 208 | notes = "Clear the Alarm. " + |
207 | 209 | "Once cleared, the 'clear_ts' field will be set to current timestamp and special rule chain event 'ALARM_CLEAR' will be generated. " + |
208 | 210 | "Referencing non-existing Alarm Id will cause an error." + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE) | ... | ... |
... | ... | @@ -3,10 +3,12 @@ package org.thingsboard.server.controller.yunteng; |
3 | 3 | import io.swagger.annotations.Api; |
4 | 4 | import io.swagger.annotations.ApiOperation; |
5 | 5 | import lombok.RequiredArgsConstructor; |
6 | +import org.apache.commons.lang3.StringUtils; | |
6 | 7 | import org.springframework.web.bind.annotation.GetMapping; |
7 | 8 | import org.springframework.web.bind.annotation.RequestMapping; |
8 | 9 | import org.springframework.web.bind.annotation.RequestParam; |
9 | 10 | import org.springframework.web.bind.annotation.RestController; |
11 | +import org.thingsboard.server.common.data.EntityType; | |
10 | 12 | import org.thingsboard.server.common.data.alarm.Alarm; |
11 | 13 | import org.thingsboard.server.common.data.alarm.AlarmInfo; |
12 | 14 | import org.thingsboard.server.common.data.alarm.AlarmStatus; |
... | ... | @@ -15,8 +17,13 @@ import org.thingsboard.server.common.data.page.TimePageLink; |
15 | 17 | import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum; |
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.yunteng.entities.YtAlarmEntity; | |
18 | 21 | import org.thingsboard.server.dao.yunteng.service.YtAlarmInfoService; |
19 | 22 | |
23 | +import java.util.HashMap; | |
24 | +import java.util.Map; | |
25 | +import java.util.UUID; | |
26 | + | |
20 | 27 | import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.*; |
21 | 28 | |
22 | 29 | /** |
... | ... | @@ -34,9 +41,9 @@ public class YtAlarmInfoController extends BaseController { |
34 | 41 | |
35 | 42 | |
36 | 43 | //分页测试通过 |
37 | - @ApiOperation(value="设备告警信息") | |
44 | + @ApiOperation(value="查询") | |
38 | 45 | @GetMapping(params = {PAGE_SIZE, PAGE}) |
39 | - public YtPageData<AlarmInfo> pageAlarmInfo( | |
46 | + public YtPageData<YtAlarmEntity> pageAlarmInfo( | |
40 | 47 | @RequestParam(PAGE_SIZE) int pageSize, |
41 | 48 | @RequestParam(PAGE) int page, |
42 | 49 | @RequestParam(value = "status", required = false) AlarmStatus status, |
... | ... | @@ -49,8 +56,16 @@ public class YtAlarmInfoController extends BaseController { |
49 | 56 | @RequestParam(value = ORDER_FILED, required = false) String orderBy, |
50 | 57 | @RequestParam(value = ORDER_TYPE, required = false) OrderTypeEnum orderType) throws ThingsboardException { |
51 | 58 | TimePageLink pageLink = createTimePageLink(pageSize, page > 1 ? page - 1 : 0, textSearch, orderBy, orderType == null ? "" : orderType.name(), startTime, endTime); |
59 | + Map<String, Object> pageFilter = new HashMap<>(); | |
60 | + pageFilter.put(PAGE_SIZE, pageSize); | |
61 | + pageFilter.put(PAGE, page); | |
62 | + pageFilter.put(ORDER_FILED, orderBy); | |
63 | + if (orderType != null) { | |
64 | + pageFilter.put(ORDER_TYPE, orderType.name()); | |
65 | + } | |
66 | + | |
52 | 67 | |
53 | - return alarmInfoService.page(pageLink,getCurrentUser().getTenantId(),alarmType,status,organizationId,deviceId); | |
68 | + return alarmInfoService.alarmPage(pageFilter,getCurrentUser().getTenantId(),alarmType,startTime,endTime,status,deviceId, EntityType.DEVICE,organizationId); | |
54 | 69 | } |
55 | 70 | |
56 | 71 | ... | ... |
common/data/src/main/java/org/thingsboard/server/common/data/yunteng/utils/YtDateTimeUtils.java
0 → 100644
1 | +package org.thingsboard.server.common.data.yunteng.utils; | |
2 | + | |
3 | +import java.time.LocalDateTime; | |
4 | +import java.time.OffsetDateTime; | |
5 | +import java.time.format.DateTimeFormatter; | |
6 | + | |
7 | +/** | |
8 | + * @version V1.0 | |
9 | + * @Description : | |
10 | + * 1.其它地方抛出异常,交由控制层统一处理 | |
11 | + * 2.服务层注意持久化的事务管理 | |
12 | + * @Dependency: 依赖包 | |
13 | + * @Author: junlianglee | |
14 | + * @Date Created in 2021/12/28$ | |
15 | + * @Copyright 2016-2018 - Powered By 李唐源研发中心 | |
16 | + */ | |
17 | +public class YtDateTimeUtils { | |
18 | + public static final String PATTERN_DATE_FORMATE="yyyy-MM-dd HH:mm:ss"; | |
19 | + | |
20 | + public static String formate(Long nacosTime) { | |
21 | + if(nacosTime == null){ | |
22 | + return null; | |
23 | + } | |
24 | + LocalDateTime createTime = LocalDateTime.ofEpochSecond(nacosTime/1000,0, OffsetDateTime.now().getOffset()); | |
25 | + return createTime.format(DateTimeFormatter.ofPattern(PATTERN_DATE_FORMATE)); | |
26 | + } | |
27 | + | |
28 | +} | ... | ... |
1 | +package org.thingsboard.server.dao.yunteng.entities; | |
2 | + | |
3 | +import com.baomidou.mybatisplus.annotation.TableField; | |
4 | +import com.baomidou.mybatisplus.annotation.TableId; | |
5 | +import com.baomidou.mybatisplus.annotation.TableName; | |
6 | +import com.fasterxml.jackson.databind.JsonNode; | |
7 | +import lombok.Data; | |
8 | +import org.thingsboard.server.common.data.alarm.AlarmSeverity; | |
9 | +import org.thingsboard.server.common.data.alarm.AlarmStatus; | |
10 | +import org.thingsboard.server.common.data.yunteng.utils.YtDateTimeUtils; | |
11 | + | |
12 | +import java.util.UUID; | |
13 | + | |
14 | +import static org.thingsboard.server.dao.model.ModelConstants.ALARM_COLUMN_FAMILY_NAME; | |
15 | + | |
16 | +@Data | |
17 | +@TableName(ALARM_COLUMN_FAMILY_NAME) | |
18 | +public class YtAlarmEntity { | |
19 | + | |
20 | + private static final long serialVersionUID = -4922707705163155569L; | |
21 | + | |
22 | + @TableId() | |
23 | + private UUID id; | |
24 | + private UUID tenantId; | |
25 | + | |
26 | + @TableField() | |
27 | + private String creator; | |
28 | + | |
29 | + @TableField() | |
30 | + private String updater; | |
31 | + | |
32 | + @TableField() | |
33 | + private long createdTime; | |
34 | + | |
35 | + @TableField() | |
36 | + private long updatedTime; | |
37 | + | |
38 | + | |
39 | + @TableField() | |
40 | + private UUID customerId; | |
41 | + | |
42 | + @TableField() | |
43 | + private UUID tbDeviceId; | |
44 | + @TableField() | |
45 | + private int originatorType; | |
46 | + @TableField() | |
47 | + private String deviceId; | |
48 | + | |
49 | + private String deviceName; | |
50 | + | |
51 | + @TableField() | |
52 | + private String type; | |
53 | + | |
54 | + @TableField() | |
55 | + private AlarmSeverity severity; | |
56 | + | |
57 | + @TableField() | |
58 | + private AlarmStatus status; | |
59 | + | |
60 | + @TableField() | |
61 | + private Long startTs; | |
62 | + | |
63 | + @TableField() | |
64 | + private Long endTs; | |
65 | + | |
66 | + @TableField() | |
67 | + private Long ackTs; | |
68 | + | |
69 | + @TableField() | |
70 | + private Long clearTs; | |
71 | + | |
72 | + @TableField() | |
73 | + private JsonNode details; | |
74 | + | |
75 | + @TableField() | |
76 | + private Boolean propagate; | |
77 | + | |
78 | + @TableField() | |
79 | + private String propagateRelationTypes; | |
80 | + | |
81 | + | |
82 | + public String getStartTs() { | |
83 | + return YtDateTimeUtils.formate(this.startTs); | |
84 | + } | |
85 | + | |
86 | + public String getEndTs() { | |
87 | + return YtDateTimeUtils.formate(this.endTs); | |
88 | + } | |
89 | + | |
90 | + public String getAckTs() { | |
91 | + return YtDateTimeUtils.formate(this.ackTs); | |
92 | + } | |
93 | + | |
94 | + public String getClearTs() { | |
95 | + return YtDateTimeUtils.formate(this.clearTs); | |
96 | + } | |
97 | + | |
98 | + public String getCreatedTime() { | |
99 | + return YtDateTimeUtils.formate(this.createdTime); | |
100 | + } | |
101 | + | |
102 | + public String getUpdatedTime() { | |
103 | + return YtDateTimeUtils.formate(this.updatedTime); | |
104 | + } | |
105 | +} | ... | ... |
1 | 1 | package org.thingsboard.server.dao.yunteng.impl; |
2 | 2 | |
3 | +import com.baomidou.mybatisplus.core.metadata.IPage; | |
4 | +import com.baomidou.mybatisplus.core.metadata.OrderItem; | |
5 | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |
3 | 6 | import lombok.RequiredArgsConstructor; |
4 | 7 | import lombok.extern.slf4j.Slf4j; |
8 | +import org.apache.commons.lang3.StringUtils; | |
5 | 9 | import org.springframework.stereotype.Service; |
10 | +import org.thingsboard.server.common.data.EntityType; | |
6 | 11 | import org.thingsboard.server.common.data.alarm.AlarmInfo; |
7 | 12 | import org.thingsboard.server.common.data.alarm.AlarmStatus; |
8 | 13 | import org.thingsboard.server.common.data.id.TenantId; |
9 | 14 | import org.thingsboard.server.common.data.page.PageData; |
10 | 15 | import org.thingsboard.server.common.data.page.TimePageLink; |
16 | +import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants; | |
17 | +import org.thingsboard.server.common.data.yunteng.dto.OrganizationDTO; | |
18 | +import org.thingsboard.server.common.data.yunteng.enums.OrderTypeEnum; | |
11 | 19 | import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData; |
12 | -import org.thingsboard.server.dao.service.Validator; | |
13 | -import org.thingsboard.server.dao.yunteng.jpa.dao.YtJpaAarmDao; | |
20 | +import org.thingsboard.server.dao.yunteng.entities.AlarmContact; | |
21 | +import org.thingsboard.server.dao.yunteng.entities.YtAlarmEntity; | |
22 | +import org.thingsboard.server.dao.yunteng.mapper.OrganizationMapper; | |
23 | +import org.thingsboard.server.dao.yunteng.mapper.YtAlarmMapper; | |
24 | +import org.thingsboard.server.dao.yunteng.service.AbstractBaseService; | |
14 | 25 | import org.thingsboard.server.dao.yunteng.service.YtAlarmInfoService; |
15 | 26 | |
27 | +import java.util.*; | |
28 | + | |
29 | +import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.*; | |
30 | +import static org.thingsboard.server.common.data.yunteng.constant.QueryConstant.ORDER_TYPE; | |
31 | + | |
16 | 32 | /** @Description 告警中心业务实现 @Author cxy @Date 2021/11/10 16:56 */ |
17 | 33 | @Service |
18 | 34 | @RequiredArgsConstructor |
19 | 35 | @Slf4j |
20 | -public class YtAlarmInfoServiceImpl implements YtAlarmInfoService { | |
36 | +public class YtAlarmInfoServiceImpl implements YtAlarmInfoService { | |
21 | 37 | |
22 | -private final YtJpaAarmDao ytJpaAarmDao; | |
38 | +private final YtAlarmMapper ytJpaAarmDao; | |
39 | +private final OrganizationMapper organizationMapper; | |
23 | 40 | @Override |
24 | - public YtPageData<AlarmInfo> page(TimePageLink pageLink, TenantId tenantId, String alarmType, AlarmStatus status, String organizationId,String deviceId) { | |
25 | - Validator.validatePageLink(pageLink); | |
26 | - PageData<AlarmInfo> tbDatas = ytJpaAarmDao.pageAlarms(pageLink,tenantId,alarmType,status,null,deviceId); | |
27 | - YtPageData<AlarmInfo> result = new YtPageData<>(tbDatas.getData(), tbDatas.getTotalPages()); | |
41 | + public YtPageData<YtAlarmEntity> alarmPage(Map<String, Object> pageMap, TenantId tenantId, String alarmType, Long startTime, Long endTime, AlarmStatus status, String deviceId, EntityType originatorType, String organizationId) { | |
42 | + IPage<YtAlarmEntity> page= getPage(pageMap, FastIotConstants.DefaultOrder.CREATE_TIME,false); | |
43 | + List<String> ids =new ArrayList<>(); | |
44 | + if(StringUtils.isNotBlank(organizationId)){ | |
45 | + Collection filter = Collections.emptyList(); | |
46 | + filter.add(organizationId); | |
47 | + List<OrganizationDTO> all =organizationMapper.findOrganizationTreeList(tenantId.getId().toString(),filter); | |
48 | + for(OrganizationDTO item:all){ | |
49 | + ids.add(item.getId()); | |
50 | + } | |
51 | + | |
52 | + } | |
53 | + | |
54 | + IPage<YtAlarmEntity> tbDatas = ytJpaAarmDao.alarmPage(page,tenantId.getId(),alarmType,startTime,endTime,status,deviceId, originatorType,ids.size()>0?ids:null); | |
55 | + YtPageData<YtAlarmEntity> result = new YtPageData<>(tbDatas.getRecords(), tbDatas.getTotal()); | |
28 | 56 | return result; |
29 | 57 | } |
30 | 58 | |
31 | 59 | |
60 | + protected IPage<YtAlarmEntity> getPage(Map<String, Object> params, String defaultOrderField, boolean isAsc) { | |
61 | + int curPage = 1; | |
62 | + int limit = 10; | |
63 | + if (params.get(PAGE) != null) { | |
64 | + curPage = Integer.parseInt(params.get(PAGE).toString()); | |
65 | + } | |
66 | + | |
67 | + if (params.get(PAGE_SIZE) != null) { | |
68 | + limit = Integer.parseInt(params.get(PAGE_SIZE).toString()); | |
69 | + } | |
70 | + | |
71 | + Page<YtAlarmEntity> page = new Page<>(curPage, limit); | |
72 | + String orderField = (String) params.get(ORDER_FILED); | |
73 | + String order = (String) params.get(ORDER_TYPE); | |
74 | + if (StringUtils.isNotBlank(orderField) && StringUtils.isNotBlank(order)) { | |
75 | + return OrderTypeEnum.ASC.name().equalsIgnoreCase(order) | |
76 | + ? page.addOrder(OrderItem.asc(orderField)) | |
77 | + : page.addOrder(OrderItem.desc(orderField)); | |
78 | + } else if (StringUtils.isBlank(defaultOrderField)) { | |
79 | + return page; | |
80 | + } else { | |
81 | + if (isAsc) { | |
82 | + page.addOrder(OrderItem.asc(defaultOrderField)); | |
83 | + } else { | |
84 | + page.addOrder(OrderItem.desc(defaultOrderField)); | |
85 | + } | |
86 | + return page; | |
87 | + } | |
88 | + } | |
89 | + | |
32 | 90 | } | ... | ... |
1 | -/** | |
2 | - * Copyright © 2016-2021 The Thingsboard Authors | |
3 | - * <p> | |
4 | - * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | - * you may not use this file except in compliance with the License. | |
6 | - * You may obtain a copy of the License at | |
7 | - * <p> | |
8 | - * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | - * <p> | |
10 | - * Unless required by applicable law or agreed to in writing, software | |
11 | - * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | - * See the License for the specific language governing permissions and | |
14 | - * limitations under the License. | |
15 | - */ | |
16 | -package org.thingsboard.server.dao.yunteng.jpa.dao; | |
17 | - | |
18 | -import lombok.AllArgsConstructor; | |
19 | -import org.springframework.context.annotation.Primary; | |
20 | -import org.springframework.data.domain.Page; | |
21 | -import org.springframework.stereotype.Component; | |
22 | -import org.thingsboard.server.common.data.EntityType; | |
23 | -import org.thingsboard.server.common.data.StringUtils; | |
24 | -import org.thingsboard.server.common.data.alarm.AlarmInfo; | |
25 | -import org.thingsboard.server.common.data.alarm.AlarmStatus; | |
26 | -import org.thingsboard.server.common.data.id.TenantId; | |
27 | -import org.thingsboard.server.common.data.page.PageData; | |
28 | -import org.thingsboard.server.common.data.page.TimePageLink; | |
29 | -import org.thingsboard.server.dao.DaoUtil; | |
30 | -import org.thingsboard.server.dao.model.sql.AlarmInfoEntity; | |
31 | -import org.thingsboard.server.dao.sql.alarm.JpaAlarmDao; | |
32 | -import org.thingsboard.server.dao.yunteng.jpa.repository.YtAlarmRepository; | |
33 | - | |
34 | -import java.util.*; | |
35 | - | |
36 | -@Component | |
37 | -@Primary | |
38 | -@AllArgsConstructor | |
39 | -public class YtJpaAarmDao extends JpaAlarmDao { | |
40 | - | |
41 | - private final YtAlarmRepository ytAlarmRepository; | |
42 | - | |
43 | - public PageData<AlarmInfo> pageAlarms(TimePageLink pageLink, TenantId tenantId, String alarmType, AlarmStatus statu, List<String> organizationId, String entityId){ | |
44 | - | |
45 | - Set<AlarmStatus> status = new HashSet<>(); | |
46 | - if(statu != null){ | |
47 | - status.add(statu); | |
48 | - } | |
49 | - Page<AlarmInfoEntity> datas =null; | |
50 | - try{ | |
51 | - datas = ytAlarmRepository.alarmsByOrganization(tenantId.getId() | |
52 | - , Optional.ofNullable(entityId).map(id ->{ | |
53 | - return UUID.fromString(entityId); | |
54 | - }).orElse(null) | |
55 | - , EntityType.DEVICE.name() | |
56 | - ,Optional.ofNullable(organizationId).map(ids ->{ | |
57 | - Set<String> result = new HashSet<>(); | |
58 | - for(String item:ids){ | |
59 | - if(StringUtils.isNotEmpty(item)){ | |
60 | - result.add(item); | |
61 | - } | |
62 | - } | |
63 | - return result; | |
64 | - }).orElse(null) | |
65 | -// , org.apache.commons.lang3.StringUtils.isEmpty(alarmType)?null:alarmType | |
66 | - ,pageLink.getStartTime(),pageLink.getEndTime(), | |
67 | - status | |
68 | - ,DaoUtil.toPageable(pageLink)); | |
69 | - | |
70 | - }catch (Exception e){ | |
71 | - e.printStackTrace(); | |
72 | - } | |
73 | - return DaoUtil.toPageData(datas); | |
74 | - } | |
75 | - | |
76 | - | |
77 | - | |
78 | - | |
79 | -} |
dao/src/main/java/org/thingsboard/server/dao/yunteng/jpa/repository/YtAlarmRepository.java
deleted
100644 → 0
1 | -/** | |
2 | - * Copyright © 2016-2021 The Thingsboard Authors | |
3 | - * | |
4 | - * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | - * you may not use this file except in compliance with the License. | |
6 | - * You may obtain a copy of the License at | |
7 | - * | |
8 | - * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | - * | |
10 | - * Unless required by applicable law or agreed to in writing, software | |
11 | - * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | - * See the License for the specific language governing permissions and | |
14 | - * limitations under the License. | |
15 | - */ | |
16 | -package org.thingsboard.server.dao.yunteng.jpa.repository; | |
17 | - | |
18 | -import org.springframework.data.domain.Page; | |
19 | -import org.springframework.data.domain.Pageable; | |
20 | -import org.springframework.data.jpa.repository.Query; | |
21 | -import org.springframework.data.repository.CrudRepository; | |
22 | -import org.springframework.data.repository.query.Param; | |
23 | -import org.thingsboard.server.common.data.alarm.AlarmSeverity; | |
24 | -import org.thingsboard.server.common.data.alarm.AlarmStatus; | |
25 | -import org.thingsboard.server.dao.model.sql.AlarmEntity; | |
26 | -import org.thingsboard.server.dao.model.sql.AlarmInfoEntity; | |
27 | - | |
28 | -import java.util.List; | |
29 | -import java.util.Set; | |
30 | -import java.util.UUID; | |
31 | - | |
32 | -/** | |
33 | - * Created by Valerii Sosliuk on 5/21/2017. | |
34 | - */ | |
35 | -public interface YtAlarmRepository extends CrudRepository<AlarmEntity, UUID> { | |
36 | - | |
37 | - | |
38 | - | |
39 | - @Query(value = "SELECT new org.thingsboard.server.dao.model.sql.AlarmInfoEntity(a) FROM AlarmEntity a " + | |
40 | - "LEFT JOIN YtJpaDeviceEntity yd ON a.originatorId = uuid(yd.tbDeviceId) " + | |
41 | - "LEFT JOIN RelationEntity re ON a.id = re.toId " + | |
42 | - "AND re.relationTypeGroup = 'ALARM' " + | |
43 | - "AND re.toType = 'ALARM' " + | |
44 | - "AND re.fromId = :affectedEntityId " + | |
45 | - "AND re.fromType = :affectedEntityType " + | |
46 | - "WHERE a.tenantId = :tenantId " + | |
47 | - "AND (a.originatorId = :affectedEntityId or re.fromId IS NOT NULL) " + | |
48 | - "AND (:startTime IS NULL OR a.createdTime >= :startTime) " + | |
49 | - "AND (:endTime IS NULL OR a.createdTime <= :endTime) " | |
50 | - + "AND ((:alarmStatuses) IS NULL OR a.status in (:alarmStatuses)) " | |
51 | -// + " AND (:affectedEntityType IS NULL OR a.originatorType = :affectedEntityType) " | |
52 | - + " AND (:organizationId IS NULL OR yd.organizationId in (:organizationId)) " | |
53 | -// + " AND (:alarmType IS NULL OR a.type = :alarmType) " | |
54 | -// "AND (LOWER(a.type) LIKE LOWER(CONCAT('%', :alarmType, '%')) " | |
55 | -// + " OR LOWER(a.severity) LIKE LOWER(CONCAT('%', :searchText, '%'))) " | |
56 | - , | |
57 | - countQuery = "" + | |
58 | - "SELECT count(a) + " + //alarms with relations only | |
59 | - " (SELECT count(a) FROM AlarmEntity a " + //alarms WITHOUT any relations | |
60 | - "LEFT JOIN YtJpaDeviceEntity yd ON a.originatorId = uuid(yd.tbDeviceId) " + | |
61 | - " LEFT JOIN RelationEntity re ON a.id = re.toId " + | |
62 | - " AND re.relationTypeGroup = 'ALARM' " + | |
63 | - " AND re.toType = 'ALARM' " + | |
64 | - " AND re.fromId = :affectedEntityId " + | |
65 | - " AND re.fromType = :affectedEntityType " + | |
66 | - " WHERE a.tenantId = :tenantId " + | |
67 | - " AND (a.originatorId = :affectedEntityId) " + | |
68 | - " AND (re.fromId IS NULL) " + //anti join | |
69 | - " AND (:startTime IS NULL OR a.createdTime >= :startTime) " + | |
70 | - " AND (:endTime IS NULL OR a.createdTime <= :endTime) " | |
71 | - +" AND ((:alarmStatuses) IS NULL OR a.status in (:alarmStatuses)) " | |
72 | -// + " AND (:affectedEntityType IS NULL OR a.originatorType = :affectedEntityType) " | |
73 | - + " AND (:organizationId IS NULL OR yd.organizationId in (:organizationId)) " | |
74 | -// + " AND (:alarmType IS NULL OR a.type = :alarmType) " | |
75 | - +" )" + | |
76 | - "FROM AlarmEntity a " + | |
77 | - "LEFT JOIN YtJpaDeviceEntity yd ON a.originatorId = uuid(yd.tbDeviceId) " + | |
78 | - "INNER JOIN RelationEntity re ON a.id = re.toId " + | |
79 | - "AND re.relationTypeGroup = 'ALARM' " + | |
80 | - "AND re.toType = 'ALARM' " + | |
81 | - "AND re.fromId = :affectedEntityId " + | |
82 | - "AND re.fromType = :affectedEntityType " + | |
83 | - "WHERE a.tenantId = :tenantId " + | |
84 | - "AND (:startTime IS NULL OR a.createdTime >= :startTime) " + | |
85 | - "AND (:endTime IS NULL OR a.createdTime <= :endTime) " | |
86 | - +"AND ((:alarmStatuses) IS NULL OR a.status in (:alarmStatuses)) " | |
87 | -// + " AND (:affectedEntityType IS NULL OR a.originatorType = :affectedEntityType) " | |
88 | - + " AND (:organizationId IS NULL OR yd.organizationId in (:organizationId)) " | |
89 | -// + " AND (:alarmType IS NULL OR a.type = :alarmType) " | |
90 | - ) | |
91 | - Page<AlarmInfoEntity> alarmsByOrganization(@Param("tenantId") UUID tenantId, | |
92 | - @Param("affectedEntityId") UUID affectedEntityId, | |
93 | - @Param("affectedEntityType") String affectedEntityType, | |
94 | - @Param("organizationId") Set<String> organizationId, | |
95 | -// @Param("alarmType") String alarmType, | |
96 | - @Param("startTime") Long startTime, | |
97 | - @Param("endTime") Long endTime, | |
98 | - @Param("alarmStatuses") Set<AlarmStatus> alarmStatuses, | |
99 | - Pageable pageable); | |
100 | - | |
101 | - | |
102 | - | |
103 | - | |
104 | - | |
105 | - | |
106 | - | |
107 | - | |
108 | - | |
109 | -} |
1 | +package org.thingsboard.server.dao.yunteng.mapper; | |
2 | + | |
3 | +/** | |
4 | + * @version V1.0 | |
5 | + * @Description : | |
6 | + * 1.其它地方抛出异常,交由控制层统一处理 | |
7 | + * 2.服务层注意持久化的事务管理 | |
8 | + * @Dependency: 依赖包 | |
9 | + * @Author: junlianglee | |
10 | + * @Date Created in 2021/12/28$ | |
11 | + * @Copyright 2016-2018 - Powered By 云腾五洲 | |
12 | + */ | |
13 | + | |
14 | +import java.sql.CallableStatement; | |
15 | +import java.sql.PreparedStatement; | |
16 | +import java.sql.ResultSet; | |
17 | +import java.sql.SQLException; | |
18 | +import java.util.UUID; | |
19 | + | |
20 | +import org.apache.ibatis.type.BaseTypeHandler; | |
21 | +import org.apache.ibatis.type.JdbcType; | |
22 | + | |
23 | +public class UUIDTypeHandler extends BaseTypeHandler { | |
24 | + | |
25 | + @Override | |
26 | + public Object getNullableResult(ResultSet rs, String columnName) | |
27 | + throws SQLException { | |
28 | + return UUID.fromString(rs.getString(columnName)); | |
29 | + } | |
30 | + | |
31 | + @Override | |
32 | + public Object getNullableResult(ResultSet resultSet, int i) throws SQLException { | |
33 | + return UUID.fromString((resultSet.getString(i))); | |
34 | + } | |
35 | + | |
36 | + @Override | |
37 | + public Object getNullableResult(CallableStatement cs, int columnIndex) | |
38 | + throws SQLException { | |
39 | + return UUID.fromString((cs.getString(columnIndex))); | |
40 | + } | |
41 | + | |
42 | + @Override | |
43 | + public void setNonNullParameter(PreparedStatement ps, int i, | |
44 | + Object parameter, JdbcType jdbcType) throws SQLException { | |
45 | + ps.setString(i, ((UUID) parameter).toString()); | |
46 | + } | |
47 | +} | ... | ... |
1 | +package org.thingsboard.server.dao.yunteng.mapper; | |
2 | + | |
3 | +import com.baomidou.mybatisplus.core.mapper.BaseMapper; | |
4 | +import com.baomidou.mybatisplus.core.metadata.IPage; | |
5 | +import org.apache.ibatis.annotations.Mapper; | |
6 | +import org.apache.ibatis.annotations.Param; | |
7 | +import org.thingsboard.server.common.data.EntityType; | |
8 | +import org.thingsboard.server.common.data.alarm.AlarmInfo; | |
9 | +import org.thingsboard.server.common.data.alarm.AlarmStatus; | |
10 | +import org.thingsboard.server.common.data.id.TenantId; | |
11 | +import org.thingsboard.server.common.data.yunteng.dto.MenuDTO; | |
12 | +import org.thingsboard.server.common.data.yunteng.dto.TenantDTO; | |
13 | +import org.thingsboard.server.dao.yunteng.entities.Menu; | |
14 | +import org.thingsboard.server.dao.yunteng.entities.YtAlarmEntity; | |
15 | + | |
16 | +import java.util.Collection; | |
17 | +import java.util.List; | |
18 | +import java.util.Set; | |
19 | +import java.util.UUID; | |
20 | + | |
21 | +@Mapper | |
22 | +public interface YtAlarmMapper extends BaseMapper<YtAlarmEntity> { | |
23 | + | |
24 | + IPage<YtAlarmEntity> alarmPage( | |
25 | + IPage<?> page | |
26 | + ,@Param("tenantId") UUID tenantId | |
27 | + ,@Param("alarmType")String alarmType | |
28 | + ,@Param("startTime")Long startTime | |
29 | + ,@Param("endTime")Long endTime | |
30 | + ,@Param("status")AlarmStatus status | |
31 | + ,@Param("deviceId")String deviceId | |
32 | + ,@Param("originatorType") EntityType originatorType | |
33 | + ,@Param("organizationId")List<String> organizationId | |
34 | + ); | |
35 | +} | ... | ... |
1 | 1 | package org.thingsboard.server.dao.yunteng.service; |
2 | 2 | |
3 | +import org.thingsboard.server.common.data.EntityType; | |
3 | 4 | import org.thingsboard.server.common.data.alarm.Alarm; |
4 | 5 | import org.thingsboard.server.common.data.alarm.AlarmInfo; |
5 | 6 | import org.thingsboard.server.common.data.alarm.AlarmStatus; |
... | ... | @@ -7,22 +8,29 @@ import org.thingsboard.server.common.data.id.TenantId; |
7 | 8 | import org.thingsboard.server.common.data.page.TimePageLink; |
8 | 9 | import org.thingsboard.server.common.data.yunteng.dto.AlarmInfoDTO; |
9 | 10 | import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData; |
11 | +import org.thingsboard.server.dao.yunteng.entities.YtAlarmEntity; | |
10 | 12 | |
11 | 13 | import java.util.Map; |
14 | +import java.util.UUID; | |
12 | 15 | |
13 | -/** @Description 告警中心数据业务层 @Author cxy @Date 2021/11/10 16:51 */ | |
16 | +/** | |
17 | + * @Description 告警中心数据业务层 @Author cxy @Date 2021/11/10 16:51 | |
18 | + */ | |
14 | 19 | public interface YtAlarmInfoService { |
15 | 20 | |
16 | 21 | |
17 | - /** | |
18 | - * 告警信息分页数据 | |
19 | - * @param pageLink 分页信息 | |
20 | - * @param tenantId 租户ID | |
21 | - * @param alarmType 告警类型 | |
22 | - * @param status 告警状态 | |
23 | - * @param organizationId 组织ID | |
24 | - * @return | |
25 | - */ | |
26 | - YtPageData<AlarmInfo> page(TimePageLink pageLink,TenantId tenantId, String alarmType, AlarmStatus status, String organizationId,String deviceId); | |
22 | + /** | |
23 | + * @param pageMap 分页排序信息 | |
24 | + * @param tenantId 租户ID | |
25 | + * @param alarmType 告警类型 | |
26 | + * @param startTime 过滤区间开始时间 | |
27 | + * @param endTime 过滤区间结束时间 | |
28 | + * @param status 告警状态 | |
29 | + * @param deviceId 告警设备ID,设备告警使用 | |
30 | + * @param originatorType 告警源类型 | |
31 | + * @param organizationId 告警所属组织 | |
32 | + * @return | |
33 | + */ | |
34 | + YtPageData<YtAlarmEntity> alarmPage(Map<String, Object> pageMap, TenantId tenantId, String alarmType, Long startTime, Long endTime, AlarmStatus status, String deviceId, EntityType originatorType, String organizationId); | |
27 | 35 | |
28 | 36 | } | ... | ... |
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |
3 | + | |
4 | +<mapper namespace="org.thingsboard.server.dao.yunteng.mapper.YtAlarmMapper"> | |
5 | + | |
6 | + <resultMap type="org.thingsboard.server.dao.yunteng.entities.YtAlarmEntity" id="alarmInfo"> | |
7 | + <result property="id" column="id" typeHandler="org.thingsboard.server.dao.yunteng.mapper.UUIDTypeHandler"/> | |
8 | + <result property="tenantId" column="tenant_id" typeHandler="org.thingsboard.server.dao.yunteng.mapper.UUIDTypeHandler"/> | |
9 | + <result property="deviceName" column="device_name"/> | |
10 | + <result property="customerId" column="customer_id" typeHandler="org.thingsboard.server.dao.yunteng.mapper.UUIDTypeHandler"/> | |
11 | + <result property="type" column="type"/> | |
12 | + <result property="deviceId" column="device_id" /> | |
13 | + <result property="originatorType" column="originator_type"/> | |
14 | + <result property="tbDeviceId" column="originator_Id" typeHandler="org.thingsboard.server.dao.yunteng.mapper.UUIDTypeHandler"/> | |
15 | + <result property="severity" column="severity" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/> | |
16 | + <result property="status" column="status" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/> | |
17 | + <result property="startTs" column="start_ts"/> | |
18 | + <result property="endTs" column="end_ts"/> | |
19 | + <result property="ackTs" column="ack_ts"/> | |
20 | + <result property="clearTs" column="clear_ts"/> | |
21 | + <result property="details" column="details" typeHandler="com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler"/> | |
22 | + <result property="propagate" column="propagate"/> | |
23 | + </resultMap> | |
24 | + | |
25 | + | |
26 | + <select id="alarmPage" resultMap="alarmInfo"> | |
27 | + SELECT d.name AS device_name,d.id device_id,m.* | |
28 | + FROM alarm m LEFT JOIN iotfs_device d ON m.originator_id = d.tb_device_id::uuid | |
29 | + <where> | |
30 | + m.tenant_id = #{tenantId} | |
31 | + <if test="alarmType!=null"> | |
32 | + AND type LIKE CONCAT('%',#{alarmType},'%') | |
33 | + </if> | |
34 | + <if test="startTime!=null"> | |
35 | + AND created_time >= #{tenantId} | |
36 | + </if> | |
37 | + <if test="endTime!=null"> | |
38 | + AND created_time <= #{endTime} | |
39 | + </if> | |
40 | + <if test="status!=null"> | |
41 | + AND status = #{status.name} | |
42 | + </if> | |
43 | + <if test="deviceId!=null"> | |
44 | + AND d.id = #{deviceId} | |
45 | + </if> | |
46 | + <if test="originatorType!=null"> | |
47 | + AND originator_type = #{originatorType.ordinal} | |
48 | + </if> | |
49 | + <if test="organizationId!=null"> | |
50 | + AND d.organization_id IN | |
51 | + <foreach collection="organizationId" item="id" index="index" open="(" close=")" separator=","> | |
52 | + #{id} | |
53 | + </foreach> | |
54 | + </if> | |
55 | + | |
56 | + </where> | |
57 | + </select> | |
58 | +</mapper> | ... | ... |