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

// @Tags Book
// @Summary 创建书籍信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body book.Book true "书籍信息"
// @Success 200 {object} response.Response{msg=string}
// @Router /book/createBook [post]
func (a *BookApi) CreateBook(c *gin.Context) {
	var item book.Book
	if err := c.ShouldBindJSON(&item); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bookService.CreateBook(item); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// @Tags Book
// @Summary 删除书籍信息
// @Security ApiKeyAuth
// @Param id query int true "主键ID"
// @Success 200 {object} response.Response{msg=string}
// @Router /book/deleteBook [delete]
func (a *BookApi) DeleteBook(c *gin.Context) {
	id, ok := bindID(c)
	if !ok {
		return
	}
	if err := bookService.DeleteBook(book.Book{HardDeleteModel: book.HardDeleteModel{ID: id}}); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// @Tags Book
// @Summary 批量删除书籍信息
// @Security ApiKeyAuth
// @Param ids query []int true "主键ID数组"
// @Success 200 {object} response.Response{msg=string}
// @Router /book/deleteBookByIds [delete]
func (a *BookApi) DeleteBookByIds(c *gin.Context) {
	ids, ok := bindIDs(c)
	if !ok {
		return
	}
	if err := bookService.DeleteBookByIds(ids); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// @Tags Book
// @Summary 更新书籍信息
// @Security ApiKeyAuth
// @Param data body book.Book true "书籍信息"
// @Success 200 {object} response.Response{msg=string}
// @Router /book/updateBook [put]
func (a *BookApi) UpdateBook(c *gin.Context) {
	var item book.Book
	if err := c.ShouldBindJSON(&item); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if item.ID == 0 {
		response.FailWithMessage("ID不能为空", c)
		return
	}
	if err := bookService.UpdateBook(item); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// @Tags Book
// @Summary 查询书籍详情
// @Security ApiKeyAuth
// @Param id query int true "主键ID"
// @Success 200 {object} response.Response{data=bookRes.BookResponse,msg=string}
// @Router /book/findBook [get]
func (a *BookApi) FindBook(c *gin.Context) {
	id, ok := bindID(c)
	if !ok {
		return
	}
	item, err := bookService.GetBook(id)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(bookRes.BookResponse{Book: item}, "获取成功", c)
}

// @Tags Book
// @Summary 分页获取书籍列表
// @Security ApiKeyAuth
// @Param data query bookReq.BookSearch true "分页与筛选条件"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string}
// @Router /book/getBookList [get]
func (a *BookApi) GetBookList(c *gin.Context) {
	var info bookReq.BookSearch
	if err := c.ShouldBindQuery(&info); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := bookService.GetBookInfoList(info)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	pageResult(c, list, total, info.PageInfo)
}
