123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- package user
- import (
- logger "eta/eta_mini_ht_api/common/component/log"
- "eta/eta_mini_ht_api/models"
- "eta/eta_mini_ht_api/models/message"
- "gorm.io/gorm"
- "time"
- )
- type MessageType string
- type UserType string
- const (
- UnReadStatus message.StatusType = "UNREAD"
- ReadStatus message.StatusType = "READ"
- MaxBatchNum = 1000
- MyMessageColumns = "id,source_id,type,message"
- Customer UserType = "user"
- Admin UserType = "admin"
- )
- var (
- messageTypeMap = []message.SourceType{
- message.ReportSourceType,
- message.VideoSourceType,
- message.AudioSourceType,
- }
- )
- // 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 message.SourceType `gorm:"column:type;type:enum('REPORT','VIDEO','AUDIO')"`
- Status message.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) bool {
- db := models.Main()
- //手动事务
- tx := db.Begin()
- err := db.CreateInBatches(messages, MaxBatchNum).Error
- if err != nil {
- logger.Error("批量插入消息失败:%v", err)
- tx.Rollback()
- return false
- }
- tx.Commit()
- return true
- }
- func NeedNotice(userId int, analystId int) bool {
- db := models.Main()
- var count int
- err := db.Model(&UserMessage{}).Select("count(*)").Where("user_id =? and user_type=? and analyst_id =? and status=?", userId, Customer, analystId, UnReadStatus).Scan(&count).Error
- if err != nil {
- logger.Error("统计未读消息失败:%v", err)
- return false
- }
- return count > 0
- }
- func GetUnReadMessageList(userId int) (messages []UserMessage, err error) {
- db := models.Main()
- err = db.Select(MyMessageColumns).Where("user_id=? and user_type=? and status=?", userId, Customer, UnReadStatus).Order("created_time desc").Find(&messages).Error
- return
- }
- func ReadMessage(userId int, messageId int) bool {
- db := models.Main()
- err := db.Model(&UserMessage{}).Where("id=? and user_id=? and user_type=?", messageId, userId, Customer).Update("status", ReadStatus).Error
- if err != nil {
- return false
- }
- return true
- }
- func ReadMessages(userId int, analystId int) bool {
- db := models.Main()
- err := db.Model(&UserMessage{}).Where("user_id=? and user_type=? and analyst_id =? ", userId, Customer, analystId).Update("status", ReadStatus).Error
- if err != nil {
- return false
- }
- return true
- }
|