using Hotline.Caching.Interfaces; using Hotline.File; using Hotline.FlowEngine.Workflows; using Hotline.Orders; using Hotline.Repository.SqlSugar.Extensions; using Hotline.Repository.SqlSugar.Ts; using Hotline.Settings.TimeLimits; using Hotline.Share.Dtos; using Hotline.Share.Dtos.File; using Hotline.Share.Dtos.FlowEngine; using Hotline.Share.Dtos.FlowEngine.Workflow; using Hotline.Share.Dtos.Order; using Hotline.Share.Dtos.Settings; using Hotline.Share.Enums.Order; using Hotline.Share.Enums.Settings; using Hotline.Tools; using Hotline.Users; using MapsterMapper; using XF.Domain.Constants; using XF.Domain.Dependency; using XF.Domain.Entities; using XF.Domain.Exceptions; using XF.Domain.Repository; namespace Hotline.Application.Orders; public class OrderApplication : IOrderApplication, IScopeDependency { private readonly IOrderDomainService _orderDomainService; private readonly IWorkflowDomainService _workflowDomainService; private readonly IOrderRepository _orderRepository; private readonly ITimeLimitDomainService _timeLimitDomainService; private readonly IMapper _mapper; private readonly ISystemSettingCacheManager _systemSettingCacheManager; private readonly IRepository _orderWrodRepository; private readonly IRepositoryTextSearch _repositoryts; private readonly IFileRepository _fileRepository; public OrderApplication( IOrderDomainService orderDomainService, IOrderRepository orderRepository, IWorkflowDomainService workflowDomainService, ITimeLimitDomainService timeLimitDomainService, ISystemSettingCacheManager systemSettingCacheManager, IMapper mapper, IRepository orderWrodRepository, IRepositoryTextSearch repositoryts, IFileRepository fileRepository ) { _orderDomainService = orderDomainService; _workflowDomainService = workflowDomainService; _orderRepository = orderRepository; _timeLimitDomainService = timeLimitDomainService; _mapper = mapper; _systemSettingCacheManager = systemSettingCacheManager; _orderWrodRepository = orderWrodRepository; _repositoryts = repositoryts; _fileRepository = fileRepository; } /// /// 更新工单办理期满时间 /// 1.更新工单 2.更新流程 3.推送省平台 /// /// public async Task DelayOrderExpiredTimeAsync(string orderId, int timeCount, ETimeType timeType, CancellationToken cancellationToken) { var order = await _orderDomainService.GetOrderAsync(orderId, cancellationToken: cancellationToken); var expiredTimeConfig = _timeLimitDomainService.CalcEndTime(order.ExpiredTime.Value, new TimeConfig(timeCount, timeType), order.AcceptTypeCode); order.TimeLimit = expiredTimeConfig.TimeText; order.TimeLimitCount = expiredTimeConfig.Count; order.TimeLimitUnit = expiredTimeConfig.TimeType; order.ExpiredTime = expiredTimeConfig.ExpiredTime; order.NearlyExpiredTime = expiredTimeConfig.NearlyExpiredTime; //if (string.IsNullOrEmpty(order.WorkflowId)) // throw new UserFriendlyException("该工单流程id异常"); //var workflow = await _workflowDomainService.GetWorkflowAsync(order.WorkflowId, cancellationToken: cancellationToken); //await _workflowDomainService.UpdateExpiredTimeAsync(workflow, expiredTimeConfig.ExpiredTime, // expiredTimeConfig.TimeText, expiredTimeConfig.Count, expiredTimeConfig.TimeType, expiredTimeConfig.NearlyExpiredTime, cancellationToken); await _orderRepository.UpdateAsync(order, cancellationToken); } /// /// 新增工单办理流程记录 /// public async Task AddOrderTracesAsync(string orderId, ICollection traces, CancellationToken cancellationToken) { var order = await _orderRepository.GetAsync(orderId, cancellationToken); if (order is null) throw new UserFriendlyException("工单不存在"); if (string.IsNullOrEmpty(order.WorkflowId)) throw new UserFriendlyException("工单未开启流程"); await _workflowDomainService.AddTracesAsync(order.WorkflowId, _mapper.Map>(traces), cancellationToken); } /// /// 撤销工单 /// public async Task CancelOrderAsync(string orderId, string opinion, CancellationToken cancellationToken) { var order = await _orderRepository.GetAsync(orderId, cancellationToken); if (order is null) throw new UserFriendlyException("工单不存在"); if (!string.IsNullOrEmpty(order.WorkflowId)) { //结束流程 await _workflowDomainService.TerminateAsync(new TerminateDto { WorkflowId = order.WorkflowId, Opinion = opinion }, cancellationToken); } //归档工单 var now = DateTime.Now; var handleDuration = order.StartTime.HasValue ? _timeLimitDomainService.CalcWorkTime(order.StartTime.Value, now, order.ProcessType is EProcessType.Zhiban) : 0; var fileDuration = order.CenterToOrgTime.HasValue ? _timeLimitDomainService.CalcWorkTime(order.CenterToOrgTime.Value, now, order.ProcessType is EProcessType.Zhiban) : 0; var allDuration = order.StartTime.HasValue ? _timeLimitDomainService.CalcWorkTime(order.StartTime.Value, now, order.ProcessType is EProcessType.Zhiban) : 0; order.File(now, handleDuration, fileDuration, allDuration); await _orderRepository.UpdateAsync(order, cancellationToken); } /// /// 即将超期列表 /// /// /// /// public async Task> GetAboutToExpireAsync(AboutToExpireListDto dto, CancellationToken cancellationToken) { var setting = _systemSettingCacheManager.GetSetting(SettingConstants.OrderAboutToExpire); var value = setting?.SettingValue[0]; value = string.IsNullOrEmpty(value) ? "0" : value; DateTime stTime = DateTime.Now.AddDays(int.Parse(value)); stTime = _timeLimitDomainService.WorkDay(DateTime.Now); DateTime stTime2 = _timeLimitDomainService.WorkDay(DateTime.Now); var (total, items) = await _orderRepository.Queryable(viewFilter:true) .WhereIF(dto.IsProvince.HasValue, x => x.IsProvince == dto.IsProvince) .WhereIF(!string.IsNullOrEmpty(dto.Keyword), x => x.Title.Contains(dto.Keyword!) || x.No.Contains(dto.Keyword!)) .Where(x => x.ExpiredTime != null && x.Status != EOrderStatus.Filed && x.Status != EOrderStatus.Published && x.Status != EOrderStatus.Visited && stTime >= x.ExpiredTime.Value && stTime2 <= x.ExpiredTime.Value) .OrderByDescending(x => x.CreationTime) .ToPagedListAsync(dto.PageIndex, dto.PageSize, cancellationToken); return new PagedDto(total, _mapper.Map>(items)); } // /// // /// 即将超期节点列表 // /// // /// // /// // /// //public async Task> GetAboutToExpireNodeAsync(AboutToExpireListDto dto, CancellationToken cancellationToken) //{ // var setting = _systemSettingCacheManager.GetSetting(SettingConstants.OrderAboutToExpire); // var value = setting?.SettingValue[0]; // value = string.IsNullOrEmpty(value) ? "0" : value; // DateTime stTime = DateTime.Now.AddDays(int.Parse(value)); // stTime = _timeLimitDomainService.WorkDay(DateTime.Now); // DateTime stTime2 = _timeLimitDomainService.WorkDay(DateTime.Now); // RefAsync total = 0; // var items = await Db.Queryable() // .LeftJoin((x, o) => x.ExternalId == o.Id) // .Where(x => x.ModuleCode == "OrderHandle") // .WhereIF(dto.IsProvince.HasValue, (x, o) => o.IsProvince == dto.IsProvince) // .WhereIF(!string.IsNullOrEmpty(dto.Keyword), (x, o) => o.Title.Contains(dto.Keyword!) || o.No.Contains(dto.Keyword!)) // .Where((x, o) => (int)x.Status < 20 && stTime >= x.ExpiredTime && stTime2 <= x.ExpiredTime) // .Select((x, o) => new WorkflowOrder { Order = o }, true) // .OrderByDescending(x => x.CreationTime) // .ToPageListAsync(dto.PageIndex, dto.PageSize, total, cancellationToken); // return new PagedDto(total, _mapper.Map>(items)); //} /// /// 已超期列表 /// /// /// /// public async Task> GetToExpireAsync(AboutToExpireListDto dto, CancellationToken cancellationToken) { DateTime stTime = _timeLimitDomainService.WorkDay(DateTime.Now); var (total, items) = await _orderRepository.Queryable(viewFilter:true) .WhereIF(dto.IsProvince.HasValue, x => x.IsProvince == dto.IsProvince) .WhereIF(!string.IsNullOrEmpty(dto.Keyword), x => x.Title.Contains(dto.Keyword!) || x.No.Contains(dto.Keyword!)) //.WhereIF(!string.IsNullOrEmpty(dto.No), x => x.No == dto.No) //.WhereIF(!string.IsNullOrEmpty(dto.Title), x => x.Title.Contains(dto.Title!)) .Where(x => x.ExpiredTime != null && (((x.Status == EOrderStatus.Filed || x.Status == EOrderStatus.Published || x.Status == EOrderStatus.Visited) && x.FiledTime >= x.ExpiredTime) || ((x.Status != EOrderStatus.Filed && x.Status != EOrderStatus.Published && x.Status != EOrderStatus.Visited) && stTime >= x.ExpiredTime.Value))) .OrderByDescending(x => x.CreationTime) .ToPagedListAsync(dto.PageIndex, dto.PageSize, cancellationToken); return new PagedDto(total, _mapper.Map>(items)); } // /// // /// 已超期节点列表 // /// // /// // /// // /// // public async Task> GetToExpireNodeAsync(AboutToExpireListDto dto, CancellationToken cancellationToken) // { // DateTime stTime = _timeLimitDomainService.WorkDay(DateTime.Now); //RefAsync total = 0; //var items= await Db.Queryable() // .LeftJoin((x,o)=>x.ExternalId == o.Id) // .Where(x => x.ModuleCode == "OrderHandle") // .WhereIF(dto.IsProvince.HasValue, (x, o) => o.IsProvince == dto.IsProvince) // .WhereIF(!string.IsNullOrEmpty(dto.Keyword), (x, o) => o.Title.Contains(dto.Keyword!) || o.No.Contains(dto.Keyword!)) // .Where((x,o) => (((int)x.Status >= 20 && x.EndTime >= x.ExpiredTime) || ((int)x.Status < 20 && stTime >= x.ExpiredTime))) // .Select((x, o) => new WorkflowOrder { Order = o }, true) // .OrderByDescending(x => x.CreationTime) // .ToPageListAsync(dto.PageIndex, dto.PageSize, total, cancellationToken); // return new PagedDto(total, _mapper.Map>(items)); // } /// /// 工单关键字分词 /// /// /// public async Task OrderParticiple(string inputStr, string orderId, CancellationToken cancellationToken) { var words = await _orderWrodRepository.Queryable().Where(x => x.IsEnable == 1 && x.Classify.Contains("普通标签")).Select(x => x.Tag).ToListAsync(cancellationToken); var res = new List(); if (words.Any()) res = ParticipleTool.SegMMDouble(inputStr, ref words); var participles = await _orderWrodRepository.Queryable().In(x => x.Tag, res).ToListAsync(cancellationToken); if (participles.Any()) { //关键词 var tags = participles.Select(x => x.Tag).ToList(); var tagsStr = string.Join(",", tags); await _orderRepository.Updateable().SetColumns(x => x.TagNames == tagsStr).Where(x => x.Id == orderId).ExecuteCommandAsync(cancellationToken); List synonyms = participles.Select(x => x.Synonym).ToList(); if (synonyms.Any()) { var synonymsStr = string.Join(",", synonyms); synonyms = synonymsStr.Split(",").Distinct().ToList(); tags.AddRange(synonyms); } var vector = await _orderRepository.Queryable().Where(x => x.Id == orderId).ToListAsync(cancellationToken); if (vector.Any()) await _repositoryts.UpdateVectorAsync(orderId, tags, cancellationToken); else await _repositoryts.AddVectorAsync(orderId, DateTime.Now, tags, cancellationToken); } } /// /// 接收外部平台工单 /// public Task ReceiveOrderFromExternalAsync(AddOrderDto dto, CancellationToken cancellationToken) { switch (dto.Source) { case ESource.ProvinceStraight: return ReceiveOrderFromProvinceAsync(dto, dto.Files, cancellationToken); case ESource.Police110: case ESource.CityDataExchangeLz: case ESource.ConvergenceMedia: case ESource.WebPortal: return ReceiveOrderFromOtherPlatformAsync(dto, dto.Files, cancellationToken); case ESource.Hotline: case ESource.HotlineImport: default: throw new ArgumentOutOfRangeException(); } } #region private /// /// 接受外部工单(除省平台) /// /// /// /// private async Task ReceiveOrderFromOtherPlatformAsync(AddOrderDto dto, List files, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(dto.ExternalId)) throw new UserFriendlyException("工单外部编号不能为空"); var order = await _orderRepository.Queryable() .FirstAsync(d => d.ExternalId == dto.ExternalId, cancellationToken); if (order == null) { order = _mapper.Map(dto); order.InitId(); if (files != null && files.Any()) order.FileJson = await _fileRepository.AddFileAsync(files, order.Id, "", cancellationToken); await _orderDomainService.AddAsync(order, cancellationToken: cancellationToken); } else { _mapper.Map(dto, order); if (files != null && files.Any()) order.FileJson = await _fileRepository.AddFileAsync(files, order.Id, "", cancellationToken); await _orderRepository.UpdateAsync(order, cancellationToken); } return _mapper.Map(order); } /// /// 接受省平台工单 /// private async Task ReceiveOrderFromProvinceAsync(AddOrderDto dto, List files, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(dto.ProvinceNo)) throw new UserFriendlyException("无效省工单编号"); var orderExtension = await _orderDomainService.GetOrderExtensionsAsync(dto.ProvinceNo, cancellationToken); var order = await _orderRepository.GetAsync(d => d.ProvinceNo == dto.ProvinceNo, cancellationToken); if (order is null) { order = _mapper.Map(dto); order.InitId(); if (files != null && files.Any()) order.FileJson = await _fileRepository.AddFileAsync(files, order.Id, "", cancellationToken); await _orderDomainService.AddAsync(order, cancellationToken: cancellationToken); if (orderExtension is not null) { orderExtension.Id = order.Id; if (dto.OrderExtension != null) _mapper.Map(dto.OrderExtension, orderExtension); await _orderDomainService.UpdateExtensionAsync(orderExtension, cancellationToken); } } else { _mapper.Map(dto, order); if (files != null && files.Any()) order.FileJson = await _fileRepository.AddFileAsync(files, order.Id, "", cancellationToken); await _orderRepository.UpdateAsync(order, cancellationToken); if (orderExtension is not null) { orderExtension.Id = order.Id; if (dto.OrderExtension != null) _mapper.Map(dto.OrderExtension, orderExtension); await _orderDomainService.UpdateExtensionAsync(orderExtension, cancellationToken); } //特提(撤回至发起) if (!string.IsNullOrEmpty(order.WorkflowId)) await _workflowDomainService.RecallToStartStepAsync(order.WorkflowId, "省工单重派", cancellationToken); } return _mapper.Map(order); } #endregion }