石墨消解仪后端服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.3 KiB

package com.iflytop.gd.common.notification;
import cn.hutool.core.date.DateTime;
import com.iflytop.gd.app.model.dto.CmdDTO;
import lombok.Getter;
/**
* 向前台推送的消息类
*/
@Getter
public class Notification {
private final String commandId;
private final String command;
private final String level;
private final String title;
private String content = null;
private final String dateTime = DateTime.now().toString("yyyy/MM/dd HH:mm:ss");
private Notification(String commandId, String command, String level, String title, String content) {
this.commandId = commandId;
this.level = level;
this.title = title;
this.content = content;
this.command = command;
}
private Notification(String commandId, String command, String level, String title) {
this.commandId = commandId;
this.level = level;
this.title = title;
this.command = command;
}
/**
* 创建Info级别通知
* @param content 通知内容
* @return 通知实例
*/
public static Notification infoNotification(String commandId, String command, String title, String content) {
return new Notification(commandId, command, "info", title, content);
}
public static Notification infoNotification(String commandId, String command, String title) {
return new Notification(commandId, command, "info", title, "");
}
public static Notification infoNotification(CmdDTO cmdDTO, String title) {
return new Notification(cmdDTO.getCommandId(), cmdDTO.getCommand(), "info", title, "");
}
public static Notification infoHandleStartNotification(CmdDTO cmdDTO) {
String title = String.format("开始执行{}指令", cmdDTO.getCommand());
return new Notification(cmdDTO.getCommandId(), cmdDTO.getCommand(), "info", title, "");
}
public static Notification infoHandleEndNotification(CmdDTO cmdDTO) {
String title = String.format("{}指令执行完成", cmdDTO.getCommand());
return new Notification(cmdDTO.getCommandId(), cmdDTO.getCommand(), "info", title, "");
}
/**
* 创建Warn级别通知
* @param content 通知内容
* @return 通知实例
*/
public static Notification warnNotification(String commandId, String command, String title, String content) {
return new Notification(commandId, command, "warn", title, content);
}
/**
* 创建Error级别通知
* @param content 通知内容
* @return 通知实例
*/
public static Notification errorNotification(String commandId, String command, String title, String content) {
return new Notification(commandId, command, "error", title, content);
}
public static Notification errorNotification(CmdDTO cmdDTO, Exception e) {
String title = String.format("执行{}出错", cmdDTO.getCommand());
return new Notification(cmdDTO.getCommandId(), cmdDTO.getCommand(), "error", title, e.getMessage());
}
/**
* 创建Fatal级别通知
* @param content 通知内容
* @return 通知实例
*/
public static Notification fatalNotification(String commandId, String command, String title, String content) {
return new Notification(commandId, command, "fatal", title, content);
}
}