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"
	"log-server/utils"
	"os"
	"strconv"
	"time"
)

type ApiRentComputer struct {
}

func (s *ApiRentComputer) GetRentComputerList(c *gin.Context) {
	//global.GVA_LOG.Info("api----GetComputerList----strat")
	var paramsInfo request.GetStatisticsComputerRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	/*if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}*/
	list, total, err := ServiceRentComputer.RentComputerList(c, paramsInfo.RentComputer, 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 *ApiRentComputer) GetRentComputerNum(c *gin.Context) {
	var paramsInfo request.GetStatisticsComputerRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	num := ServiceRentComputer.GetRentComputerNum(c, paramsInfo.RentComputer)
	response.OkWithDetailed(num, "获取成功", c)
}

func (s *ApiRentComputer) GetRentComputerById(c *gin.Context) {
	var idInfo request.GetById
	_ = c.ShouldBindJSON(&idInfo)
	if err := utils.Verify(idInfo, utils.IdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	api, err := ServiceRentComputer.GetRentComputerById(idInfo.ID)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(api, "获取成功", c)
	}
}

func (s *ApiRentComputer) AddRentComputer(c *gin.Context) {
	var api request.ComputerRequest
	_ = c.ShouldBindJSON(&api)
	//if err := utils.Verify(api, utils.LogCodingVerify); err != nil {
	//	response.FailWithMessage(err.Error(), c)
	//	return
	//}
	//fmt.Printf("%#v\n", &api)
	if err := ServiceRentComputer.AddRentComputer(api); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败,"+err.Error(), c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

func (s *ApiRentComputer) EditRentComputer(c *gin.Context) {
	var detail rentComputer.RentComputer
	_ = c.ShouldBindJSON(&detail)
	if err := ServiceRentComputer.EditRentComputer(detail); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

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

func (s *ApiRentComputer) DeleteRentComputerById(c *gin.Context) {
	var api rentComputer.RentComputer
	_ = c.ShouldBindJSON(&api)

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

func (e *ApiRentComputer) ImportExcel(c *gin.Context) {
	_, header, err := c.Request.FormFile("file")
	if err != nil {
		global.GVA_LOG.Error("接收文件失败!", zap.Error(err))
		response.FailWithMessage("接收文件失败", c)
		return
	}
	//创建目录

	uploadDir := global.GVA_CONFIG.Excel.Dir + strconv.Itoa(int(time.Now().Year())) + "-" + strconv.Itoa(int(time.Now().Month())) + "-" + strconv.Itoa(int(time.Now().Day())) + "/"
	err = os.MkdirAll(uploadDir, os.ModePerm)
	if err != nil {
		response.FailWithMessage("接收文件失败"+err.Error(), c)
		return
	}
	filePath := uploadDir + strconv.Itoa(int(time.Now().Unix())) + ".xlsx"
	err = c.SaveUploadedFile(header, filePath)
	if err != nil {
		response.FailWithMessage("接收文件失败"+err.Error(), c)
		return
	}
	err = ServiceRentComputer.ParseExcel2InfoList(filePath)
	if err != nil {
		response.FailWithMessage("接收文件失败"+err.Error(), c)
		return
	}
	response.OkWithMessage("导入成功,请刷新查看", c)
}

// RenewRentComputer 租机续费
func (s *ApiRentComputer) RenewRentComputer(c *gin.Context) {
	var api request.RenewRentComputerRequest
	_ = c.ShouldBindJSON(&api)
	if err := ServiceRentComputer.RenewRentComputer(api); err != nil {
		global.GVA_LOG.Error("租机续费失败!", zap.Error(err))
		response.FailWithMessage("租机续费失败", c)
	} else {
		response.OkWithMessage("租机续费成功", c)
	}
}

// RenewRentComputerByBatch 租机批量续费
func (s *ApiRentComputer) RenewRentComputerByBatch(c *gin.Context) {
	var api request.RenewRentComputersRequest
	_ = c.ShouldBindJSON(&api)
	if err := ServiceRentComputer.RenewRentComputerByBatch(api); err != nil {
		global.GVA_LOG.Error("租机批量续费失败!", zap.Error(err))
		response.FailWithMessage("租机批量续费失败", c)
	} else {
		response.OkWithMessage("租机批量续费成功", c)
	}
}

// RentingOutRentComputer 租机退租
func (s *ApiRentComputer) RentingOutRentComputer(c *gin.Context) {
	var api request.RentingOutRentComputerRequest
	_ = c.ShouldBindJSON(&api)
	if errMessage, err, okMessage := ServiceRentComputer.RentingOutRentComputer(api); err != nil || errMessage != "" {
		global.GVA_LOG.Error("租机退租失败!", zap.Error(err))
		response.FailWithMessage("租机退租失败", c)
	} else {
		response.OkWithMessage(okMessage, c)
	}
}

// ReplaceNumRentComputer 租机换编号
func (s *ApiRentComputer) ReplaceNumRentComputer(c *gin.Context) {
	var api request.ReplaceNumRentComputerRequest
	_ = c.ShouldBindJSON(&api)
	if errMessage, err, okMessage := ServiceRentComputer.ReplaceNumRentComputer(api); err != nil || errMessage != "" {
		//global.GVA_LOG.Error("租机换编号失败!"+errMessage, zap.Error(err))
		response.FailWithMessage("租机换编号失败!"+errMessage, c)
	} else {
		response.OkWithMessage(okMessage, c)
	}
}

// OffShelfRentComputer 租机下架
func (s *ApiRentComputer) OffShelfRentComputer(c *gin.Context) {
	var api request.ComputerRequest
	_ = c.ShouldBindJSON(&api)
	if err := ServiceRentComputer.OffShelfRentComputer(api); err != nil {
		global.GVA_LOG.Error("租机下架失败!", zap.Error(err))
		response.FailWithMessage("租机下架失败", c)
	} else {
		response.OkWithMessage(time.Now().Format("2006-01-02 15:04:05")+",租机编号"+api.PcNum+"下架成功", c)
	}
}
