using CallCenter.Caches;
using CallCenter.Devices;
using CallCenter.Share.Dtos;
using CallCenter.Users;
using MapsterMapper;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;
namespace CallCenter.Tels;
public class TelDomainService : ITelDomainService, IScopeDependency
{
private readonly IDeviceManager _deviceManager;
private readonly ITelRepository _telRepository;
private readonly ITelRestRepository _telRestRepository;
private readonly ITelCacheManager _telCacheManager;
private readonly IUserRepository _userRepository;
public TelDomainService(
IDeviceManager deviceManager,
ITelRepository telRepository,
ITelRestRepository telRestRepository,
ITelCacheManager telCacheManager,
IUserRepository userRepository)
{
_deviceManager = deviceManager;
_telRepository = telRepository;
_telRestRepository = telRestRepository;
_telCacheManager = telCacheManager;
_userRepository = userRepository;
}
///
/// 查询所有分机
///
///
///
public Task> QueryTelsAsync(CancellationToken cancellationToken)
=> _deviceManager.QueryTelsAsync(cancellationToken);
///
/// 查询所有分机组
///
///
///
public Task> QueryTelGroupsAsync(CancellationToken cancellationToken)
=> _deviceManager.QueryTelGroupsAsync(cancellationToken);
///
/// 分机休息
///
///
///
///
public async Task RestAsync(Work currentWork, CancellationToken cancellationToken)
{
var isResting = await _telRepository.IsRestingAsync(currentWork.TelNo, cancellationToken);
if (!isResting)
{
//await _deviceManager.TelRestAsync(currentWork.TelNo, cancellationToken);
var tel = _telCacheManager.GetTel(currentWork.TelNo);
await _telRestRepository.AddAsync(new TelRest(currentWork.TelId, currentWork.TelNo, currentWork.UserId, currentWork.UserName),
cancellationToken);
//暂时移除分机组
#region 处理设备
//更新分机组
foreach (var group in tel.Groups)
{
await _deviceManager.ModifyGroupExtAsync(group.No, group.Distribution, group.Voice,tel.No, true, cancellationToken);
}
#endregion
}
}
///
/// 分机结束休息
///
///
///
///
public async Task UnRestAsync(string userId, string telId, CancellationToken cancellationToken)
{
var user = await _userRepository.GetAsync(userId, cancellationToken);
var tel = await _telRepository.GetAsync(telId, cancellationToken);
var telCache = _telCacheManager.GetTel(tel.No);
if (telCache is null)
throw new UserFriendlyException("无效分机编号");
//await _deviceManager.TelEndRestAsync(tel.No, cancellationToken);
var restingTel = await _telRestRepository.GetAsync(d => d.TelId == telId && !d.EndTime.HasValue, cancellationToken);
if (restingTel is null)
throw new UserFriendlyException("未查询到分机休息信息");
restingTel.EndRest();
await _telRestRepository.UpdateAsync(restingTel, cancellationToken);
//将分机加入到分机组
#region 处理设备
//更新分机组
foreach (var group in telCache.Groups)
{
await _deviceManager.ModifyGroupExtAsync(group.No, group.Distribution, group.Voice, "", true, cancellationToken);
}
#endregion
return restingTel;
}
///
/// 保持通话
///
///
///
///
public async Task HoldAsync(string telId, CancellationToken cancellationToken)
{
var tel = await _telRepository.GetAsync(telId, cancellationToken);
if (tel is null)
throw new UserFriendlyException("无效分机编号");
await _deviceManager.HoldAsync(tel.No, cancellationToken);
}
///
/// 恢复通话(解除hold状态)
///
///
///
///
public async Task UnHoldAsync(string telId, CancellationToken cancellationToken)
{
var tel = await _telRepository.GetAsync(telId, cancellationToken);
if (tel is null)
throw new UserFriendlyException("无效分机编号");
await _deviceManager.UnHoldAsync(tel.No, cancellationToken);
}
///
/// 开启静音
///
///
///
///
///
public async Task MuteAsync(string telId, CancellationToken cancellationToken)
{
var tel = await _telRepository.GetAsync(telId, cancellationToken);
if (tel is null)
throw new UserFriendlyException("无效分机编号");
await _deviceManager.MuteAsync(tel.No, cancellationToken);
}
///
/// 解除静音
///
///
///
///
public async Task UnMuteAsync(string telId, CancellationToken cancellationToken)
{
var tel = await _telRepository.GetAsync(telId, cancellationToken);
if (tel is null)
throw new UserFriendlyException("无效分机编号");
await _deviceManager.MuteAsync(tel.No, cancellationToken);
}
}