package guild

import (
	"context"

	"bindbox-game/internal/repository/mysql/model"
)

func (s *service) ListMembers(ctx context.Context, guildID int64, page, pageSize int) (items []*model.GuildMembers, total int64, err error) {
	q := s.readDB.GuildMembers.WithContext(ctx).ReadDB().Where(s.readDB.GuildMembers.GuildID.Eq(guildID)).Where(s.readDB.GuildMembers.Status.Eq(1))
	total, err = q.Count()
	if err != nil {
		return nil, 0, err
	}
	if page <= 0 {
		page = 1
	}
	if pageSize <= 0 {
		pageSize = 20
	}
	if pageSize > 100 {
		pageSize = 100
	}
	items, err = q.Order(s.readDB.GuildMembers.ID.Desc()).Offset((page - 1) * pageSize).Limit(pageSize).Find()
	if err != nil {
		return nil, 0, err
	}
	return items, total, nil
}
