package service

import (
	"gadmin/config"
	"gadmin/internal/admin/consts"
	"gadmin/internal/gorm/query"
	"github.com/sirupsen/logrus"
)

// DayBasic 每日用户统计
var DayBasic = new(sDayBasic)

type sDayBasic struct{}

// GetPlayerCount 获取指定渠道玩家数量统计
func (s *sDayBasic) GetPlayerCount(serverId int, date, channelId string, flag int32, typ string) int64 {
	rdb := query.Use(config.DB).ReportDayBasic
	m := rdb.Select(rdb.ALL,
		rdb.OldCount.Sum().As("old_count"),
		rdb.NewCount.Sum().As("new_count"),
		rdb.ValidCount.Sum().As("valid_count"),
		rdb.ActiveCount.Sum().As("active_count"),
	).Where(rdb.Date.Eq(date))

	if flag > -1 {
		m = m.Where(rdb.Flag.Eq(flag))
	}

	switch channelId {
	case consts.ChannelIdNone:
		// 不选择渠道
	case consts.ChannelIdAllAdv, consts.ChannelIdAllWx, consts.ChannelIdAllTT:
		// 所有的广告渠道
		m = m.Where(rdb.ChannelID.In(Channel.GetIdsByType(channelId)...))
	default:
		// 指定渠道
		m = m.Where(rdb.ChannelID.Eq(channelId))
	}

	if serverId > 0 {
		m = m.Where(rdb.ServerID.Eq(int32(serverId)))
	}

	result, err := m.First()
	if err != nil {
		logrus.Warnf("service GetPlayerCount err:%+v", err)
		return 0
	}

	switch typ {
	case "new_count":
		return result.NewCount
	case "old_count":
		return result.OldCount
	case "valid_count":
		return result.ValidCount
	case "active_count":
		return result.ActiveCount
	default:
		return 0
	}
}
