123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- 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 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
- }
|