石墨消解仪后端服务
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

  1. package com.iflytop.gd.common.notification;
  2. import cn.hutool.core.date.DateTime;
  3. import com.iflytop.gd.app.model.dto.CmdDTO;
  4. import lombok.Getter;
  5. /**
  6. * 向前台推送的消息类
  7. */
  8. @Getter
  9. public class Notification {
  10. private final String commandId;
  11. private final String command;
  12. private final String level;
  13. private final String title;
  14. private String content = null;
  15. private final String dateTime = DateTime.now().toString("yyyy/MM/dd HH:mm:ss");
  16. private Notification(String commandId, String command, String level, String title, String content) {
  17. this.commandId = commandId;
  18. this.level = level;
  19. this.title = title;
  20. this.content = content;
  21. this.command = command;
  22. }
  23. private Notification(String commandId, String command, String level, String title) {
  24. this.commandId = commandId;
  25. this.level = level;
  26. this.title = title;
  27. this.command = command;
  28. }
  29. /**
  30. * 创建Info级别通知
  31. * @param content 通知内容
  32. * @return 通知实例
  33. */
  34. public static Notification infoNotification(String commandId, String command, String title, String content) {
  35. return new Notification(commandId, command, "info", title, content);
  36. }
  37. public static Notification infoNotification(String commandId, String command, String title) {
  38. return new Notification(commandId, command, "info", title, "");
  39. }
  40. public static Notification infoNotification(CmdDTO cmdDTO, String title) {
  41. return new Notification(cmdDTO.getCommandId(), cmdDTO.getCommand(), "info", title, "");
  42. }
  43. public static Notification infoHandleStartNotification(CmdDTO cmdDTO) {
  44. String title = String.format("开始执行{}指令", cmdDTO.getCommand());
  45. return new Notification(cmdDTO.getCommandId(), cmdDTO.getCommand(), "info", title, "");
  46. }
  47. public static Notification infoHandleEndNotification(CmdDTO cmdDTO) {
  48. String title = String.format("{}指令执行完成", cmdDTO.getCommand());
  49. return new Notification(cmdDTO.getCommandId(), cmdDTO.getCommand(), "info", title, "");
  50. }
  51. /**
  52. * 创建Warn级别通知
  53. * @param content 通知内容
  54. * @return 通知实例
  55. */
  56. public static Notification warnNotification(String commandId, String command, String title, String content) {
  57. return new Notification(commandId, command, "warn", title, content);
  58. }
  59. /**
  60. * 创建Error级别通知
  61. * @param content 通知内容
  62. * @return 通知实例
  63. */
  64. public static Notification errorNotification(String commandId, String command, String title, String content) {
  65. return new Notification(commandId, command, "error", title, content);
  66. }
  67. public static Notification errorNotification(CmdDTO cmdDTO, Exception e) {
  68. String title = String.format("执行{}出错", cmdDTO.getCommand());
  69. return new Notification(cmdDTO.getCommandId(), cmdDTO.getCommand(), "error", title, e.getMessage());
  70. }
  71. /**
  72. * 创建Fatal级别通知
  73. * @param content 通知内容
  74. * @return 通知实例
  75. */
  76. public static Notification fatalNotification(String commandId, String command, String title, String content) {
  77. return new Notification(commandId, command, "fatal", title, content);
  78. }
  79. }