73 lines
2.3 KiB
Go
73 lines
2.3 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/perfect-panel/server/internal/model/user"
|
|
"github.com/perfect-panel/server/internal/svc"
|
|
"github.com/perfect-panel/server/internal/types"
|
|
"github.com/perfect-panel/server/pkg/constant"
|
|
"github.com/perfect-panel/server/pkg/logger"
|
|
"github.com/perfect-panel/server/pkg/xerr"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type GetUserInviteStatsLogic struct {
|
|
logger.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// Get user invite statistics
|
|
func NewGetUserInviteStatsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInviteStatsLogic {
|
|
return &GetUserInviteStatsLogic{
|
|
Logger: logger.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *GetUserInviteStatsLogic) GetUserInviteStats(req *types.GetUserInviteStatsRequest) (resp *types.GetUserInviteStatsResponse, err error) {
|
|
// 1. 从 context 中获取当前登录用户
|
|
u, ok := l.ctx.Value(constant.CtxKeyUser).(*user.User)
|
|
if !ok {
|
|
l.Errorw("[GetUserInviteStats] user not found in context")
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "Invalid Access")
|
|
}
|
|
userId := u.Id
|
|
|
|
// 2. 获取有效邀请数 (FriendlyCount): 被邀请用户中至少有1个已支付订单 (status=2或5)
|
|
var friendlyCount int64
|
|
err = l.svcCtx.DB.WithContext(l.ctx).
|
|
Table("user").
|
|
Where("referer_id = ? AND EXISTS (SELECT 1 FROM `order` o WHERE o.user_id = user.id AND o.status IN (?, ?))", userId, 2, 5).
|
|
Count(&friendlyCount).Error
|
|
|
|
if err != nil {
|
|
l.Errorw("[GetUserInviteStats] count friendly users failed",
|
|
logger.Field("error", err.Error()),
|
|
logger.Field("user_id", userId))
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError),
|
|
"count friendly users failed: %v", err.Error())
|
|
}
|
|
|
|
// 3. 获取历史邀请总数 (HistoryCount)
|
|
var historyCount int64
|
|
err = l.svcCtx.DB.WithContext(l.ctx).
|
|
Table("user").
|
|
Where("referer_id = ?", userId).
|
|
Count(&historyCount).Error
|
|
if err != nil {
|
|
l.Errorw("[GetUserInviteStats] count history users failed",
|
|
logger.Field("error", err.Error()),
|
|
logger.Field("user_id", userId))
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError),
|
|
"count history users failed: %v", err.Error())
|
|
}
|
|
|
|
return &types.GetUserInviteStatsResponse{
|
|
FriendlyCount: friendlyCount,
|
|
HistoryCount: historyCount,
|
|
}, nil
|
|
}
|