123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- 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<User> _userRepository;
- private readonly IWorkRepository _workRepository;
- private readonly IDeviceManager _deviceManager;
- private readonly IUserCacheManager _userCacheManager;
- private readonly ITelCacheManager _telCacheManager;
- private readonly ITypedCache<Work> _cacheWork;
- private readonly IMapper _mapper;
- private readonly ISystemSettingCacheManager _systemSettingCacheManager;
- private readonly ISessionContext _sessionContext;
-
- public UserDomainService(
- IRepository<User> userRepository,
- IWorkRepository workRepository,
- IDeviceManager deviceManager,
- ITypedCache<Work> 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;
- }
- /// <summary>
- /// 上班
- /// </summary>
- /// <param name="userId">userId</param>
- /// <param name="tel"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- 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<string>() { tel.No }, group.Voice, cancellationToken);
- }
- }
- /// <summary>
- /// 下班
- /// </summary>
- /// <param name="userId"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public async Task<WorkDto?> 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<WorkDto>(work);
- }
- #region 添添呼
- /// <summary>
- /// 上班
- /// </summary>
- /// <param name="userId"></param>
- /// <param name="telNo"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public async Task<TrOnDutyResponseDto> 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();
- *
- */
- }
- /// <summary>
- /// 下班
- /// </summary>
- /// <param name="userId"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- 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
- }
- }
|