user_bookmark.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package user
  2. import (
  3. "eta/eta_mini_ht_api/models"
  4. "gorm.io/gorm/clause"
  5. "time"
  6. )
  7. type Status string
  8. type SourceType string
  9. const (
  10. Marked Status = "marked"
  11. Unmark Status = "unmark"
  12. Report SourceType = "report"
  13. Chart SourceType = "chart"
  14. )
  15. type UserBookmark struct {
  16. ID int `gorm:"primaryKey;column:id;type:int(11);not null;comment:主键"`
  17. UserID int `gorm:"column:user_id;type:int(11);not null;comment:用户Id"`
  18. SourceID int `gorm:"column:source_id;type:int(11);not null;comment:资源Id"`
  19. SourceType SourceType `gorm:"column:source_type;type:enum('report','chart');not null;comment:资源类型:report-研报 chart-图表"`
  20. Status Status `gorm:"column:status;type:enum('marked','unmark');not null;comment:是否收藏 :marked-收藏 ,unmark-取消收藏"`
  21. MarkedTime time.Time `gorm:"column:marked_time;type:datetime;comment:收藏时间"`
  22. CreatedTime time.Time `gorm:"column:created_time;type:datetime;comment:创建时间"`
  23. UpdatedTime time.Time `gorm:"column:updated_time;type:datetime;default:null;onUpdate:CURRENT_TIMESTAMP;comment:更新时间"`
  24. }
  25. func (UserBookmark) TableName() string {
  26. return "user_bookmarks"
  27. }
  28. func BookMark(templateUserId int, sourceId int, sourceType SourceType) (err error) {
  29. db := models.Main()
  30. OnConflictFunc := clause.OnConflict{
  31. Columns: []clause.Column{{Name: "source_id"}, {Name: "source_type"}},
  32. DoUpdates: clause.AssignmentColumns([]string{"status", "marked_time"}),
  33. }
  34. err = db.Clauses(OnConflictFunc).Create(&UserBookmark{
  35. UserID: templateUserId,
  36. SourceID: sourceId,
  37. SourceType: sourceType,
  38. MarkedTime: time.Now(),
  39. Status: Marked,
  40. CreatedTime: time.Now(),
  41. }).Error
  42. return
  43. }
  44. func UnBookMark(templateUserId int, sourceId int, sourceType SourceType) (err error) {
  45. db := models.Main()
  46. err = db.Model(&UserBookmark{}).Where("user_id = ? AND source_id = ? AND source_type = ?", templateUserId, sourceId, sourceType).Update("status", Unmark).Error
  47. return
  48. }
  49. func CheckBookMarkStatus(templateUserId int, sourceId int, sourceType SourceType) (bookmark UserBookmark, err error) {
  50. db := models.Main()
  51. err = db.Model(&UserBookmark{}).Select("*").Where("user_id = ? AND source_id = ? AND source_type = ?", templateUserId, sourceId, sourceType).First(&bookmark).Error
  52. return
  53. }
  54. func GetTotalBookMarkPageBySourceType(templateUserId int, sourceType SourceType) (total int64, sourceIds []int, err error) {
  55. db := models.Main()
  56. 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
  57. total = int64(len(sourceIds))
  58. return
  59. }
  60. func GetBookMarkPageBySourceType(templateUserId int, sourceType SourceType, offset int, limit int) (sourceIds []int, err error) {
  61. db := models.Main()
  62. 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
  63. return
  64. }
  65. func GetBookMarkListBySourceType(templateUserId int, sourceType SourceType) (sourceIds []int, err error) {
  66. db := models.Main()
  67. 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
  68. return
  69. }
  70. func GetBookMarkPageRangeBySourceType(templateUserId int, sourceType SourceType, offset int, limit int, sourceIds []int) (filterSourceIds []int, err error) {
  71. db := models.Main()
  72. 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
  73. return
  74. }
  75. func GetBookMarkedBySource(sourceId int, templateUserId int, sourceType SourceType) (bookMark UserBookmark, err error) {
  76. db := models.Main()
  77. err = db.Model(&UserBookmark{}).Select("*").Where("user_id = ? AND source_type = ? and source_id =?", templateUserId, sourceType, sourceId).First(&bookMark).Error
  78. return
  79. }