user_message.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type UserType string
  7. type MessageType string
  8. const (
  9. UnReadStatus StatusType = "UNREAD"
  10. ReadStatus StatusType = "READ"
  11. MyMessageColumns = "id,source_id,type,message"
  12. Admin UserType = "admin"
  13. )
  14. // UserMessage 表示 user_message 表的模型
  15. type UserMessage struct {
  16. Id int `gorm:"primaryKey;autoIncrement;column:id"`
  17. AnalystId int `gorm:"column:analyst_id"`
  18. UserId int `gorm:"column:user_id"`
  19. UserType UserType `gorm:"column:user_type;type:enum('user','admin')"`
  20. SourceId int `gorm:"column:source_id"`
  21. Message string `gorm:"column:message"`
  22. Type SourceType `gorm:"column:type;type:enum('REPORT','VIDEO','AUDIO','REFUND')"`
  23. Status StatusType `gorm:"column:status;type:enum('UNREAD','READ')"`
  24. CreatedTime time.Time `gorm:"column:created_time;type:timestamps;comment:创建时间"`
  25. UpdatedTime time.Time `gorm:"column:updated_time"`
  26. }
  27. type UserMessageView struct {
  28. Id int
  29. Message string
  30. Type SourceType
  31. Status StatusType
  32. CreatedTime string
  33. }
  34. func (u *UserMessage) ToView() *UserMessageView {
  35. return &UserMessageView{
  36. Id: u.Id,
  37. Message: u.Message,
  38. Type: u.Type,
  39. Status: u.Status,
  40. CreatedTime: u.CreatedTime.Format(time.DateTime),
  41. }
  42. }
  43. func GetMessageList(userId int, offset, size int) (messages []UserMessage, err error) {
  44. o := orm.NewOrm()
  45. sql := `select id,type,message,status,created_time from user_messages where user_id=? and user_type=? order by Field(status,'UNREAD','READ'), created_time desc limit ?,?`
  46. _, err = o.Raw(sql, userId, Admin, offset, size).QueryRows(&messages)
  47. return
  48. }
  49. func ReadMessage(userId int, messageId int) bool {
  50. o := orm.NewOrm()
  51. sql := `update user_messages set status=? where id=? and user_id =? and user_type=?`
  52. _, err := o.Raw(sql, ReadStatus, messageId, userId, Admin).Exec()
  53. if err != nil {
  54. return false
  55. }
  56. return true
  57. }
  58. func ReadMessages(userId int) bool {
  59. o := orm.NewOrm()
  60. sql := `update user_messages set status=? where user_id =? and user_type=?`
  61. _, err := o.Raw(sql, ReadStatus, userId, Admin).Exec()
  62. if err != nil {
  63. return false
  64. }
  65. return true
  66. }
  67. func GetMessageListCount(userId int) (count int, err error) {
  68. o := orm.NewOrm()
  69. sql := `select count(*) from user_messages where user_id=? and user_type=? `
  70. err = o.Raw(sql, userId, Admin).QueryRow(&count)
  71. return
  72. }