[manager] 消息通知配置API-消息接收人管理,消息通知策略管理

This commit is contained in:
tomsun28
2021-12-16 18:00:20 +08:00
parent e644de7736
commit 1dae784118
10 changed files with 504 additions and 0 deletions

View File

@@ -86,6 +86,8 @@ public class DispatchAlarm {
private void sendAlertDataListener(Alert alert) {
// todo 转发配置的邮件 微信 webhook
}

View File

@@ -0,0 +1,127 @@
package com.usthe.manager.controller;
import com.usthe.common.entity.dto.Message;
import com.usthe.manager.pojo.entity.NoticeReceiver;
import com.usthe.manager.pojo.entity.NoticeRule;
import com.usthe.manager.service.NoticeConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.persistence.criteria.Predicate;
import javax.validation.Valid;
import java.util.List;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
/**
* 消息通知配置API
* @author tom
* @date 2021/12/16 16:18
*/
@Api(tags = "消息通知配置API")
@RestController()
@RequestMapping(value = "/notice", produces = {APPLICATION_JSON_VALUE})
public class NoticeConfigController {
@Autowired
private NoticeConfigService noticeConfigService;
@PostMapping(path = "/receiver")
@ApiOperation(value = "新增接收人", notes = "新增一个接收人")
public ResponseEntity<Message<Void>> addNewNoticeReceiver(@Valid @RequestBody NoticeReceiver noticeReceiver) {
noticeConfigService.addReceiver(noticeReceiver);
return ResponseEntity.ok(new Message<>("Add success"));
}
@PutMapping(path = "/receiver")
@ApiOperation(value = "修改接收人", notes = "修改已存在的接收人信息")
public ResponseEntity<Message<Void>> editNoticeReceiver(@Valid @RequestBody NoticeReceiver noticeReceiver) {
noticeConfigService.editReceiver(noticeReceiver);
return ResponseEntity.ok(new Message<>("Edit success"));
}
@DeleteMapping(path = "/receiver/{id}")
@ApiOperation(value = "删除指定接收人", notes = "删除已存在的接收人信息")
public ResponseEntity<Message<Void>> deleteNoticeReceiver(
@ApiParam(value = "接收人ID", example = "6565463543") @PathVariable("id") Long receiverId) {
// 不存在或删除成功都返回成功
noticeConfigService.deleteReceiver(receiverId);
return ResponseEntity.ok(new Message<>("Delete success"));
}
@GetMapping(path = "/receivers")
@ApiOperation(value = "查询消息通知接收人", notes = "根据查询过滤项获取消息通知接收人列表")
public ResponseEntity<Message<List<NoticeReceiver>>> getReceivers(
@ApiParam(value = "接收人名称,模糊查询", example = "tom") @RequestParam(required = false) String name) {
Specification<NoticeReceiver> specification = (root, query, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.conjunction();
if (name != null && !"".equals(name)) {
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
predicate = criteriaBuilder.and(predicateName);
}
return predicate;
};
List<NoticeReceiver> receivers = noticeConfigService.getNoticeReceivers(specification);
Message<List<NoticeReceiver>> message = new Message<>(receivers);
return ResponseEntity.ok(message);
}
@PostMapping(path = "/rule")
@ApiOperation(value = "新增通知策略", notes = "新增一个通知策略")
public ResponseEntity<Message<Void>> addNewNoticeRule(@Valid @RequestBody NoticeRule noticeRule) {
noticeConfigService.addNoticeRule(noticeRule);
return ResponseEntity.ok(new Message<>("Add success"));
}
@PutMapping(path = "/rule")
@ApiOperation(value = "修改通知策略", notes = "修改已存在的通知策略信息")
public ResponseEntity<Message<Void>> editNoticeRule(@Valid @RequestBody NoticeRule noticeRule) {
noticeConfigService.editNoticeRule(noticeRule);
return ResponseEntity.ok(new Message<>("Edit success"));
}
@DeleteMapping(path = "/rule/{id}")
@ApiOperation(value = "删除指定通知策略", notes = "删除已存在的通知策略信息")
public ResponseEntity<Message<Void>> deleteNoticeRule(
@ApiParam(value = "通知策略ID", example = "6565463543") @PathVariable("id") Long ruleId) {
// 不存在或删除成功都返回成功
noticeConfigService.deleteNoticeRule(ruleId);
return ResponseEntity.ok(new Message<>("Delete success"));
}
@GetMapping(path = "/rules")
@ApiOperation(value = "查询消息通知策略", notes = "根据查询过滤项获取消息通知策略列表")
public ResponseEntity<Message<List<NoticeRule>>> getRules(
@ApiParam(value = "接收人名称,模糊查询", example = "rule1") @RequestParam(required = false) String name) {
Specification<NoticeRule> specification = (root, query, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.conjunction();
if (name != null && !"".equals(name)) {
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
predicate = criteriaBuilder.and(predicateName);
}
return predicate;
};
List<NoticeRule> receiverPage = noticeConfigService.getNoticeRules(specification);
Message<List<NoticeRule>> message = new Message<>(receiverPage);
return ResponseEntity.ok(message);
}
}

View File

@@ -0,0 +1,14 @@
package com.usthe.manager.dao;
import com.usthe.manager.pojo.entity.NoticeReceiver;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
* NoticeReceiver数据库操作
* @author tom
* @date 2021/12/16 16:12
*/
public interface NoticeReceiverDao extends JpaRepository<NoticeReceiver, Long>, JpaSpecificationExecutor<NoticeReceiver> {
}

View File

@@ -0,0 +1,14 @@
package com.usthe.manager.dao;
import com.usthe.manager.pojo.entity.NoticeRule;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
* NoticeRule数据库操作
* @author tom
* @date 2021/12/16 16:13
*/
public interface NoticeRuleDao extends JpaRepository<NoticeRule, Long>, JpaSpecificationExecutor<NoticeRule> {
}

View File

@@ -0,0 +1,85 @@
package com.usthe.manager.pojo.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_ONLY;
import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_WRITE;
/**
* 消息通知接收人实体
* @author tomsun28
* @date 2021/11/13 22:19
*/
@Entity
@Table(name = "notice_receiver")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "消息通知接收人实体")
public class NoticeReceiver {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ApiModelProperty(value = "接收人实体主键索引ID", example = "87584674384", accessMode = READ_ONLY, position = 0)
private Long id;
@ApiModelProperty(value = "接收人名称", example = "tom", accessMode = READ_WRITE, position = 1)
@Length(max = 100)
@NotNull
private String name;
@ApiModelProperty(value = "通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号", accessMode = READ_WRITE, position = 2)
@Min(0)
@Max(3)
@NotNull
private Byte type;
@ApiModelProperty(value = "手机号, 通知方式为手机短信时有效", example = "18923435643", accessMode = READ_WRITE, position = 3)
@Length(max = 100)
private String phone;
@ApiModelProperty(value = "邮箱账号, 通知方式为邮箱时有效", example = "tom@qq.com", accessMode = READ_WRITE, position = 4)
@Length(max = 100)
private String email;
@ApiModelProperty(value = "URL地址, 通知方式为webhook有效", example = "https://www.tancloud.cn", accessMode = READ_WRITE, position = 5)
@Length(max = 100)
private String hookUrl;
@ApiModelProperty(value = "wechat用户openId, 通知方式为微信公众号有效", example = "343432", accessMode = READ_WRITE, position = 6)
@Length(max = 100)
private String wechatId;
@ApiModelProperty(value = "此条记录创建者", example = "tom", accessMode = READ_ONLY, position = 7)
private String creator;
@ApiModelProperty(value = "此条记录最新修改者", example = "tom", accessMode = READ_ONLY, position = 8)
private String modifier;
@ApiModelProperty(value = "记录创建时间(毫秒时间戳)", example = "1612198922000", accessMode = READ_ONLY, position = 9)
@Column(insertable = false, updatable = false)
private LocalDateTime gmtCreate;
@ApiModelProperty(value = "记录最新修改时间(毫秒时间戳)", example = "1612198444000", accessMode = READ_ONLY, position = 10)
@Column(insertable = false, updatable = false)
private LocalDateTime gmtUpdate;
}

