package service

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

// PlayerChannel 玩家渠道绑定关系
var PlayerChannel = new(sPlayerChannel)

type sPlayerChannel struct{}

// PlayerIds 选项
func (s *sPlayerChannel) PlayerIds(ctx context.Context, channelId string) (list []int64) {
	rdb := query.Use(config.DB).PlayerChannel
	m := rdb.WithContext(ctx)

	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 err := m.Pluck(rdb.Playerid, &list); err != nil {
		logrus.Warnf("service.PlayerIds err:%+v", err)
		return nil
	}
	return
}

func (s *sPlayerChannel) GetChannelId(userId int64) string {
	rdb := query.Use(config.DB).PlayerChannel
	result, err := rdb.Where(rdb.Playerid.Eq(userId)).First()
	if err != nil {
		return "0"
	}

	if result == nil {
		return "0"
	}

	return result.ChannelID
}
