using CallCenter.Caches; using CallCenter.Devices; using CallCenter.Share.Dtos; using CallCenter.Tels; using MapsterMapper; using SqlSugar; using XF.Domain.Cache; using XF.Domain.Dependency; using XF.Domain.Exceptions; namespace CallCenter.Users { public class UserDomainService : IUserDomainService, IScopeDependency { private readonly IUserRepository _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; public UserDomainService( IUserRepository userRepository, IWorkRepository workRepository, IDeviceManager deviceManager, ITypedCache cacheWork, IUserCacheManager userCacheManager, IMapper mapper, ITelCacheManager telCacheManager) { _userRepository = userRepository; _workRepository = workRepository; _deviceManager = deviceManager; _userCacheManager = userCacheManager; _cacheWork = cacheWork; _mapper = mapper; _telCacheManager = telCacheManager; } /// /// 上班 /// /// userId /// /// /// public async Task OnDutyAsync(string userId, Tel tel, CancellationToken cancellationToken) { var user = await _userRepository.GetAsync(userId, cancellationToken); if (user is null) throw new UserFriendlyException("无效的用户编号"); var workingTel = await _workRepository.GetCurrentWorkByUserAsync(userId, cancellationToken); if (workingTel is not null) throw new UserFriendlyException($"当前用户已上班,userId:{userId}"); var telIsWorking = await _workRepository.AnyAsync(d => d.TelId == tel.Id && !d.EndTime.HasValue, cancellationToken); if (telIsWorking) throw new UserFriendlyException("当前分机已被占用"); 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.ModifyGroupExtAsync(group.No,group.Distribution,group.Voice, "", true, 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)); foreach (var group in tel.Groups) { await _deviceManager.ModifyGroupExtAsync(group.No,group.Distribution, group.Voice,"", false, cancellationToken); } return _mapper.Map(work); } } }