View File

@@ -0,0 +1,77 @@
package com.usthe.manager.pojo.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_ONLY;
import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_WRITE;
/**
* 通知策略
* @author tomsun28
* @date 2021/11/13 22:19
*/
@Entity
@Table(name = "notice_rule")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "通知策略实体")
public class NoticeRule {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ApiModelProperty(value = "通知策略实体主键索引ID", example = "87584674384", accessMode = READ_ONLY, position = 0)
private Long id;
@ApiModelProperty(value = "策略名称", example = "dispatch-1", accessMode = READ_WRITE, position = 1)
@Length(max = 100)
@NotNull
private Long name;
@ApiModelProperty(value = "接收人ID", example = "4324324", accessMode = READ_WRITE, position = 2)
@Length(max = 100)
@NotNull
private Long receiverId;
@ApiModelProperty(value = "接收人标识", example = "tom", accessMode = READ_WRITE, position = 3)
@Length(max = 100)
@NotNull
private Long receiverName;
@ApiModelProperty(value = "是否启用此策略", example = "true", accessMode = READ_WRITE, position = 4)
private boolean enable = true;
@ApiModelProperty(value = "是否转发所有", example = "false", accessMode = READ_WRITE, position = 5)
private boolean filterAll = true;
@ApiModelProperty(value = "此条记录创建者", example = "tom", accessMode = READ_ONLY, position = 7)
private String creator;
@ApiModelProperty(value = "此条记录最新修改者", example = "tom", accessMode = READ_ONLY, position = 8)
private String modifier;
@ApiModelProperty(value = "记录创建时间(毫秒时间戳)", example = "1612198922000", accessMode = READ_ONLY, position = 9)
@Column(insertable = false, updatable = false)
private LocalDateTime gmtCreate;
@ApiModelProperty(value = "记录最新修改时间(毫秒时间戳)", example = "1612198444000", accessMode = READ_ONLY, position = 10)
@Column(insertable = false, updatable = false)
private LocalDateTime gmtUpdate;
}

