using Hotline.Caching.Interfaces; using Hotline.CallCenter.Tels; using Hotline.Share.Dtos.CallCenter; using Hotline.Share.Dtos.TrCallCenter; using Hotline.Share.Enums.CallCenter; using Hotline.Users; using Tr.Sdk; using XF.Domain.Authentications; using XF.Domain.Cache; using XF.Domain.Constants; using XF.Domain.Dependency; using XF.Domain.Exceptions; using XF.Domain.Repository; namespace Hotline.Application.CallCenter.Calls { public class TrApplication : ITrApplication, IScopeDependency { private readonly ITrClient _trClient; private readonly ISessionContext _sessionContext; private readonly IWorkRepository _workRepository; private readonly ISystemSettingCacheManager _systemSettingCacheManager; private readonly IUserCacheManager _userCacheManager; private readonly ITelRestRepository _telRestRepository; private readonly ITypedCache _cacheWork; public TrApplication(ITrClient trClient, ISessionContext sessionContext,IWorkRepository workRepository,ISystemSettingCacheManager systemSettingCacheManager,IUserCacheManager userCacheManager, ITelRestRepository telRestRepository, ITypedCache cacheWork) { _trClient = trClient; _sessionContext = sessionContext; _workRepository = workRepository; _systemSettingCacheManager = systemSettingCacheManager; _userCacheManager = userCacheManager; _telRestRepository = telRestRepository; _cacheWork = cacheWork; } /// /// 签入 /// /// /// /// /// public async Task OnSign(string userId,string telNo,CancellationToken cancellationToken) { var work = _userCacheManager.GetWorkByUserNoExp(userId); if (work is not null) { if (work.TelNo != telNo) { throw UserFriendlyException.SameMessage("当前用户已签入其他分机"); } } var telWork = _userCacheManager.GetWorkByTelNoExp(telNo); if (telWork is not null) { throw UserFriendlyException.SameMessage("当前分机已被占用"); } var telModel = await _trClient.QueryTelsAsync(new Tr.Sdk.Tels.QueryTelRequest() { TelNo = telNo }, cancellationToken); if (telModel !=null && telModel.Count>0) { work = new Work(_sessionContext.UserId, _sessionContext.UserName, telModel[0].Id, telNo, telModel[0].Password, telModel[0].Description, telModel[0].QueueId,_sessionContext.StaffNo, ETelModel.OrdinaryModel); await _workRepository.AddAsync(work, cancellationToken); bool IsTelNeedVerify = bool.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.IsTelNeedVerify).SettingValue[0]); if (IsTelNeedVerify) { return new TrOnDutyResponseDto() { TelNo = telNo, TelPwd = "", Description = telModel[0].Description, QueueId = telModel[0].QueueId,StartTime = work.StartTime}; } else { return new TrOnDutyResponseDto() { TelNo = telNo, TelPwd = telModel[0].Password, Description = telModel[0].Description, QueueId = telModel[0].QueueId,StartTime = work.StartTime }; } } throw UserFriendlyException.SameMessage("签入异常,未查询到对应分机信息"); } /// /// 查询当前用户的分机状态 /// /// /// public async Task TelState(string userId) { var work = _userCacheManager.GetWorkByUserNoExp(userId); if (work is not null) { bool isRest = await _telRestRepository.AnyAsync(x => x.TelNo == work.TelNo && x.UserId == userId && !x.EndTime.HasValue); bool IsTelNeedVerify = bool.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.IsTelNeedVerify).SettingValue[0]); if (IsTelNeedVerify) { return new TrOnDutyResponseDto() { TelNo = work.TelNo, TelPwd = "", Description = work.Description, QueueId = work.QueueId, StartTime = work.StartTime, IsRest = isRest, TelModel = work.TelModel }; } else { return new TrOnDutyResponseDto() { TelNo = work.TelNo, TelPwd = work.TelPwd, Description = work.Description, QueueId = work.QueueId, StartTime = work.StartTime, IsRest = isRest, TelModel = work.TelModel }; } } return null; } /// /// 切换状态 /// /// /// public async Task ChangeTelModel(string userId,bool isCallOut) { var work = _userCacheManager.GetWorkByUserNoExp(userId); if (work!=null) { if (isCallOut) { //切换外呼模式 if (work.TelModel== ETelModel.CallOutModel) { throw UserFriendlyException.SameMessage("已是外呼模式,无需切换"); } work.TelModel = ETelModel.CallOutModel; await _workRepository.UpdateAsync(work); _cacheWork.Remove(work.GetKey(KeyMode.UserId)); _cacheWork.Remove(work.GetKey(KeyMode.TelNo)); } else { //切换普通模式 if (work.TelModel == ETelModel.OrdinaryModel) { throw UserFriendlyException.SameMessage("已是普通模式,无需切换"); } work.TelModel = ETelModel.OrdinaryModel; await _workRepository.UpdateAsync(work); } _cacheWork.Remove(work.GetKey(KeyMode.UserId)); _cacheWork.Remove(work.GetKey(KeyMode.TelNo)); } else { throw UserFriendlyException.SameMessage("当前用户没有签入,不能切换状态"); } } } }