package log

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

type ApiLoging struct {
}

// @Tags loging
// @Summary 添加日志
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.AddLogRequest true "simulator_ip（模拟器ip）,simulator_mac（模拟器mac地址）,pc_code（租机编号）,pc_mac（租机mac地址）,pc_ip（租机ip）,device_id（绑定的设备id）,account（账号）,game_id（游戏id）,coding（上传编码）,computer_type（电脑类型）,env_code（环境编号）,log_uuid（日志uuid唯一，可以时间戳加随机数。上报一轮后更换）,operator（脚本开发员）,account_type（账号类型）,remarks（备注）,report_points_data（打点数据）,task_type（1新增，2活跃）,script_type(1中控，2脚本)脚本类型"
// @Success 200 {object} response.Response{msg=string} "添加日志"
// @Router /loging/setLog [Post]
func (s *ApiLoging) CreateLog(c *gin.Context) {
	var api request.AddLogRequest
	_ = c.ShouldBindJSON(&api)
	body, _ := json.Marshal(api)
	global.GVA_LOG.Info(string(body))
	if err := utils.Verify(api, utils.LogAddVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	coding := strconv.Itoa(api.Coding)
	if len(coding) != 7 {
		response.FailWithMessage("该日志码没有7位"+coding, c)
		return
	}
	nodeCoding := coding[:3]
	status := coding[5:]
	noLogStatus := coding[3:5]
	var logical log.ServiceLoging
	switch nodeCoding {
	case "410":
		logical = new(loging2.PullAccountLog)
		break
	case "430":
		logical = new(loging2.SimulatorStartLog)
		break
	case "450":
		logical = new(loging2.GameStartLog)
		break
	case "460":
		logical = new(loging2.LoginLog)
		break
	case "470":
		logical = new(loging2.EnterMainLog)
		break
	case "480":
		logical = new(loging2.FeeLog)
		break
	default:
		logical = new(loging2.OtherLog)
		break
	}

	go ServiceLogList.CreateLog(c, api, logical, status, noLogStatus)
	response.OkWithMessage("后台已接收数据", c)
}

// @Tags loging
// @Summary 获取log
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetLogListRequest true "获取log"
// @Success 200 {object} response.Response{data=response.GetLogCodingReply} "根据id获取coding,返回包括coding详情"
// @Router /loging/getLogList [post]
func (s *ApiLoging) GetLogList(c *gin.Context) {
	var paramsInfo request.GetLogListRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := ServiceLogList.GetCodeLogList(c, paramsInfo.Loging, paramsInfo.PageInfo, paramsInfo.OrderKey, paramsInfo.Desc, paramsInfo.Code)
	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 获取打点log
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetLogListRequest true "获取打点log"
// @Success 200 {object} response.Response{data=response.GetLogCodingReply} "获取打点log列表"
// @Router /loging/getReportLogList [post]
func (s *ApiLoging) GetReportPointsLogList(c *gin.Context) {
	var paramsInfo request.ReportPointsListRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := ServiceReportPointsLog.GetReportPointsLogList(paramsInfo.ReportPointsLog, 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 获取统计节点log
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=interface{}} "获取统计log"
// @Router /loging/getNodeStatisticsLogList [post]
func (s *ApiLoging) GetNodeStatisticsLogList(c *gin.Context) {
	var paramsInfo request.GetStatisticsLogRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := ServiceStatisticsLog.StatisticsNodeLogList(c, paramsInfo.StatisticsLog, 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 获取统计log
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=interface{}} "获取统计log"
// @Router /loging/getStatisticsLogList [post]
func (s *ApiLoging) GetStatisticsLogList(c *gin.Context) {
	var paramsInfo request.GetStatisticsLogRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := ServiceStatisticsLog.OtherStatisticsLogList(c, paramsInfo.StatisticsLog, 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 重置统计log
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "根据id重置统计log"
// @Success 200 {object} response.Response{data=interface{}} "重置统计log"
// @Router /loging/resetStatisticsLog [post]
func (s *ApiLoging) ResetStatisticsLog(c *gin.Context) {
	var paramsInfo request.GetById
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo, utils.IdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	current := time.Now().Format("2006-01-02")
	key := current + ":" + strconv.Itoa(paramsInfo.ID) + ":reset"
	i, err := global.GVA_REDIS.Exists(c, key).Result()
	if err != nil {
		global.GVA_LOG.Error("重置统计数据检测key失败", zap.Error(err))
		response.FailWithMessage("重置失败", c)
		return
	}
	if i != 0 {
		global.GVA_LOG.Info("还在重置中", zap.Error(err))
		response.FailWithMessage("在重置中", c)
		return
	}
	go ServiceStatisticsLog.ResetStatisticsLog(c, paramsInfo.ID, current)
	response.OkWithMessage("后台重置中", c)
}

// @Tags loging
// @Summary 获取统计电脑
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=interface{}} "获取统计电脑"
// @Router /loging/getComputerStatistics [post]
func (s *ApiLoging) GetComputerStatistics(c *gin.Context) {
	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 := ServiceStatisticsLog.ComputerStatistics(c, paramsInfo.LogComputer, 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 在线电脑
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=interface{}} "在线电脑"
// @Router /loging/getOnlineComputer [post]
func (s *ApiLoging) GetOnlineComputer(c *gin.Context) {
	var paramsInfo request.GetStatisticsComputerRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	list, total, err := ServiceStatisticsLog.OnlineComputerStatistics(c, paramsInfo.LogComputer, 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 获取统计电脑数量
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=interface{}} "获取统计电脑数量"
// @Router /loging/getComputerNum [post]
func (s *ApiLoging) GetComputerNum(c *gin.Context) {
	var paramsInfo request.GetStatisticsComputerRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	num := ServiceStatisticsLog.GetComputerNum(paramsInfo.CreateDate)
	response.OkWithDetailed(num, "获取成功", c)
}

// @Tags loging
// @Summary 获取统计数据通过GameId
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=interface{}} "获取统计数据通过GameId"
// @Router /loging/getGameIdStatistics [post]
func (s *ApiLoging) GetGameIdStatistics(c *gin.Context) {
	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 := ServiceStatisticsLog.GameStatistics(c, paramsInfo.LogComputer, paramsInfo.PageInfo)
	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 获取统计电脑数量
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=interface{}} "获取统计电脑数量"
// @Router /loging/computerHeartbeat [post]
func (s *ApiLoging) ComputerHeartbeat(c *gin.Context) {
	var paramsInfo request.OnlineComputerRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if paramsInfo.PcCode == "" || paramsInfo.Operator == "" {
		global.GVA_LOG.Error("参数错误!")
		response.FailWithMessage("参数错误", c)
		return
	}
	num := ServiceStatisticsLog.ComputerHeartbeat(c, paramsInfo)
	response.OkWithDetailed(num, "上报成功", c)
}

// @Tags loging
// @Summary 获取统计电脑数量
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=interface{}} "获取统计电脑数量"
// @Router /loging/test [post]
func (s *ApiLoging) ComputerTest(c *gin.Context) {
	var paramsInfo request.OnlineComputerRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	ServiceStatisticsLog.CreateComputerStatisticsData()
	response.OkWithDetailed(0, "上报成功", c)
}
