package models import ( "github.com/beego/beego/v2/client/orm" "time" ) type UserType string type MessageType string const ( UnReadStatus StatusType = "UNREAD" ReadStatus StatusType = "READ" MyMessageColumns = "id,source_id,type,message" Admin UserType = "admin" ) // UserMessage 表示 user_message 表的模型 type UserMessage struct { Id int `gorm:"primaryKey;autoIncrement;column:id"` AnalystId int `gorm:"column:analyst_id"` UserId int `gorm:"column:user_id"` UserType UserType `gorm:"column:user_type;type:enum('user','admin')"` SourceId int `gorm:"column:source_id"` Message string `gorm:"column:message"` Type SourceType `gorm:"column:type;type:enum('REPORT','VIDEO','AUDIO','REFUND')"` Status StatusType `gorm:"column:status;type:enum('UNREAD','READ')"` CreatedTime time.Time `gorm:"column:created_time;type:timestamps;comment:创建时间"` UpdatedTime time.Time `gorm:"column:updated_time"` } type UserMessageView struct { Id int Message string Type SourceType Status StatusType CreatedTime string } func (u *UserMessage) ToView() *UserMessageView { return &UserMessageView{ Id: u.Id, Message: u.Message, Type: u.Type, Status: u.Status, CreatedTime: u.CreatedTime.Format(time.DateTime), } } func GetMessageList(userId int, offset, size int) (messages []UserMessage, err error) { o := orm.NewOrm() 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 ?,?` _, err = o.Raw(sql, userId, Admin, offset, size).QueryRows(&messages) return } func ReadMessage(userId int, messageId int) bool { o := orm.NewOrm() sql := `update user_messages set status=? where id=? and user_id =? and user_type=?` _, err := o.Raw(sql, ReadStatus, messageId, userId, Admin).Exec() if err != nil { return false } return true } func ReadMessages(userId int) bool { o := orm.NewOrm() sql := `update user_messages set status=? where user_id =? and user_type=?` _, err := o.Raw(sql, ReadStatus, userId, Admin).Exec() if err != nil { return false } return true } func GetMessageListCount(userId int) (count int, err error) { o := orm.NewOrm() sql := `select count(*) from user_messages where user_id=? and user_type=? ` err = o.Raw(sql, userId, Admin).QueryRow(&count) return }