feat: 新增翻牌顺序追踪并重构翻牌/邀请码逻辑到 Manager,更新前端
- 在 CardFlipStatusOutput 与前端 types 添加 FlipOrderIndex 字段以记录牌在翻牌顺序中的位置 - 在域实体 CardFlipTaskAggregateRoot 增加 FlippedOrder(Json 列)以保存用户实际翻牌顺序 - 将 CardFlipService 重构为调用 CardFlipManager 与 InviteCodeManager,移除大量内聚的业务实现与常量(职责下沉到 Manager) - 调整翻牌、使用邀请码和查询相关流程为 Manager 驱动,更新返回结构与提示文本 - 更新前端 CardFlipActivity 组件与 types,允许任意未翻的卡片被点击并显示翻牌顺序位置 - 若干文案、格式与日志细节修正
This commit is contained in:
@@ -0,0 +1,217 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SqlSugar;
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Yi.Framework.AiHub.Domain.Entities;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Managers;
|
||||
|
||||
/// <summary>
|
||||
/// 邀请码管理器 - 负责邀请码核心业务逻辑
|
||||
/// </summary>
|
||||
public class InviteCodeManager : DomainService
|
||||
{
|
||||
private readonly ISqlSugarRepository<InviteCodeAggregateRoot> _inviteCodeRepository;
|
||||
private readonly ISqlSugarRepository<InvitationRecordAggregateRoot> _invitationRecordRepository;
|
||||
private readonly ILogger<InviteCodeManager> _logger;
|
||||
|
||||
public InviteCodeManager(
|
||||
ISqlSugarRepository<InviteCodeAggregateRoot> inviteCodeRepository,
|
||||
ISqlSugarRepository<InvitationRecordAggregateRoot> invitationRecordRepository,
|
||||
ILogger<InviteCodeManager> logger)
|
||||
{
|
||||
_inviteCodeRepository = inviteCodeRepository;
|
||||
_invitationRecordRepository = invitationRecordRepository;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成用户的邀请码
|
||||
/// </summary>
|
||||
/// <param name="userId">用户ID</param>
|
||||
/// <returns>邀请码</returns>
|
||||
public async Task<string> GenerateInviteCodeForUserAsync(Guid userId)
|
||||
{
|
||||
// 检查是否已有邀请码
|
||||
var existingCode = await _inviteCodeRepository._DbQueryable
|
||||
.Where(x => x.UserId == userId)
|
||||
.FirstAsync();
|
||||
|
||||
if (existingCode != null)
|
||||
{
|
||||
return existingCode.Code;
|
||||
}
|
||||
|
||||
// 生成新邀请码
|
||||
var code = GenerateUniqueInviteCode();
|
||||
var inviteCode = new InviteCodeAggregateRoot(userId, code);
|
||||
await _inviteCodeRepository.InsertAsync(inviteCode);
|
||||
|
||||
_logger.LogInformation($"用户 {userId} 生成邀请码 {code}");
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户的邀请码信息
|
||||
/// </summary>
|
||||
public async Task<InviteCodeAggregateRoot?> GetUserInviteCodeAsync(Guid userId)
|
||||
{
|
||||
return await _inviteCodeRepository._DbQueryable
|
||||
.Where(x => x.UserId == userId)
|
||||
.FirstAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 统计用户本周邀请人数
|
||||
/// </summary>
|
||||
public async Task<int> GetWeeklyInvitationCountAsync(Guid userId, DateTime weekStart)
|
||||
{
|
||||
return await _invitationRecordRepository._DbQueryable
|
||||
.Where(x => x.InviterId == userId)
|
||||
.Where(x => x.InvitationTime >= weekStart)
|
||||
.CountAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取邀请历史记录
|
||||
/// </summary>
|
||||
public async Task<List<InvitationHistoryDto>> GetInvitationHistoryAsync(Guid userId, int limit = 10)
|
||||
{
|
||||
return await _invitationRecordRepository._DbQueryable
|
||||
.Where(x => x.InviterId == userId)
|
||||
.OrderBy(x => x.InvitationTime, OrderByType.Desc)
|
||||
.Take(limit)
|
||||
.Select(x => new InvitationHistoryDto
|
||||
{
|
||||
InvitedUserName = "用户***", // 脱敏处理
|
||||
InvitationTime = x.InvitationTime
|
||||
})
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用邀请码
|
||||
/// </summary>
|
||||
/// <param name="userId">使用者ID</param>
|
||||
/// <param name="inviteCode">邀请码</param>
|
||||
/// <returns>邀请人ID</returns>
|
||||
public async Task<Guid> UseInviteCodeAsync(Guid userId, string inviteCode)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(inviteCode))
|
||||
{
|
||||
throw new UserFriendlyException("邀请码不能为空");
|
||||
}
|
||||
|
||||
// 查找邀请码
|
||||
var inviteCodeEntity = await _inviteCodeRepository._DbQueryable
|
||||
.Where(x => x.Code == inviteCode)
|
||||
.FirstAsync();
|
||||
|
||||
if (inviteCodeEntity == null)
|
||||
{
|
||||
throw new UserFriendlyException("邀请码不存在");
|
||||
}
|
||||
|
||||
// 验证不能使用自己的邀请码
|
||||
if (inviteCodeEntity.UserId == userId)
|
||||
{
|
||||
throw new UserFriendlyException("不能使用自己的邀请码");
|
||||
}
|
||||
|
||||
// 验证邀请码是否已被使用
|
||||
if (inviteCodeEntity.IsUsed)
|
||||
{
|
||||
throw new UserFriendlyException("该邀请码已被使用");
|
||||
}
|
||||
|
||||
// 验证邀请码拥有者是否已被邀请
|
||||
if (inviteCodeEntity.IsUserInvited)
|
||||
{
|
||||
throw new UserFriendlyException("该用户已被邀请,邀请码无效");
|
||||
}
|
||||
|
||||
// 检查当前用户是否已被邀请
|
||||
var myInviteCode = await _inviteCodeRepository._DbQueryable
|
||||
.Where(x => x.UserId == userId)
|
||||
.FirstAsync();
|
||||
|
||||
if (myInviteCode?.IsUserInvited == true)
|
||||
{
|
||||
throw new UserFriendlyException("您已使用过邀请码,无法重复使用");
|
||||
}
|
||||
|
||||
// 标记邀请码为已使用
|
||||
inviteCodeEntity.MarkAsUsed(userId);
|
||||
await _inviteCodeRepository.UpdateAsync(inviteCodeEntity);
|
||||
|
||||
// 标记当前用户已被邀请
|
||||
if (myInviteCode == null)
|
||||
{
|
||||
myInviteCode = new InviteCodeAggregateRoot(userId, GenerateUniqueInviteCode());
|
||||
myInviteCode.MarkUserAsInvited();
|
||||
await _inviteCodeRepository.InsertAsync(myInviteCode);
|
||||
}
|
||||
else
|
||||
{
|
||||
myInviteCode.MarkUserAsInvited();
|
||||
await _inviteCodeRepository.UpdateAsync(myInviteCode);
|
||||
}
|
||||
|
||||
// 创建邀请记录
|
||||
var invitationRecord = new InvitationRecordAggregateRoot(
|
||||
inviteCodeEntity.UserId,
|
||||
userId,
|
||||
inviteCode);
|
||||
await _invitationRecordRepository.InsertAsync(invitationRecord);
|
||||
|
||||
_logger.LogInformation($"用户 {userId} 使用邀请码 {inviteCode} 成功");
|
||||
|
||||
return inviteCodeEntity.UserId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查用户是否已被邀请
|
||||
/// </summary>
|
||||
public async Task<bool> IsUserInvitedAsync(Guid userId)
|
||||
{
|
||||
var inviteCode = await _inviteCodeRepository._DbQueryable
|
||||
.Where(x => x.UserId == userId)
|
||||
.FirstAsync();
|
||||
|
||||
return inviteCode?.IsUserInvited ?? false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成唯一邀请码
|
||||
/// </summary>
|
||||
private string GenerateUniqueInviteCode()
|
||||
{
|
||||
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
var random = new Random();
|
||||
var code = new char[8];
|
||||
|
||||
for (int i = 0; i < code.Length; i++)
|
||||
{
|
||||
code[i] = chars[random.Next(chars.Length)];
|
||||
}
|
||||
|
||||
return new string(code);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 邀请历史记录DTO
|
||||
/// </summary>
|
||||
public class InvitationHistoryDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 被邀请人名称(脱敏)
|
||||
/// </summary>
|
||||
public string InvitedUserName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 邀请时间
|
||||
/// </summary>
|
||||
public DateTime InvitationTime { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user