using WingInterfaceLibrary.Enum;
using WingServerCommon.Log;
using WingServerCommon.Service;
using WingInterfaceLibrary.Internal.Request;
using WingInterfaceLibrary.Internal.Interface;
using WingServerCommon.Config;
using WingServerCommon.Config.Parameters;
using WingInterfaceLibrary.Enum.NotificationEnum;
using WingInterfaceLibrary.Interface;
using WingServerCommon.Interfaces.Cache;
using WingInterfaceLibrary.LiveConsultation;
using WingServerCommon.Mapper;
namespace WingLiveConsultationService
{
public partial class LiveConsultationService : JsonRpcService, ISyncLiveConsultationService
{
/// 转发请求给主服务器
///
///
///
///
///
private async Task SyncToMasterAsync(SyncTypeEnum syncType, string roomId, string patientName, BaseLiveConsultationJson message)
{
try
{
if (ConfigurationManager.IsDistributed)
{
var syncRequest = new SyncReceiveServiceDataRequest
{
SyncService = SyncServiceEnum.LiveConsultation,
SyncType = syncType,
SourceUrl = EnvironmentConfigManager.GetParammeter("Gateway", "Domains").Value,
ServerID = _serverID,
ServiceDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(message),
};
Logger.WriteLineWarn($"LiveConsultationService SyncToMasterAsync, roomId:{roomId}, syncType:{syncType.ToString()}, {syncRequest.ServiceDataJson}");
_masterInteractionCenterService.SyncReceiveSlaveServiceDataAsync(syncRequest);
}
}
catch (Exception ex)
{
Logger.WriteLineWarn($"LiveConsultationService SyncToMasterAsync err, roomId:{roomId}, syncType:{syncType.ToString()}, err: {ex}");
}
}
///
/// 服务器间会诊通知
///
/// The request
///
public async Task SyncServerMessageAsync(SyncReceiveServiceDataRequest request)
{
try
{
Logger.WriteLineInfo($"LiveConsultationService SyncServerMeaasge, source url:{request.SourceUrl}, syncType:{request.SyncType.ToString()}");
switch (request.SyncType)
{
case SyncTypeEnum.UploadData:
var message = Newtonsoft.Json.JsonConvert.DeserializeObject(request.ServiceDataJson);
var userCode = message.OperatorCode;
//获取房间缓存
var roomId = message.ConsultationRecordCode;
var room = await _rtcService.GetLiveRoomAsync(new GetLiveRoomRequest { LiveRoomCode = roomId });
if (room != null)
{
try
{
var members = await _rtcService.GetLiveMemberInfosAsync(new GetLiveMemberInfosRequest { LiveRoomCode = roomId });
var otherJoinMembers = members.Where(x => x.MemberType == LiveMemberEnum.User && x.Status == LiveConsultationMemberStatus.Joined && x.Id != userCode)?.ToList() ?? new List();
if (otherJoinMembers?.Count > 0)
{
//同时转发给其他用户
var sendMessage = new UploadConsultationDataNotification
{
ConsultationCode = roomId,
UserCode = userCode
};
var openNotifyQueueRequest = new WingInterfaceLibrary.Request.Notification.OpenNotifyQueueRequest { Module = roomId };
var clientIds = otherJoinMembers.Select(x => x.Id).ToList();
var tokenInfos = CacheMaintenance.Instance.Get().Where(x => clientIds.Contains(x.ClientId));
if (tokenInfos != null && tokenInfos.Any())
{
Logger.WriteLineInfo($"UpdateConsultationFilesInfoAsync PushMessage, roomId:{roomId}, type:{sendMessage.NotificationType.ToString()}, receivers:{string.Join(";", tokenInfos.Select(x => x.AccountName).Distinct())}");
var notificationRequest = new BroadcastNotificationRequest
{
ClientIds = clientIds,
Tokens = tokenInfos.MappingTo>() ,
Message = sendMessage,
JsonMessage = Newtonsoft.Json.JsonConvert.SerializeObject(sendMessage),
NotificationType = sendMessage.NotificationType,
TransactionType = TransactionTypeEnum.Consultion,
RelevanceCode = roomId,
ReceiverType = ApplicantTypeEnum.Client,
WSConnectType = WSConnectTypeEnum.ConsultationSecondWindow,
IsNeedSyn = false
};
var res = await _notificationService.BroadcastMessageAsync(notificationRequest);
}
}
}
catch (Exception ex)
{
Logger.WriteLineError($"UpdateConsultationFilesInfoAsync send message error:{ex.ToString()}");
}
}
break;
default:
return false;
}
return await Task.FromResult(true);
}
catch (Exception ex)
{
Logger.WriteLineWarn($"LiveConsultationService SyncServerMessageAsync err, {ex}");
return false;
}
return false;
}
}
}