Merge branch 'card-flip' into ai-hub
This commit is contained in:
@@ -14,4 +14,9 @@ public class AnnouncementCacheDto
|
||||
/// 公告日志列表
|
||||
/// </summary>
|
||||
public List<AnnouncementLogDto> Logs { get; set; } = new List<AnnouncementLogDto>();
|
||||
|
||||
/// <summary>
|
||||
/// 跳转链接
|
||||
/// </summary>
|
||||
public string? Url { get; set; }
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||
|
||||
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement;
|
||||
|
||||
/// <summary>
|
||||
@@ -5,18 +7,38 @@ namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement;
|
||||
/// </summary>
|
||||
public class AnnouncementLogDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 日期
|
||||
/// </summary>
|
||||
public string Date { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 标题
|
||||
/// </summary>
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 内容列表
|
||||
/// </summary>
|
||||
public List<string> Content { get; set; } = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// 图片url
|
||||
/// </summary>
|
||||
public string? ImageUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间(系统公告时间、活动开始时间)
|
||||
/// </summary>
|
||||
public DateTime StartTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 活动结束时间
|
||||
/// </summary>
|
||||
public DateTime? EndTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 公告类型(系统、活动)
|
||||
/// </summary>
|
||||
public AnnouncementTypeEnum Type{ get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 跳转链接
|
||||
/// </summary>
|
||||
public string? Url { get; set; }
|
||||
}
|
||||
|
||||
@@ -39,12 +39,7 @@ public class CardFlipStatusOutput
|
||||
/// 本周邀请人数
|
||||
/// </summary>
|
||||
public int InvitedCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已被邀请(被邀请后不可再提供邀请码)
|
||||
/// </summary>
|
||||
public bool IsInvited { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 翻牌记录
|
||||
/// </summary>
|
||||
@@ -54,6 +49,11 @@ public class CardFlipStatusOutput
|
||||
/// 下次可翻牌提示
|
||||
/// </summary>
|
||||
public string? NextFlipTip { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前用户是否已经填写过邀请码
|
||||
/// </summary>
|
||||
public bool IsFilledInviteCode { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using Volo.Abp.Application.Dtos;
|
||||
using Yi.Framework.Ddd.Application.Contracts;
|
||||
|
||||
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.UsageStatistics;
|
||||
|
||||
public class PremiumTokenUsageGetListInput : PagedAllResultRequestDto
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否免费
|
||||
/// </summary>
|
||||
public bool? IsFree { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using Volo.Abp.Application.Dtos;
|
||||
using Yi.Framework.Ddd.Application.Contracts;
|
||||
|
||||
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.UsageStatistics;
|
||||
|
||||
public class PremiumTokenUsageGetListOutput : CreationAuditedEntityDto
|
||||
{
|
||||
/// <summary>
|
||||
/// id
|
||||
/// </summary>
|
||||
public Guid Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户ID
|
||||
/// </summary>
|
||||
public Guid UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 包名称
|
||||
/// </summary>
|
||||
public string PackageName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 总用量(总token数)
|
||||
/// </summary>
|
||||
public long TotalTokens { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 剩余用量(剩余token数)
|
||||
/// </summary>
|
||||
public long RemainingTokens { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 已使用token数
|
||||
/// </summary>
|
||||
public long UsedTokens { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 到期时间
|
||||
/// </summary>
|
||||
public DateTime? ExpireDateTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否激活
|
||||
/// </summary>
|
||||
public bool IsActive { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 购买金额
|
||||
/// </summary>
|
||||
public decimal PurchaseAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
public string? Remark { get; set; }
|
||||
}
|
||||
@@ -11,5 +11,5 @@ public interface IAnnouncementService
|
||||
/// 获取公告信息
|
||||
/// </summary>
|
||||
/// <returns>公告信息</returns>
|
||||
Task<AnnouncementOutput> GetAsync();
|
||||
Task<List<AnnouncementLogDto>> GetAsync();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Mapster;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Volo.Abp.Application.Services;
|
||||
@@ -14,13 +15,13 @@ namespace Yi.Framework.AiHub.Application.Services;
|
||||
/// </summary>
|
||||
public class AnnouncementService : ApplicationService, IAnnouncementService
|
||||
{
|
||||
private readonly ISqlSugarRepository<AnnouncementLogAggregateRoot> _announcementRepository;
|
||||
private readonly ISqlSugarRepository<AnnouncementAggregateRoot> _announcementRepository;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IDistributedCache<AnnouncementCacheDto> _announcementCache;
|
||||
private const string AnnouncementCacheKey = "AiHub:Announcement";
|
||||
|
||||
public AnnouncementService(
|
||||
ISqlSugarRepository<AnnouncementLogAggregateRoot> announcementRepository,
|
||||
ISqlSugarRepository<AnnouncementAggregateRoot> announcementRepository,
|
||||
IConfiguration configuration,
|
||||
IDistributedCache<AnnouncementCacheDto> announcementCache)
|
||||
{
|
||||
@@ -32,7 +33,7 @@ public class AnnouncementService : ApplicationService, IAnnouncementService
|
||||
/// <summary>
|
||||
/// 获取公告信息
|
||||
/// </summary>
|
||||
public async Task<AnnouncementOutput> GetAsync()
|
||||
public async Task<List<AnnouncementLogDto>> GetAsync()
|
||||
{
|
||||
// 使用 GetOrAddAsync 从缓存获取或添加数据,缓存1小时
|
||||
var cacheData = await _announcementCache.GetOrAddAsync(
|
||||
@@ -44,11 +45,7 @@ public class AnnouncementService : ApplicationService, IAnnouncementService
|
||||
}
|
||||
);
|
||||
|
||||
return new AnnouncementOutput
|
||||
{
|
||||
Version = cacheData?.Version ?? "v1.0.0",
|
||||
Logs = cacheData?.Logs ?? new List<AnnouncementLogDto>()
|
||||
};
|
||||
return cacheData?.Logs ?? new List<AnnouncementLogDto>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -56,25 +53,15 @@ public class AnnouncementService : ApplicationService, IAnnouncementService
|
||||
/// </summary>
|
||||
private async Task<AnnouncementCacheDto> LoadAnnouncementDataAsync()
|
||||
{
|
||||
// 从配置文件读取版本号,如果没有配置则使用默认值
|
||||
var version = _configuration["AiHubVersion"] ?? "v1.0.0";
|
||||
|
||||
// 查询所有公告日志,按日期降序排列
|
||||
var logs = await _announcementRepository._DbQueryable
|
||||
.OrderByDescending(x => x.Date)
|
||||
.OrderByDescending(x => x.StartTime)
|
||||
.ToListAsync();
|
||||
|
||||
// 转换为 DTO
|
||||
var logDtos = logs.Select(log => new AnnouncementLogDto
|
||||
{
|
||||
Date = log.Date.ToString("yyyy-MM-dd"),
|
||||
Title = log.Title,
|
||||
Content = log.Content
|
||||
}).ToList();
|
||||
|
||||
var logDtos = logs.Adapt<List<AnnouncementLogDto>>();
|
||||
return new AnnouncementCacheDto
|
||||
{
|
||||
Version = version,
|
||||
Logs = logDtos
|
||||
};
|
||||
}
|
||||
|
||||
@@ -51,9 +51,8 @@ public class CardFlipService : ApplicationService, ICardFlipService
|
||||
// 统计本周邀请人数
|
||||
var invitedCount = await _inviteCodeManager.GetWeeklyInvitationCountAsync(userId, weekStart);
|
||||
|
||||
// 检查用户是否已被邀请
|
||||
var isInvited = await _inviteCodeManager.IsUserInvitedAsync(userId);
|
||||
|
||||
//当前用户是否已填写过邀请码
|
||||
var isFilledInviteCode = await _inviteCodeManager.IsFilledInviteCodeAsync(userId);
|
||||
var output = new CardFlipStatusOutput
|
||||
{
|
||||
TotalFlips = task?.TotalFlips ?? 0,
|
||||
@@ -63,8 +62,8 @@ public class CardFlipService : ApplicationService, ICardFlipService
|
||||
CanFlip = _cardFlipManager.CanFlipCard(task),
|
||||
MyInviteCode = inviteCode?.Code,
|
||||
InvitedCount = invitedCount,
|
||||
IsInvited = isInvited,
|
||||
FlipRecords = BuildFlipRecords(task)
|
||||
FlipRecords = BuildFlipRecords(task),
|
||||
IsFilledInviteCode = isFilledInviteCode
|
||||
};
|
||||
|
||||
// 生成提示信息
|
||||
@@ -87,7 +86,7 @@ public class CardFlipService : ApplicationService, ICardFlipService
|
||||
// 如果中奖,发放奖励
|
||||
if (result.IsWin)
|
||||
{
|
||||
await GrantRewardAsync(userId, result.RewardAmount, $"翻牌活动第{input.FlipNumber}次中奖");
|
||||
await GrantRewardAsync(userId, result.RewardAmount, $"翻牌活动-序号{input.FlipNumber}中奖");
|
||||
}
|
||||
|
||||
// 构建输出
|
||||
@@ -147,7 +146,6 @@ public class CardFlipService : ApplicationService, ICardFlipService
|
||||
{
|
||||
MyInviteCode = inviteCode?.Code,
|
||||
InvitedCount = invitedCount,
|
||||
IsInvited = inviteCode?.IsUserInvited ?? false,
|
||||
InvitationHistory = invitationHistory.Select(x => new InvitationHistoryItem
|
||||
{
|
||||
InvitedUserName = x.InvitedUserName,
|
||||
@@ -183,6 +181,9 @@ public class CardFlipService : ApplicationService, ICardFlipService
|
||||
var flippedOrder = task != null ? _cardFlipManager.GetFlippedOrder(task) : new List<int>();
|
||||
var flippedNumbers = new HashSet<int>(flippedOrder);
|
||||
|
||||
// 获取中奖记录
|
||||
var winRecords = task?.WinRecords ?? new Dictionary<int, long>();
|
||||
|
||||
// 构建记录,按照原始序号1-10排列
|
||||
for (int i = 1; i <= CardFlipManager.TOTAL_MAX_FLIPS; i++)
|
||||
{
|
||||
@@ -202,23 +203,11 @@ public class CardFlipService : ApplicationService, ICardFlipService
|
||||
{
|
||||
var flipOrderIndex = flippedOrder.IndexOf(i) + 1; // 第几次翻的(1-based)
|
||||
|
||||
// 第8次翻的卡中奖
|
||||
if (flipOrderIndex == 8)
|
||||
// 检查这次翻牌是否中奖
|
||||
if (winRecords.TryGetValue(flipOrderIndex, out var rewardAmount))
|
||||
{
|
||||
record.IsWin = true;
|
||||
record.RewardAmount = task.EighthRewardAmount;
|
||||
}
|
||||
// 第9次翻的卡中奖
|
||||
else if (flipOrderIndex == 9)
|
||||
{
|
||||
record.IsWin = true;
|
||||
record.RewardAmount = task.NinthRewardAmount;
|
||||
}
|
||||
// 第10次翻的卡中奖
|
||||
else if (flipOrderIndex == 10)
|
||||
{
|
||||
record.IsWin = true;
|
||||
record.RewardAmount = task.TenthRewardAmount;
|
||||
record.RewardAmount = rewardAmount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,10 +235,10 @@ public class CardFlipService : ApplicationService, ICardFlipService
|
||||
{
|
||||
if (status.TotalFlips >= 7)
|
||||
{
|
||||
return $"本周使用他人邀请码可解锁{status.RemainingInviteFlips}次翻牌,且必中大奖!每次中奖最大额度将翻倍!";
|
||||
return $"本周使用他人邀请码或他人使用你的邀请码,可解锁{status.RemainingInviteFlips}次翻牌,且必中大奖!每次中奖最大额度将翻倍!";
|
||||
}
|
||||
|
||||
return $"本周使用他人邀请码可解锁{status.RemainingInviteFlips}次翻牌,必中大奖!每次中奖最大额度将翻倍!";
|
||||
return $"本周使用他人邀请码或他人使用你的邀请码,可解锁{status.RemainingInviteFlips}次翻牌,必中大奖!每次中奖最大额度将翻倍!";
|
||||
}
|
||||
|
||||
return "继续加油!";
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SqlSugar;
|
||||
using Volo.Abp.Application.Dtos;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Volo.Abp.Users;
|
||||
using Yi.Framework.AiHub.Application.Contracts.Dtos.UsageStatistics;
|
||||
@@ -7,6 +10,7 @@ using Yi.Framework.AiHub.Application.Contracts.IServices;
|
||||
using Yi.Framework.AiHub.Domain.Entities;
|
||||
using Yi.Framework.AiHub.Domain.Entities.Chat;
|
||||
using Yi.Framework.AiHub.Domain.Extensions;
|
||||
using Yi.Framework.Ddd.Application.Contracts;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.AiHub.Application.Services;
|
||||
@@ -137,4 +141,27 @@ public class UsageStatisticsService : ApplicationService, IUsageStatisticsServic
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前用户尊享服务token用量统计列表
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("usage-statistics/premium-token-usage/list")]
|
||||
public async Task<PagedResultDto<PremiumTokenUsageGetListOutput>> GetPremiumTokenUsageListAsync(
|
||||
PremiumTokenUsageGetListInput input)
|
||||
{
|
||||
var userId = CurrentUser.GetId();
|
||||
RefAsync<int> total = 0;
|
||||
// 获取尊享包Token信息
|
||||
var entities = await _premiumPackageRepository._DbQueryable
|
||||
.Where(x => x.UserId == userId)
|
||||
.WhereIF(input.IsFree == false, x => x.PurchaseAmount > 0)
|
||||
.WhereIF(input.IsFree == true, x => x.PurchaseAmount == 0)
|
||||
.WhereIF(input.StartTime is not null && input.EndTime is not null,
|
||||
x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime)
|
||||
.OrderByDescending(x => x.CreationTime)
|
||||
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
|
||||
return new PagedResultDto<PremiumTokenUsageGetListOutput>(total,
|
||||
entities.Adapt<List<PremiumTokenUsageGetListOutput>>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||
|
||||
public enum AnnouncementTypeEnum
|
||||
{
|
||||
Activity=1,
|
||||
System=2
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using SqlSugar;
|
||||
using Volo.Abp.Domain.Entities.Auditing;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// 公告日志
|
||||
/// </summary>
|
||||
[SugarTable("Ai_Announcement")]
|
||||
public class AnnouncementAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||
{
|
||||
public AnnouncementAggregateRoot()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标题
|
||||
/// </summary>
|
||||
public string Title { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 内容列表(JSON格式存储)
|
||||
/// </summary>
|
||||
[SugarColumn(IsJson = true, IsNullable = false)]
|
||||
public List<string> Content { get; set; } = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
public string? Remark { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 图片url
|
||||
/// </summary>
|
||||
public string? ImageUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间(系统公告时间、活动开始时间)
|
||||
/// </summary>
|
||||
public DateTime StartTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 活动结束时间
|
||||
/// </summary>
|
||||
public DateTime? EndTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 公告类型(系统、活动)
|
||||
/// </summary>
|
||||
public AnnouncementTypeEnum Type{ get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 跳转链接
|
||||
/// </summary>
|
||||
public string? Url { get; set; }
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
using SqlSugar;
|
||||
using Volo.Abp.Domain.Entities.Auditing;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// 公告日志
|
||||
/// </summary>
|
||||
[SugarTable("Ai_AnnouncementLog")]
|
||||
[SugarIndex($"index_{nameof(Date)}", nameof(Date), OrderByType.Desc)]
|
||||
public class AnnouncementLogAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||
{
|
||||
public AnnouncementLogAggregateRoot()
|
||||
{
|
||||
}
|
||||
|
||||
public AnnouncementLogAggregateRoot(DateTime date, string title, List<string> content)
|
||||
{
|
||||
Date = date;
|
||||
Title = title;
|
||||
Content = content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 日期
|
||||
/// </summary>
|
||||
public DateTime Date { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 标题
|
||||
/// </summary>
|
||||
public string Title { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 内容列表(JSON格式存储)
|
||||
/// </summary>
|
||||
[SugarColumn(IsJson = true, IsNullable = false)]
|
||||
public List<string> Content { get; set; } = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
public string? Remark { get; set; }
|
||||
}
|
||||
@@ -25,9 +25,8 @@ public class CardFlipTaskAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||
BonusFlipsUsed = 0;
|
||||
InviteFlipsUsed = 0;
|
||||
IsFirstFlipDone = false;
|
||||
HasNinthReward = false;
|
||||
HasTenthReward = false;
|
||||
FlippedOrder = new List<int>();
|
||||
WinRecords = new Dictionary<int, long>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -66,34 +65,10 @@ public class CardFlipTaskAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||
public bool IsFirstFlipDone { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已获得第8次奖励
|
||||
/// 中奖记录(以翻牌顺序为key,例如第1次翻牌中奖则key为1,奖励金额为value)
|
||||
/// </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; }
|
||||
[SugarColumn(IsJson = true, IsNullable = true)]
|
||||
public Dictionary<int, long>? WinRecords { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 备注信息
|
||||
@@ -135,33 +110,17 @@ public class CardFlipTaskAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 记录第8次奖励
|
||||
/// 记录中奖
|
||||
/// </summary>
|
||||
/// <param name="flipCount">第几次翻牌(1-10)</param>
|
||||
/// <param name="amount">奖励金额</param>
|
||||
public void SetEighthReward(long amount)
|
||||
public void SetWinReward(int flipCount, 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;
|
||||
if (WinRecords == null)
|
||||
{
|
||||
WinRecords = new Dictionary<int, long>();
|
||||
}
|
||||
WinRecords[flipCount] = amount;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,8 +19,6 @@ public class InviteCodeAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||
{
|
||||
UserId = userId;
|
||||
Code = code;
|
||||
IsUsed = false;
|
||||
IsUserInvited = false;
|
||||
UsedCount = 0;
|
||||
}
|
||||
|
||||
@@ -34,55 +32,16 @@ public class InviteCodeAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||
/// </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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,11 @@ public class CardFlipManager : DomainService
|
||||
private const int NINTH_FLIP = 9; // 第9次翻牌
|
||||
private const int TENTH_FLIP = 10; // 第10次翻牌
|
||||
|
||||
// 前7次免费翻牌奖励配置
|
||||
private const long FREE_MIN_REWARD = 10000; // 前7次最小奖励 1w
|
||||
private const long FREE_MAX_REWARD = 30000; // 前7次最大奖励 3w
|
||||
private const double FREE_WIN_RATE = 0.5; // 前7次中奖概率 50%
|
||||
|
||||
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
|
||||
@@ -112,23 +117,23 @@ public class CardFlipManager : DomainService
|
||||
throw new UserFriendlyException(GetFlipTypeErrorMessage(flipType));
|
||||
}
|
||||
|
||||
// 如果是邀请类型翻牌,必须验证用户本周填写的邀请码数量足够
|
||||
// 如果是邀请类型翻牌,必须验证用户本周的邀请记录数量足够(包括填写别人的邀请码和别人填写我的邀请码)
|
||||
if (flipType == FlipType.Invite)
|
||||
{
|
||||
// 查询本周已使用的邀请码数量
|
||||
var weeklyInviteCodeUsedCount = await _invitationRecordRepository._DbQueryable
|
||||
.Where(x => x.InvitedUserId == userId)
|
||||
// 查询本周作为邀请人或被邀请人的记录数量(双方都会增加翻牌次数)
|
||||
var weeklyInviteRecordCount = await _invitationRecordRepository._DbQueryable
|
||||
.Where(x => x.InviterId == userId || 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)
|
||||
// 本周邀请记录数量必须 >= 即将使用的邀请翻牌次数
|
||||
// 例如: 要翻第8次(InviteFlipsUsed=0->1), 需要至少有1条邀请记录(我邀请别人或别人邀请我)
|
||||
// 要翻第9次(InviteFlipsUsed=1->2), 需要至少有2条邀请记录
|
||||
// 要翻第10次(InviteFlipsUsed=2->3), 需要至少有3条邀请记录
|
||||
var requiredInviteRecordCount = task.InviteFlipsUsed + 1;
|
||||
if (weeklyInviteRecordCount < requiredInviteRecordCount)
|
||||
{
|
||||
throw new UserFriendlyException($"需本周累积使用{requiredInviteCodeCount}个他人邀请码才能解锁第{task.TotalFlips + 1}次翻牌,您还差一个~");
|
||||
throw new UserFriendlyException($"需本周累积{requiredInviteRecordCount}次邀请记录(填写别人的邀请码或别人填写你的邀请码)才能解锁第{task.TotalFlips + 1}次翻牌");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,18 +157,7 @@ public class CardFlipManager : DomainService
|
||||
// 如果中奖,记录奖励金额(用于后续查询显示)
|
||||
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);
|
||||
}
|
||||
task.SetWinReward(flipCount, result.RewardAmount);
|
||||
}
|
||||
|
||||
await _cardFlipTaskRepository.UpdateAsync(task);
|
||||
@@ -223,11 +217,24 @@ public class CardFlipManager : DomainService
|
||||
IsWin = false
|
||||
};
|
||||
|
||||
// 前7次固定失败
|
||||
var random = new Random();
|
||||
|
||||
// 前7次: 50%概率中奖,奖励1w-3w
|
||||
if (flipCount <= 7)
|
||||
{
|
||||
result.IsWin = false;
|
||||
result.RewardDesc = "很遗憾,未中奖";
|
||||
// 50%概率中奖
|
||||
if (random.NextDouble() < FREE_WIN_RATE)
|
||||
{
|
||||
var rewardAmount = GenerateRandomReward(FREE_MIN_REWARD, FREE_MAX_REWARD);
|
||||
result.IsWin = true;
|
||||
result.RewardAmount = rewardAmount;
|
||||
result.RewardDesc = $"恭喜获得尊享包 {rewardAmount / 10000m:0.##}w tokens!";
|
||||
}
|
||||
else
|
||||
{
|
||||
result.IsWin = false;
|
||||
result.RewardDesc = "很遗憾,未中奖";
|
||||
}
|
||||
}
|
||||
// 第8次中奖 (邀请码解锁)
|
||||
else if (flipCount == EIGHTH_FLIP)
|
||||
@@ -271,21 +278,24 @@ public class CardFlipManager : DomainService
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成随机奖励金额 (最小单位100w)
|
||||
/// 生成随机奖励金额
|
||||
/// </summary>
|
||||
private long GenerateRandomReward(long min, long max)
|
||||
{
|
||||
var random = new Random();
|
||||
const long unit = 1000000; // 100w的单位
|
||||
|
||||
// 将min和max转换为100w的倍数
|
||||
// 根据最小值判断单位
|
||||
// 如果min小于100000,则使用1w(10000)作为单位;否则使用100w(1000000)作为单位
|
||||
long unit = min < 100000 ? 10000 : 1000000;
|
||||
|
||||
// 将min和max转换为单位的倍数
|
||||
long minUnits = min / unit;
|
||||
long maxUnits = max / unit;
|
||||
|
||||
// 在倍数范围内随机
|
||||
long randomUnits = random.Next((int)minUnits, (int)maxUnits + 1);
|
||||
|
||||
// 返回100w的倍数
|
||||
// 返回单位的倍数
|
||||
return randomUnits * unit;
|
||||
}
|
||||
|
||||
|
||||
@@ -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>
|
||||
@@ -91,7 +96,7 @@ public class InviteCodeManager : DomainService
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用邀请码
|
||||
/// 使用邀请码(双方都增加翻牌次数)
|
||||
/// </summary>
|
||||
/// <param name="userId">使用者ID</param>
|
||||
/// <param name="inviteCode">邀请码</param>
|
||||
@@ -118,75 +123,40 @@ public class InviteCodeManager : DomainService
|
||||
{
|
||||
throw new UserFriendlyException("不能使用自己的邀请码");
|
||||
}
|
||||
|
||||
// 检查当前用户是否已经填写过别人的邀请码(一辈子只能填写一次)
|
||||
var hasUsedOthersCode = await IsFilledInviteCodeAsync(userId);
|
||||
|
||||
// 验证邀请码是否已被使用
|
||||
if (inviteCodeEntity.IsUsed)
|
||||
if (hasUsedOthersCode)
|
||||
{
|
||||
throw new UserFriendlyException("该邀请码已被使用");
|
||||
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);
|
||||
|
||||
// 增加邀请码被使用次数
|
||||
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,
|
||||
userId,
|
||||
inviteCode);
|
||||
await _invitationRecordRepository.InsertAsync(invitationRecord);
|
||||
|
||||
_logger.LogInformation($"用户 {userId} 使用邀请码 {inviteCode} 成功");
|
||||
_logger.LogInformation($"用户 {userId} 使用邀请码 {inviteCode} 成功,邀请人 {inviteCodeEntity.UserId} 和被邀请人 {userId} 都增加一次翻牌机会");
|
||||
|
||||
return inviteCodeEntity.UserId;
|
||||
}
|
||||
|
||||
/// <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>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace Yi.Framework.Rbac.Domain.Shared.Caches;
|
||||
namespace Yi.Framework.Rbac.Domain.Shared.Caches;
|
||||
|
||||
public class FileCacheItem
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user