user_message.go 3.0 KB

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