package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/mhaya/game/game_cluster/nodes/webadmin/entity"
	"github.com/mhaya/game/game_cluster/nodes/webadmin/service"
)

type Synthesis struct {
	sev *service.Synthesis
}

func NewSynthesis() *Synthesis {
	return &Synthesis{
		sev: service.NewSynthesis(),
	}
}

// FindUserLogDaily 查询用户日活跃
func (s *Synthesis) FindUserLogDaily(ctx *gin.Context) {
	req := &entity.UserLogDailyReq{}
	if err := ctx.ShouldBindJSON(req); err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	}
	resp, err := s.sev.FindMDBUserLogDaily(req)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  resp,
		"total": req.Total,
	})
}

// FindUserLogTotal 查询总提现数量，总注册人数，
func (s *Synthesis) FindUserLogTotal(ctx *gin.Context) {
	resp, err := s.sev.FindMDBUserLogTotal()
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": resp,
	})
}

// FindUserRetention 查询用户留存
func (s *Synthesis) FindUserRetention(ctx *gin.Context) {
	req := &entity.UserRetentionReq{}
	if err := ctx.ShouldBindJSON(req); err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	}
	resp, err := s.sev.FindUserRetention(req)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	} else {
		ctx.JSON(200, gin.H{
			"code":  200,
			"msg":   "success",
			"data":  resp,
			"total": req.Total,
		})
	}

}

// FindUserCountryCount 查询用户国家分布
func (s *Synthesis) FindUserCountryCount(ctx *gin.Context) {
	resp, err := s.sev.FindUserCountryCount()
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	} else {
		ctx.JSON(200, gin.H{
			"code": 200,
			"msg":  "success",
			"data": resp,
		})
	}
}

// FindWithdrawal 查询提现记录
func (s *Synthesis) FindWithdrawal(ctx *gin.Context) {
	req := &entity.UserWithdrawalReq{}
	if err := ctx.ShouldBindJSON(req); err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	}
	var total int64
	resp, total, err := s.sev.FindWithdrawal(req)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	} else {
		ctx.JSON(200, gin.H{
			"code":  200,
			"msg":   "success",
			"data":  resp,
			"total": total,
		})
	}
}

// WithdrawalStatus 修改提现状态
func (s *Synthesis) WithdrawalStatus(ctx *gin.Context) {
	req := &entity.UserWithdrawalStatus{}
	if err := ctx.ShouldBindJSON(req); err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	}
	err := s.sev.WithdrawalStatus(req)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	} else {
		ctx.JSON(200, gin.H{
			"code": 200,
			"msg":  "success",
		})
	}
}

// WithdrawalStatusBatch 修改提现状态批量
func (s *Synthesis) WithdrawalStatusBatch(ctx *gin.Context) {
	req := &entity.UserWithdrawalStatusBatch{}
	if err := ctx.ShouldBindJSON(req); err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	}
	err := s.sev.WithdrawalStatusBatch(req)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	} else {
		ctx.JSON(200, gin.H{
			"code": 200,
			"msg":  "success",
		})
	}
}

// FindUserLevel 查询用户等级
func (s *Synthesis) FindUserLevel(ctx *gin.Context) {
	resp, err := s.sev.FindUserLevel()
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	} else {
		ctx.JSON(200, gin.H{
			"code": 200,
			"msg":  "success",
			"data": resp,
		})
	}
}
