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
{
///
/// 消息服务
///
public class MessageService : IMessageService
{
private readonly ICommandBus _bus; // 命令总线
private readonly IMapper _mapper; // 模型映射
private readonly INotification _notifications; // 总线通知
///
/// 依赖注入
///
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 SendMessage(Message message, params int[] targets)
{
var command = _mapper.Map(message);
command.Targets = targets;
await _bus.Execute(command);
return !_notifications.Any();
}
/*
* 实现 Kean.Application.Command.Interfaces.IMessageService.MarkMessage(int userId, IEnumerable messageId, bool flag) 方法
*/
public async System.Threading.Tasks.Task> MarkMessage(int userId, IEnumerable 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 messageId) 方法
*/
public async System.Threading.Tasks.Task> DeleteMessage(int userId, IEnumerable messageId)
{
var command = new DeleteMessageCommand
{
UserId = userId,
MessageId = messageId
};
await _bus.Execute(command);
return command.MessageId;
}
}
}