| | |
| | | using WIDESEA_InboundRepository; |
| | | using System.Drawing.Printing; |
| | | using System; |
| | | using WIDESEA_Common.Log; |
| | | using static WIDESEA_Common.HouseInboundPassBack.data.data1; |
| | | |
| | | namespace WIDESEA_TaskInfoService |
| | | { |
| | |
| | | private readonly IStockInfoDetailRepository _stockInfoDetailRepository; |
| | | private readonly IReturnOrderRepository _returnOrderRepository; |
| | | private readonly IProductionRepository _productionRepository; |
| | | private readonly IInboundRepository _inboundRepository; |
| | | private readonly IInboundRepository _inboundRepository; |
| | | |
| | | public ITaskRepository Repository => BaseDal; |
| | | |
| | | public TaskService(ITaskRepository BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IMaterielInfoService materielInfoService, IInboundOrderDetail_HtyService inboundOrderDetail_HtyService, IOutboundOrder_HtyService outboundOrder_HtyService, IOutboundOrderDetail_HtyService outboundOrderDetail_HtyService, IInboundOrder_HtyService inboundOrder_HtyService, IStockRepository stockRepository, IInboundOrderDetailService inboundOrderDetailService, IBasicService basicService, IOutboundService outboundService, IInboundService inboundService, IRecordService recordService, IStockService stockService, ITask_HtyService taskHtyService, ILocationInfoService locationInfoService, IOutboundOrderDetailRepository outboundOrderDetailRepository, IBasicRepository basicRepository, IStockInfoDetailRepository stockInfoDetailRepository, IPalletTypeInfoRepository palletTypeInfoRepository, IReturnOrderRepository returnOrderRepository, IProductionRepository productionRepository,IInboundRepository inboundRepository) : base(BaseDal) |
| | | public TaskService(ITaskRepository BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IMaterielInfoService materielInfoService, IInboundOrderDetail_HtyService inboundOrderDetail_HtyService, IOutboundOrder_HtyService outboundOrder_HtyService, IOutboundOrderDetail_HtyService outboundOrderDetail_HtyService, IInboundOrder_HtyService inboundOrder_HtyService, IStockRepository stockRepository, IInboundOrderDetailService inboundOrderDetailService, IBasicService basicService, IOutboundService outboundService, IInboundService inboundService, IRecordService recordService, IStockService stockService, ITask_HtyService taskHtyService, ILocationInfoService locationInfoService, IOutboundOrderDetailRepository outboundOrderDetailRepository, IBasicRepository basicRepository, IStockInfoDetailRepository stockInfoDetailRepository, IPalletTypeInfoRepository palletTypeInfoRepository, IReturnOrderRepository returnOrderRepository, IProductionRepository productionRepository, IInboundRepository inboundRepository) : base(BaseDal) |
| | | { |
| | | _mapper = mapper; |
| | | _stockRepository = stockRepository; |
| | |
| | | public string ReceiveERPTaskout = WIDESEA_Core.Helper.AppSettings.Configuration["ReceiveERPTaskout"]; |
| | | public string InMaterialWarehousingCallback = WIDESEA_Core.Helper.AppSettings.Configuration["InMaterialWarehousingCallback"]; |
| | | |
| | | |
| | | /// <summary> |
| | | /// 任务信息推送至WCS |
| | | /// </summary> |
| | |
| | | } |
| | | } |
| | | |
| | | public WebResponseContent PushTasksWCS(List<Dt_Task> tasks, string agvDescription = "") |
| | | { |
| | | try |
| | | { |
| | | List<WMSTaskDTO> taskDTOs = _mapper.Map<List<WMSTaskDTO>>(tasks); |
| | | taskDTOs.ForEach(x => |
| | | { |
| | | x.AGVArea = agvDescription; |
| | | }); |
| | | string url = AppSettings.app("WCS"); |
| | | if (string.IsNullOrEmpty(url)) |
| | | { |
| | | return WebResponseContent.Instance.Error($"未找到WCSApi地址,请检查配置文件"); |
| | | } |
| | | string response = HttpHelper.Post($"{url}/api/Task/ReceiveCPTask", taskDTOs.Serialize()); |
| | | |
| | | return JsonConvert.DeserializeObject<WebResponseContent>(response) ?? WebResponseContent.Instance.Error("返回错误"); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | return WebResponseContent.Instance.Error(ex.Message); |
| | | } |
| | | } |
| | | |
| | | public WebResponseContent FeedBackWCSTaskCompleted(int taskNum) |
| | | { |
| | | try |
| | |
| | | { |
| | | return WebResponseContent.Instance.Error($"未找到WCSApi地址,请检查配置文件"); |
| | | } |
| | | string response = HttpHelper.Get($"{url}/api/Task/RecWMSTaskCompleted?taskNum=" + taskNum); |
| | | string response = HttpHelper.Post($"{url}/api/Task/RecWMSTaskCompleted?taskNum=" + taskNum); |
| | | |
| | | return JsonConvert.DeserializeObject<WebResponseContent>(response) ?? WebResponseContent.Instance.Error("返回错误"); |
| | | } |
| | |
| | | |
| | | dt_StockInfo.LocationCode = dt_LocationInfo.LocationCode; |
| | | |
| | | |
| | | Dt_InboundOrder? inboundOrder = _inboundService.InbounOrderService.Db.Queryable<Dt_InboundOrder>().Where(x => x.OrderNo == dt_StockInfo.Details.FirstOrDefault().OrderNo).Includes(x => x.Details).First(); |
| | | Dt_InboundOrderDetail? inboundOrderDetail = null; |
| | | |
| | | // 3. 处理入库逻辑 |
| | | if (task.TaskType == TaskTypeEnum.Inbound.ObjToInt()) |
| | | if (warehouse.WarehouseCode.Contains("TestJCLK")) |
| | | { |
| | | return ProcessInbound(task, dt_StockInfo, dt_LocationInfo); |
| | | } |
| | | if (task.TaskType == TaskTypeEnum.SurplusReturn.ObjToInt() || task.TaskType == TaskTypeEnum.ReverseIn.ObjToInt()) |
| | | { |
| | | return ReturnInbound(task, dt_StockInfo, dt_LocationInfo); |
| | | } |
| | | if(task.TaskType == TaskTypeEnum.InInventory.ObjToInt()) |
| | | { |
| | | int lastStatus = dt_LocationInfo.LocationStatus; |
| | | dt_LocationInfo.LocationStatus = LocationStatusEnum.InStock.ObjToInt(); |
| | | dt_StockInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt(); |
| | | // 更新库存明细状态 |
| | | dt_StockInfo.Details.ForEach(x => x.Status = StockStatusEmun.入库完成.ObjToInt()); |
| | | _unitOfWorkManage.BeginTran(); |
| | | UpdateTaskStatus(task); |
| | | UpdateLocationStatus(dt_LocationInfo, lastStatus); |
| | | UpdateStockInfo(dt_StockInfo); |
| | | AddRecords(task, dt_StockInfo, dt_LocationInfo, lastStatus); |
| | | _unitOfWorkManage.CommitTran(); |
| | | return WebResponseContent.Instance.OK(); |
| | | // 1. 获取入库单信息 |
| | | Dt_InboundOrder? inboundOrder = null; |
| | | if (!string.IsNullOrEmpty(dt_StockInfo.Details.FirstOrDefault()?.OrderNo)) |
| | | { |
| | | inboundOrder = _inboundService.InbounOrderService.Db.Queryable<Dt_InboundOrder>() |
| | | .Where(x => x.OrderNo == dt_StockInfo.Details.FirstOrDefault().OrderNo) |
| | | .Includes(x => x.Details) |
| | | .First(); |
| | | } |
| | | |
| | | // 3. 处理入库逻辑 |
| | | if (task.TaskType == TaskTypeEnum.Inbound.ObjToInt()) |
| | | { |
| | | int lastStatus = dt_LocationInfo.LocationStatus; |
| | | dt_LocationInfo.LocationStatus = LocationStatusEnum.InStock.ObjToInt(); |
| | | |
| | | if (task.TaskType == TaskTypeEnum.Inbound.ObjToInt() && inboundOrder == null) |
| | | { |
| | | return WebResponseContent.Instance.Error("未找到入库单信息"); |
| | | } |
| | | var allBoxCodesForDetail = BaseDal.Db.Queryable<Dt_StockInfoDetailCP>() |
| | | .Where(x => x.StockDetailId == dt_StockInfo.Details.FirstOrDefault().Id) |
| | | .Select(x => x.BoxCode) |
| | | .ToList(); |
| | | |
| | | // 2. 获取托盘下的所有箱号 |
| | | var boxCodes = new List<string>(); |
| | | |
| | | // 方法1:从库存明细的BoxCodes字段获取(组盘时保存的) |
| | | foreach (var detail in allBoxCodesForDetail) |
| | | { |
| | | if (!string.IsNullOrEmpty(detail)) |
| | | { |
| | | var codes = detail.Split(',', StringSplitOptions.RemoveEmptyEntries); |
| | | boxCodes.AddRange(codes); |
| | | } |
| | | } |
| | | |
| | | // 根据托盘明细获取对应的入库单明细 |
| | | List<Dt_InboundOrderDetail> inboundOrderDetails = new List<Dt_InboundOrderDetail>(); |
| | | |
| | | // 处理多个库存明细的情况 |
| | | foreach (var stockDetail in dt_StockInfo.Details) |
| | | { |
| | | if (!string.IsNullOrEmpty(stockDetail.OrderNo)) |
| | | { |
| | | var detail = inboundOrder.Details.FirstOrDefault(x => x.LinId == stockDetail.LinId && x.BatchNo == stockDetail.BatchNo); |
| | | |
| | | if (detail != null && !inboundOrderDetails.Any(x => x.Id == detail.Id)) |
| | | { |
| | | inboundOrderDetails.Add(detail); |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (!inboundOrderDetails.Any()) |
| | | { |
| | | return WebResponseContent.Instance.Error($"未找到对应的入库单明细"); |
| | | } |
| | | |
| | | // 更新入库单明细状态 |
| | | foreach (var inboundOrderDetail in inboundOrderDetails) |
| | | { |
| | | // 检查该明细是否已全部入库 |
| | | decimal totalStockQuantity = dt_StockInfo.Details |
| | | .Where(x => x.LinId == inboundOrderDetail.LinId && x.BatchNo == inboundOrderDetail.BatchNo) |
| | | .Sum(x => x.StockQuantity); |
| | | |
| | | decimal orderQuantity = Convert.ToDecimal(inboundOrderDetail.OrderQuantity); |
| | | decimal receiptQuantity = Convert.ToDecimal(inboundOrderDetail.ReceiptQuantity); |
| | | decimal overInQuantity = Convert.ToDecimal(inboundOrderDetail.OverInQuantity); |
| | | |
| | | // 更新收货数量 |
| | | inboundOrderDetail.OverInQuantity = totalStockQuantity += overInQuantity; |
| | | |
| | | // 检查是否全部完成 |
| | | if (inboundOrderDetail.OverInQuantity == inboundOrderDetail.OrderQuantity) |
| | | { |
| | | inboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.Over.ObjToInt(); |
| | | } |
| | | else |
| | | { |
| | | inboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.Inbounding.ObjToInt(); |
| | | } |
| | | } |
| | | |
| | | ///更新库存状态 |
| | | dt_StockInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt(); |
| | | // 更新库存明细状态 |
| | | dt_StockInfo.Details.ForEach(x => x.Status = StockStatusEmun.入库完成.ObjToInt()); |
| | | |
| | | // 3. 查询成品入库明细(CP入库单明细) |
| | | List<Dt_CPInboundOrderDetail> cpInboundDetails = new List<Dt_CPInboundOrderDetail>(); |
| | | if (boxCodes.Any()) |
| | | { |
| | | cpInboundDetails = BaseDal.Db.Queryable<Dt_CPInboundOrderDetail>() |
| | | .Where(x => boxCodes.Contains(x.BoxCode)) |
| | | .ToList(); |
| | | } |
| | | |
| | | // 更新成品入库明细状态 |
| | | if (cpInboundDetails.Any()) |
| | | { |
| | | foreach (var cpDetail in cpInboundDetails) |
| | | { |
| | | cpDetail.CPOrderDetailStatus = InboundStatusEnum.入库完成.ObjToInt(); |
| | | } |
| | | } |
| | | |
| | | // 4. 查询并更新成品库存明细(如果有单独的成品库存表) |
| | | List<Dt_StockInfoDetailCP> cpStockDetails = new List<Dt_StockInfoDetailCP>(); |
| | | if (boxCodes.Any()) |
| | | { |
| | | cpStockDetails = BaseDal.Db.Queryable<Dt_StockInfoDetailCP>() |
| | | .Where(x => boxCodes.Contains(x.BoxCode)) |
| | | .ToList(); |
| | | |
| | | // 更新成品库存明细状态 |
| | | foreach (var cpStockDetail in cpStockDetails) |
| | | { |
| | | cpStockDetail.CPStockDetailStatus = StockStatusEmun.入库完成.ObjToInt(); |
| | | } |
| | | } |
| | | |
| | | try |
| | | { |
| | | _unitOfWorkManage.BeginTran(); |
| | | |
| | | // 更新任务状态 |
| | | UpdateTaskStatus(task); |
| | | |
| | | // 更新货位状态 |
| | | UpdateLocationStatus(dt_LocationInfo, lastStatus); |
| | | |
| | | // 更新库存信息 |
| | | UpdateStockInfo(dt_StockInfo); |
| | | |
| | | // 添加记录 |
| | | AddRecords(task, dt_StockInfo, dt_LocationInfo, lastStatus); |
| | | |
| | | // 更新入库单明细 |
| | | if (inboundOrderDetails.Any()) |
| | | { |
| | | BaseDal.Db.Updateable(inboundOrderDetails).ExecuteCommand(); |
| | | |
| | | // 检查入库单是否全部完成 |
| | | bool allCompleted = inboundOrder.Details.All(x => |
| | | x.OrderDetailStatus == OrderDetailStatusEnum.Over.ObjToInt()); |
| | | |
| | | if (allCompleted) |
| | | { |
| | | inboundOrder.OrderStatus = InboundStatusEnum.入库完成.ObjToInt(); |
| | | } |
| | | else |
| | | { |
| | | inboundOrder.OrderStatus = InboundStatusEnum.入库中.ObjToInt(); |
| | | } |
| | | } |
| | | |
| | | // 更新入库单 |
| | | UpdateInboundOrder(inboundOrder, inboundOrderDetails.FirstOrDefault()); |
| | | |
| | | // 更新成品入库明细状态 |
| | | if (cpInboundDetails.Any()) |
| | | { |
| | | BaseDal.Db.Updateable(cpInboundDetails).ExecuteCommand(); |
| | | } |
| | | |
| | | // 更新成品库存明细状态 |
| | | if (cpStockDetails.Any()) |
| | | { |
| | | BaseDal.Db.Updateable(cpStockDetails).ExecuteCommand(); |
| | | } |
| | | |
| | | // 单据数量全部上架完成,处理WMS回传 |
| | | if (inboundOrderDetails.Any(x => x.OrderDetailStatus == OrderDetailStatusEnum.Over.ObjToInt()) && inboundOrder.System.Equals("ERP")) |
| | | { |
| | | foreach (var detail in inboundOrderDetails.Where(x => x.OrderDetailStatus == OrderDetailStatusEnum.Over.ObjToInt())) |
| | | { |
| | | FeedBackInboundERP(inboundOrder.UpperOrderNo, detail.LinId); |
| | | } |
| | | } |
| | | if (inboundOrder.OrderStatus == InboundStatusEnum.入库完成.ObjToInt() && inboundOrder.OrderType == 0 && inboundOrder.System.Equals("SMOM")) |
| | | { |
| | | List<Dt_StockInfo> StockInfos = _stockRepository.StockInfoRepository.Db |
| | | .Queryable<Dt_StockInfo>() |
| | | .Includes(x => x.Details, d => d.StockDetails) |
| | | .Where(x => x.WarehouseId == task.WarehouseId && |
| | | x.Details.Any(v => v.OrderNo == inboundOrder.OrderNo)) |
| | | .ToList(); |
| | | |
| | | if (StockInfos.Count == 0) throw new Exception("未找到库存信息"); |
| | | var houseInboundPassBack = new HouseInboundPassBack |
| | | { |
| | | ApiType = "AsnController", |
| | | Method = "AsrsGroudingAsn", |
| | | Parameters = new List<HouseInboundPassBack.data> |
| | | { |
| | | new HouseInboundPassBack.data |
| | | { |
| | | Value = new List<data.data1> |
| | | { |
| | | new data.data1 |
| | | { |
| | | AsnNo = inboundOrder.OrderNo, |
| | | //InWarehouse = task.Roadway, |
| | | TransactionCode = inboundOrder.TransactionCode, |
| | | InoutType = inboundOrder.OrderType, |
| | | OrderType = inboundOrder.InoutType, |
| | | // 遍历所有库存明细,添加到 DetailList |
| | | DetailList = StockInfos.Select(d => new data.data1.Inbound |
| | | { |
| | | LinId = d.Details.FirstOrDefault()?.LinId?? "", |
| | | MaterielCode = d.Details.FirstOrDefault()?.MaterielCode?? "", |
| | | OrderQuantity = d.Details.FirstOrDefault().StockQuantity, |
| | | BatchNo = d.Details.FirstOrDefault()?.BatchNo, |
| | | LPNNo = d.PalletCode, |
| | | FinishQty = d.Details.FirstOrDefault().StockQuantity, |
| | | LocationName = d.LocationCode, |
| | | LabelList= d.Details |
| | | .Where(detail => detail.StockDetails != null) |
| | | .SelectMany(detail => detail.StockDetails) |
| | | .Where(stockDetail => !string.IsNullOrEmpty(stockDetail.BoxCode)) |
| | | .Select(stockDetail => stockDetail.BoxCode ) |
| | | .ToList() |
| | | }).ToList() |
| | | } |
| | | } |
| | | } |
| | | } |
| | | }; |
| | | |
| | | var authResult = AuthenticateWithWMS(); |
| | | if (authResult.IsSuccess) |
| | | { |
| | | houseInboundPassBack.Context = new Dictionary<string, string> |
| | | { |
| | | { "Ticket", authResult.Ticket }, |
| | | { "InvOrgId", authResult.InvOrgId } |
| | | }; |
| | | |
| | | var response = HttpHelper.Post<MomRequestContent>(ReceiveWMSTaskin, houseInboundPassBack, "立库入库数量回传WMS"); |
| | | // 判断Success的值 |
| | | if (!response.Success) |
| | | { |
| | | throw new Exception($"操作失败: {response.Message ?? "未提供错误信息"}"); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | else if (inboundOrder.OrderStatus == InboundStatusEnum.入库完成.ObjToInt() && inboundOrder.OrderType == 2 && inboundOrder.System.Equals("SMOM")) |
| | | { |
| | | SendAllocateInfoToWMS(task, inboundOrder, dt_StockInfo); |
| | | } |
| | | |
| | | _unitOfWorkManage.CommitTran(); |
| | | return WebResponseContent.Instance.OK(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | _unitOfWorkManage.RollbackTran(); |
| | | throw new Exception($"入库处理失败:{ex.Message}", ex); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | else |
| | | { |
| | | Dt_InboundOrder? inboundOrder = _inboundService.InbounOrderService.Db.Queryable<Dt_InboundOrder>().Where(x => x.OrderNo == dt_StockInfo.Details.FirstOrDefault().OrderNo).Includes(x => x.Details).First(); |
| | | Dt_InboundOrderDetail? inboundOrderDetail = null; |
| | | |
| | | // 3. 处理入库逻辑 |
| | | if (task.TaskType == TaskTypeEnum.Inbound.ObjToInt()) |
| | | { |
| | | return ProcessInbound(task, dt_StockInfo, dt_LocationInfo); |
| | | } |
| | | if (task.TaskType == TaskTypeEnum.SurplusReturn.ObjToInt() || task.TaskType == TaskTypeEnum.ReverseIn.ObjToInt()) |
| | | { |
| | | return ReturnInbound(task, dt_StockInfo, dt_LocationInfo); |
| | | } |
| | | if (task.TaskType == TaskTypeEnum.InInventory.ObjToInt()) |
| | | { |
| | | int lastStatus = dt_LocationInfo.LocationStatus; |
| | | dt_LocationInfo.LocationStatus = LocationStatusEnum.InStock.ObjToInt(); |
| | | dt_StockInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt(); |
| | | // 更新库存明细状态 |
| | | dt_StockInfo.Details.ForEach(x => x.Status = StockStatusEmun.入库完成.ObjToInt()); |
| | | _unitOfWorkManage.BeginTran(); |
| | | UpdateTaskStatus(task); |
| | | UpdateLocationStatus(dt_LocationInfo, lastStatus); |
| | | UpdateStockInfo(dt_StockInfo); |
| | | AddRecords(task, dt_StockInfo, dt_LocationInfo, lastStatus); |
| | | _unitOfWorkManage.CommitTran(); |
| | | return WebResponseContent.Instance.OK(); |
| | | |
| | | } |
| | | } |
| | | |
| | | return WebResponseContent.Instance.OK(); |
| | | } |
| | | catch (Exception ex) |
| | |
| | | System = returnOrder.System, |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | }; |
| | | var authResult = AuthenticateWithWMS(); |
| | |
| | | // 如果Success为false,抛出异常并包含错误信息 |
| | | throw new Exception($"操作失败: {response.Message ?? "未提供错误信息"}"); |
| | | } |
| | | |
| | | |
| | | } |
| | | return WebResponseContent.Instance.OK(); |
| | | } |
| | |
| | | |
| | | if (stockInfo == null) return WebResponseContent.Instance.Error("未找到库存信息"); |
| | | if (locationInfo == null) return WebResponseContent.Instance.Error("未找到货位信息"); |
| | | if ((outStockLockInfos == null || outStockLockInfos.Count == 0)&& task.TaskType != TaskTypeEnum.OutInventory.ObjToInt()) |
| | | if ((outStockLockInfos == null || outStockLockInfos.Count == 0) && task.TaskType != TaskTypeEnum.OutInventory.ObjToInt()) |
| | | return WebResponseContent.Instance.Error("未找到出库详情信息"); |
| | | |
| | | return WebResponseContent.Instance.OK(); |
| | |
| | | if (outboundOrderDetails.OrderDetailStatus == OrderDetailStatusEnum.Over.ObjToInt() && inboundOrder.System.Equals("ERP")) |
| | | { |
| | | FeedBackOutERP(outboundOrder.OrderNo, outboundOrderDetails.LinId); |
| | | //DownloadReport(path, savePath, outboundOrderDetails.Id); |
| | | //printTest(savePath); |
| | | DownloadReport(path, savePath, outboundOrderDetails.Id); |
| | | } |
| | | ///单据完成推送SMOM系统 |
| | | if (outboundOrder.OrderStatus == OutboundStatusEnum.出库完成.ObjToInt() && inboundOrder.System.Equals("SMOM")) |
| | |
| | | private void UpdateOutboundOrderDetails(Dt_StockInfo stockInfo, Dt_OutboundOrder inboundOrder, ref Dt_OutboundOrderDetail inboundOrderDetail) |
| | | { |
| | | int overCount = inboundOrder.Details.Count(x => x.OrderDetailStatus == OrderDetailStatusEnum.Over.ObjToInt()); |
| | | inboundOrderDetail = inboundOrder.Details.FirstOrDefault(x => x.BatchNo == stockInfo.Details.FirstOrDefault()?.BatchNo); |
| | | //inboundOrderDetail = inboundOrder.Details.FirstOrDefault(x => x.BatchNo == stockInfo.Details.FirstOrDefault()?.BatchNo&&x.LPNNo==stockInfo.PalletCode); |
| | | |
| | | foreach (var item in stockInfo.Details) |
| | | { |
| | |
| | | OrderType = outboundOrder.InoutType, |
| | | DetailList = outStockLockInfos.Select(item => |
| | | { |
| | | var detail = _stockService.StockInfoDetailService.Repository.QueryFirst(x => x.StockId == item.Id); |
| | | //var detail = _stockService.StockInfoDetailService.Repository.QueryFirst(x => x.StockId == item.StockId); |
| | | return new Allocate.data1.Inventory |
| | | { |
| | | LinId = outDetail.LinId, |
| | | LPN_No = item.PalletCode, |
| | | MaterielCode = detail.MaterielCode, |
| | | OrderQuantity = detail.OutboundQuantity, |
| | | BatchNo = detail.BatchNo, |
| | | FinishQty = detail.OutboundQuantity, |
| | | MaterielCode = outDetail.MaterielCode, |
| | | OrderQuantity = outDetail.OrderQuantity, |
| | | BatchNo = outDetail.BatchNo, |
| | | FinishQty = outDetail.OrderQuantity, |
| | | LocationName = task.SourceAddress |
| | | }; |
| | | }).ToList() |
| | |
| | | throw new Exception($"操作失败: {response.Message ?? "未提供错误信息"}"); |
| | | } |
| | | } |
| | | public string DownloadReport(string path, string savePath, int orderId) |
| | | // 方法声明添加 async 关键字,返回值改为 Task<string> |
| | | public async Task<string> DownloadReport(string path, string savePath, int orderId) |
| | | { |
| | | // 构建完整 URL,(报表传参) |
| | | string reportUrl = path + "&orderId=" + orderId; |
| | | |
| | | try |
| | | { |
| | | // 确保保存目录存在 |
| | | string directory = Path.GetDirectoryName(savePath); |
| | | Console.WriteLine($"下载地址: {reportUrl}"); |
| | | Console.WriteLine($"保存路径: {savePath}"); |
| | | |
| | | // 确保目录存在 |
| | | if (!Directory.Exists(directory)) |
| | | { |
| | | Directory.CreateDirectory(directory); |
| | | Console.WriteLine($"已创建目录: {directory}"); |
| | | } |
| | | |
| | | string[] files = Directory.GetFiles(directory); |
| | | // 删除每个文件 |
| | | foreach (string filePath in files) |
| | | // 清理目录下现有文件 |
| | | if (File.Exists(savePath)) |
| | | { |
| | | File.Delete(filePath); |
| | | File.Delete(savePath); |
| | | Console.WriteLine($"已删除旧文件: {savePath}"); |
| | | } |
| | | // 使用 HttpClient 下载文件 |
| | | |
| | | // 使用临时文件名下载,下载完成后再重命名 |
| | | string tempFilePath = savePath + ".tmp"; |
| | | |
| | | // 异步下载文件 |
| | | using (var httpClient = new HttpClient()) |
| | | { |
| | | // 设置超时时间(帆软报表生成可能需要较长时间) |
| | | httpClient.Timeout = TimeSpan.FromMinutes(5); |
| | | |
| | | // 发送 GET 请求(同步方式) |
| | | using (var response = httpClient.GetAsync(reportUrl, HttpCompletionOption.ResponseHeadersRead).GetAwaiter().GetResult()) |
| | | using (var response = await httpClient.GetAsync(reportUrl, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false)) |
| | | { |
| | | // 检查响应状态 |
| | | response.EnsureSuccessStatusCode(); |
| | | |
| | | // 获取内容流(同步方式) |
| | | using (var contentStream = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult()) |
| | | using (var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) |
| | | { |
| | | // 创建文件流 |
| | | using (var fileStream = new FileStream(savePath, FileMode.Create, FileAccess.Write, FileShare.None)) |
| | | Console.WriteLine($"开始写入临时文件: {tempFilePath}"); |
| | | |
| | | using (var fileStream = new FileStream( |
| | | tempFilePath, |
| | | FileMode.Create, |
| | | FileAccess.Write, |
| | | FileShare.None, |
| | | bufferSize: 4096, |
| | | useAsync: true |
| | | )) |
| | | { |
| | | // 复制内容到文件(同步方式) |
| | | contentStream.CopyToAsync(fileStream).GetAwaiter().GetResult(); |
| | | await contentStream.CopyToAsync(fileStream).ConfigureAwait(false); |
| | | await fileStream.FlushAsync().ConfigureAwait(false); |
| | | Console.WriteLine($"临时文件写入完成: {tempFilePath}"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return savePath; |
| | | |
| | | // 关键优化:确保文件完全写入磁盘 |
| | | await EnsureFileCompletelyWritten(tempFilePath); |
| | | |
| | | // 重命名为目标文件 |
| | | File.Move(tempFilePath, savePath, true); |
| | | Console.WriteLine($"文件重命名完成: {savePath}"); |
| | | |
| | | // 验证文件可访问 |
| | | await ValidateFileAccessible(savePath); |
| | | |
| | | // 打印文件 |
| | | PrintTestDirect(savePath); |
| | | |
| | | return savePath; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | throw new ValidationException(ex.Message); |
| | | Console.WriteLine($"下载失败: {ex.Message}"); |
| | | throw new InvalidOperationException($"文件下载异常: {ex.Message}", ex); |
| | | } |
| | | } |
| | | |
| | | public virtual void printTest(string fullPath) |
| | | /// <summary> |
| | | /// 确保文件完全写入磁盘 |
| | | /// </summary> |
| | | private async Task EnsureFileCompletelyWritten(string filePath) |
| | | { |
| | | Spire.Pdf.PdfDocument pdf = new PdfDocument(); |
| | | //文件地址 |
| | | pdf.LoadFromFile(fullPath); |
| | | //指定打印机位置 |
| | | string url = AppSettings.app("PrinterName"); |
| | | pdf.PrintSettings.PrinterName = url; |
| | | //执行打印 |
| | | pdf.Print(); |
| | | //内存释放 |
| | | pdf.Dispose(); |
| | | const int maxRetries = 5; |
| | | const int delayMs = 500; |
| | | |
| | | for (int i = 0; i < maxRetries; i++) |
| | | { |
| | | try |
| | | { |
| | | // 尝试以读取模式打开文件,确保文件没有被占用 |
| | | using (var fs = new FileStream(filePath, |
| | | FileMode.Open, |
| | | FileAccess.Read, |
| | | FileShare.Read, |
| | | bufferSize: 4096, |
| | | useAsync: true)) |
| | | { |
| | | // 验证文件有内容 |
| | | if (fs.Length > 0) |
| | | { |
| | | Console.WriteLine($"文件验证成功: 大小={fs.Length}字节 (尝试 {i + 1}/{maxRetries})"); |
| | | return; |
| | | } |
| | | else |
| | | { |
| | | Console.WriteLine($"警告: 文件大小为0 (尝试 {i + 1}/{maxRetries})"); |
| | | } |
| | | } |
| | | } |
| | | catch (IOException ex) |
| | | { |
| | | Console.WriteLine($"文件可能仍在写入中 (尝试 {i + 1}/{maxRetries}): {ex.Message}"); |
| | | } |
| | | |
| | | if (i < maxRetries - 1) |
| | | { |
| | | await Task.Delay(delayMs); |
| | | } |
| | | } |
| | | |
| | | throw new IOException($"文件写入未完成或无法访问: {filePath}"); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 验证文件可访问且不为空 |
| | | /// </summary> |
| | | private async Task ValidateFileAccessible(string filePath) |
| | | { |
| | | const int maxRetries = 3; |
| | | const int delayMs = 300; |
| | | |
| | | for (int i = 0; i < maxRetries; i++) |
| | | { |
| | | try |
| | | { |
| | | if (!File.Exists(filePath)) |
| | | { |
| | | throw new FileNotFoundException($"文件不存在: {filePath}"); |
| | | } |
| | | |
| | | var fileInfo = new FileInfo(filePath); |
| | | |
| | | // 验证文件大小大于0 |
| | | if (fileInfo.Length == 0) |
| | | { |
| | | throw new InvalidDataException($"文件大小为0: {filePath}"); |
| | | } |
| | | |
| | | // 验证文件不是完全由空字符组成(可选检查) |
| | | await ValidateFileContent(filePath); |
| | | |
| | | Console.WriteLine($"文件验证通过: {filePath} (大小={fileInfo.Length}字节)"); |
| | | return; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Console.WriteLine($"文件验证失败 (尝试 {i + 1}/{maxRetries}): {ex.Message}"); |
| | | |
| | | if (i == maxRetries - 1) |
| | | { |
| | | throw; |
| | | } |
| | | |
| | | await Task.Delay(delayMs); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 验证文件内容(可选) |
| | | /// </summary> |
| | | private async Task ValidateFileContent(string filePath) |
| | | { |
| | | try |
| | | { |
| | | // 读取文件前几个字节,确保不是完全由空字符组成 |
| | | using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true)) |
| | | { |
| | | byte[] buffer = new byte[Math.Min(1024, fs.Length)]; |
| | | int bytesRead = await fs.ReadAsync(buffer, 0, buffer.Length); |
| | | |
| | | // 检查是否都是0(空文件) |
| | | if (bytesRead > 0 && buffer.All(b => b == 0)) |
| | | { |
| | | throw new InvalidDataException("文件内容异常: 全部为0字节"); |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Console.WriteLine($"文件内容验证异常: {ex.Message}"); |
| | | // 可以选择是否抛出异常 |
| | | } |
| | | } |
| | | |
| | | public virtual void PrintTestDirect(string fullPath) |
| | | { |
| | | const int maxRetryCount = 3; |
| | | const int retryDelayMs = 1000; |
| | | |
| | | if (!File.Exists(fullPath)) |
| | | { |
| | | Console.WriteLine($"打印失败:文件不存在 {fullPath}"); |
| | | return; |
| | | } |
| | | |
| | | // 获取打印机名称 |
| | | string printerName = AppSettings.app("PrinterName"); |
| | | |
| | | // 尝试不同的打印方法 |
| | | for (int retryCount = 0; retryCount < maxRetryCount; retryCount++) |
| | | { |
| | | try |
| | | { |
| | | Console.WriteLine($"尝试打印 (方法 {retryCount + 1}/{maxRetryCount}): {fullPath}"); |
| | | |
| | | switch (retryCount) |
| | | { |
| | | case 0: |
| | | // 方法1: 使用原始打印命令 |
| | | PrintUsingRawCommand(fullPath, printerName); |
| | | break; |
| | | case 1: |
| | | // 方法3: 直接使用Spire.PDF但简化设置 |
| | | PrintUsingSpireSimple(fullPath, printerName); |
| | | break; |
| | | case 2: |
| | | // 方法3: 直接使用Spire.PDF但简化设置 |
| | | PrintUsingSpireSimple(fullPath, printerName); |
| | | break; |
| | | } |
| | | |
| | | Console.WriteLine("打印任务发送成功"); |
| | | return; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Console.WriteLine($"打印方法 {retryCount + 1} 失败: {ex.Message}"); |
| | | |
| | | if (retryCount < maxRetryCount - 1) |
| | | { |
| | | Thread.Sleep(retryDelayMs); |
| | | } |
| | | } |
| | | } |
| | | |
| | | Console.WriteLine("所有打印方法都失败"); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 使用原始打印命令 |
| | | /// </summary> |
| | | private void PrintUsingRawCommand(string filePath, string printerName) |
| | | { |
| | | try |
| | | { |
| | | // 方法1: 使用Process直接打印 |
| | | var process = new System.Diagnostics.Process(); |
| | | process.StartInfo.FileName = filePath; |
| | | process.StartInfo.Verb = "print"; |
| | | process.StartInfo.CreateNoWindow = true; |
| | | process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; |
| | | process.Start(); |
| | | |
| | | // 等待一段时间让打印任务提交 |
| | | if (!process.WaitForExit(5000)) |
| | | { |
| | | Console.WriteLine("打印进程未及时退出,但任务可能已提交"); |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | // 如果上述方法失败,尝试使用rundll32 |
| | | try |
| | | { |
| | | var args = $@"/c rundll32.exe C:\Windows\System32\shimgvw.dll,ImageView_PrintTo ""{filePath}"" ""{printerName}"""; |
| | | System.Diagnostics.Process.Start("cmd.exe", args); |
| | | } |
| | | catch |
| | | { |
| | | throw; |
| | | } |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 使用Spire.PDF但简化设置 |
| | | /// </summary> |
| | | private void PrintUsingSpireSimple(string filePath, string printerName) |
| | | { |
| | | using (Spire.Pdf.PdfDocument pdf = new Spire.Pdf.PdfDocument()) |
| | | { |
| | | // 使用最小配置加载文件 |
| | | pdf.LoadFromFile(filePath); |
| | | |
| | | // 设置基本打印机信息 |
| | | if (!string.IsNullOrEmpty(printerName)) |
| | | { |
| | | pdf.PrintSettings.PrinterName = printerName; |
| | | } |
| | | |
| | | // 直接打印,不检查 |
| | | pdf.Print(); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 检查文件是否可访问 |
| | | /// </summary> |
| | | private bool IsFileAccessible(string filePath) |
| | | { |
| | | try |
| | | { |
| | | using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| | | { |
| | | return fs.Length > 0; |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 尝试强制垃圾回收,释放可能存在的文件句柄 |
| | | /// </summary> |
| | | private void TryForceGarbageCollection() |
| | | { |
| | | try |
| | | { |
| | | GC.Collect(); |
| | | GC.WaitForPendingFinalizers(); |
| | | Console.WriteLine("已执行垃圾回收"); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Console.WriteLine($"垃圾回收失败: {ex.Message}"); |
| | | } |
| | | } |
| | | |
| | | |