package user import ( "errors" logger "eta/eta_mini_ht_api/common/component/log" "eta/eta_mini_ht_api/common/utils/page" userDao "eta/eta_mini_ht_api/models/user" "gorm.io/gorm" "time" ) var ( sourceTransMap = map[string]userDao.SourceType{ "chart": userDao.Chart, "report": userDao.Report, } ) type BookMarkDTO struct { ID int UserID int SourceID int SourceType string Status string MarkedTime string } func BookMark(templateUserId int, sourceId int, sourceType string) (err error) { sourceTypeItem := sourceTransMap[sourceType] if sourceTypeItem == "" { logger.Error("非法的资源类型") err = errors.New("非法的资源类型") return } return userDao.BookMark(templateUserId, sourceId, sourceTypeItem) } func UnBookMark(templateUserId int, sourceId int, sourceType string) (err error) { sourceTypeItem := sourceTransMap[sourceType] if sourceTypeItem == "" { logger.Error("非法的资源类型") err = errors.New("非法的资源类型") return } return userDao.UnBookMark(templateUserId, sourceId, sourceTypeItem) } func CheckBookMarkStatus(templateUserId int, sourceId int, sourceType string) (status string, err error) { sourceTypeItem := sourceTransMap[sourceType] if sourceTypeItem == "" { logger.Error("非法的资源类型") err = errors.New("非法的资源类型") return } bookmark, err := userDao.CheckBookMarkStatus(templateUserId, sourceId, sourceTypeItem) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { logger.Error("获取用户收藏状态失败:%v", err) return } if errors.Is(err, gorm.ErrRecordNotFound) { status = string(userDao.Unmark) return } status = string(bookmark.Status) return } func GetTotalBookMarkPageBySourceType(userId int, sourceType string) (total int64, sourceIds []int, err error) { sourceTypeItem := sourceTransMap[sourceType] if sourceTypeItem == "" { logger.Error("非法的资源类型") err = errors.New("非法的资源类型") return } return userDao.GetTotalBookMarkPageBySourceType(userId, sourceTypeItem) } func GetBookMarkPageBySourceType(templateUserId int, sourceType string, info page.PageInfo) (sourceIds []int, err error) { sourceTypeItem := sourceTransMap[sourceType] if sourceTypeItem == "" { logger.Error("非法的资源类型") err = errors.New("非法的资源类型") return } offset := page.StartIndex(info.Current, info.PageSize) return userDao.GetBookMarkPageBySourceType(templateUserId, sourceTypeItem, offset, info.PageSize) } func GetBookMarkListBySourceType(templateUserId int, sourceType string) (sourceIds []int, err error) { sourceTypeItem := sourceTransMap[sourceType] if sourceTypeItem == "" { logger.Error("非法的资源类型") err = errors.New("非法的资源类型") return } return userDao.GetBookMarkListBySourceType(templateUserId, sourceTypeItem) } func GetBookMarkPageRangeBySourceType(templateUserId int, sourceType string, info page.PageInfo, sourceIds []int) (filterSourceIds []int, err error) { sourceTypeItem := sourceTransMap[sourceType] if sourceTypeItem == "" { logger.Error("非法的资源类型") err = errors.New("非法的资源类型") return } offset := page.StartIndex(info.Current, info.PageSize) return userDao.GetBookMarkPageRangeBySourceType(templateUserId, sourceTypeItem, offset, info.PageSize, sourceIds) } func GetBookMarkedBySource(sourceId int, templateUserId int, sourceType string) (bookMarkDTO BookMarkDTO, err error) { sourceTypeItem := sourceTransMap[sourceType] if sourceTypeItem == "" { logger.Error("非法的资源类型") err = errors.New("非法的资源类型") return } bookMark, err := userDao.GetBookMarkedBySource(sourceId, templateUserId, sourceTypeItem) if err != nil { logger.Error("获取用户收藏记录失败:%v", err) return } bookMarkDTO = convertUserBookmark(bookMark) return } func convertUserBookmark(bookmark userDao.UserBookmark) (bookMark BookMarkDTO) { bookMark.ID = bookmark.ID bookMark.UserID = bookmark.UserID bookMark.SourceID = bookmark.SourceID bookMark.SourceType = string(bookmark.SourceType) bookMark.Status = string(bookmark.Status) bookMark.MarkedTime = bookmark.MarkedTime.Format(time.DateTime) return }