fix: 修复审计日志判断当前用户为空的问题

This commit is contained in:
陈淳
2024-01-31 18:16:17 +08:00
parent c50f1ffcb4
commit ff19cb68b9
6 changed files with 110 additions and 7 deletions

View File

@@ -104,7 +104,7 @@ namespace Yi.Framework.SqlSugarCore
} }
if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.LastModifierId))) if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.LastModifierId)))
{ {
if (CurrentUser != null) if (CurrentUser.Id != null)
{ {
entityInfo.SetValue(CurrentUser.Id); entityInfo.SetValue(CurrentUser.Id);
} }
@@ -130,7 +130,7 @@ namespace Yi.Framework.SqlSugarCore
} }
if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.CreatorId))) if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.CreatorId)))
{ {
if (CurrentUser != null) if (CurrentUser.Id != null)
{ {
entityInfo.SetValue(CurrentUser.Id); entityInfo.SetValue(CurrentUser.Id);
} }

View File

@@ -3,12 +3,17 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar; using SqlSugar;
using Volo.Abp;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
using Yi.Framework.Bbs.Application.Contracts.Dtos.Level; using Yi.Framework.Bbs.Application.Contracts.Dtos.Level;
using Yi.Framework.Bbs.Application.Contracts.IServices; using Yi.Framework.Bbs.Application.Contracts.IServices;
using Yi.Framework.Bbs.Domain.Entities.Integral; using Yi.Framework.Bbs.Domain.Entities.Integral;
using Yi.Framework.Bbs.Domain.Managers;
using Yi.Framework.Bbs.Domain.Shared.Consts;
using Yi.Framework.Ddd.Application; using Yi.Framework.Ddd.Application;
using Yi.Framework.SqlSugarCore.Abstractions; using Yi.Framework.SqlSugarCore.Abstractions;
@@ -20,9 +25,11 @@ namespace Yi.Framework.Bbs.Application.Services.Integral
public class LevelService : YiCrudAppService<LevelEntity, LevelOutputDto, Guid, LevelGetListInputDto>, ILevelService public class LevelService : YiCrudAppService<LevelEntity, LevelOutputDto, Guid, LevelGetListInputDto>, ILevelService
{ {
private ISqlSugarRepository<LevelEntity, Guid> _repository; private ISqlSugarRepository<LevelEntity, Guid> _repository;
public LevelService(ISqlSugarRepository<LevelEntity, Guid> repository) : base(repository) private LevelManager _levelManager;
public LevelService(ISqlSugarRepository<LevelEntity, Guid> repository, LevelManager levelManager) : base(repository)
{ {
_repository= repository; _repository = repository;
_levelManager = levelManager;
} }
/// <summary> /// <summary>
@@ -35,11 +42,25 @@ namespace Yi.Framework.Bbs.Application.Services.Integral
RefAsync<int> total = 0; RefAsync<int> total = 0;
var entities = await _repository._DbQueryable var entities = await _repository._DbQueryable
.WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name!)) .WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name!))
.WhereIF(input.MinLevel is not null , x => x.CurrentLevel>=input.MinLevel) .WhereIF(input.MinLevel is not null, x => x.CurrentLevel >= input.MinLevel)
.OrderBy(x => x.CurrentLevel) .OrderBy(x => x.CurrentLevel)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total); .ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<LevelOutputDto>(total, await MapToGetListOutputDtosAsync(entities)); return new PagedResultDto<LevelOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
} }
/// <summary>
/// 升级等级
/// </summary>
/// <returns></returns>
[Authorize]
public async Task UpdateUpgradeAsync(int experience)
{
if (experience <= 0)
{
throw new UserFriendlyException(LevelConst.Level_Low_Zero);
}
await _levelManager.ChangeLevelByMoneyAsync(CurrentUser.Id!.Value, experience);
}
} }
} }

View File