View File

@@ -0,0 +1,65 @@
package com.usthe.manager.service;
import com.usthe.manager.pojo.entity.NoticeReceiver;
import com.usthe.manager.pojo.entity.NoticeRule;
import org.springframework.data.jpa.domain.Specification;
import java.util.List;
/**
* 消息通知配置接口
* @author tom
* @date 2021/12/16 16:14
*/
public interface NoticeConfigService {
/**
* 动态条件查询
* @param specification 查询条件
* @return 查询结果
*/
List<NoticeReceiver> getNoticeReceivers(Specification<NoticeReceiver> specification);
/**
* 动态条件查询
* @param specification 查询条件
* @return 查询结果
*/
List<NoticeRule> getNoticeRules(Specification<NoticeRule> specification);
/**
* 新增一个通知接收人
* @param noticeReceiver 接收人信息
*/
void addReceiver(NoticeReceiver noticeReceiver);
/**
* 修改通知接收人
* @param noticeReceiver 接收人信息
*/
void editReceiver(NoticeReceiver noticeReceiver);
/**
* 根据接收人ID删除接收人信息
* @param receiverId 接收人ID
*/
void deleteReceiver(Long receiverId);
/**
* 新增通知策略
* @param noticeRule 通知策略
*/
void addNoticeRule(NoticeRule noticeRule);
/**
* 修改通知策略
* @param noticeRule 通知策略
*/
void editNoticeRule(NoticeRule noticeRule);
/**
* 删除指定的通知策略
* @param ruleId 通知策略ID
*/
void deleteNoticeRule(Long ruleId);
}

