package service

import (
	"gadmin/config"
	"gadmin/internal/admin/consts"
	"gadmin/internal/admin/forms"
	"gadmin/internal/gorm/model"
	"gadmin/internal/gorm/query"
	"gadmin/utility/player"
	"gadmin/utility/serializer"

	"github.com/gin-gonic/gin"
)

var PlayerAttr = new(sPlayerAttr)

type sPlayerAttr struct{}

func (s *sPlayerAttr) Get(ctx *gin.Context, req forms.PlayerAttrGetReq) serializer.Response {
	type M2 struct {
		*model.PlayerAttr
		AdvCount       int64  `json:"advCount"`
		CT             int64  `json:"create_time"`
		OnlineDuration string `json:"onlineDuration"`
	}

	DB, err := player.GetDBByUserId(req.Playerid)
	if err != nil {
		return serializer.Err(consts.CodeParamErr, "GetDBByUserId err", err)
	}
	var (
		q      = query.Use(config.GDBGroup[DB]).PlayerAttr
		m      = q.WithContext(ctx).Where(q.Playerid.Eq(req.Playerid))
		models *model.PlayerAttr
		m2     = new(M2)
		//l      = query.Use(config.DB).AdvertisementLog
		//o      = query.Use(config.DB).OnlineDurationLog
	)

	if err = m.Scan(&models); err != nil {
		return serializer.Err(consts.CodeParamErr, "查询出错 ", err)
	}

	m2.PlayerAttr = models
	m2.CT = models.CreateTime.Unix()

	/*m2.AdvCount, err = l.WithContext(ctx).Where(l.UserID.Eq(req.Playerid), l.EventID.Eq(4)).Count()
	if err != nil {
		return serializer.Err(consts.CodeParamErr, "查询出错 Count", err)
	}

	type OnlineResult struct {
		Total int64
	}

	var onlineResult OnlineResult
	err = o.WithContext(ctx).Select(o.Duration.Sum().As("total")).Where(o.UserID.Eq(req.Playerid)).Scan(&onlineResult)
	if err != nil {
		return serializer.Err(consts.CodeParamErr, "查询出错 onlineResult", err)
	}

	m2.OnlineDuration = utility.ConvertTimeUnit(onlineResult.Total)*/

	return serializer.Suc(m2)
}

func (s *sPlayerAttr) GetAll(ctx *gin.Context, req forms.PlayerAttrGetAllReq) serializer.Response {
	result := make([]*model.PlayerAttr, 0)
	for _, db := range config.GDBGroup {
		var (
			models *model.PlayerAttr
			q      = query.Use(db).PlayerAttr
			m      = q.WithContext(ctx).Where(q.AccID.Eq(req.AccId))
		)
		if err := m.Scan(&models); err != nil {
			return serializer.Err(consts.CodeParamErr, "查询出错 ", err)
		}
		if models == nil {
			continue
		}
		result = append(result, models)
	}

	return serializer.Suc(result)
}

func (s *sPlayerAttr) LevelRanking(ctx *gin.Context, req forms.LevelRankingReq) serializer.Response {

	type mods struct {
		model.PlayerAttr
		//NickName string `json:"nickName"`
	}

	DB, err := player.GetDBByServerID(req.ServerId)
	if err != nil {
		return serializer.Err(consts.CodeParamErr, "LevelRanking GetDBByUserId err", err)
	}

	var (
		q      = query.Use(config.GDBGroup[DB]).PlayerAttr
		m      = q.WithContext(ctx)
		models forms.ListRes
		lists  []mods
	)

	m = m.Limit(100).Order(q.Level.Desc())

	req.Page, req.PerPage, _ = forms.CalculatePage(req.Page, req.PerPage)

	if err := m.Limit(100).Scan(&lists); err != nil {
		return serializer.Err(consts.CodeParamErr, "查询出错 lists", err)
	}

	for i := 0; i < len(lists); i++ {
		lists[i].ID = int64(i + 1)
		//user := data.GetUser(lists[i].Playerid)
		//if user != nil {
		//	lists[i].NickName = user.NickName
		//}
		//
		//lists[i].NickName = lists[i].NickName
	}

	models.List = lists
	models.Page = req.Page
	models.PerPage = req.PerPage
	models.PageCount = 0

	return serializer.Suc(models)
}