@@ -9,5 +9,7 @@ namespace Yi.Framework.Bbs.Domain.Shared.Consts
public class LevelConst public class LevelConst
{ {
public const string LevelCacheKey=nameof(LevelCacheKey); public const string LevelCacheKey=nameof(LevelCacheKey);
public const string Level_Low_Zero = "经验提升等级低于或等于0";
} }
} }

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Bbs.Domain.Shared.Consts
{
public class MoneyConst
{
public const string Money_Low_Zero = "钱钱不足";
}
}

View File

@@ -1,6 +1,8 @@
using Volo.Abp.DependencyInjection; using Volo.Abp;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus; using Volo.Abp.EventBus;
using Yi.Framework.Bbs.Domain.Entities; using Yi.Framework.Bbs.Domain.Entities;
using Yi.Framework.Bbs.Domain.Shared.Consts;
using Yi.Framework.Bbs.Domain.Shared.Etos; using Yi.Framework.Bbs.Domain.Shared.Etos;
using Yi.Framework.SqlSugarCore.Abstractions; using Yi.Framework.SqlSugarCore.Abstractions;
@@ -15,6 +17,13 @@ namespace Yi.Framework.Bbs.Domain.EventHandlers
} }
public async Task HandleEventAsync(MoneyChangeEventArgs eventData) public async Task HandleEventAsync(MoneyChangeEventArgs eventData)
{ {
var userIfno = await _userInfoRepository.GetFirstAsync(x => x.UserId == eventData.UserId);
//如果变化后的钱钱少于0直接丢出去
if ((userIfno.Money + eventData.Number)<0)
{
throw new UserFriendlyException(MoneyConst.Money_Low_Zero);
}
//原子性sql //原子性sql
await _userInfoRepository._Db.Updateable<BbsUserExtraInfoEntity>() await _userInfoRepository._Db.Updateable<BbsUserExtraInfoEntity>()
.SetColumns(it => it.Money == it.Money + eventData.Number) .SetColumns(it => it.Money == it.Money + eventData.Number)

View File

@@ -0,0 +1,58 @@
using Mapster;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Services;
using Volo.Abp.EventBus.Local;
using Yi.Framework.Bbs.Domain.Entities;
using Yi.Framework.Bbs.Domain.Shared.Caches;
using Yi.Framework.Bbs.Domain.Shared.Consts;
using Yi.Framework.Bbs.Domain.Shared.Etos;
namespace Yi.Framework.Bbs.Domain.Managers
{
public class LevelManager : DomainService
{
private BbsUserManager _bbsUserManager;
private ILocalEventBus _localEventBus;
private List<LevelCacheItem> _levelCacheItem;
public LevelManager(BbsUserManager bbsUserManager, ILocalEventBus localEventBus, IDistributedCache<List<LevelCacheItem>> levelCache)
{
_bbsUserManager = bbsUserManager;
_localEventBus = localEventBus;
_levelCacheItem = levelCache.Get(LevelConst.LevelCacheKey);
}
/// <summary>
/// 使用钱钱投喂等级
/// </summary>
/// <returns></returns>
public async Task ChangeLevelByMoneyAsync(Guid userId, int moneyNumber)
{
//通过用户id获取用户信息的经验和等级
var userInfo = await _bbsUserManager.GetBbsUserInfoAsync(userId);
//钱钱和经验的比例为11
//根据钱钱修改经验
var currentNewExperience = userInfo.Experience + moneyNumber * 1;
//修改钱钱,如果钱钱不足,直接会丢出去
await _localEventBus.PublishAsync(new MoneyChangeEventArgs { UserId = userId, Number = moneyNumber });
//更改最终的经验再变化等级
var levelList = _levelCacheItem.OrderBy(x => x.CurrentLevel).ToList();
var currentNewLevel = 0;
foreach (var level in levelList)
{
if (currentNewExperience >= level.MinExperience)
{
currentNewLevel = level.CurrentLevel;
break;
}
}
userInfo.Level = currentNewLevel;
userInfo.Experience = currentNewExperience;
await _bbsUserManager._bbsUserInfoRepository.UpdateAsync(userInfo.Adapt<BbsUserExtraInfoEntity>());
}
}
}