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

func (s *BookCommentService) CreateBookComment(item book.BookComment) error {
	if err := validateBookComment(item); err != nil {
		return err
	}
	return global.GVA_DB.Create(&item).Error
}

func (s *BookCommentService) DeleteBookComment(item book.BookComment) error {
	return global.GVA_DB.Delete(&item).Error
}

func (s *BookCommentService) DeleteBookCommentByIds(ids commonReq.IdsReq) error {
	return deleteByIDs[book.BookComment](ids.Ids)
}

func (s *BookCommentService) UpdateBookComment(item book.BookComment) error {
	if err := validateBookComment(item); err != nil {
		return err
	}
	return global.GVA_DB.Save(&item).Error
}

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

func (s *BookCommentService) GetBookCommentInfoList(info bookReq.BookCommentSearch) (list []book.BookComment, total int64, err error) {
	db := global.GVA_DB.Model(&book.BookComment{})
	if info.MemberUserID != nil {
		db = db.Where("member_user_id = ?", *info.MemberUserID)
	}
	if info.BookID != nil {
		db = db.Where("book_id = ?", *info.BookID)
	}
	if info.ChapterID != nil {
		db = db.Where("chapter_id = ?", *info.ChapterID)
	}
	if info.CommentStatus != "" {
		db = db.Where("comment_status = ?", info.CommentStatus)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Scopes(paginate(info.PageInfo)).Order("id desc").Find(&list).Error
	return
}
