package book

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/book"
	bookReq "github.com/flipped-aurora/gin-vue-admin/server/model/book/request"
	commonReq "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type BookFavoriteRecordService struct{}

func (s *BookFavoriteRecordService) CreateBookFavoriteRecord(item book.BookFavoriteRecord) error {
	return global.GVA_DB.Create(&item).Error
}

func (s *BookFavoriteRecordService) DeleteBookFavoriteRecord(item book.BookFavoriteRecord) error {
	return global.GVA_DB.Delete(&item).Error
}

func (s *BookFavoriteRecordService) DeleteBookFavoriteRecordByIds(ids commonReq.IdsReq) error {
	return deleteByIDs[book.BookFavoriteRecord](ids.Ids)
}

func (s *BookFavoriteRecordService) UpdateBookFavoriteRecord(item book.BookFavoriteRecord) error {
	return global.GVA_DB.Save(&item).Error
}

func (s *BookFavoriteRecordService) GetBookFavoriteRecord(id uint) (item book.BookFavoriteRecord, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&item).Error
	return
}

func (s *BookFavoriteRecordService) GetBookFavoriteRecordInfoList(info bookReq.BookFavoriteRecordSearch) (list []book.BookFavoriteRecord, total int64, err error) {
	db := global.GVA_DB.Model(&book.BookFavoriteRecord{})
	if info.MemberUserID != nil {
		db = db.Where("member_user_id = ?", *info.MemberUserID)
	}
	if info.BookID != nil {
		db = db.Where("book_id = ?", *info.BookID)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Scopes(paginate(info.PageInfo)).Order("id desc").Find(&list).Error
	return
}
