package log

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"log-server/global"
	"log-server/model/common/response"
	"log-server/model/log"
	"log-server/model/log/request"
	"log-server/utils"
	"os"
	"strconv"
	"time"
)

type ApiComputer struct {
}

// @Tags computer
// @Summary 分页获租机号列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetCardListRequest true "分页获取租机列表"
// @Success 200 {object} response.Response{data=response.GetPcListReply,msg=string} "分页获取租机列表,返回包括列表,总数,页码,每页数量"
// @Router /computer/getPcList [post]
func (s *ApiComputer) GetPcList(c *gin.Context) {
	var paramsInfo request.GetPcListRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := ServiceComputer.GetPcInfoList(paramsInfo.Computer, paramsInfo.PageInfo, paramsInfo.OrderKey, paramsInfo.Desc); 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)
	}
}

// @Tags computer
// @Summary 根据id获取api
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "根据id获取api"
// @Success 200 {object} response.Response{data=systemRes.SysAPIResponse} "根据id获取api,返回包括api详情"
// @Router /computer/getPcById [post]
func (s *ApiComputer) GetPcById(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 := ServiceComputer.GetPcById(idInfo.ID)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(api, "获取成功", c)
	}
}

// @Tags computer
// @Summary 创建租机记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.CreateUpdatePcRequest true "租机编号, 使用者, 租机供应商"
// @Success 200 {object} response.Response{msg=string} "创建租机信息"
// @Router /computer/createPc [post]
func (s *ApiComputer) CreatePc(c *gin.Context) {
	var paramsInfo request.CreateUpdatePcRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo, utils.PcVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	var computer log.Computer
	computer.Supplier = paramsInfo.Supplier
	computer.User = paramsInfo.User
	computer.PcCode = paramsInfo.PcCode
	computer.Status = paramsInfo.Status
	if err := ServiceComputer.CreatePc(computer); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags computer
// @Summary 修改租机信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.CreateUpdatePcRequest true "id, 租机编号, 使用者, 租机供应商"
// @Success 200 {object} response.Response{msg=string} "修改租机信息"
// @Router /computer/updatePc [post]
func (s *ApiComputer) UpdatePc(c *gin.Context) {
	var paramsInfo request.CreateUpdatePcRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo, utils.PcVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if paramsInfo.Id == 0 {
		response.FailWithMessage("Id不能为空", c)
		return
	}
	var computer log.Computer
	computer.Id = paramsInfo.Id
	computer.Supplier = paramsInfo.Supplier
	computer.User = paramsInfo.User
	computer.PcCode = paramsInfo.PcCode
	computer.Status = paramsInfo.Status
	if err := ServiceComputer.UpdatePc(computer); err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("修改成功", c)
	}
}

// @Tags computer
// @Summary 修改租机状态
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.PcStatusOperationRequest true "id, 状态"
// @Success 200 {object} response.Response{msg=string} "修改卡状态"
// @Router /computer/statusOperation [post]
func (s *ApiComputer) PcStatusOperation(c *gin.Context) {
	var paramsInfo request.PcStatusOperationRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if paramsInfo.Id == 0 {
		response.FailWithMessage("Id不能为空", c)
		return
	}
	if paramsInfo.Status == 0 {
		response.FailWithMessage("状态值不能为空", c)
		return
	}
	var computer log.Computer
	computer.Id = paramsInfo.Id
	computer.Status = paramsInfo.Status
	if err := ServiceComputer.UpdatePcStatus(computer); err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("修改成功", c)
	}
}

// @Tags computer
// @Summary 导入Excel文件
// @Security ApiKeyAuth
// @accept multipart/form-data
// @Produce  application/json
// @Param file formData file true "导入Excel文件"
// @Success 200 {object} response.Response{msg=string} "导入Excel文件"
// @Router /computer/importExcel [post]
func (e *ApiComputer) 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().Unix())) + "/"
	err = os.MkdirAll(uploadDir, os.ModePerm)
	if err != nil {
		response.FailWithMessage("接收文件失败"+err.Error(), c)
		return
	}
	filePath := uploadDir + "PcCodeExcelImport.xlsx"
	err = c.SaveUploadedFile(header, filePath)
	if err != nil {
		response.FailWithMessage("接收文件失败"+err.Error(), c)
		return
	}
	err = ServiceComputer.ParseExcel2InfoList(filePath)
	if err != nil {
		response.FailWithMessage("接收文件失败"+err.Error(), c)
		return
	}
	response.OkWithMessage("导入成功", c)
}
