Files
Yi.Admin/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/EventHandlers/AgreeChangeEventHandler.cs

106 lines
4.5 KiB
C#
Raw Normal View History

2024-01-27 17:42:09 +08:00
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus;
2024-05-23 23:40:55 +08:00
using Volo.Abp.EventBus.Local;
2024-01-27 17:42:09 +08:00
using Yi.Framework.Bbs.Domain.Entities;
using Yi.Framework.Bbs.Domain.Entities.Forum;
2024-05-23 23:40:55 +08:00
using Yi.Framework.Bbs.Domain.Shared.Consts;
2024-08-13 16:45:56 +08:00
using Yi.Framework.Bbs.Domain.Shared.Enums;
2024-05-23 23:40:55 +08:00
using Yi.Framework.Bbs.Domain.Shared.Etos;
using Yi.Framework.Rbac.Domain.Entities;
2024-01-27 17:42:09 +08:00
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Bbs.Domain.EventHandlers
{
/// <summary>
/// 被点赞
/// </summary>
2024-08-13 16:45:56 +08:00
public class AgreeChangeEventHandler : ILocalEventHandler<EntityCreatedEventData<AgreeEntity>>,
ITransientDependency
2024-01-27 17:42:09 +08:00
{
2024-05-23 23:40:55 +08:00
private ISqlSugarRepository<UserAggregateRoot> _userRepository;
private ISqlSugarRepository<BbsUserExtraInfoEntity> _userInfoRepository;
2024-01-27 17:42:09 +08:00
private ISqlSugarRepository<AgreeEntity> _agreeRepository;
2024-05-23 23:40:55 +08:00
private ILocalEventBus _localEventBus;
2024-08-13 16:45:56 +08:00
public AgreeChangeEventHandler(ISqlSugarRepository<BbsUserExtraInfoEntity> userInfoRepository,
ISqlSugarRepository<AgreeEntity> agreeRepository, ILocalEventBus localEventBus,
ISqlSugarRepository<UserAggregateRoot> userRepository)
2024-01-27 17:42:09 +08:00
{
2024-05-23 23:40:55 +08:00
_userInfoRepository = userInfoRepository;
2024-01-27 17:42:09 +08:00
_agreeRepository = agreeRepository;
2024-05-23 23:40:55 +08:00
_localEventBus = localEventBus;
_userRepository = userRepository;
2024-01-27 17:42:09 +08:00
}
2024-08-13 16:45:56 +08:00
2024-01-27 17:42:09 +08:00
public async Task HandleEventAsync(EntityCreatedEventData<AgreeEntity> eventData)
{
var agreeEntity = eventData.Entity;
2024-05-23 23:40:55 +08:00
//查询主题的信息
var discussAndAgreeDto = await _agreeRepository._DbQueryable
2024-08-15 21:53:28 +08:00
.Where(agree=>agree.Id==agreeEntity.Id)
2024-05-23 23:40:55 +08:00
.LeftJoin<DiscussAggregateRoot>((agree, discuss) => agree.DiscussId == discuss.Id)
2024-08-13 16:45:56 +08:00
.Select((agree, discuss) =>
new
{
DiscussId = discuss.Id,
DiscussTitle = discuss.Title,
DiscussCreatorId = discuss.CreatorId,
})
.FirstAsync();
2024-05-23 23:40:55 +08:00
//查询点赞者用户
var agreeUser = await _userRepository.GetFirstAsync(x => x.Id == agreeEntity.CreatorId);
//给创建者点赞数量+1
await _userInfoRepository._Db.Updateable<BbsUserExtraInfoEntity>()
2024-08-13 16:45:56 +08:00
.SetColumns(it => it.AgreeNumber == it.AgreeNumber + 1)
.Where(it => it.UserId == discussAndAgreeDto.DiscussCreatorId)
.ExecuteCommandAsync();
2024-05-23 23:40:55 +08:00
//通知主题作者,有人点赞
2024-08-13 16:45:56 +08:00
await _localEventBus.PublishAsync(
new BbsNoticeEventArgs(discussAndAgreeDto.DiscussCreatorId!.Value,
string.Format(DiscussConst.AgreeNotice, discussAndAgreeDto.DiscussTitle, agreeUser.UserName,
discussAndAgreeDto.DiscussId)), false);
2024-05-23 23:40:55 +08:00
2024-08-13 16:45:56 +08:00
//最后发布任务触发事件
await _localEventBus.PublishAsync(
new AssignmentEventArgs(AssignmentRequirementTypeEnum.Agree, agreeUser.Id),false);
2024-01-27 17:42:09 +08:00
}
}
2024-08-13 16:45:56 +08:00
2024-01-27 17:42:09 +08:00
/// <summary>
/// 取消点赞
/// </summary>
2024-05-23 23:40:55 +08:00
public class AgreeDeletedEventHandler : ILocalEventHandler<EntityDeletedEventData<AgreeEntity>>,
2024-08-13 16:45:56 +08:00
ITransientDependency
2024-01-27 17:42:09 +08:00
{
private ISqlSugarRepository<BbsUserExtraInfoEntity> _userRepository;
private ISqlSugarRepository<AgreeEntity> _agreeRepository;
2024-05-23 23:40:55 +08:00
private ILocalEventBus _localEventBus;
2024-08-13 16:45:56 +08:00
public AgreeDeletedEventHandler(ISqlSugarRepository<BbsUserExtraInfoEntity> userRepository,
ISqlSugarRepository<AgreeEntity> agreeRepository, ILocalEventBus localEventBus)
2024-01-27 17:42:09 +08:00
{
_userRepository = userRepository;
_agreeRepository = agreeRepository;
2024-05-23 23:40:55 +08:00
_localEventBus = localEventBus;
2024-01-27 17:42:09 +08:00
}
2024-08-13 16:45:56 +08:00
2024-05-23 23:40:55 +08:00
public async Task HandleEventAsync(EntityDeletedEventData<AgreeEntity> eventData)
2024-01-27 17:42:09 +08:00
{
var agreeEntity = eventData.Entity;
2024-08-13 16:45:56 +08:00
var userId = await _agreeRepository._DbQueryable
.LeftJoin<DiscussAggregateRoot>((agree, discuss) => agree.DiscussId == discuss.Id)
.Select((agree, discuss) => discuss.CreatorId).FirstAsync();
2024-01-27 17:42:09 +08:00
2024-05-23 23:40:55 +08:00
//给创建者点赞数量-1
2024-01-27 17:42:09 +08:00
await _userRepository._Db.Updateable<BbsUserExtraInfoEntity>()
2024-08-13 16:45:56 +08:00
.SetColumns(it => it.AgreeNumber == it.AgreeNumber - 1)
.Where(it => it.UserId == userId)
.ExecuteCommandAsync();
2024-01-27 17:42:09 +08:00
}
}
2024-08-13 16:45:56 +08:00
}