using Hotline.Caches; using Hotline.CallCenter.Devices; using Hotline.Realtimes; using Hotline.Users; using System.Reflection; using XF.Domain.Constants; using XF.Domain.Dependency; using XF.Domain.Exceptions; namespace Hotline.CallCenter.Tels; public class TelDomainService : ITelDomainService, IScopeDependency { private readonly IDeviceManager _deviceManager; private readonly ITelRepository _telRepository; private readonly ITelRestRepository _telRestRepository; private readonly ITelHoldRepository _telHoldRepository; private readonly IRealtimeService _realtimeService; public TelDomainService( IDeviceManager deviceManager, ITelRepository telRepository, ITelRestRepository telRestRepository, ITelHoldRepository telHoldRepository, IRealtimeService realtimeService) { _deviceManager = deviceManager; _telRepository = telRepository; _telRestRepository = telRestRepository; _telHoldRepository = telHoldRepository; _realtimeService = realtimeService; } /// /// 查询所有分机 /// /// /// public Task> QueryTelsAsync(CancellationToken cancellationToken) => _deviceManager.QueryTelsAsync(cancellationToken); /// /// 查询所有分机组 /// /// /// public Task> QueryTelGroupsAsync(CancellationToken cancellationToken) => _deviceManager.QueryTelGroupsAsync(cancellationToken); /// /// 分机休息 /// /// /// /// public async Task RestAsync(Work currentWork,string reason,bool isApply, CancellationToken cancellationToken) { var isResting = await _telRepository.IsRestingAsync(currentWork.TelNo, cancellationToken); if (!isResting) { await _deviceManager.TelRestAsync(currentWork.TelNo, cancellationToken); return await _telRestRepository.AddAsync(new TelRest(currentWork.TelId, currentWork.TelNo, currentWork.UserId, currentWork.UserName,reason, isApply), cancellationToken); } throw new UserFriendlyException("当前坐席正在休息"); } /// /// 分机审批通过 /// /// /// /// public async Task RestApplyPass(string id, CancellationToken cancellationToken) { var telrest = await _telRestRepository.GetAsync(id, cancellationToken); if(telrest!=null) { telrest.ApplyStatus = Share.Enums.Order.ERestApplyStatus.Resting; telrest.StartTime = DateTime.Now; await _telRestRepository.UpdateAsync(telrest, cancellationToken); //通知前端休息通过 await _realtimeService.RestApplyPassAsync(telrest.UserId, cancellationToken); } } /// /// 分机结束休息 /// /// /// /// public async Task UnRestAsync(string telId, CancellationToken cancellationToken) { var tel = await _telRepository.GetAsync(telId, cancellationToken); if (tel 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); return restingTel; } /// /// 保持通话 /// /// /// /// public async Task HoldAsync(string telId,string userId,string userName,string callId, CancellationToken cancellationToken) { var isHolding = await _telHoldRepository.IsHoldingAsync(telId, userId, callId, cancellationToken); if (!isHolding) { var tel = await _telRepository.GetAsync(telId, cancellationToken); if (tel is null) throw new UserFriendlyException("无效分机编号"); await _deviceManager.HoldAsync(tel.No, cancellationToken); await _telHoldRepository.AddAsync(new Calls.TelHold() { TelId = telId, TelNo = tel.No, UserId = userId, UserName = userName, CallId = callId }, cancellationToken); } } /// /// 恢复通话(解除hold状态) /// /// /// /// public async Task UnHoldAsync(string telId,string userId,string callId, CancellationToken cancellationToken) { var tel = await _telRepository.GetAsync(telId, cancellationToken); if (tel is null) throw new UserFriendlyException("无效分机编号"); await _deviceManager.UnHoldAsync(tel.No, cancellationToken); var holdingTel = await _telHoldRepository.GetAsync(d => d.TelId == telId && d.TelNo == tel.No && d.UserId == userId && d.CallId == callId && !d.EndTime.HasValue ); if (holdingTel is null) throw new UserFriendlyException("未找到分机保持信息"); holdingTel.EndHold(); await _telHoldRepository.UpdateAsync(holdingTel, 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); } }