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

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

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

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

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

func (a *BookReadRecordApi) FindBookReadRecord(c *gin.Context) {
	id, ok := bindID(c)
	if !ok {
		return
	}
	item, err := bookReadRecordService.GetBookReadRecord(id)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(bookRes.BookReadRecordResponse{BookReadRecord: item}, "获取成功", c)
}

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