bookmark_service.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package user
  2. import (
  3. "errors"
  4. logger "eta/eta_mini_ht_api/common/component/log"
  5. "eta/eta_mini_ht_api/common/utils/page"
  6. userDao "eta/eta_mini_ht_api/models/user"
  7. "gorm.io/gorm"
  8. "time"
  9. )
  10. var (
  11. sourceTransMap = map[string]userDao.SourceType{
  12. "chart": userDao.Chart,
  13. "report": userDao.Report,
  14. }
  15. )
  16. type BookMarkDTO struct {
  17. ID int
  18. UserID int
  19. SourceID int
  20. SourceType string
  21. Status string
  22. MarkedTime string
  23. }
  24. func BookMark(templateUserId int, sourceId int, sourceType string) (err error) {
  25. sourceTypeItem := sourceTransMap[sourceType]
  26. if sourceTypeItem == "" {
  27. logger.Error("非法的资源类型")
  28. err = errors.New("非法的资源类型")
  29. return
  30. }
  31. return userDao.BookMark(templateUserId, sourceId, sourceTypeItem)
  32. }
  33. func UnBookMark(templateUserId int, sourceId int, sourceType string) (err error) {
  34. sourceTypeItem := sourceTransMap[sourceType]
  35. if sourceTypeItem == "" {
  36. logger.Error("非法的资源类型")
  37. err = errors.New("非法的资源类型")
  38. return
  39. }
  40. return userDao.UnBookMark(templateUserId, sourceId, sourceTypeItem)
  41. }
  42. func CheckBookMarkStatus(templateUserId int, sourceId int, sourceType string) (status string, err error) {
  43. sourceTypeItem := sourceTransMap[sourceType]
  44. if sourceTypeItem == "" {
  45. logger.Error("非法的资源类型")
  46. err = errors.New("非法的资源类型")
  47. return
  48. }
  49. bookmark, err := userDao.CheckBookMarkStatus(templateUserId, sourceId, sourceTypeItem)
  50. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  51. logger.Error("获取用户收藏状态失败:%v", err)
  52. return
  53. }
  54. if errors.Is(err, gorm.ErrRecordNotFound) {
  55. status = string(userDao.Unmark)
  56. return
  57. }
  58. status = string(bookmark.Status)
  59. return
  60. }
  61. func GetTotalBookMarkPageBySourceType(userId int, sourceType string) (total int64, sourceIds []int, err error) {
  62. sourceTypeItem := sourceTransMap[sourceType]
  63. if sourceTypeItem == "" {
  64. logger.Error("非法的资源类型")
  65. err = errors.New("非法的资源类型")
  66. return
  67. }
  68. return userDao.GetTotalBookMarkPageBySourceType(userId, sourceTypeItem)
  69. }
  70. func GetBookMarkPageBySourceType(templateUserId int, sourceType string, info page.PageInfo) (sourceIds []int, err error) {
  71. sourceTypeItem := sourceTransMap[sourceType]
  72. if sourceTypeItem == "" {
  73. logger.Error("非法的资源类型")
  74. err = errors.New("非法的资源类型")
  75. return
  76. }
  77. offset := page.StartIndex(info.Current, info.PageSize)
  78. return userDao.GetBookMarkPageBySourceType(templateUserId, sourceTypeItem, offset, info.PageSize)
  79. }
  80. func GetBookMarkPageRangeBySourceType(templateUserId int, sourceType string, info page.PageInfo, sourceIds []int) (filterSourceIds []int, err error) {
  81. sourceTypeItem := sourceTransMap[sourceType]
  82. if sourceTypeItem == "" {
  83. logger.Error("非法的资源类型")
  84. err = errors.New("非法的资源类型")
  85. return
  86. }
  87. offset := page.StartIndex(info.Current, info.PageSize)
  88. return userDao.GetBookMarkPageRangeBySourceType(templateUserId, sourceTypeItem, offset, info.PageSize, sourceIds)
  89. }
  90. func GetBookMarkedBySource(sourceId int, templateUserId int, sourceType string) (bookMarkDTO BookMarkDTO, err error) {
  91. sourceTypeItem := sourceTransMap[sourceType]
  92. if sourceTypeItem == "" {
  93. logger.Error("非法的资源类型")
  94. err = errors.New("非法的资源类型")
  95. return
  96. }
  97. bookMark, err := userDao.GetBookMarkedBySource(sourceId, templateUserId, sourceTypeItem)
  98. if err != nil {
  99. logger.Error("获取用户收藏记录失败:%v", err)
  100. return
  101. }
  102. bookMarkDTO = convertUserBookmark(bookMark)
  103. return
  104. }
  105. func convertUserBookmark(bookmark userDao.UserBookmark) (bookMark BookMarkDTO) {
  106. bookMark.ID = bookmark.ID
  107. bookMark.UserID = bookmark.UserID
  108. bookMark.SourceID = bookmark.SourceID
  109. bookMark.SourceType = string(bookmark.SourceType)
  110. bookMark.Status = string(bookmark.Status)
  111. bookMark.MarkedTime = bookmark.MarkedTime.Format(time.DateTime)
  112. return
  113. }