package user import ( "eta/eta_mini_ht_api/models" "gorm.io/gorm/clause" "time" ) type Status string type SourceType string const ( Marked Status = "marked" Unmark Status = "unmark" Report SourceType = "report" Chart SourceType = "chart" ) type UserBookmark struct { ID int `gorm:"primaryKey;column:id;type:int(11);not null;comment:主键"` UserID int `gorm:"column:user_id;type:int(11);not null;comment:用户Id"` SourceID int `gorm:"column:source_id;type:int(11);not null;comment:资源Id"` SourceType SourceType `gorm:"column:source_type;type:enum('report','chart');not null;comment:资源类型:report-研报 chart-图表"` Status Status `gorm:"column:status;type:enum('marked','unmark');not null;comment:是否收藏 :marked-收藏 ,unmark-取消收藏"` MarkedTime time.Time `gorm:"column:marked_time;type:datetime;comment:收藏时间"` CreatedTime time.Time `gorm:"column:created_time;type:datetime;comment:创建时间"` UpdatedTime time.Time `gorm:"column:updated_time;type:datetime;default:null;onUpdate:CURRENT_TIMESTAMP;comment:更新时间"` } func (UserBookmark) TableName() string { return "user_bookmarks" } func BookMark(templateUserId int, sourceId int, sourceType SourceType) (err error) { db := models.Main() OnConflictFunc := clause.OnConflict{ Columns: []clause.Column{{Name: "source_id"}, {Name: "source_type"}}, DoUpdates: clause.AssignmentColumns([]string{"status", "marked_time"}), } err = db.Clauses(OnConflictFunc).Create(&UserBookmark{ UserID: templateUserId, SourceID: sourceId, SourceType: sourceType, MarkedTime: time.Now(), Status: Marked, CreatedTime: time.Now(), }).Error return } func UnBookMark(templateUserId int, sourceId int, sourceType SourceType) (err error) { db := models.Main() err = db.Model(&UserBookmark{}).Where("user_id = ? AND source_id = ? AND source_type = ?", templateUserId, sourceId, sourceType).Update("status", Unmark).Error return } func CheckBookMarkStatus(templateUserId int, sourceId int, sourceType SourceType) (bookmark UserBookmark, err error) { db := models.Main() err = db.Model(&UserBookmark{}).Select("*").Where("user_id = ? AND source_id = ? AND source_type = ?", templateUserId, sourceId, sourceType).First(&bookmark).Error return } func GetTotalBookMarkPageBySourceType(templateUserId int, sourceType SourceType) (total int64, sourceIds []int, err error) { db := models.Main() err = db.Model(&UserBookmark{}).Select("source_id").Where("user_id = ? AND source_type = ? and status =?", templateUserId, sourceType, Marked).Order("marked_time DESC").Scan(&sourceIds).Error total = int64(len(sourceIds)) return } func GetBookMarkPageBySourceType(templateUserId int, sourceType SourceType, offset int, limit int) (sourceIds []int, err error) { db := models.Main() err = db.Model(&UserBookmark{}).Select("source_id").Where("user_id = ? AND source_type = ? and status =?", templateUserId, sourceType, Marked).Order("marked_time DESC").Offset(offset).Limit(limit).Scan(&sourceIds).Error return } func GetBookMarkListBySourceType(templateUserId int, sourceType SourceType) (sourceIds []int, err error) { db := models.Main() err = db.Model(&UserBookmark{}).Select("source_id").Where("user_id = ? AND source_type = ? and status =?", templateUserId, sourceType, Marked).Order("marked_time DESC").Scan(&sourceIds).Error return } func GetBookMarkPageRangeBySourceType(templateUserId int, sourceType SourceType, offset int, limit int, sourceIds []int) (filterSourceIds []int, err error) { db := models.Main() err = db.Model(&UserBookmark{}).Select("source_id").Where("user_id = ? AND source_type = ? and source_id in ? and status =? ", templateUserId, sourceType, sourceIds, Marked).Order("marked_time DESC").Offset(offset).Limit(limit).Scan(&filterSourceIds).Error return } func GetBookMarkedBySource(sourceId int, templateUserId int, sourceType SourceType) (bookMark UserBookmark, err error) { db := models.Main() err = db.Model(&UserBookmark{}).Select("*").Where("user_id = ? AND source_type = ? and source_id =?", templateUserId, sourceType, sourceId).First(&bookMark).Error return }