Merge branch 'invitation' into ai-hub
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
using SqlSugar;
|
||||
using Volo.Abp.Domain.Entities.Auditing;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// 翻牌任务记录
|
||||
/// </summary>
|
||||
[SugarTable("Ai_CardFlipTask")]
|
||||
[SugarIndex($"index_{nameof(UserId)}_{nameof(WeekStartDate)}",
|
||||
nameof(UserId), OrderByType.Asc,
|
||||
nameof(WeekStartDate), OrderByType.Desc)]
|
||||
public class CardFlipTaskAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||
{
|
||||
public CardFlipTaskAggregateRoot()
|
||||
{
|
||||
}
|
||||
|
||||
public CardFlipTaskAggregateRoot(Guid userId, DateTime weekStartDate)
|
||||
{
|
||||
UserId = userId;
|
||||
WeekStartDate = weekStartDate.Date; // 确保只存储日期部分
|
||||
TotalFlips = 0;
|
||||
FreeFlipsUsed = 0;
|
||||
BonusFlipsUsed = 0;
|
||||
InviteFlipsUsed = 0;
|
||||
IsFirstFlipDone = false;
|
||||
HasNinthReward = false;
|
||||
HasTenthReward = false;
|
||||
FlippedOrder = new List<int>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户ID
|
||||
/// </summary>
|
||||
public Guid UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 本周开始日期(每周一)
|
||||
/// </summary>
|
||||
public DateTime WeekStartDate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 总共已翻牌次数
|
||||
/// </summary>
|
||||
public int TotalFlips { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 已使用的免费次数(最多7次)
|
||||
/// </summary>
|
||||
public int FreeFlipsUsed { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 已使用的赠送次数(已废弃,保持为0)
|
||||
/// </summary>
|
||||
public int BonusFlipsUsed { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 已使用的邀请解锁次数(最多3次)
|
||||
/// </summary>
|
||||
public int InviteFlipsUsed { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已完成首次翻牌(用于判断是否创建任务)
|
||||
/// </summary>
|
||||
public bool IsFirstFlipDone { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已获得第8次奖励
|
||||
/// </summary>
|
||||
public bool HasEighthReward { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 第8次奖励金额(100-300w)
|
||||
/// </summary>
|
||||
public long? EighthRewardAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已获得第9次奖励
|
||||
/// </summary>
|
||||
public bool HasNinthReward { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 第9次奖励金额(100-500w)
|
||||
/// </summary>
|
||||
public long? NinthRewardAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已获得第10次奖励
|
||||
/// </summary>
|
||||
public bool HasTenthReward { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 第10次奖励金额(100-1000w)
|
||||
/// </summary>
|
||||
public long? TenthRewardAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 备注信息
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 500, IsNullable = true)]
|
||||
public string? Remark { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 已翻牌的顺序(存储用户实际翻牌的序号列表,如[3,7,1,5]表示依次翻了3号、7号、1号、5号牌)
|
||||
/// </summary>
|
||||
[SugarColumn(IsJson = true, IsNullable = true)]
|
||||
public List<int>? FlippedOrder { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 增加翻牌次数
|
||||
/// </summary>
|
||||
/// <param name="flipType">翻牌类型</param>
|
||||
public void IncrementFlip(FlipType flipType)
|
||||
{
|
||||
TotalFlips++;
|
||||
|
||||
switch (flipType)
|
||||
{
|
||||
case FlipType.Free:
|
||||
FreeFlipsUsed++;
|
||||
break;
|
||||
case FlipType.Bonus:
|
||||
BonusFlipsUsed++;
|
||||
break;
|
||||
case FlipType.Invite:
|
||||
InviteFlipsUsed++;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!IsFirstFlipDone)
|
||||
{
|
||||
IsFirstFlipDone = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录第8次奖励
|
||||
/// </summary>
|
||||
/// <param name="amount">奖励金额</param>
|
||||
public void SetEighthReward(long amount)
|
||||
{
|
||||
HasEighthReward = true;
|
||||
EighthRewardAmount = amount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录第9次奖励
|
||||
/// </summary>
|
||||
/// <param name="amount">奖励金额</param>
|
||||
public void SetNinthReward(long amount)
|
||||
{
|
||||
HasNinthReward = true;
|
||||
NinthRewardAmount = amount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录第10次奖励
|
||||
/// </summary>
|
||||
/// <param name="amount">奖励金额</param>
|
||||
public void SetTenthReward(long amount)
|
||||
{
|
||||
HasTenthReward = true;
|
||||
TenthRewardAmount = amount;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 翻牌类型枚举
|
||||
/// </summary>
|
||||
public enum FlipType
|
||||
{
|
||||
/// <summary>
|
||||
/// 免费翻牌(1-7次)
|
||||
/// </summary>
|
||||
Free = 0,
|
||||
|
||||
/// <summary>
|
||||
/// 赠送翻牌(已废弃)
|
||||
/// </summary>
|
||||
Bonus = 1,
|
||||
|
||||
/// <summary>
|
||||
/// 邀请解锁翻牌(8-10次)
|
||||
/// </summary>
|
||||
Invite = 2
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using SqlSugar;
|
||||
using Volo.Abp.Domain.Entities.Auditing;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// 邀请记录
|
||||
/// </summary>
|
||||
[SugarTable("Ai_InvitationRecord")]
|
||||
[SugarIndex($"index_{nameof(InviterId)}_{nameof(InvitedUserId)}",
|
||||
nameof(InviterId), OrderByType.Asc,
|
||||
nameof(InvitedUserId), OrderByType.Asc)]
|
||||
[SugarIndex($"index_{nameof(InvitedUserId)}", nameof(InvitedUserId), OrderByType.Asc)]
|
||||
public class InvitationRecordAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||
{
|
||||
public InvitationRecordAggregateRoot()
|
||||
{
|
||||
}
|
||||
|
||||
public InvitationRecordAggregateRoot(Guid inviterId, Guid invitedUserId, string inviteCode)
|
||||
{
|
||||
InviterId = inviterId;
|
||||
InvitedUserId = invitedUserId;
|
||||
InviteCode = inviteCode;
|
||||
InvitationTime = DateTime.Now;
|
||||
Status = InvitationStatus.Valid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 邀请人ID
|
||||
/// </summary>
|
||||
public Guid InviterId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 被邀请人ID
|
||||
/// </summary>
|
||||
public Guid InvitedUserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 使用的邀请码
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 50)]
|
||||
public string InviteCode { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 邀请时间
|
||||
/// </summary>
|
||||
public DateTime InvitationTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 邀请状态(0=有效,1=已撤销)
|
||||
/// </summary>
|
||||
public InvitationStatus Status { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 备注信息
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 500, IsNullable = true)]
|
||||
public string? Remark { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 邀请状态枚举
|
||||
/// </summary>
|
||||
public enum InvitationStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// 有效
|
||||
/// </summary>
|
||||
Valid = 0,
|
||||
|
||||
/// <summary>
|
||||
/// 已撤销
|
||||
/// </summary>
|
||||
Revoked = 1
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using SqlSugar;
|
||||
using Volo.Abp.Domain.Entities.Auditing;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// 用户邀请码
|
||||
/// </summary>
|
||||
[SugarTable("Ai_InviteCode")]
|
||||
[SugarIndex($"index_{nameof(UserId)}", nameof(UserId), OrderByType.Asc, true)]
|
||||
[SugarIndex($"index_{nameof(Code)}", nameof(Code), OrderByType.Asc, true)]
|
||||
public class InviteCodeAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||
{
|
||||
public InviteCodeAggregateRoot()
|
||||
{
|
||||
}
|
||||
|
||||
public InviteCodeAggregateRoot(Guid userId, string code)
|
||||
{
|
||||
UserId = userId;
|
||||
Code = code;
|
||||
IsUsed = false;
|
||||
IsUserInvited = false;
|
||||
UsedCount = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户ID(邀请码拥有者)
|
||||
/// </summary>
|
||||
public Guid UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 邀请码(唯一)
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 50)]
|
||||
public string Code { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 是否已被使用(一个邀请码只能被使用一次)
|
||||
/// </summary>
|
||||
public bool IsUsed { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 邀请码拥有者是否已被他人邀请(被邀请后不可再提供邀请码)
|
||||
/// </summary>
|
||||
public bool IsUserInvited { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 被使用次数(统计用)
|
||||
/// </summary>
|
||||
public int UsedCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 使用时间
|
||||
/// </summary>
|
||||
public DateTime? UsedTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 使用人ID
|
||||
/// </summary>
|
||||
public Guid? UsedByUserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 备注信息
|
||||
/// </summary>
|
||||
[SugarColumn(Length = 500, IsNullable = true)]
|
||||
public string? Remark { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 标记邀请码已被使用
|
||||
/// </summary>
|
||||
/// <param name="usedByUserId">使用者ID</param>
|
||||
public void MarkAsUsed(Guid usedByUserId)
|
||||
{
|
||||
IsUsed = true;
|
||||
UsedTime = DateTime.Now;
|
||||
UsedByUserId = usedByUserId;
|
||||
UsedCount++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标记用户已被邀请
|
||||
/// </summary>
|
||||
public void MarkUserAsInvited()
|
||||
{
|
||||
IsUserInvited = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,346 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
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 CardFlipManager : DomainService
|
||||
{
|
||||
private readonly ISqlSugarRepository<CardFlipTaskAggregateRoot> _cardFlipTaskRepository;
|
||||
private readonly ISqlSugarRepository<InvitationRecordAggregateRoot> _invitationRecordRepository;
|
||||
private readonly InviteCodeManager _inviteCodeManager;
|
||||
private readonly ILogger<CardFlipManager> _logger;
|
||||
|
||||
// 翻牌规则配置
|
||||
public const int MAX_FREE_FLIPS = 7; // 免费翻牌次数
|
||||
public const int MAX_INVITE_FLIPS = 3; // 邀请解锁翻牌次数
|
||||
public const int TOTAL_MAX_FLIPS = 10; // 总最大翻牌次数
|
||||
|
||||
private const int EIGHTH_FLIP = 8; // 第8次翻牌
|
||||
private const int NINTH_FLIP = 9; // 第9次翻牌
|
||||
private const int TENTH_FLIP = 10; // 第10次翻牌
|
||||
|
||||
private const long EIGHTH_MIN_REWARD = 1000000; // 第8次最小奖励 100w
|
||||
private const long EIGHTH_MAX_REWARD = 3000000; // 第8次最大奖励 300w
|
||||
private const long NINTH_MIN_REWARD = 1000000; // 第9次最小奖励 100w
|
||||
private const long NINTH_MAX_REWARD = 5000000; // 第9次最大奖励 500w
|
||||
private const long TENTH_MIN_REWARD = 1000000; // 第10次最小奖励 100w
|
||||
private const long TENTH_MAX_REWARD = 10000000; // 第10次最大奖励 1000w
|
||||
|
||||
public CardFlipManager(
|
||||
ISqlSugarRepository<CardFlipTaskAggregateRoot> cardFlipTaskRepository,
|
||||
ISqlSugarRepository<InvitationRecordAggregateRoot> invitationRecordRepository,
|
||||
InviteCodeManager inviteCodeManager,
|
||||
ILogger<CardFlipManager> logger)
|
||||
{
|
||||
_cardFlipTaskRepository = cardFlipTaskRepository;
|
||||
_invitationRecordRepository = invitationRecordRepository;
|
||||
_inviteCodeManager = inviteCodeManager;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或创建本周任务
|
||||
/// </summary>
|
||||
public async Task<CardFlipTaskAggregateRoot?> GetOrCreateWeeklyTaskAsync(
|
||||
Guid userId,
|
||||
DateTime weekStart,
|
||||
bool createIfNotExists)
|
||||
{
|
||||
var task = await _cardFlipTaskRepository._DbQueryable
|
||||
.Where(x => x.UserId == userId && x.WeekStartDate == weekStart)
|
||||
.FirstAsync();
|
||||
|
||||
if (task == null && createIfNotExists)
|
||||
{
|
||||
task = new CardFlipTaskAggregateRoot(userId, weekStart);
|
||||
await _cardFlipTaskRepository.InsertAsync(task);
|
||||
}
|
||||
|
||||
return task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取已翻牌的顺序列表
|
||||
/// </summary>
|
||||
public List<int> GetFlippedOrder(CardFlipTaskAggregateRoot task)
|
||||
{
|
||||
return task.FlippedOrder ?? new List<int>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行翻牌逻辑
|
||||
/// </summary>
|
||||
/// <param name="userId">用户ID</param>
|
||||
/// <param name="flipNumber">翻牌序号</param>
|
||||
/// <param name="weekStart">本周开始日期</param>
|
||||
/// <returns>翻牌结果</returns>
|
||||
public async Task<FlipResult> ExecuteFlipAsync(Guid userId, int flipNumber, DateTime weekStart)
|
||||
{
|
||||
// 验证翻牌序号
|
||||
if (flipNumber < 1 || flipNumber > TOTAL_MAX_FLIPS)
|
||||
{
|
||||
throw new UserFriendlyException($"翻牌序号必须在1-{TOTAL_MAX_FLIPS}之间");
|
||||
}
|
||||
|
||||
// 获取或创建本周任务
|
||||
var task = await GetOrCreateWeeklyTaskAsync(userId, weekStart, createIfNotExists: true);
|
||||
|
||||
// 验证翻牌次数
|
||||
if (task.TotalFlips >= TOTAL_MAX_FLIPS)
|
||||
{
|
||||
throw new UserFriendlyException("本周翻牌次数已用完,请下周再来!");
|
||||
}
|
||||
|
||||
// 验证该牌是否已经翻过
|
||||
var flippedOrder = GetFlippedOrder(task);
|
||||
if (flippedOrder.Contains(flipNumber))
|
||||
{
|
||||
throw new UserFriendlyException($"第 {flipNumber} 号牌已经翻过了!");
|
||||
}
|
||||
|
||||
// 判断翻牌类型
|
||||
var flipType = DetermineFlipType(task);
|
||||
|
||||
// 验证是否有足够的次数
|
||||
if (!CanUseFlipType(task, flipType))
|
||||
{
|
||||
throw new UserFriendlyException(GetFlipTypeErrorMessage(flipType));
|
||||
}
|
||||
|
||||
// 如果是邀请类型翻牌,必须验证用户本周填写的邀请码数量足够
|
||||
if (flipType == FlipType.Invite)
|
||||
{
|
||||
// 查询本周已使用的邀请码数量
|
||||
var weeklyInviteCodeUsedCount = await _invitationRecordRepository._DbQueryable
|
||||
.Where(x => x.InvitedUserId == userId)
|
||||
.Where(x => x.InvitationTime >= weekStart)
|
||||
.CountAsync();
|
||||
|
||||
// 本周填写的邀请码数量必须 >= 即将使用的邀请翻牌次数
|
||||
// 例如: 要翻第8次(InviteFlipsUsed=0->1), 需要至少填写了1个邀请码
|
||||
// 要翻第9次(InviteFlipsUsed=1->2), 需要至少填写了2个邀请码
|
||||
// 要翻第10次(InviteFlipsUsed=2->3), 需要至少填写了3个邀请码
|
||||
var requiredInviteCodeCount = task.InviteFlipsUsed + 1;
|
||||
if (weeklyInviteCodeUsedCount < requiredInviteCodeCount)
|
||||
{
|
||||
throw new UserFriendlyException($"需本周累积使用{requiredInviteCodeCount}个他人邀请码才能解锁第{task.TotalFlips + 1}次翻牌,您还差一个~");
|
||||
}
|
||||
}
|
||||
|
||||
// 计算翻牌结果(基于当前是第几次翻牌,而不是卡片序号)
|
||||
var flipCount = task.TotalFlips + 1; // 当前这次翻牌是第几次
|
||||
var result = CalculateFlipResult(flipCount);
|
||||
|
||||
// 将卡片序号信息也返回
|
||||
result.FlipNumber = flipNumber;
|
||||
|
||||
// 更新翻牌次数(必须在记录奖励之前,因为需要先确定是第几次)
|
||||
task.IncrementFlip(flipType);
|
||||
|
||||
// 记录翻牌顺序
|
||||
if (task.FlippedOrder == null)
|
||||
{
|
||||
task.FlippedOrder = new List<int>();
|
||||
}
|
||||
task.FlippedOrder.Add(flipNumber);
|
||||
|
||||
// 如果中奖,记录奖励金额(用于后续查询显示)
|
||||
if (result.IsWin)
|
||||
{
|
||||
if (flipCount == EIGHTH_FLIP)
|
||||
{
|
||||
task.SetEighthReward(result.RewardAmount);
|
||||
}
|
||||
else if (flipCount == NINTH_FLIP)
|
||||
{
|
||||
task.SetNinthReward(result.RewardAmount);
|
||||
}
|
||||
else if (flipCount == TENTH_FLIP)
|
||||
{
|
||||
task.SetTenthReward(result.RewardAmount);
|
||||
}
|
||||
}
|
||||
|
||||
await _cardFlipTaskRepository.UpdateAsync(task);
|
||||
|
||||
_logger.LogInformation($"用户 {userId} 完成第 {flipNumber} 次翻牌,中奖:{result.IsWin}");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否可以翻牌
|
||||
/// </summary>
|
||||
public bool CanFlipCard(CardFlipTaskAggregateRoot? task)
|
||||
{
|
||||
if (task == null) return true; // 没有任务记录,可以开始翻牌
|
||||
|
||||
return task.TotalFlips < TOTAL_MAX_FLIPS;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断翻牌类型
|
||||
/// </summary>
|
||||
public FlipType DetermineFlipType(CardFlipTaskAggregateRoot task)
|
||||
{
|
||||
if (task.FreeFlipsUsed < MAX_FREE_FLIPS)
|
||||
{
|
||||
return FlipType.Free;
|
||||
}
|
||||
else
|
||||
{
|
||||
return FlipType.Invite;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否可以使用该翻牌类型
|
||||
/// </summary>
|
||||
public bool CanUseFlipType(CardFlipTaskAggregateRoot task, FlipType flipType)
|
||||
{
|
||||
return flipType switch
|
||||
{
|
||||
FlipType.Free => task.FreeFlipsUsed < MAX_FREE_FLIPS,
|
||||
FlipType.Invite => task.InviteFlipsUsed < MAX_INVITE_FLIPS,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算翻牌结果
|
||||
/// </summary>
|
||||
/// <param name="flipCount">第几次翻牌(1-10)</param>
|
||||
private FlipResult CalculateFlipResult(int flipCount)
|
||||
{
|
||||
var result = new FlipResult
|
||||
{
|
||||
FlipNumber = 0, // 稍后会被设置为实际的卡片序号
|
||||
IsWin = false
|
||||
};
|
||||
|
||||
// 前7次固定失败
|
||||
if (flipCount <= 7)
|
||||
{
|
||||
result.IsWin = false;
|
||||
result.RewardDesc = "很遗憾,未中奖";
|
||||
}
|
||||
// 第8次中奖 (邀请码解锁)
|
||||
else if (flipCount == EIGHTH_FLIP)
|
||||
{
|
||||
var rewardAmount = GenerateRandomReward(EIGHTH_MIN_REWARD, EIGHTH_MAX_REWARD);
|
||||
result.IsWin = true;
|
||||
result.RewardAmount = rewardAmount;
|
||||
result.RewardDesc = $"恭喜获得尊享包 {rewardAmount / 10000}w tokens!";
|
||||
}
|
||||
// 第9次中奖 (邀请码解锁)
|
||||
else if (flipCount == NINTH_FLIP)
|
||||
{
|
||||
var rewardAmount = GenerateRandomReward(NINTH_MIN_REWARD, NINTH_MAX_REWARD);
|
||||
result.IsWin = true;
|
||||
result.RewardAmount = rewardAmount;
|
||||
result.RewardDesc = $"恭喜获得尊享包 {rewardAmount / 10000}w tokens!";
|
||||
}
|
||||
// 第10次中奖 (邀请码解锁)
|
||||
else if (flipCount == TENTH_FLIP)
|
||||
{
|
||||
var rewardAmount = GenerateRandomReward(TENTH_MIN_REWARD, TENTH_MAX_REWARD);
|
||||
result.IsWin = true;
|
||||
result.RewardAmount = rewardAmount;
|
||||
result.RewardDesc = $"恭喜获得尊享包 {rewardAmount / 10000}w tokens!";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取翻牌类型错误提示
|
||||
/// </summary>
|
||||
private string GetFlipTypeErrorMessage(FlipType flipType)
|
||||
{
|
||||
return flipType switch
|
||||
{
|
||||
FlipType.Free => "免费翻牌次数已用完",
|
||||
FlipType.Invite => "需要使用邀请码解锁更多次数",
|
||||
_ => "无法翻牌"
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成随机奖励金额 (最小单位100w)
|
||||
/// </summary>
|
||||
private long GenerateRandomReward(long min, long max)
|
||||
{
|
||||
var random = new Random();
|
||||
const long unit = 1000000; // 100w的单位
|
||||
|
||||
// 将min和max转换为100w的倍数
|
||||
long minUnits = min / unit;
|
||||
long maxUnits = max / unit;
|
||||
|
||||
// 在倍数范围内随机
|
||||
long randomUnits = random.Next((int)minUnits, (int)maxUnits + 1);
|
||||
|
||||
// 返回100w的倍数
|
||||
return randomUnits * unit;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取本周开始日期(周一)
|
||||
/// </summary>
|
||||
public static DateTime GetWeekStartDate(DateTime date)
|
||||
{
|
||||
var dayOfWeek = (int)date.DayOfWeek;
|
||||
// 将周日(0)转换为7
|
||||
if (dayOfWeek == 0) dayOfWeek = 7;
|
||||
|
||||
// 计算本周一的日期
|
||||
var monday = date.Date.AddDays(-(dayOfWeek - 1));
|
||||
return monday;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取翻牌类型描述
|
||||
/// </summary>
|
||||
public static string GetFlipTypeDesc(int flipNumber)
|
||||
{
|
||||
if (flipNumber <= MAX_FREE_FLIPS)
|
||||
{
|
||||
return "免费";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "邀请解锁";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 翻牌结果
|
||||
/// </summary>
|
||||
public class FlipResult
|
||||
{
|
||||
/// <summary>
|
||||
/// 翻牌序号
|
||||
/// </summary>
|
||||
public int FlipNumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否中奖
|
||||
/// </summary>
|
||||
public bool IsWin { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 奖励金额
|
||||
/// </summary>
|
||||
public long RewardAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 奖励描述
|
||||
/// </summary>
|
||||
public string RewardDesc { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
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.InvitedUserId == 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 weekStart = CardFlipManager.GetWeekStartDate(DateTime.Now);
|
||||
var weeklyUseCount = await _invitationRecordRepository._DbQueryable
|
||||
.Where(x => x.InvitedUserId == userId)
|
||||
.Where(x => x.InvitationTime >= weekStart)
|
||||
.CountAsync();
|
||||
|
||||
if (weeklyUseCount >= CardFlipManager.MAX_INVITE_FLIPS)
|
||||
{
|
||||
throw new UserFriendlyException($"本周邀请码使用次数已达上限({CardFlipManager.MAX_INVITE_FLIPS}次),请下周再来");
|
||||
}
|
||||
|
||||
// 检查当前用户的邀请码信息
|
||||
var myInviteCode = await _inviteCodeRepository._DbQueryable
|
||||
.Where(x => x.UserId == userId)
|
||||
.FirstAsync();
|
||||
|
||||
// 标记邀请码为已使用
|
||||
inviteCodeEntity.MarkAsUsed(userId);
|
||||
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,
|
||||
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