package log

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

type ApiIpLog struct {

}

// @Tags loging
// @Summary 获取iplog
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IpLogListRequest true "获取iplog"
// @Success 200 {object} response.Response{data=log2.IpLog} "获取iplog列表"
// @Router /loging/getIpLogList [post]
func (s *ApiIpLog) GetIpLogList(c *gin.Context) {
	var paramsInfo request.IpLogListRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := ServiceIpLog.GetIpLogList(paramsInfo.IpLogRequest, 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)
	}
}

//根据gameId分组获取ip
func (s *ApiIpLog) GetGameIPList(c *gin.Context) {
	var paramsInfo request.GameIpRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := ServiceIpLog.GetGameIpList(paramsInfo.GameIpRequest, 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)
	}
}




// @Tags loging
// @Summary 获取某天具体ip
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body log2.IpLog true "获取ip"
// @Success 200 {object} response.Response{data=log2.IpResponse} "获取iplog列表"
// @Router /loging/getIp [post]
func (s *ApiIpLog) GetIp(c *gin.Context) {
	var ip log2.IpLogResponse
	_ = c.ShouldBindJSON(&ip)
	if ip.GameId == 0 {
		response.FailWithMessage("游戏id不能为空", c)
		return
	}
	if ip.PcCode == "" {
		response.FailWithMessage("租机编号不能为空", c)
		return
	}
	if ip.CreateDate == ""{
		response.FailWithMessage("创建日期不能为空", c)
		return
	}
	list, total, err := ServiceIpLog.GetIp(ip)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
		}, "获取成功", c)
	}
}

//游戏id分组获取某天具体ip
func (s *ApiIpLog) GetGameIp(c *gin.Context) {
	var ip log2.GameIpResponse
	_ = c.ShouldBindJSON(&ip)
	if ip.GameId == 0 {
		response.FailWithMessage("游戏id不能为空", c)
		return
	}

	if ip.CreateDate == ""{
		response.FailWithMessage("创建日期不能为空", c)
		return
	}
	list, total, err := ServiceIpLog.GetGameIp(ip)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
		}, "获取成功", c)
	}
}

//获取异常租机ip
func (s *ApiIpLog) GetAbnormalIpLogList(c *gin.Context) {
	var paramsInfo request.AbnormalIpLogListRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := ServiceIpLog.GetAbnormalIpLogList(paramsInfo.AbnormalIpLogRequest, 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 *ApiIpLog) GameIpExport(c *gin.Context) {
	var excelInfo request.ExcelIpInfo
	_ = c.ShouldBindJSON(&excelInfo)
	fmt.Println(excelInfo)
	paramsInfo := excelInfo.InfoList
	paramsInfo.PageSize = 300
	paramsInfo.Page = 1
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, _, err := ServiceIpLog.GetGameIpList(paramsInfo.GameIpRequest, paramsInfo.PageInfo, paramsInfo.OrderKey, paramsInfo.Desc)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败 "+err.Error(), c)
	}
	//var nowTime string = time.Now().Format("2006-01-02_15:04")
	//excelInfo.FileName = nowTime + "-ip.xlsx"
	//fmt.Println(excelInfo.FileName)
	filePath := global.GVA_CONFIG.Excel.Dir + excelInfo.FileName
	err = ServiceIpLog.GameIpListExcel(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)
}