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 BookSeriesService struct{}

func (s *BookSeriesService) CreateBookSeries(item book.BookSeries) error {
	return global.GVA_DB.Create(&item).Error
}

func (s *BookSeriesService) DeleteBookSeries(item book.BookSeries) error {
	return global.GVA_DB.Delete(&item).Error
}

func (s *BookSeriesService) DeleteBookSeriesByIds(ids commonReq.IdsReq) error {
	return deleteByIDs[book.BookSeries](ids.Ids)
}

func (s *BookSeriesService) UpdateBookSeries(item book.BookSeries) error {
	return global.GVA_DB.Save(&item).Error
}

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

func (s *BookSeriesService) GetBookSeriesInfoList(info bookReq.BookSeriesSearch) (list []book.BookSeries, total int64, err error) {
	db := global.GVA_DB.Model(&book.BookSeries{})
	if info.Name != "" {
		db = db.Where("name LIKE ?", "%"+info.Name+"%")
	}
	if info.Keyword != "" {
		db = db.Where("name LIKE ?", "%"+info.Keyword+"%")
	}
	if info.IsEnabled != nil {
		db = db.Where("is_enabled = ?", *info.IsEnabled)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Scopes(paginate(info.PageInfo)).Order("id desc").Find(&list).Error
	return
}
