feat: 完成积分、价值排行榜

This commit is contained in:
chenchun
2024-11-13 19:01:23 +08:00
parent f9217dc066
commit bf2bcd1395
14 changed files with 358 additions and 16 deletions

View File

@@ -1,4 +1,6 @@
using Volo.Abp.Domain.Services;
using Microsoft.Extensions.Caching.Distributed;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Services;
using Volo.Abp.Users;
using Yi.Framework.DigitalCollectibles.Domain.Entities;
using Yi.Framework.SqlSugarCore.Abstractions;
@@ -11,13 +13,20 @@ namespace Yi.Framework.DigitalCollectibles.Domain.Managers;
public class CollectiblesManager:DomainService
{
private readonly ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> _collectiblesUserStoreRepository;
public CollectiblesManager(ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> collectiblesUserStoreRepository)
private readonly ISqlSugarRepository<CollectiblesAggregateRoot> _collectiblesRepository;
private readonly IDistributedCache<List<(Guid userId, decimal value)>> _distributedCache;
public CollectiblesManager(ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> collectiblesUserStoreRepository, ISqlSugarRepository<CollectiblesAggregateRoot> collectiblesRepository, IDistributedCache<List<(Guid userId, decimal value)>> distributedCache)
{
_collectiblesUserStoreRepository = collectiblesUserStoreRepository;
_collectiblesRepository = collectiblesRepository;
_distributedCache = distributedCache;
}
/// <summary>
/// 获取某个用户的价值
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public async Task<decimal> GetAccountValueAsync(Guid userId)
{
var collectiblesList = await _collectiblesUserStoreRepository._DbQueryable
@@ -30,7 +39,14 @@ public class CollectiblesManager:DomainService
c.ValueNumber
}
).ToListAsync();
var groupBy = collectiblesList.GroupBy(x => x.Id);
var totalValue=ComputeValue(collectiblesList.Select(x=> (x.Id,x.ValueNumber)).ToList());
return totalValue;
}
//计算价值,需要每个藏品的唯一值和藏品的价值即可
private decimal ComputeValue(List<(Guid collectiblesId,decimal valueNumber)> data)
{
var groupBy = data.GroupBy(x => x.collectiblesId);
decimal totalValue = 0;
//首个价值百分之百后续每个只有百分之40最大10个
@@ -41,7 +57,7 @@ public class CollectiblesManager:DomainService
if (item.index == 0)
{
totalValue += item.value.ValueNumber;
totalValue += item.value.valueNumber;
}
else if (item.index == 10)
{
@@ -50,11 +66,44 @@ public class CollectiblesManager:DomainService
}
else
{
totalValue += item.value.ValueNumber * 0.4m;
totalValue += item.value.valueNumber * 0.4m;
}
}
}
return totalValue;
}
/// <summary>
/// 获取全量的排行榜
/// </summary>
/// <returns></returns>
public async Task<List<(Guid userId, decimal value)>?> GetAllAccountValueByCacheAsync()
{
return await _distributedCache.GetOrAddAsync("AllAccountValue", async () => await GetAccountValueAsync(),
() => new DistributedCacheEntryOptions()
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1)
});
}
private async Task<List<(Guid userId, decimal value)>> GetAccountValueAsync()
{
var output = new List<(Guid userId, decimal value)>();
//获取全部用户的库存
var allStore= await _collectiblesUserStoreRepository._DbQueryable.ToListAsync();
//获取全部藏品
var allCollectiblesDic= (await _collectiblesRepository._DbQueryable.ToListAsync()).ToDictionary(x=>x.Id,y=>y.ValueNumber);
//根据用户分组
var userGroup= allStore.GroupBy(x => x.UserId);
//每个用户进行计算价值
foreach (var item in userGroup)
{
var value= ComputeValue(item.Select(x => (x.CollectiblesId, allCollectiblesDic[x.CollectiblesId])).ToList());
output.Add((item.Key,value));
}
return output;
}
}