using Hotline.Caching.Interfaces; using Hotline.CallCenter.Devices; using Hotline.CallCenter.Tels; using Hotline.Share.Dtos.CallCenter; using Hotline.Share.Dtos.TrCallCenter; using Hotline.Share.Dtos.Users; using MapsterMapper; using System.Security.Cryptography; 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.Users { public class UserDomainService : IUserDomainService, IScopeDependency { private readonly IRepository _userRepository; private readonly IWorkRepository _workRepository; private readonly IDeviceManager _deviceManager; private readonly IUserCacheManager _userCacheManager; private readonly ITelCacheManager _telCacheManager; private readonly ITypedCache _cacheWork; private readonly IMapper _mapper; private readonly ISystemSettingCacheManager _systemSettingCacheManager; private readonly ISessionContext _sessionContext; public UserDomainService( IRepository userRepository, IWorkRepository workRepository, IDeviceManager deviceManager, ITypedCache cacheWork, IUserCacheManager userCacheManager, IMapper mapper, ITelCacheManager telCacheManager, ISystemSettingCacheManager systemSettingCacheManager, ISessionContext sessionContext ) { _userRepository = userRepository; _workRepository = workRepository; _deviceManager = deviceManager; _userCacheManager = userCacheManager; _cacheWork = cacheWork; _mapper = mapper; _telCacheManager = telCacheManager; _systemSettingCacheManager = systemSettingCacheManager; _sessionContext = sessionContext; } /// /// 上班 /// /// userId /// /// /// public async Task OnDutyAsync(string userId, Tel tel, CancellationToken cancellationToken) { var user = await _userRepository.GetAsync(userId, cancellationToken); if (user is null) throw UserFriendlyException.SameMessage("无效的用户编号"); var workingTel = await _workRepository.GetCurrentWorkByUserAsync(userId, cancellationToken); if (workingTel is not null) throw UserFriendlyException.SameMessage($"当前用户已上班,userId:{userId}"); var telIsWorking = await _workRepository.AnyAsync(d => d.TelId == tel.Id && !d.EndTime.HasValue, cancellationToken); if (telIsWorking) throw UserFriendlyException.SameMessage("当前分机已被占用"); var work = new Work(userId, user.Name, tel.Id, tel.No); await _workRepository.AddAsync(work, cancellationToken); if (!string.IsNullOrEmpty(user.StaffNo)) await _deviceManager.UpdateStaffNoAsync(tel.No, user.StaffNo, tel.LineId, cancellationToken); //更新分机组 foreach (var group in tel.Groups) { await _deviceManager.AssginConfigGroupAsync(group.No, group.Distribution, new List() { tel.No }, group.Voice, cancellationToken); } } /// /// 下班 /// /// /// /// public async Task OffDutyAsync(string userId, CancellationToken cancellationToken) { var work = _userCacheManager.GetWorkByUser(userId); var tel = _telCacheManager.GetTel(work.TelNo); work.OffDuty(); await _workRepository.UpdateAsync(work, cancellationToken); await _deviceManager.UpdateStaffNoAsync(work.TelNo, string.Empty, tel.LineId, cancellationToken); _cacheWork.Remove(work.GetKey(KeyMode.UserId)); _cacheWork.Remove(work.GetKey(KeyMode.TelNo)); #region 初始化话机 //初始化解除静音 await _deviceManager.UnMuteAsync(tel.No, cancellationToken); foreach (var group in tel.Groups) { await _deviceManager.ModifyGroupExtAsync(group.No, group.Distribution, group.Voice, "", false, cancellationToken); } #endregion return _mapper.Map(work); } #region 添添呼 /// /// 上班 /// /// /// /// /// public async Task TrOnDutyAsync(string userId, string telNo, CancellationToken cancellationToken) { var work = _userCacheManager.GetWorkByUserNoExp(userId); if(work is not null) { if(work.TelNo == telNo) { return new TrOnDutyResponseDto() { TelNo = work.TelNo, TelPwd = work.TelPwd, Description = work.Description }; } else { throw UserFriendlyException.SameMessage("当前用户已签入其他分机"); } } var telWork = _userCacheManager.GetWorkByTelNoExp(telNo); if (telWork is not null) { throw UserFriendlyException.SameMessage("当前分机已被占用"); } bool IsTelNeedVerify = bool.Parse(_systemSettingCacheManager.GetSetting(SettingConstants.IsTelNeedVerify).SettingValue[0]); //var telModel = await _trClient.QueryTelsAsync(new Tr.Sdk.Tels.QueryTelRequest() { TelNo = telNo }, cancellationToken); //if (telModel != null && telModel.Count > 0) //{ // work = new Work(userId, _sessionContext.UserName, telModel[0].Id, telNo, telModel[0].Password, telModel[0].Description); // await _workRepository.AddAsync(work, cancellationToken); // if (IsTelNeedVerify) // { // return new TrOnDutyResponseDto() { TelNo = telNo, TelPwd = "", Description = telModel[0].Description }; // } // else // { // return new TrOnDutyResponseDto() { TelNo = telNo, TelPwd = telModel[0].Password, Description = telModel[0].Description }; // } //} throw UserFriendlyException.SameMessage("签入异常,未查询到对应分机信息"); /* *var work = 缓存.get(userId); * if(work.TelNo == telNo) * t: return work f: throw * * 获取配置 *IsNeedTelNo * IsTelNeedVerify * * user * tel = trClient.get(telNo); * work * * 根据配置 return new(); * */ } /// /// 下班 /// /// /// /// public async Task TrOffDutyAsync(string userId, CancellationToken cancellationToken) { var work = _userCacheManager.GetWorkByUser(userId); work.OffDuty(); await _workRepository.UpdateAsync(work, cancellationToken); _cacheWork.Remove(work.GetKey(KeyMode.UserId)); _cacheWork.Remove(work.GetKey(KeyMode.TelNo)); } #endregion } }