package user import ( logger "eta/eta_mini_ht_api/common/component/log" "eta/eta_mini_ht_api/models" "gorm.io/gorm" "time" ) type MessageType string const ( UnReadStatus StatusType = "UNREAD" ReadStatus StatusType = "READ" MaxBatchNum = 1000 ) // UserMessage 表示 user_message 表的模型 type UserMessage struct { Id int `gorm:"primaryKey;autoIncrement;column:id"` From int `gorm:"column:from"` To int `gorm:"column:to"` SourceId int `gorm:"column:source_id"` Message string `gorm:"column:message"` Type SourceType `gorm:"column:type;type:enum('REPORT','VIDEO','AUDIO')"` 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"` } func (u *UserMessage) BeforeCreate(_ *gorm.DB) (err error) { u.CreatedTime = time.Now() return } func CreateMessage(message UserMessage) (err error) { db := models.Main() return db.Create(&message).Error } func BatchInsertMessage(messages []UserMessage) (err error) { db := models.Main() //手动事务 tx := db.Begin() err = db.CreateInBatches(messages, MaxBatchNum).Error if err != nil { logger.Error("批量插入消息失败:%v", err) tx.Rollback() return } tx.Commit() return nil }