using GroupService.Domain.IReposities; using GroupService.Infrastructure; using IM.Commons.IntegrationEvents; using MassTransit; namespace GroupService.WebApi.Application.EventHandler { public class MessageCreatedHandler : IConsumer { private readonly IGroupReposity reposity; private readonly GroupDbContext groupDb; public MessageCreatedHandler(IGroupReposity reposity, GroupDbContext groupDb) { this.reposity = reposity; this.groupDb = groupDb; } public async Task Consume(ConsumeContext context) { var @event = context.Message; if(@event.MsgType == "GROUP") { var group = await reposity.FindByIdAsync(@event.TargetId); if (group is null) return; group.UpdateLastMsg(@event.SequenceId, @event.Content.Fallback, "未知用户"); groupDb.Groups.Update(group); await groupDb.SaveChangesAsync(); } } } }