refactor: 邀请码逻辑调整,支持双方填写/使用邀请码统计,并移除已被邀请状态字段

This commit is contained in:
ccnetcore
2025-11-14 23:53:29 +08:00
parent da23d17af8
commit 4bd2fc357d
5 changed files with 32 additions and 71 deletions

View File

@@ -63,14 +63,19 @@ public class InviteCodeManager : DomainService
}
/// <summary>
/// 统计用户本周邀请人数(别人填写我的邀请码的次数)
/// 统计用户本周邀请人数(别人填写我的邀请码的次数/或者我填写别人邀请码
/// </summary>
public async Task<int> GetWeeklyInvitationCountAsync(Guid userId, DateTime weekStart)
{
return await _invitationRecordRepository._DbQueryable
var inviterCount= await _invitationRecordRepository._DbQueryable
.Where(x => x.InviterId == userId)
.Where(x => x.InvitationTime >= weekStart)
.CountAsync();
var invitedUserIdCount= await _invitationRecordRepository._DbQueryable
.Where(x => x.InvitedUserId == userId)
.Where(x => x.InvitationTime >= weekStart)
.CountAsync();
return inviterCount + invitedUserIdCount;
}
/// <summary>
@@ -118,45 +123,19 @@ public class InviteCodeManager : DomainService
{
throw new UserFriendlyException("不能使用自己的邀请码");
}
// 验证邀请码拥有者是否已被邀请
if (inviteCodeEntity.IsUserInvited)
{
throw new UserFriendlyException("该用户已被邀请,邀请码无效");
}
// 检查当前用户是否已经填写过别人的邀请码(一辈子只能填写一次)
var hasUsedOthersCode = await _invitationRecordRepository._DbQueryable
.Where(x => x.InvitedUserId == userId)
.AnyAsync();
var hasUsedOthersCode = await IsFilledInviteCodeAsync(userId);
if (hasUsedOthersCode)
{
throw new UserFriendlyException("您已经填写过别人的邀请码了,每个账号只能填写一次");
}
// 检查当前用户的邀请码信息
var myInviteCode = await _inviteCodeRepository._DbQueryable
.Where(x => x.UserId == userId)
.FirstAsync();
// 增加邀请码被使用次数
inviteCodeEntity.UsedCount++;
await _inviteCodeRepository.UpdateAsync(inviteCodeEntity);
// 标记当前用户已被邀请(一辈子只能填写一次)
if (myInviteCode == null)
{
myInviteCode = new InviteCodeAggregateRoot(userId, GenerateUniqueInviteCode());
myInviteCode.MarkUserAsInvited();
await _inviteCodeRepository.InsertAsync(myInviteCode);
}
else if (!myInviteCode.IsUserInvited)
{
myInviteCode.MarkUserAsInvited();
await _inviteCodeRepository.UpdateAsync(myInviteCode);
}
// 创建邀请记录(双方都会因为这条记录增加一次翻牌机会)
var invitationRecord = new InvitationRecordAggregateRoot(
inviteCodeEntity.UserId,
@@ -170,15 +149,14 @@ public class InviteCodeManager : DomainService
}
/// <summary>
/// 检查用户是否已邀请
/// 检查用户是否已填写过邀请
/// </summary>
public async Task<bool> IsUserInvitedAsync(Guid userId)
public async Task<bool> IsFilledInviteCodeAsync(Guid userId)
{
var inviteCode = await _inviteCodeRepository._DbQueryable
.Where(x => x.UserId == userId)
.FirstAsync();
return inviteCode?.IsUserInvited ?? false;
// 检查当前用户是否已经填写过别人的邀请码(一辈子只能填写一次)
return await _invitationRecordRepository._DbQueryable
.Where(x => x.InvitedUserId == userId)
.AnyAsync();
}
/// <summary>