View File

@@ -0,0 +1,10 @@
package com.usthe.manager.service;
/**
* 消息通知转发接口
* @author tom
* @date 2021/12/16 16:14
*/
public interface NoticeDispatchService {
}

View File

@@ -0,0 +1,71 @@
package com.usthe.manager.service.impl;
import com.usthe.manager.dao.NoticeReceiverDao;
import com.usthe.manager.dao.NoticeRuleDao;
import com.usthe.manager.pojo.entity.NoticeReceiver;
import com.usthe.manager.pojo.entity.NoticeRule;
import com.usthe.manager.service.NoticeConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* 消息通知配置实现
* @author tom
* @date 2021/12/16 16:16
*/
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class NoticeConfigServiceImpl implements NoticeConfigService {
@Autowired
private NoticeReceiverDao noticeReceiverDao;
@Autowired
private NoticeRuleDao noticeRuleDao;
@Override
public List<NoticeReceiver> getNoticeReceivers(Specification<NoticeReceiver> specification) {
return noticeReceiverDao.findAll(specification);
}
@Override
public List<NoticeRule> getNoticeRules(Specification<NoticeRule> specification) {
return noticeRuleDao.findAll(specification);
}
@Override
public void addReceiver(NoticeReceiver noticeReceiver) {
noticeReceiverDao.save(noticeReceiver);
}
@Override
public void editReceiver(NoticeReceiver noticeReceiver) {
noticeReceiverDao.save(noticeReceiver);
}
@Override
public void deleteReceiver(Long receiverId) {
noticeReceiverDao.deleteById(receiverId);
}
@Override
public void addNoticeRule(NoticeRule noticeRule) {
noticeRuleDao.save(noticeRule);
}
@Override
public void editNoticeRule(NoticeRule noticeRule) {
noticeRuleDao.save(noticeRule);
}
@Override
public void deleteNoticeRule(Long ruleId) {
noticeRuleDao.deleteById(ruleId);
}
}

View File

@@ -122,4 +122,43 @@ CREATE TABLE alert
primary key (id)
) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Table structure for notice_rule
-- ----------------------------
DROP TABLE IF EXISTS notice_rule ;
CREATE TABLE notice_rule
(
id bigint not null auto_increment comment '通知策略主键索引ID',
name varchar(100) not null comment '策略名称',
receiverId bigint not null comment '消息接收人ID',
receiverName varchar(100) not null comment '消息接收人标识',
enable boolean not null default true comment '是否启用此策略',
filter_all boolean not null default true comment '是否转发所有',
creator varchar(100) comment '创建者',
modifier varchar(100) comment '最新修改者',
gmt_create timestamp default current_timestamp comment 'create time',
gmt_update datetime default current_timestamp on update current_timestamp comment 'update time',
primary key (id)
) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Table structure for notice_receiver
-- ----------------------------
DROP TABLE IF EXISTS notice_receiver ;
CREATE TABLE notice_receiver
(
id bigint not null auto_increment comment '消息接收人ID',
name varchar(100) not null comment '消息接收人姓名',
type tinyint not null comment '通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号',
phone varchar(100) comment '手机号, 通知方式为手机短信时有效',
email varchar(100) comment '邮箱账号, 通知方式为邮箱时有效',
hook_url varchar(100) comment 'URL地址, 通知方式为webhook有效',
wechat_id varchar(100) comment 'wechat用户openId, 通知方式为微信公众号有效',
creator varchar(100) comment '创建者',
modifier varchar(100) comment '最新修改者',
gmt_create timestamp default current_timestamp comment 'create time',
gmt_update datetime default current_timestamp on update current_timestamp comment 'update time',
primary key (id)
) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4;
COMMIT;