using Hotline.Caching.Interfaces; 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.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; public OrderApplication( IOrderDomainService orderDomainService, IOrderRepository orderRepository, IWorkflowDomainService workflowDomainService, ITimeLimitDomainService timeLimitDomainService, ISystemSettingCacheManager systemSettingCacheManager, IMapper mapper, IRepository orderWrodRepository, IRepositoryTextSearch repositoryts ) { _orderDomainService = orderDomainService; _workflowDomainService = workflowDomainService; _orderRepository = orderRepository; _timeLimitDomainService = timeLimitDomainService; _mapper = mapper; _systemSettingCacheManager = systemSettingCacheManager; _orderWrodRepository = orderWrodRepository; _repositoryts = repositoryts; } /// /// 更新工单办理期满时间 /// 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.TimeLimit = expiredTimeConfig.TimeText; order.TimeLimitCount = expiredTimeConfig.Count; order.TimeLimitUnit = expiredTimeConfig.TimeType; order.ExpiredTime = expiredTimeConfig.ExpiredTime; 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, 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); } //归档工单 order.File(); 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() .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() .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); } } }