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"
	bookRes "github.com/flipped-aurora/gin-vue-admin/server/model/book/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type BookSeriesApi struct{}

func (a *BookSeriesApi) CreateBookSeries(c *gin.Context) {
	var item book.BookSeries
	if err := c.ShouldBindJSON(&item); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bookSeriesService.CreateBookSeries(item); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

func (a *BookSeriesApi) DeleteBookSeries(c *gin.Context) {
	id, ok := bindID(c)
	if !ok {
		return
	}
	if err := bookSeriesService.DeleteBookSeries(book.BookSeries{HardDeleteModel: book.HardDeleteModel{ID: id}}); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

func (a *BookSeriesApi) DeleteBookSeriesByIds(c *gin.Context) {
	ids, ok := bindIDs(c)
	if !ok {
		return
	}
	if err := bookSeriesService.DeleteBookSeriesByIds(ids); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

func (a *BookSeriesApi) UpdateBookSeries(c *gin.Context) {
	var item book.BookSeries
	if err := c.ShouldBindJSON(&item); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if item.ID == 0 {
		response.FailWithMessage("ID不能为空", c)
		return
	}
	if err := bookSeriesService.UpdateBookSeries(item); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

func (a *BookSeriesApi) FindBookSeries(c *gin.Context) {
	id, ok := bindID(c)
	if !ok {
		return
	}
	item, err := bookSeriesService.GetBookSeries(id)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(bookRes.BookSeriesResponse{BookSeries: item}, "获取成功", c)
}

func (a *BookSeriesApi) GetBookSeriesList(c *gin.Context) {
	var info bookReq.BookSeriesSearch
	if err := c.ShouldBindQuery(&info); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := bookSeriesService.GetBookSeriesInfoList(info)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	pageResult(c, list, total, info.PageInfo)
}
