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

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

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

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

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

func (a *BookAuthorRelationApi) FindBookAuthorRelation(c *gin.Context) {
	id, ok := bindID(c)
	if !ok {
		return
	}
	item, err := bookAuthorRelationService.GetBookAuthorRelation(id)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(bookRes.BookAuthorRelationResponse{BookAuthorRelation: item}, "获取成功", c)
}

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