package rentComputer

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"log-server/global"
	"log-server/model/log/response"
	"log-server/model/rentComputer"
	"log-server/model/rentComputer/request"
)

type ApiRentComputerLedger struct {
}

func (s *ApiRentComputerLedger) GetRentComputerLedgerList(c *gin.Context) {
	var paramsInfo request.GetComputerLedgerRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	/*if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}*/
	list, total, err := ServiceRentComputerLedger.RentComputerLedgerList(c, paramsInfo.RentComputerLedger, paramsInfo.PageInfo, paramsInfo.OrderKey, paramsInfo.Desc)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     paramsInfo.Page,
			PageSize: paramsInfo.PageSize,
		}, "获取成功", c)
	}
}

func (s *ApiRentComputerLedger) GetRentComputerLedgerNum(c *gin.Context) {
	var paramsInfo request.GetComputerLedgerRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	num := ServiceRentComputerLedger.GetRentComputerLedgerNum(c, paramsInfo.RentComputerLedger)
	response.OkWithDetailed(num, "获取成功", c)
}

func (s *ApiRentComputerLedger) ExportLedgerListExcel(c *gin.Context) {
	var excelInfo request.ExcelLedgerInfo
	_ = c.ShouldBindJSON(&excelInfo)
	paramsInfo := excelInfo.InfoList
	paramsInfo.PageSize = 300
	paramsInfo.Page = 1
	list, _, err := ServiceRentComputerLedger.RentComputerLedgerList(c, paramsInfo.RentComputerLedger, paramsInfo.PageInfo, paramsInfo.OrderKey, paramsInfo.Desc)
	if err != nil {
		global.GVA_LOG.Error("获取数据失败!", zap.Error(err))
		response.FailWithMessage("获取数据失败", c)
		return
	}
	filePath := global.GVA_CONFIG.Excel.Dir + excelInfo.FileName
	err = ServiceRentComputerLedger.ParseInfoList2Excel(list, filePath)
	if err != nil {
		global.GVA_LOG.Error("转换Excel失败!", zap.Error(err))
		response.FailWithMessage("转换Excel失败", c)
		return
	}
	c.Writer.Header().Add("success", "true")
	c.File(filePath)
}

func (s *ApiRentComputerLedger) DeleteRentComputerLedgerByIds(c *gin.Context) {
	var ids request.IdsReq
	_ = c.ShouldBindJSON(&ids)
	if err := ServiceRentComputerLedger.DeleteRentComputerLedgerByIds(ids); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

func (s *ApiRentComputerLedger) DeleteRentComputerLedgerById(c *gin.Context) {
	var api rentComputer.RentComputerLedger
	_ = c.ShouldBindJSON(&api)

	if err := ServiceRentComputerLedger.DeleteRentComputerLedgerById(api); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}
