186 lines
4.3 KiB
Go
186 lines
4.3 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"strings"
|
|
|
|
userentity "spark-server/internal/module/user/entity"
|
|
"spark-server/internal/pkg/errs"
|
|
)
|
|
|
|
func (s *Service) ListUsers(ctx context.Context, email string, page, size int) (list []UserView, total int64, err error) {
|
|
if !s.ready() {
|
|
return nil, 0, ErrNotReady
|
|
}
|
|
if size <= 0 || size > 200 {
|
|
size = 50
|
|
}
|
|
if page < 0 {
|
|
page = 0
|
|
}
|
|
rows, total, err := s.repo.ListUsers(ctx, email, page, size)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
list = make([]UserView, 0, len(rows))
|
|
for i := range rows {
|
|
list = append(list, userView(&rows[i]))
|
|
}
|
|
return list, total, nil
|
|
}
|
|
|
|
func (s *Service) UserByEmail(ctx context.Context, email string) (map[string]any, error) {
|
|
email = strings.ToLower(strings.TrimSpace(email))
|
|
if email == "" {
|
|
return nil, errs.ErrInvalidArgument
|
|
}
|
|
if !s.ready() {
|
|
return nil, ErrNotReady
|
|
}
|
|
rows, err := s.repo.UsersByEmail(ctx, email)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items := make([]UserView, 0, len(rows))
|
|
for i := range rows {
|
|
items = append(items, userView(&rows[i]))
|
|
}
|
|
return map[string]any{
|
|
"email": email, "appUsers": items, "appCode": "spark",
|
|
}, nil
|
|
}
|
|
|
|
func (s *Service) UserByID(ctx context.Context, id int64) (*UserView, error) {
|
|
if !s.ready() {
|
|
return nil, ErrNotReady
|
|
}
|
|
r, err := s.repo.UserByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
v := userView(r)
|
|
return &v, nil
|
|
}
|
|
|
|
func (s *Service) UsersByNickname(ctx context.Context, nickname string) ([]UserView, error) {
|
|
if !s.ready() {
|
|
return nil, ErrNotReady
|
|
}
|
|
rows, err := s.repo.UsersByNickname(ctx, nickname)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]UserView, 0, len(rows))
|
|
for i := range rows {
|
|
out = append(out, userView(&rows[i]))
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (s *Service) UsersByDevice(ctx context.Context, deviceID string) ([]UserView, error) {
|
|
if !s.ready() {
|
|
return nil, ErrNotReady
|
|
}
|
|
deviceID = strings.TrimSpace(deviceID)
|
|
did, _ := strconv.ParseUint(deviceID, 10, 64)
|
|
rows, err := s.repo.UsersByDevice(ctx, deviceID, did)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]UserView, 0, len(rows))
|
|
for i := range rows {
|
|
out = append(out, userView(&rows[i]))
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (s *Service) UsersByIP(ctx context.Context, ip string) ([]UserView, error) {
|
|
if !s.ready() {
|
|
return nil, ErrNotReady
|
|
}
|
|
rows, err := s.repo.UsersByIP(ctx, strings.TrimSpace(ip))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]UserView, 0, len(rows))
|
|
for i := range rows {
|
|
out = append(out, userView(&rows[i]))
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (s *Service) BanByEmail(ctx context.Context, email string, banned bool) error {
|
|
email = strings.ToLower(strings.TrimSpace(email))
|
|
if email == "" {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
if !s.ready() {
|
|
return ErrNotReady
|
|
}
|
|
if !banned {
|
|
return s.repo.SetUserStatusByEmail(ctx, email, userentity.StatusActive)
|
|
}
|
|
rows, err := s.repo.UsersByEmail(ctx, email)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(rows) == 0 {
|
|
return errs.ErrNotFound
|
|
}
|
|
if s.disabler == nil {
|
|
return s.repo.SetUserStatusByEmail(ctx, email, userentity.StatusDisabled)
|
|
}
|
|
for i := range rows {
|
|
if err := s.disabler.DisableAccount(ctx, rows[i].ID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) ResetNicknameByEmail(ctx context.Context, email, nickname string) error {
|
|
email = strings.ToLower(strings.TrimSpace(email))
|
|
if email == "" {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
if strings.TrimSpace(nickname) == "" {
|
|
nickname = "用户"
|
|
}
|
|
if !s.ready() {
|
|
return ErrNotReady
|
|
}
|
|
return s.repo.SetNicknameByEmail(ctx, email, nickname)
|
|
}
|
|
|
|
func (s *Service) ResetAvatarByEmail(ctx context.Context, email string) error {
|
|
email = strings.ToLower(strings.TrimSpace(email))
|
|
if email == "" {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
if !s.ready() {
|
|
return ErrNotReady
|
|
}
|
|
return s.repo.ClearAvatarByEmail(ctx, email)
|
|
}
|
|
|
|
func (s *Service) SameIPRank(ctx context.Context, limit int) ([]SameIPItem, error) {
|
|
if !s.ready() {
|
|
return nil, ErrNotReady
|
|
}
|
|
if limit <= 0 || limit > 200 {
|
|
limit = 50
|
|
}
|
|
rows, err := s.repo.SameIPRank(ctx, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]SameIPItem, 0, len(rows))
|
|
for i := range rows {
|
|
out = append(out, SameIPItem{
|
|
IP: rows[i].IP, UserCount: rows[i].UserCount, DeviceCount: rows[i].DeviceCount, Count: rows[i].UserCount,
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|