user_message.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package user
  2. import (
  3. logger "eta/eta_mini_ht_api/common/component/log"
  4. "eta/eta_mini_ht_api/models"
  5. "eta/eta_mini_ht_api/models/message"
  6. "gorm.io/gorm"
  7. "time"
  8. )
  9. type MessageType string
  10. const (
  11. UnReadStatus message.StatusType = "UNREAD"
  12. ReadStatus message.StatusType = "READ"
  13. MaxBatchNum = 1000
  14. MyMessageColumns = "id,source_id,type,message"
  15. )
  16. var (
  17. messageTypeMap = []message.SourceType{
  18. message.ReportSourceType,
  19. message.VideoSourceType,
  20. message.AudioSourceType,
  21. }
  22. )
  23. // UserMessage 表示 user_message 表的模型
  24. type UserMessage struct {
  25. Id int `gorm:"primaryKey;autoIncrement;column:id"`
  26. AnalystId int `gorm:"column:analyst_id"`
  27. UserId int `gorm:"column:user_id"`
  28. SourceId int `gorm:"column:source_id"`
  29. Message string `gorm:"column:message"`
  30. Type message.SourceType `gorm:"column:type;type:enum('REPORT','VIDEO','AUDIO')"`
  31. Status message.StatusType `gorm:"column:status;type:enum('UNREAD','READ')"`
  32. CreatedTime time.Time `gorm:"column:created_time;type:timestamps;comment:创建时间"`
  33. UpdatedTime time.Time `gorm:"column:updated_time"`
  34. }
  35. func (u *UserMessage) BeforeCreate(_ *gorm.DB) (err error) {
  36. u.CreatedTime = time.Now()
  37. return
  38. }
  39. func CreateMessage(message UserMessage) (err error) {
  40. db := models.Main()
  41. return db.Create(&message).Error
  42. }
  43. func BatchInsertMessage(messages []UserMessage) bool {
  44. db := models.Main()
  45. //手动事务
  46. tx := db.Begin()
  47. err := db.CreateInBatches(messages, MaxBatchNum).Error
  48. if err != nil {
  49. logger.Error("批量插入消息失败:%v", err)
  50. tx.Rollback()
  51. return false
  52. }
  53. tx.Commit()
  54. return true
  55. }
  56. func NeedNotice(userId int, analystId int) bool {
  57. db := models.Main()
  58. var count int
  59. err := db.Model(&UserMessage{}).Select("count(*)").Where("user_id =? and analyst_id =? and status=?", userId, analystId, UnReadStatus).Scan(&count).Error
  60. if err != nil {
  61. logger.Error("统计未读消息失败:%v", err)
  62. return false
  63. }
  64. return count > 0
  65. }
  66. func GetUnReadMessageList(userId int) (messages []UserMessage, err error) {
  67. db := models.Main()
  68. err = db.Select(MyMessageColumns).Where("user_id=? and status=?", userId, UnReadStatus).Order("created_time desc").Find(&messages).Error
  69. return
  70. }
  71. func ReadMessage(userId int, messageId int) bool {
  72. db := models.Main()
  73. err := db.Model(&UserMessage{}).Where("id=? and user_id=? and type in ?", messageId, userId, messageTypeMap).Update("status", ReadStatus).Error
  74. if err != nil {
  75. return false
  76. }
  77. return true
  78. }
  79. func ReadMessages(userId int, analystId int) bool {
  80. db := models.Main()
  81. err := db.Model(&UserMessage{}).Where("user_id=? and analyst_id =? and type in ?", userId, analystId, messageTypeMap).Update("status", ReadStatus).Error
  82. if err != nil {
  83. return false
  84. }
  85. return true
  86. }