2024-01-31 18:16:17 +08:00
|
|
|
|
using Mapster;
|
2024-08-09 10:50:18 +08:00
|
|
|
|
using Microsoft.Extensions.Caching.Distributed;
|
2024-01-31 18:16:17 +08:00
|
|
|
|
using Volo.Abp.Caching;
|
2024-08-09 00:39:39 +08:00
|
|
|
|
using Volo.Abp.Domain.Repositories;
|
2024-01-31 18:16:17 +08:00
|
|
|
|
using Volo.Abp.Domain.Services;
|
|
|
|
|
|
using Volo.Abp.EventBus.Local;
|
|
|
|
|
|
using Yi.Framework.Bbs.Domain.Entities;
|
2024-08-09 00:39:39 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Entities.Integral;
|
2024-01-31 18:16:17 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Shared.Caches;
|
|
|
|
|
|
using Yi.Framework.Bbs.Domain.Shared.Consts;
|
|
|
|
|
|
using Yi.Framework.Bbs.Domain.Shared.Etos;
|
2024-08-09 10:50:18 +08:00
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
2024-01-31 18:16:17 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Yi.Framework.Bbs.Domain.Managers
|
|
|
|
|
|
{
|
|
|
|
|
|
public class LevelManager : DomainService
|
|
|
|
|
|
{
|
|
|
|
|
|
private ILocalEventBus _localEventBus;
|
2024-08-09 00:39:39 +08:00
|
|
|
|
private IDistributedCache<List<LevelCacheItem>> _levelCache;
|
|
|
|
|
|
private IRepository<LevelAggregateRoot> _repository;
|
2024-08-09 10:50:18 +08:00
|
|
|
|
private ISqlSugarRepository<BbsUserExtraInfoEntity> _bbsUserRepository;
|
2024-08-12 20:05:42 +08:00
|
|
|
|
|
|
|
|
|
|
public LevelManager(ILocalEventBus localEventBus,
|
|
|
|
|
|
IDistributedCache<List<LevelCacheItem>> levelCache, IRepository<LevelAggregateRoot> repository,
|
|
|
|
|
|
ISqlSugarRepository<BbsUserExtraInfoEntity> bbsUserRepository)
|
2024-01-31 18:16:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
_localEventBus = localEventBus;
|
2024-08-09 00:39:39 +08:00
|
|
|
|
_repository = repository;
|
|
|
|
|
|
_bbsUserRepository = bbsUserRepository;
|
|
|
|
|
|
_levelCache = levelCache;
|
2024-01-31 18:16:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-09 00:39:39 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取等级映射,所有获取等级操作通过这里操作
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<Dictionary<int, LevelCacheItem>> GetCacheMapAsync()
|
|
|
|
|
|
{
|
2024-08-12 20:05:42 +08:00
|
|
|
|
var items = _levelCache.GetOrAdd(LevelConst.LevelCacheKey, () =>
|
2024-08-09 00:39:39 +08:00
|
|
|
|
{
|
2024-08-12 20:05:42 +08:00
|
|
|
|
var cacheItem = (_repository.GetListAsync().Result)
|
2024-08-09 00:39:39 +08:00
|
|
|
|
.OrderByDescending(x => x.CurrentLevel).ToList()
|
|
|
|
|
|
.Adapt<List<LevelCacheItem>>();
|
|
|
|
|
|
return cacheItem;
|
|
|
|
|
|
});
|
2024-08-12 20:05:42 +08:00
|
|
|
|
return items.ToDictionary(x => x.CurrentLevel);
|
2024-08-09 00:39:39 +08:00
|
|
|
|
}
|
2024-08-12 20:05:42 +08:00
|
|
|
|
|
2024-01-31 18:16:17 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 使用钱钱投喂等级
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task ChangeLevelByMoneyAsync(Guid userId, int moneyNumber)
|
|
|
|
|
|
{
|
|
|
|
|
|
//通过用户id获取用户信息的经验和等级
|
2024-08-12 20:05:42 +08:00
|
|
|
|
var userInfo = await _bbsUserRepository.GetAsync(x => x.UserId == userId);
|
2024-01-31 18:16:17 +08:00
|
|
|
|
|
|
|
|
|
|
//钱钱和经验的比例为1:1
|
|
|
|
|
|
//根据钱钱修改经验
|
|
|
|
|
|
var currentNewExperience = userInfo.Experience + moneyNumber * 1;
|
|
|
|
|
|
|
|
|
|
|
|
//修改钱钱,如果钱钱不足,直接会丢出去
|
2024-08-09 00:39:39 +08:00
|
|
|
|
await _localEventBus.PublishAsync(new MoneyChangeEventArgs { UserId = userId, Number = -moneyNumber },
|
|
|
|
|
|
false);
|
2024-01-31 18:16:17 +08:00
|
|
|
|
|
|
|
|
|
|
//更改最终的经验再变化等级
|
2024-08-12 20:05:42 +08:00
|
|
|
|
var levelList = (await GetCacheMapAsync()).Values.OrderByDescending(x => x.CurrentLevel);
|
2024-02-01 15:16:38 +08:00
|
|
|
|
var currentNewLevel = 1;
|
2024-01-31 18:16:17 +08:00
|
|
|
|
foreach (var level in levelList)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (currentNewExperience >= level.MinExperience)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentNewLevel = level.CurrentLevel;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-08-12 20:05:42 +08:00
|
|
|
|
|
2024-08-09 10:50:18 +08:00
|
|
|
|
//这里注意,只更新等级
|
2024-08-09 00:39:39 +08:00
|
|
|
|
userInfo.Level = currentNewLevel;
|
|
|
|
|
|
userInfo.Experience = currentNewExperience;
|
2024-08-09 10:50:18 +08:00
|
|
|
|
await _bbsUserRepository._Db.Updateable(userInfo)
|
2024-08-12 20:05:42 +08:00
|
|
|
|
.UpdateColumns(it => new { it.Level, it.Experience })
|
2024-08-09 10:50:18 +08:00
|
|
|
|
.ExecuteCommandAsync();
|
2024-01-31 18:16:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-08-09 00:39:39 +08:00
|
|
|
|
}
|