From bf2bcd1395a856cc33b573e2aa9d8ff1db55731c Mon Sep 17 00:00:00 2001 From: chenchun Date: Wed, 13 Nov 2024 19:01:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E7=A7=AF=E5=88=86?= =?UTF-8?q?=E3=80=81=E4=BB=B7=E5=80=BC=E6=8E=92=E8=A1=8C=E6=A6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BaseAnalyseTopUserDto.cs} | 10 +- .../Dtos/Analyse/MoneyTopUserDto.cs | 7 + .../Dtos/Analyse/PointsTopUserDto.cs | 8 ++ .../Dtos/Analyse/ValueTopUserDto.cs | 6 + ...Framework.Bbs.Application.Contracts.csproj | 1 + .../Analyses/BbsUserAnalyseService.cs | 123 +++++++++++++++++- .../Dtos/Analyse/DcPointsTopUserDto.cs | 9 ++ .../Dtos/Analyse/DcValueTopUserDto.cs | 9 ++ .../IServices/IPointAnalyseService.cs | 16 +++ .../IServices/IValueAnalyseService.cs | 16 +++ ...lCollectibles.Application.Contracts.csproj | 4 - .../Services/Analyses/PointAnalyseService.cs | 54 ++++++++ .../Services/Analyses/ValueAnalyseService.cs | 48 +++++++ .../Managers/CollectiblesManager.cs | 63 ++++++++- 14 files changed, 358 insertions(+), 16 deletions(-) rename Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/{BbsUser/MoneyTopUserDto.cs => Analyse/BaseAnalyseTopUserDto.cs} (70%) create mode 100644 Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/MoneyTopUserDto.cs create mode 100644 Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/PointsTopUserDto.cs create mode 100644 Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/ValueTopUserDto.cs create mode 100644 Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Analyse/DcPointsTopUserDto.cs create mode 100644 Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Analyse/DcValueTopUserDto.cs create mode 100644 Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/IServices/IPointAnalyseService.cs create mode 100644 Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/IServices/IValueAnalyseService.cs create mode 100644 Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Analyses/PointAnalyseService.cs create mode 100644 Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Analyses/ValueAnalyseService.cs diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/BbsUser/MoneyTopUserDto.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/BaseAnalyseTopUserDto.cs similarity index 70% rename from Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/BbsUser/MoneyTopUserDto.cs rename to Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/BaseAnalyseTopUserDto.cs index e957f079..48386106 100644 --- a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/BbsUser/MoneyTopUserDto.cs +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/BaseAnalyseTopUserDto.cs @@ -1,12 +1,15 @@ using Yi.Framework.Bbs.Domain.Shared.Enums; -namespace Yi.Framework.Bbs.Application.Contracts.Dtos.BbsUser; +namespace Yi.Framework.Bbs.Application.Contracts.Dtos.Analyse; -public class MoneyTopUserDto +/// +/// 用户排行榜 +/// +public class BaseAnalyseTopUserDto { + public Guid UserId { get; set; } public string UserName { get; set; } public string? Nick { get; set; } - public decimal Money { get; set; } public int Order { get; set; } public string? Icon { get; set; } public int Level { get; set; } @@ -18,5 +21,4 @@ public class MoneyTopUserDto /// 用户限制 /// public UserLimitEnum UserLimit { get; set; } - } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/MoneyTopUserDto.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/MoneyTopUserDto.cs new file mode 100644 index 00000000..23781369 --- /dev/null +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/MoneyTopUserDto.cs @@ -0,0 +1,7 @@ +namespace Yi.Framework.Bbs.Application.Contracts.Dtos.Analyse; + +public class MoneyTopUserDto:BaseAnalyseTopUserDto +{ + public decimal Money { get; set; } + +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/PointsTopUserDto.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/PointsTopUserDto.cs new file mode 100644 index 00000000..573c69aa --- /dev/null +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/PointsTopUserDto.cs @@ -0,0 +1,8 @@ +using Yi.Framework.Bbs.Domain.Shared.Enums; + +namespace Yi.Framework.Bbs.Application.Contracts.Dtos.Analyse; + +public class PointsTopUserDto:BaseAnalyseTopUserDto +{ + public int Points { get; set; } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/ValueTopUserDto.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/ValueTopUserDto.cs new file mode 100644 index 00000000..01ef7681 --- /dev/null +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Dtos/Analyse/ValueTopUserDto.cs @@ -0,0 +1,6 @@ +namespace Yi.Framework.Bbs.Application.Contracts.Dtos.Analyse; + +public class ValueTopUserDto:BaseAnalyseTopUserDto +{ + public decimal Value { get; set; } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Yi.Framework.Bbs.Application.Contracts.csproj b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Yi.Framework.Bbs.Application.Contracts.csproj index f7d35469..a4775b67 100644 --- a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Yi.Framework.Bbs.Application.Contracts.csproj +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application.Contracts/Yi.Framework.Bbs.Application.Contracts.csproj @@ -3,6 +3,7 @@ + diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/Analyses/BbsUserAnalyseService.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/Analyses/BbsUserAnalyseService.cs index 63588b25..78ca69af 100644 --- a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/Analyses/BbsUserAnalyseService.cs +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/Analyses/BbsUserAnalyseService.cs @@ -9,6 +9,7 @@ using Yi.Framework.Bbs.Domain.Entities; using Yi.Framework.Bbs.Domain.Entities.Integral; using Yi.Framework.Bbs.Domain.Managers; using Yi.Framework.Bbs.Domain.Shared.Enums; +using Yi.Framework.DigitalCollectibles.Application.Contracts.IServices; using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Domain.Authorization; using Yi.Framework.Rbac.Domain.Shared.Consts; @@ -20,11 +21,16 @@ namespace Yi.Framework.Bbs.Application.Services.Analyses { private BbsUserManager _bbsUserManager; private IOnlineService _onlineService; + private readonly IPointAnalyseService _pointAnalyseService; + private readonly IValueAnalyseService _valueAnalyseService; - public BbsUserAnalyseService(BbsUserManager bbsUserManager, IOnlineService onlineService) + public BbsUserAnalyseService(BbsUserManager bbsUserManager, IOnlineService onlineService, + IPointAnalyseService pointAnalyseService, IValueAnalyseService valueAnalyseService) { _bbsUserManager = bbsUserManager; _onlineService = onlineService; + _pointAnalyseService = pointAnalyseService; + _valueAnalyseService = valueAnalyseService; } @@ -169,5 +175,120 @@ namespace Yi.Framework.Bbs.Application.Services.Analyses return output; } } + + + /// + /// 积分排行榜 + /// + /// + [HttpGet("analyse/dc-user/points-top/{userId?}")] + public async Task> GetPointsTopAsync([FromQuery] PagedResultRequestDto input, + [FromRoute] Guid? userId) + { + var result = await _pointAnalyseService.GetValueTopAsync(input, null); + + RefAsync total = 0; + var userIds = result.Items.Select(x => x.UserId).ToList(); + + var baseOutput = await _bbsUserManager._userRepository._DbQueryable + .Where(u => userIds.Contains(u.Id)) + .LeftJoin((u, info) => u.Id == info.UserId) + .OrderByDescending((u, info) => info.Money) + .Select((u, info) => + new BaseAnalyseTopUserDto + { + UserName = u.UserName, + Nick = u.Nick, + Icon = u.Icon, + Level = info.Level, + UserLimit = info.UserLimit, + UserId = info.UserId + } + ).ToPageListAsync(input.SkipCount, input.MaxResultCount, total); + + + var output = new List(); + result.Items.ToList().ForEach(x => + { + var currentUserInfo = baseOutput.Where(u => u.UserId == x.UserId).FirstOrDefault(); + + if (currentUserInfo is not null) + { + output.Add(new PointsTopUserDto + { + UserName = currentUserInfo.UserName, + Nick = currentUserInfo.Nick, + Order = x.Order, + Icon = currentUserInfo.Icon, + Level = currentUserInfo.Level, + LevelName = _bbsUserManager._levelCacheDic[currentUserInfo.Level].Name, + UserLimit = UserLimitEnum.Normal, + Points = x.Points + }); + } + }); + return new PagedResultDto + { + Items = output, + TotalCount = total + }; + } + + /// + /// 价值排行榜 + /// + /// + [HttpGet("analyse/dc-user/value-top/{userId?}")] + public async Task> GetValueTopAsync([FromQuery] PagedResultRequestDto input, + [FromRoute] Guid? userId) + { + var result = await _valueAnalyseService.GetValueTopAsync(input, null); + + RefAsync total = 0; + var userIds = result.Items.Select(x => x.UserId).ToList(); + + var baseOutput = await _bbsUserManager._userRepository._DbQueryable + .Where(u => userIds.Contains(u.Id)) + .LeftJoin((u, info) => u.Id == info.UserId) + .OrderByDescending((u, info) => info.Money) + .Select((u, info) => + new BaseAnalyseTopUserDto + { + UserName = u.UserName, + Nick = u.Nick, + Icon = u.Icon, + Level = info.Level, + UserLimit = info.UserLimit, + UserId = info.UserId + } + ).ToPageListAsync(input.SkipCount, input.MaxResultCount, total); + + + var output = new List(); + result.Items.ToList().ForEach(x => + { + var currentUserInfo = baseOutput.Where(u => u.UserId == x.UserId).FirstOrDefault(); + + if (currentUserInfo is not null) + { + output.Add(new ValueTopUserDto + { + UserName = currentUserInfo.UserName, + Nick = currentUserInfo.Nick, + Order = x.Order, + Icon = currentUserInfo.Icon, + Level = currentUserInfo.Level, + LevelName = _bbsUserManager._levelCacheDic[currentUserInfo.Level].Name, + UserLimit = UserLimitEnum.Normal, + Value = x.Value + }); + } + }); + return new PagedResultDto + { + Items = output, + TotalCount = total + }; + } } } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Analyse/DcPointsTopUserDto.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Analyse/DcPointsTopUserDto.cs new file mode 100644 index 00000000..225b407c --- /dev/null +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Analyse/DcPointsTopUserDto.cs @@ -0,0 +1,9 @@ +namespace Yi.Framework.DigitalCollectibles.Application.Contracts.Dtos.Analyse; + +public class DcPointsTopUserDto +{ + public Guid UserId { get; set; } + public int Points { get; set; } + + public int Order { get; set; } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Analyse/DcValueTopUserDto.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Analyse/DcValueTopUserDto.cs new file mode 100644 index 00000000..46616744 --- /dev/null +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Dtos/Analyse/DcValueTopUserDto.cs @@ -0,0 +1,9 @@ +namespace Yi.Framework.DigitalCollectibles.Application.Contracts.Dtos.Analyse; + +public class DcValueTopUserDto +{ + public Guid UserId { get; set; } + public decimal Value { get; set; } + + public int Order { get; set; } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/IServices/IPointAnalyseService.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/IServices/IPointAnalyseService.cs new file mode 100644 index 00000000..d7a1feb2 --- /dev/null +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/IServices/IPointAnalyseService.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Mvc; +using Volo.Abp.Application.Dtos; +using Yi.Framework.DigitalCollectibles.Application.Contracts.Dtos.Analyse; + +namespace Yi.Framework.DigitalCollectibles.Application.Contracts.IServices; + +public interface IPointAnalyseService +{ + /// + /// 积分排行榜 + /// + /// + // [HttpGet("analyse/dc-user/points-top/{userId?}")] + Task> GetValueTopAsync([FromQuery] PagedResultRequestDto input, + [FromRoute] Guid? userId); +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/IServices/IValueAnalyseService.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/IServices/IValueAnalyseService.cs new file mode 100644 index 00000000..5ee848e4 --- /dev/null +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/IServices/IValueAnalyseService.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Mvc; +using Volo.Abp.Application.Dtos; +using Yi.Framework.DigitalCollectibles.Application.Contracts.Dtos.Analyse; + +namespace Yi.Framework.DigitalCollectibles.Application.Contracts.IServices; + +public interface IValueAnalyseService +{ + /// + /// 价值排行榜 + /// + /// + // [HttpGet("analyse/dc-user/value-top/{userId?}")] + Task> GetValueTopAsync([FromQuery] PagedResultRequestDto input, + [FromRoute] Guid? userId); +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Yi.Framework.DigitalCollectibles.Application.Contracts.csproj b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Yi.Framework.DigitalCollectibles.Application.Contracts.csproj index d94c8ceb..5d20f5fd 100644 --- a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Yi.Framework.DigitalCollectibles.Application.Contracts.csproj +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application.Contracts/Yi.Framework.DigitalCollectibles.Application.Contracts.csproj @@ -7,8 +7,4 @@ - - - - diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Analyses/PointAnalyseService.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Analyses/PointAnalyseService.cs new file mode 100644 index 00000000..246024e0 --- /dev/null +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Analyses/PointAnalyseService.cs @@ -0,0 +1,54 @@ +using Microsoft.AspNetCore.Mvc; +using SqlSugar; +using Volo.Abp.Application.Dtos; +using Volo.Abp.Application.Services; +using Yi.Framework.DigitalCollectibles.Application.Contracts.Dtos.Analyse; +using Yi.Framework.DigitalCollectibles.Application.Contracts.IServices; +using Yi.Framework.DigitalCollectibles.Domain.Entities; +using Yi.Framework.SqlSugarCore.Abstractions; + + +namespace Yi.Framework.DigitalCollectibles.Application.Services.Analyses; + +/// +/// 用户积分分析 +/// +public class PointAnalyseService: ApplicationService,IPointAnalyseService +{ + + private readonly ISqlSugarRepository _repository; + + public PointAnalyseService(ISqlSugarRepository repository) + { + _repository = repository; + } + + /// + /// 积分排行榜 + /// + /// + // [HttpGet("analyse/dc-user/points-top/{userId?}")] + [RemoteService(isEnabled:false)] + public async Task> GetValueTopAsync([FromQuery] PagedResultRequestDto input, + [FromRoute] Guid? userId) + { + + var pageIndex = input.SkipCount; + RefAsync total = 0; + var output = await _repository._DbQueryable + .OrderByDescending(x=>x.PointsNumber) + .Select(x => + new DcPointsTopUserDto{ + UserId = x.UserId, + Points = x.PointsNumber, + Order=SqlFunc.RowNumber(SqlFunc.Desc(x.PointsNumber)) + }) + .ToPageListAsync(pageIndex, input.MaxResultCount, total); + return new PagedResultDto + { + Items = output, + TotalCount = total + }; + + } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Analyses/ValueAnalyseService.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Analyses/ValueAnalyseService.cs new file mode 100644 index 00000000..711047fb --- /dev/null +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Analyses/ValueAnalyseService.cs @@ -0,0 +1,48 @@ +using Microsoft.AspNetCore.Mvc; +using SqlSugar; +using Volo.Abp.Application.Dtos; +using Volo.Abp.Application.Services; +using Yi.Framework.DigitalCollectibles.Application.Contracts.Dtos.Analyse; +using Yi.Framework.DigitalCollectibles.Application.Contracts.IServices; +using Yi.Framework.DigitalCollectibles.Domain.Entities; +using Yi.Framework.DigitalCollectibles.Domain.Managers; +using Yi.Framework.SqlSugarCore.Abstractions; + +namespace Yi.Framework.DigitalCollectibles.Application.Services.Analyses; + +public class ValueAnalyseService: ApplicationService,IValueAnalyseService +{ + + private readonly CollectiblesManager _manager; + + public ValueAnalyseService( CollectiblesManager manager) + { + _manager = manager; + } + + /// + /// 价值排行榜 + /// + /// + // [HttpGet("analyse/dc-user/value-top/{userId?}")] + [RemoteService(isEnabled:false)] + public async Task> GetValueTopAsync([FromQuery] PagedResultRequestDto input, + [FromRoute] Guid? userId) + { + //每个人的价值需要进行计算才能获取,这里计算时间较长,放入缓存,绝对过期 + var allValue= await _manager.GetAllAccountValueByCacheAsync(); + var output = allValue.OrderByDescending(x => x.value).Select((x, index) => new DcValueTopUserDto + { + UserId = x.userId, + Value = x.value, + Order = index + 1 + }).Skip((input.SkipCount - 1) * input.MaxResultCount) // 跳过前面(当前页码 - 1)* 每页数量条记录 + .Take(input.MaxResultCount).ToList(); + return new PagedResultDto + { + Items = output, + TotalCount = allValue.Count + }; + + } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/Managers/CollectiblesManager.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/Managers/CollectiblesManager.cs index b0e3bdd3..e6851872 100644 --- a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/Managers/CollectiblesManager.cs +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/Managers/CollectiblesManager.cs @@ -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 _collectiblesUserStoreRepository; - - public CollectiblesManager(ISqlSugarRepository collectiblesUserStoreRepository) + private readonly ISqlSugarRepository _collectiblesRepository; + private readonly IDistributedCache> _distributedCache; + public CollectiblesManager(ISqlSugarRepository collectiblesUserStoreRepository, ISqlSugarRepository collectiblesRepository, IDistributedCache> distributedCache) { _collectiblesUserStoreRepository = collectiblesUserStoreRepository; + _collectiblesRepository = collectiblesRepository; + _distributedCache = distributedCache; } - + /// + /// 获取某个用户的价值 + /// + /// + /// public async Task 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; } + +/// +/// 获取全量的排行榜 +/// +/// + public async Task?> GetAllAccountValueByCacheAsync() + { + return await _distributedCache.GetOrAddAsync("AllAccountValue", async () => await GetAccountValueAsync(), + () => new DistributedCacheEntryOptions() + { + AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1) + }); + } + + private async Task> 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; + } } \ No newline at end of file