edb_monitor_message.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package edbmonitor
  2. import (
  3. "errors"
  4. edbmonitor "eta/eta_api/models/edb_monitor"
  5. "eta/eta_api/models/edb_monitor/response"
  6. "eta/eta_api/utils"
  7. "time"
  8. "github.com/gorilla/websocket"
  9. )
  10. var (
  11. EDB_MONITOR_MESSAGE_CONNECT_CACHE = "edb_monitor_message_cache:"
  12. )
  13. func ReadEdbMonitorMessage(messageId, adminId int) (msg string, err error) {
  14. message, err := edbmonitor.GetEdbMonitorMessageById(messageId)
  15. if err != nil {
  16. if err.Error() == utils.ErrNoRow() {
  17. msg = "消息不存在"
  18. return
  19. }
  20. msg = "获取消息失败"
  21. return
  22. }
  23. if message.AdminId != adminId {
  24. msg = "您没有权限查看该消息"
  25. err = errors.New("no permission")
  26. return
  27. }
  28. message.IsRead = 1
  29. err = message.Update([]string{"IsRead"})
  30. if err != nil {
  31. msg = "已读失败"
  32. return
  33. }
  34. return
  35. }
  36. func LogMessage(content string, triggerTime time.Time, edbInfoId, edbInfoType, adminId int) (err error) {
  37. message := &edbmonitor.EdbMonitorMessage{
  38. EdbInfoId: edbInfoId,
  39. EdbInfoType: edbInfoType,
  40. AdminId: adminId,
  41. IsRead: 0,
  42. Message: content,
  43. MonitorTriggerTime: triggerTime,
  44. CreateTime: time.Now(),
  45. }
  46. _, err = message.Insert()
  47. return err
  48. }
  49. func SendMessages(conn *websocket.Conn, adminId int) (err error) {
  50. message, err := edbmonitor.GetEdbMonitorMessageByAdminId(adminId)
  51. if err != nil {
  52. return
  53. }
  54. msg := response.EdbMonitorMessageResp{
  55. EdbMonitorMessageId: message.EdbMonitorMessageId,
  56. EdbInfoId: message.EdbInfoId,
  57. EdbInfoType: message.EdbInfoType,
  58. Message: message.Message,
  59. TriggerTime: utils.TimeTransferString(utils.FormatDateTime, message.MonitorTriggerTime),
  60. }
  61. return conn.WriteJSON(msg)
  62. }