using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus;
using Volo.Abp.EventBus.Local;
using Yi.Framework.Bbs.Domain.Entities;
using Yi.Framework.Bbs.Domain.Entities.Forum;
using Yi.Framework.Bbs.Domain.Shared.Consts;
using Yi.Framework.Bbs.Domain.Shared.Etos;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Bbs.Domain.EventHandlers
{
///
/// 被点赞
///
public class AgreeCreatedEventHandler : ILocalEventHandler>,
ITransientDependency
{
private ISqlSugarRepository _userRepository;
private ISqlSugarRepository _userInfoRepository;
private ISqlSugarRepository _agreeRepository;
private ILocalEventBus _localEventBus;
public AgreeCreatedEventHandler(ISqlSugarRepository userInfoRepository, ISqlSugarRepository agreeRepository, ILocalEventBus localEventBus, ISqlSugarRepository userRepository)
{
_userInfoRepository = userInfoRepository;
_agreeRepository = agreeRepository;
_localEventBus = localEventBus;
_userRepository = userRepository;
}
public async Task HandleEventAsync(EntityCreatedEventData eventData)
{
var agreeEntity = eventData.Entity;
//查询主题的信息
var discussAndAgreeDto = await _agreeRepository._DbQueryable
.LeftJoin((agree, discuss) => agree.DiscussId == discuss.Id)
.Select((agree, discuss) =>
new
{
DiscussTitle = discuss.Title,
DiscussCreatorId = discuss.CreatorId,
})
.FirstAsync();
//查询点赞者用户
var agreeUser = await _userRepository.GetFirstAsync(x => x.Id == agreeEntity.CreatorId);
//给创建者点赞数量+1
await _userInfoRepository._Db.Updateable()
.SetColumns(it => it.AgreeNumber == it.AgreeNumber + 1)
.Where(it => it.UserId == discussAndAgreeDto.DiscussCreatorId)
.ExecuteCommandAsync();
//通知主题作者,有人点赞
await _localEventBus.PublishAsync(new BbsNoticeEventArgs(discussAndAgreeDto.DiscussCreatorId!.Value, string.Format(DiscussConst.AgreeNotice, discussAndAgreeDto.DiscussTitle, agreeUser.UserName)), false);
}
}
///
/// 取消点赞
///
public class AgreeDeletedEventHandler : ILocalEventHandler>,
ITransientDependency
{
private ISqlSugarRepository _userRepository;
private ISqlSugarRepository _agreeRepository;
private ILocalEventBus _localEventBus;
public AgreeDeletedEventHandler(ISqlSugarRepository userRepository, ISqlSugarRepository agreeRepository, ILocalEventBus localEventBus)
{
_userRepository = userRepository;
_agreeRepository = agreeRepository;
_localEventBus = localEventBus;
}
public async Task HandleEventAsync(EntityDeletedEventData eventData)
{
var agreeEntity = eventData.Entity;
var userId = await _agreeRepository._DbQueryable.LeftJoin((agree, discuss) => agree.DiscussId == discuss.Id)
.Select((agree, discuss) => discuss.CreatorId).FirstAsync();
//给创建者点赞数量-1
await _userRepository._Db.Updateable()
.SetColumns(it => it.AgreeNumber == it.AgreeNumber - 1)
.Where(it => it.UserId == userId)
.ExecuteCommandAsync();
}
}
}