//using Hotline.Ai.Quality;
//using Hotline.Caching.Interfaces;
//using Hotline.CallCenter.Calls;
//using Hotline.Orders;
//using Hotline.Settings;
//using Hotline.Share.Dtos.Quality;
//using Hotline.Share.Enums.Quality;
//using MapsterMapper;
//using XF.Domain.Authentications;
//using XF.Domain.Constants;
//using XF.Domain.Dependency;
//using XF.Domain.Exceptions;
//using XF.Domain.Repository;

//namespace Hotline.Quality
//{
//    public class QualityDomainService : IQualityDomainService, IScopeDependency
//    {
//        private readonly ISessionContext _sessionContext;
//        private readonly IMapper _mapper;
//        private readonly IRepository<QualityDetail> _qualiteyDetail;
//        private readonly IQualityRepository _qualityRepository;
//        private readonly IAiQualityService _aiQualityService;
//        private readonly IOrderRepository _orderRepository;
//        private readonly IRepository<TrCallRecord> _trCallRecordRepository;
//        private readonly IRepository<QualityTemplate> _qualityTemplate;
//        private readonly ISystemSettingCacheManager _systemSettingCacheManager;

//		public QualityDomainService(
//            ISessionContext sessionContext,
//            IMapper mapper,
//            IRepository<QualityDetail> qualiteyDetail,
//            IQualityRepository qualityRepository,
//            IAiQualityService aiQualityService,
//            IRepository<TrCallRecord> trCallRecordRepository,
//            IRepository<QualityTemplate> qualityTemplate,
//            IOrderRepository orderRepository,
//            ISystemSettingCacheManager systemSettingCacheManager
//			)
//        {
//            _sessionContext = sessionContext;
//            _mapper = mapper;
//            _qualiteyDetail = qualiteyDetail;
//            _qualityRepository = qualityRepository;
//            _aiQualityService = aiQualityService;
//            _trCallRecordRepository = trCallRecordRepository;
//            _qualityTemplate = qualityTemplate;
//            _orderRepository = orderRepository;
//            _systemSettingCacheManager = systemSettingCacheManager;

//		}

//        public async Task AddQualityAsync(EQualitySource Source, string OrderId, string VisitId, CancellationToken cancellationToken)
//        {
//            QualityAddDto quality = new QualityAddDto
//            {
//                Source = Source,
//                OrderId = OrderId,
//                VisitId = VisitId
//            };
//            var count = await _qualityRepository.CountAsync(x => x.OrderId == OrderId && x.Source == Source, cancellationToken);
//            if (count <= 0)
//                await AddQualityAsync(quality, cancellationToken);
//        }

//        public async Task AddQualityAsync(EQualitySource Source, string OrderId, CancellationToken cancellationToken)
//        {
//            QualityAddDto quality = new QualityAddDto
//            {
//                Source = Source,
//                OrderId = OrderId
//            };
//            var count = await _qualityRepository.CountAsync(x => x.OrderId == OrderId && x.Source == Source, cancellationToken);
//            if (count <= 0)
//                await AddQualityAsync(quality, cancellationToken);
//        }

//        public async Task AddQualityAsync(QualityAddDto model, CancellationToken cancellationToken)
//        {
//            var quality = _mapper.Map<Hotline.Quality.Quality>(model);
//            quality.InitId();

//            if (model.QualityDetails.Any())
//            {
//                foreach (var item in model.QualityDetails)
//                {
//                    item.QualityId = quality.Id;
//                }
//                List<QualityDetail> details = _mapper.Map<List<QualityDetail>>(model.QualityDetails);
//                await _qualiteyDetail.AddRangeAsync(details, cancellationToken);
//            }
//            //受理智能质检
//            if (model.Source == EQualitySource.Accepted)
//            {
//                var teAny = await _qualityTemplate.Queryable()
//                    .LeftJoin<QualityTemplateDetail>((x, d) => x.Id == d.TemplateId)
//                    .LeftJoin<QualityItem>((x, d, i) => d.ItemId == i.Id)
//                    .Where((x, d, i) => i.IsIntelligent == 1).AnyAsync();
//                if (teAny)
//                {
//                    var order = await _orderRepository.GetAsync(model.OrderId);
//                    if (order != null && !string.IsNullOrEmpty(order.CallId))
//                    {
//                        quality.AiQuality = true;
//                        quality.Mode = "智能质检";
//                        //var call = await _trCallRecordRepository.GetAsync(x => x.CallAccept == order.CallId); //由CallAccept改成OtherAccept
//                        var call = await _trCallRecordRepository.GetAsync(x => x.OtherAccept == order.CallId);
//                        var setting = _systemSettingCacheManager.GetSetting(SettingConstants.ViteRecordPrefix);
//						await _aiQualityService.CreateAiOrderQualityTask(quality, call, order, setting?.SettingValue[0],cancellationToken);
//                    }
//                }
//            }
//            await _qualityRepository.AddAsync(quality, cancellationToken);
//        }

//        public async Task UpdateQualityAsync(UpdateQualityDto model, CancellationToken cancellationToken)
//        {
//            //验证工单是否可以申请
//            var quality = await _qualityRepository.GetAsync(model.Id, cancellationToken);
//            if (quality is null)
//                throw UserFriendlyException.SameMessage("无效质检");
//            _mapper.Map(model, quality);
//            quality.State = EQualityState.End;
//            quality.UserName = _sessionContext.UserName;
//            quality.QualityTime = DateTime.Now;
//            await _qualityRepository.UpdateAsync(quality, cancellationToken);
//            List<QualityDetail> deleteDetails = await _qualiteyDetail.Queryable().Where(x => x.QualityId == model.Id).ToListAsync(cancellationToken);
//            await _qualiteyDetail.RemoveRangeAsync(deleteDetails, cancellationToken);
//            if (model.QualityDetails.Any())
//            {
//                foreach (var item in model.QualityDetails)
//                {
//                    item.QualityId = model.Id;
//                }
//                List<QualityDetail> details = _mapper.Map<List<QualityDetail>>(model.QualityDetails);
//                await _qualiteyDetail.AddRangeAsync(details, cancellationToken);
//            }
//        }
//    }
//}
