|  |  | 
 |  |  | using Masuit.Tools; | 
 |  |  | using SixLabors.Fonts.Tables.AdvancedTypographic; | 
 |  |  | using SqlSugar; | 
 |  |  | using System.Linq.Expressions; | 
 |  |  | using System.Text.RegularExpressions; | 
 |  |  | using System.Threading.Tasks; | 
 |  |  | using WIDESEA_Cache; | 
 |  |  | 
 |  |  | using WIDESEA_IServices; | 
 |  |  | using WIDESEA_IStoragIntegrationServices; | 
 |  |  | using WIDESEA_Model.Models; | 
 |  |  | using WIDESEA_Repository; | 
 |  |  | using WIDESEAWCS_BasicInfoRepository; | 
 |  |  | using WIDESEAWCS_QuartzJob.Models; | 
 |  |  |  | 
 |  |  | 
 |  |  |     private readonly IAgingInOrOutInputService _agingInOrOutInputService; //静置\陈化 | 
 |  |  |     private readonly IDt_StationManagerRepository _stationManagerRepository; | 
 |  |  |     private readonly ISys_ConfigService _configService; | 
 |  |  |     private readonly IDt_ChangeoversRepository _dt_ChangeoversRepository; | 
 |  |  |     private readonly ISimpleCacheService _simpleCacheService; | 
 |  |  |  | 
 |  |  |     public Dt_TaskService(IDt_TaskRepository BaseDal, | 
 |  |  | 
 |  |  |                                 IStockInfoDetailRepository stockInfoDetailRepository, | 
 |  |  |                                 IDt_StationManagerRepository stationManagerRepository, | 
 |  |  |                                 ISys_ConfigService configService, | 
 |  |  |                                 IDt_ChangeoversRepository dt_ChangeoversRepository, | 
 |  |  |                                 ISimpleCacheService simpleCacheService) : base(BaseDal) | 
 |  |  |     { | 
 |  |  |         _unitOfWorkManage = unitOfWorkManage; | 
 |  |  | 
 |  |  |         _stockInfoDetailRepository = stockInfoDetailRepository; | 
 |  |  |         _stationManagerRepository = stationManagerRepository; | 
 |  |  |         _configService = configService; | 
 |  |  |         _dt_ChangeoversRepository = dt_ChangeoversRepository; | 
 |  |  |         _simpleCacheService = simpleCacheService; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |         } | 
 |  |  |         LogFactory.GetLog("任务完成").InfoFormat(true, "验证任务是否存在", JsonConvert.SerializeObject(task)); | 
 |  |  |  | 
 |  |  |         if (task.TaskType == (int)TaskOutboundTypeEnum.InToOut) | 
 |  |  |         { | 
 |  |  |             return await CompleteInToOutTaskAsync(task); | 
 |  |  |         } | 
 |  |  |         | 
 |  |  |         // 验证库存是否存在 | 
 |  |  |         var stock = await _stockInfoRepository.QueryFirstNavAsync(x => x.PalletCode == task.PalletCode); | 
 |  |  |  | 
 |  |  |         if (task.TaskType == (int)TaskOutboundTypeEnum.InToOut) | 
 |  |  |         { | 
 |  |  |             return await CompleteInToOutTaskAsync(task,stock); | 
 |  |  |         } | 
 |  |  |         // 根据任务类型调用相应的完成任务方法 | 
 |  |  |         switch (task.TaskType) | 
 |  |  |         { | 
 |  |  | 
 |  |  |             var stock = await QueryStockInfo(input.PalletCode); | 
 |  |  |             if (stock != null) | 
 |  |  |             { | 
 |  |  |                 List<string> strings = new List<string>() { "GW", "CW", "FR" }; | 
 |  |  |                 if (stock.AreaCode.Contains(strings)) | 
 |  |  |                 { | 
 |  |  |                     return content.Error($"托盘【{stock.PalletCode}】存在库存不允许入库"); | 
 |  |  |                 } | 
 |  |  |                 Dt_Task taskNew = new Dt_Task | 
 |  |  |                 { | 
 |  |  |                     Grade = 1, | 
 |  |  | 
 |  |  |             else | 
 |  |  |             { | 
 |  |  |                 // 处理异常电芯情况 | 
 |  |  |                 var serialNosError = result.SerialNos.Where(x => x.SerialNoStatus != 1 && x.SerialNoStatus != 4).ToList(); | 
 |  |  |                 if (serialNosError.Count > 0) | 
 |  |  |                 { | 
 |  |  |                     return await HandleErrorCells(input, area, serialNosError); | 
 |  |  |                 } | 
 |  |  |                 #region 电芯分结果异常不需要我们处理 | 
 |  |  |                 //var serialNosError = result.SerialNos.Where(x => x.SerialNoStatus != 1 && x.SerialNoStatus != 4).ToList(); | 
 |  |  |                 //if (serialNosError.Count > 0) | 
 |  |  |                 //{ | 
 |  |  |                 //    return await HandleErrorCells(input, area, serialNosError); | 
 |  |  |                 //} | 
 |  |  |                 #endregion | 
 |  |  |  | 
 |  |  |                 var boxing = CreateBoxingInfo(result, input.PalletCode); | 
 |  |  |                 if (boxing == null) return content.Error("组盘失败"); | 
 |  |  | 
 |  |  |     // 获取组盘信息 | 
 |  |  |     private DtBoxingInfo CreateBoxingInfo(ResultTrayCellsStatus result, string palletCode) | 
 |  |  |     { | 
 |  |  |         return new DtBoxingInfo | 
 |  |  |         var boxing = _boxingInfoRepository.QueryFirst(x => x.PalletCode == palletCode); | 
 |  |  |         if (boxing == null) | 
 |  |  |         { | 
 |  |  |             PalletCode = palletCode, | 
 |  |  |             IsFull = true, | 
 |  |  |             ProcessCode = result.ProcessCode, | 
 |  |  |             ProductionLine = result.ProductionLine, | 
 |  |  |             BoxingInfoDetails = result.SerialNos.Select(serialNoObj => new DtBoxingInfoDetail | 
 |  |  |             return new DtBoxingInfo | 
 |  |  |             { | 
 |  |  |                 SerialNumber = serialNoObj.SerialNo, | 
 |  |  |                 OrderNo = serialNoObj.PositionNo.ToString(), | 
 |  |  |                 Status = serialNoObj.SerialNoStatus, | 
 |  |  |                 MaterielCode = result.BindCode, | 
 |  |  |                 Remark = result.TrayBarcodePropertys.ToJsonString(), | 
 |  |  |             }).ToList() | 
 |  |  |         }; | 
 |  |  |                 PalletCode = palletCode, | 
 |  |  |                 IsFull = true, | 
 |  |  |                 ProcessCode = result.ProcessCode, | 
 |  |  |                 ProductionLine = result.ProductionLine, | 
 |  |  |                 BoxingInfoDetails = result.SerialNos.Select(serialNoObj => new DtBoxingInfoDetail | 
 |  |  |                 { | 
 |  |  |                     SerialNumber = serialNoObj.SerialNo, | 
 |  |  |                     OrderNo = serialNoObj.PositionNo.ToString(), | 
 |  |  |                     Status = serialNoObj.SerialNoStatus, | 
 |  |  |                     MaterielCode = result.BindCode, | 
 |  |  |                     Remark = result.TrayBarcodePropertys.ToJsonString(), | 
 |  |  |                 }).ToList() | 
 |  |  |             }; | 
 |  |  |         } | 
 |  |  |         else | 
 |  |  |         { | 
 |  |  |             _boxingInfoRepository.Db.DeleteNav<DtBoxingInfo>(x => x.Id == boxing.Id) | 
 |  |  |                                             .Include(x => x.BoxingInfoDetails) | 
 |  |  |                                             .ExecuteCommandAsync(); | 
 |  |  |             return new DtBoxingInfo | 
 |  |  |             { | 
 |  |  |                 PalletCode = palletCode, | 
 |  |  |                 IsFull = true, | 
 |  |  |                 ProcessCode = result.ProcessCode, | 
 |  |  |                 ProductionLine = result.ProductionLine, | 
 |  |  |                 BoxingInfoDetails = result.SerialNos.Select(seriaINoObj => new DtBoxingInfoDetail | 
 |  |  |                 { | 
 |  |  |                     SerialNumber = seriaINoObj.SerialNo, | 
 |  |  |                     OrderNo = seriaINoObj.PositionNo.ToString(), | 
 |  |  |                     Status = seriaINoObj.SerialNoStatus, | 
 |  |  |                     MaterielCode = result.BindCode, | 
 |  |  |                     Remark = result.TrayBarcodePropertys.ToJsonString(), | 
 |  |  |                 }).ToList() | 
 |  |  |             }; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     // 获取工艺申请 | 
 |  |  | 
 |  |  |                 return null; | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             var outBoundMateriel = AppSettings.app<OutBoundMateriel>("OutBoundMateriel"); | 
 |  |  |             //var outBoundMateriel = AppSettings.app<OutBoundMateriel>("OutBoundMateriel"); | 
 |  |  |  | 
 |  |  |             var outBoundMateriel = _dt_ChangeoversRepository.QueryData(x => x.Status == "1").ToList(); | 
 |  |  |             List<string>? materielCodes = outBoundMateriel.Count != 0 | 
 |  |  |                 ? outBoundMateriel.Where(x => x.ProductionLine == productionLine && x.ProcessCode == area.AreaCode) | 
 |  |  |                                   .Select(x => x.MaterielCode) | 
 |  |  | 
 |  |  |             //    materielCodes = outBoundMateriel.Where(x => x.ProductionLine == productionLine && x.ProcessCode == areaCodes[0]).Select(x => x.MaterielCode).ToList(); | 
 |  |  |             //} | 
 |  |  |  | 
 |  |  |             var outBoundMateriel = AppSettings.app<OutBoundMateriel>("OutBoundMateriel"); | 
 |  |  |             //var outBoundMateriel = AppSettings.app<OutBoundMateriel>("OutBoundMateriel"); | 
 |  |  |  | 
 |  |  |             var outBoundMateriel = _dt_ChangeoversRepository.QueryData(x => x.Status == "1").ToList(); | 
 |  |  |             List<string>? materielCodes = outBoundMateriel.Count != 0 | 
 |  |  |                 ? outBoundMateriel.Where(x => x.ProductionLine == productionLine && x.ProcessCode == areaCodes[0]) | 
 |  |  |                                   .Select(x => x.MaterielCode) | 
 |  |  | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |     #endregion | 
 |  |  |     public WebResponseContent GetTimeout() | 
 |  |  |     { | 
 |  |  |         WebResponseContent content = new WebResponseContent(); | 
 |  |  |         try | 
 |  |  |         { | 
 |  |  |             var now = DateTime.Now; | 
 |  |  |             // 使用Subtract方法 | 
 |  |  |             var threeHoursAgo = now.Subtract(TimeSpan.FromHours(3)); | 
 |  |  |  | 
 |  |  |             List<DtStockInfo> dtStocks = _stockInfoRepository.Db.Queryable<DtStockInfo>() | 
 |  |  |                                .Where(x => x.OutboundTime < threeHoursAgo).ToList(); | 
 |  |  |             return content.OK1(total1: dtStocks.Count, data: dtStocks); | 
 |  |  |         } | 
 |  |  |         catch (Exception ex) | 
 |  |  |         { | 
 |  |  |             return content.Error(ex.Message); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public WebResponseContent Getproductionstatistics() | 
 |  |  |     { | 
 |  |  |         WebResponseContent content = new WebResponseContent(); | 
 |  |  |         try | 
 |  |  |         { | 
 |  |  |             var now = DateTime.Now; | 
 |  |  |             var thirtyDaysAgo = now.AddDays(-30); | 
 |  |  |             var roadwayMappings = new Dictionary<string, string> { | 
 |  |  |                     { "JZ", "静置库" }, | 
 |  |  |                     { "CH", "陈化库" }, | 
 |  |  |                     { "FR", "分容库" }, | 
 |  |  |                     { "GW", "高温库" }, | 
 |  |  |                     { "CW", "常温库" } | 
 |  |  |                 }; | 
 |  |  |  | 
 |  |  |             var roadwayKeys = roadwayMappings.Keys.ToArray(); | 
 |  |  |  | 
 |  |  |             var taskHty = _task_HtyRepository.Db.Queryable<Dt_Task_Hty>() | 
 |  |  |                 .Where(it => | 
 |  |  |                     it.CreateDate >= thirtyDaysAgo && | 
 |  |  |                     it.CreateDate <= now && | 
 |  |  |                     it.TaskType == 100 && | 
 |  |  |                     roadwayKeys.Any(rk => it.Roadway.Contains(rk))) | 
 |  |  |                 .ToList() | 
 |  |  |                 .Select(t => new { | 
 |  |  |                     OriginalRoadway = t.Roadway, | 
 |  |  |                     MatchedKey = roadwayKeys.FirstOrDefault(rk => t.Roadway.Contains(rk)), | 
 |  |  |                     CreateDate = t.CreateDate | 
 |  |  |                 }) | 
 |  |  |                 .Where(t => t.MatchedKey != null) | 
 |  |  |                 .GroupBy(t => new { | 
 |  |  |                     RoadwayKey = t.MatchedKey, | 
 |  |  |                     Date = t.CreateDate.Date, | 
 |  |  |                     Hour = t.CreateDate.Hour | 
 |  |  |                 }) | 
 |  |  |                 .OrderByDescending(group => group.Key.Date) | 
 |  |  |                 .ThenByDescending(group => group.Key.Hour) | 
 |  |  |                 .Select(group => new { | 
 |  |  |                     Hour = $"{group.Key.Date:yyyy/M/d} {group.Key.Hour}:00", | 
 |  |  |                     Count = group.Count(), | 
 |  |  |                     Roadway = roadwayMappings[group.Key.RoadwayKey] // 映射为中文名称 | 
 |  |  |                 }) | 
 |  |  |                 .ToList(); | 
 |  |  |             return content.OK1(total1: taskHty.Count, data: taskHty); | 
 |  |  |         } | 
 |  |  |         catch (Exception ex) | 
 |  |  |         { | 
 |  |  |             return content.Error(ex.Message); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |     #endregion 外部接口方法 | 
 |  |  |  | 
 |  |  |     #region 内部调用方法 | 
 |  |  | 
 |  |  |         // 创建历史任务实例模型 | 
 |  |  |         try | 
 |  |  |         { | 
 |  |  |             Dt_Task task = BaseDal.QueryFirst(x => x.TaskId == Convert.ToInt32(key[0])); | 
 |  |  |             if (task == null) | 
 |  |  |             foreach (var item in key) | 
 |  |  |             { | 
 |  |  |                 return content.Error("未找到任务信息!"); | 
 |  |  |                 Dt_Task task = BaseDal.QueryFirst(x => x.TaskId == Convert.ToInt32(key)); | 
 |  |  |                 if (task == null) | 
 |  |  |                 { | 
 |  |  |                     return content.Error("未找到任务信息!"); | 
 |  |  |                 } | 
 |  |  |                 var taskHtyNG = CreateHistoricalTask(task, true); | 
 |  |  |  | 
 |  |  |                 // 执行数据库事务 | 
 |  |  |  | 
 |  |  |                 // 添加历史任务 | 
 |  |  |                 var isTaskHtyAdd = _task_HtyRepository.AddData(taskHtyNG) > 0; | 
 |  |  |  | 
 |  |  |                 // 删除任务数据 | 
 |  |  |                 var isTaskDelete = BaseDal.Delete(task.TaskId); | 
 |  |  |             } | 
 |  |  |             var taskHtyNG = CreateHistoricalTask(task, true); | 
 |  |  |  | 
 |  |  |             // 执行数据库事务 | 
 |  |  |  | 
 |  |  |             // 添加历史任务 | 
 |  |  |             var isTaskHtyAdd = _task_HtyRepository.AddData(taskHtyNG) > 0; | 
 |  |  |  | 
 |  |  |             // 删除任务数据 | 
 |  |  |             var isTaskDelete = BaseDal.Delete(task.TaskId); | 
 |  |  |  | 
 |  |  |             return content.OK("删除成功!"); | 
 |  |  |         } | 
 |  |  | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             #region 更新库存信息(暂时不需要) | 
 |  |  |             if (stock.IsFull) | 
 |  |  |             { | 
 |  |  |                 // 查询符合条件的库存信息 | 
 |  |  |                 var stocks = _stockInfoRepository.QueryData(x => x.AreaCode == stock.AreaCode && x.ProductionLine == stock.ProductionLine && x.SpecialParameterDuration != stock.SpecialParameterDuration); | 
 |  |  |             //if (stock.IsFull) | 
 |  |  |             //{ | 
 |  |  |             //    // 查询符合条件的库存信息 | 
 |  |  |             //    var stocks = _stockInfoRepository.QueryData(x => x.AreaCode == stock.AreaCode && x.ProductionLine == stock.ProductionLine && x.SpecialParameterDuration != stock.SpecialParameterDuration); | 
 |  |  |  | 
 |  |  |                 // 查询任务信息 | 
 |  |  |                 var tasks = BaseDal.QueryData(x => x.PalletCode != stock.PalletCode && x.ProductionLine == stock.ProductionLine).Select(x => x.PalletCode).ToList(); | 
 |  |  |             //    // 查询任务信息 | 
 |  |  |             //    var tasks = BaseDal.QueryData(x => x.PalletCode != stock.PalletCode && x.ProductionLine == stock.ProductionLine).Select(x => x.PalletCode).ToList(); | 
 |  |  |  | 
 |  |  |                 if (stocks != null && stocks.Count > 0) | 
 |  |  |                 { | 
 |  |  |                     // 过滤出需要更新的库存信息 | 
 |  |  |                     var stocksToUpdate = stocks.Where(item => !tasks.Contains(item.PalletCode)).ToList(); | 
 |  |  |                     foreach (var item in stocksToUpdate) | 
 |  |  |                     { | 
 |  |  |                         // 更新库存信息的特定参数 | 
 |  |  |                         item.SpecialParameterDuration = stock.SpecialParameterDuration; | 
 |  |  |                         item.ParameterInfos = stock.ParameterInfos; | 
 |  |  |                         item.OutboundTime = Convert.ToDateTime(item.LinedProcessFeedbackTime == null ? item.CreateDate : item.LinedProcessFeedbackTime).AddHours(Convert.ToDouble(stock.SpecialParameterDuration)); | 
 |  |  |                     } | 
 |  |  |                     if (stocksToUpdate.Count > 0) | 
 |  |  |                     { | 
 |  |  |                         // 异步更新库存信息 | 
 |  |  |                         var isUpdates = await _stockInfoRepository.UpdateDataAsync(stocksToUpdate); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             //    if (stocks != null && stocks.Count > 0) | 
 |  |  |             //    { | 
 |  |  |             //        // 过滤出需要更新的库存信息 | 
 |  |  |             //        var stocksToUpdate = stocks.Where(item => !tasks.Contains(item.PalletCode)).ToList(); | 
 |  |  |             //        foreach (var item in stocksToUpdate) | 
 |  |  |             //        { | 
 |  |  |             //            // 更新库存信息的特定参数 | 
 |  |  |             //            item.SpecialParameterDuration = stock.SpecialParameterDuration; | 
 |  |  |             //            item.ParameterInfos = stock.ParameterInfos; | 
 |  |  |             //            item.OutboundTime = Convert.ToDateTime(item.LinedProcessFeedbackTime == null ? item.CreateDate : item.LinedProcessFeedbackTime).AddHours(Convert.ToDouble(stock.SpecialParameterDuration)); | 
 |  |  |             //        } | 
 |  |  |             //        if (stocksToUpdate.Count > 0) | 
 |  |  |             //        { | 
 |  |  |             //            // 异步更新库存信息 | 
 |  |  |             //            var isUpdates = await _stockInfoRepository.UpdateDataAsync(stocksToUpdate); | 
 |  |  |             //        } | 
 |  |  |             //    } | 
 |  |  |             //} | 
 |  |  |             #endregion | 
 |  |  |  | 
 |  |  |             // 添加历史任务 | 
 |  |  | 
 |  |  |                 // 获取目标地址和更新任务状态 | 
 |  |  |  | 
 |  |  |                 input.Position = Regex.Replace(input.Position, @"-(\d+)", ""); | 
 |  |  |                 if (Convert.ToInt32(input.Position) > 1999) | 
 |  |  |                 { | 
 |  |  |                     input.Position = (Convert.ToInt32(input.Position) - 1000).ToString(); | 
 |  |  |                 } | 
 |  |  |                 //if (Convert.ToInt32(input.Position) > 1999) | 
 |  |  |                 //{ | 
 |  |  |                 //    input.Position = (Convert.ToInt32(input.Position) - 1000).ToString(); | 
 |  |  |                 //} | 
 |  |  |  | 
 |  |  |                 if (task.TaskType == (int)TaskInboundTypeEnum.InNG) | 
 |  |  |                 { | 
 |  |  | 
 |  |  |             ToAddress = await GetRoadWayAsync(process); | 
 |  |  |         else | 
 |  |  |             ToAddress = process[0]; | 
 |  |  |  | 
 |  |  |         if (string.IsNullOrEmpty(ToAddress)) | 
 |  |  |         { | 
 |  |  |             return content.Error("无法获取目标地址"); | 
 |  |  |         } | 
 |  |  |         // 创建新任务实例 | 
 |  |  |         var task = new Dt_Task | 
 |  |  |         { |