山东雷驰
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.1 KiB

3 months ago
using AutoMapper;
using Kean.Application.Command.Interfaces;
using Kean.Application.Command.ViewModels;
using Kean.Domain;
using Kean.Domain.Message.Commands;
using System.Collections.Generic;
using System.Linq;
namespace Kean.Application.Command.Implements
{
/// <summary>
/// 消息服务
/// </summary>
public class MessageService : IMessageService
{
private readonly ICommandBus _bus; // 命令总线
private readonly IMapper _mapper; // 模型映射
private readonly INotification _notifications; // 总线通知
/// <summary>
/// 依赖注入
/// </summary>
public MessageService(
ICommandBus bus,
IMapper mapper,
INotification notifications)
{
_bus = bus;
_mapper = mapper;
_notifications = notifications;
}
/*
* Kean.Application.Command.Interfaces.IMessageService.Connect(string token, string id)
*/
public async System.Threading.Tasks.Task Connect(string token, string id)
{
await _bus.Execute(new ConnectCommand
{
Token = token,
Id = id
});
}
/*
* Kean.Application.Command.Interfaces.IMessageService.Disconnect(string token, string id)
*/
public async System.Threading.Tasks.Task Disconnect(string token, string id)
{
await _bus.Execute(new DisconnectCommand
{
Token = token,
Id = id
});
}
/*
* Kean.Application.Command.Interfaces.IMessageService.SendMessage(Message message, params int[] targets)
*/
public async System.Threading.Tasks.Task<bool> SendMessage(Message message, params int[] targets)
{
var command = _mapper.Map<SendMessageCommand>(message);
command.Targets = targets;
await _bus.Execute(command);
return !_notifications.Any();
}
/*
* Kean.Application.Command.Interfaces.IMessageService.MarkMessage(int userId, IEnumerable<int> messageId, bool flag)
*/
public async System.Threading.Tasks.Task<IEnumerable<int>> MarkMessage(int userId, IEnumerable<int> messageId, bool flag)
{
var command = new MarkMessageCommand
{
UserId = userId,
MessageId = messageId,
Flag = flag
};
await _bus.Execute(command);
return command.MessageId;
}
/*
* Kean.Application.Command.Interfaces.IMessageService.DeleteMessage(int userId, IEnumerable<int> messageId)
*/
public async System.Threading.Tasks.Task<IEnumerable<int>> DeleteMessage(int userId, IEnumerable<int> messageId)
{
var command = new DeleteMessageCommand
{
UserId = userId,
MessageId = messageId
};
await _bus.Execute(command);
return command.MessageId;
}
}
}