message_model.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package model
  2. import (
  3. "context"
  4. "time"
  5. "gorm.io/gorm"
  6. )
  7. // PMessage 消息通知
  8. type Message struct {
  9. ID int64 `json:"id" gorm:"id"` // 消息通知ID
  10. SenderUserId int64 `json:"sender_user_id" gorm:"sender_user_id"` // 发送方用户ID
  11. ReceiverUserId int64 `json:"receiver_user_id" gorm:"receiver_user_id"` // 接收方用户ID
  12. Type int8 `json:"type" gorm:"type"` // 通知类型,1动态,2评论,3回复,4私信,99系统通知
  13. Brief string `json:"brief" gorm:"brief"` // 摘要说明
  14. Content string `json:"content" gorm:"content"` // 详细内容
  15. PostId int64 `json:"post_id" gorm:"post_id"` // 动态ID
  16. CommentId int64 `json:"comment_id" gorm:"comment_id"` // 评论ID
  17. ReplyId int64 `json:"reply_id" gorm:"reply_id"` // 回复ID
  18. IsRead int8 `json:"is_read" gorm:"is_read"` // 是否已读
  19. CreatedOn int64 `json:"created_on" gorm:"created_on"` // 创建时间
  20. ModifiedOn int64 `json:"modified_on" gorm:"modified_on"` // 修改时间
  21. DeletedOn int64 `json:"deleted_on" gorm:"deleted_on"` // 删除时间
  22. IsDel int8 `json:"is_del" gorm:"is_del"` // 是否删除 0 为未删除、1 为已删除
  23. }
  24. // TableName 表名称
  25. func (*Message) TableName() string {
  26. return "p_message"
  27. }
  28. // MessageModel 消息模型
  29. type MessageModel struct {
  30. conn *gorm.DB
  31. }
  32. func NewMessageModel(conn *gorm.DB) *MessageModel {
  33. return &MessageModel{
  34. conn: conn,
  35. }
  36. }
  37. // SendMessage 发送站内信
  38. func (m *MessageModel) SendMessage(ctx context.Context, senderId, receiverId int64, postId int64, content string) error {
  39. message := &Message{
  40. SenderUserId: senderId,
  41. ReceiverUserId: receiverId,
  42. Type: 1, // 假设1表示动态通知
  43. Brief: "你被艾特了",
  44. Content: content,
  45. PostId: postId,
  46. IsRead: 0,
  47. CreatedOn: time.Now().Unix(),
  48. ModifiedOn: time.Now().Unix(),
  49. IsDel: 0,
  50. }
  51. return m.conn.WithContext(ctx).Create(message).Error
  52. }
  53. // SendNotification 发送站内信
  54. func (m *MessageModel) SendNotification(ctx context.Context, senderId, receiverId int64, messageType int8, brief, content string, postId, commentId, replyId int64) error {
  55. message := &Message{
  56. SenderUserId: senderId,
  57. ReceiverUserId: receiverId,
  58. Type: messageType,
  59. Brief: brief,
  60. Content: content,
  61. PostId: postId,
  62. CommentId: commentId,
  63. ReplyId: replyId,
  64. IsRead: 0,
  65. CreatedOn: time.Now().Unix(),
  66. ModifiedOn: time.Now().Unix(),
  67. IsDel: 0,
  68. }
  69. return m.conn.WithContext(ctx).Create(message).Error
  70. }