From 34922eb239109534679d35a75661d6e2a5603347 Mon Sep 17 00:00:00 2001
From: wankeda <Administrator@DESKTOP-HAU3ST3>
Date: 星期六, 27 十二月 2025 10:16:36 +0800
Subject: [PATCH] 成品库流程

---
 WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs |  362 +++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 320 insertions(+), 42 deletions(-)

diff --git a/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs b/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
index dc164dc..998c96d 100644
--- a/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
+++ b/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
@@ -79,6 +79,7 @@
 using WIDESEA_InboundRepository;
 using System.Drawing.Printing;
 using System;
+using WIDESEA_Common.Log;
 
 namespace WIDESEA_TaskInfoService
 {
@@ -107,7 +108,8 @@
         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)
@@ -144,6 +146,7 @@
         public string ReceiveWMSTaskAllocatein = WIDESEA_Core.Helper.AppSettings.Configuration["ReceiveWMSTaskAllocatein"];
         public string ReceiveERPTaskout = WIDESEA_Core.Helper.AppSettings.Configuration["ReceiveERPTaskout"];
         public string InMaterialWarehousingCallback = WIDESEA_Core.Helper.AppSettings.Configuration["InMaterialWarehousingCallback"];
+
 
         /// <summary>
         /// 浠诲姟淇℃伅鎺ㄩ�佽嚦WCS
@@ -187,7 +190,7 @@
                 {
                     return WebResponseContent.Instance.Error($"鏈壘鍒癢CSApi鍦板潃,璇锋鏌ラ厤缃枃浠�");
                 }
-                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("杩斿洖閿欒");
             }
@@ -1205,8 +1208,7 @@
                 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);
                 }
                 ///鍗曟嵁瀹屾垚鎺ㄩ�丼MOM绯荤粺
                 if (outboundOrder.OrderStatus == OutboundStatusEnum.鍑哄簱瀹屾垚.ObjToInt() && inboundOrder.System.Equals("SMOM"))
@@ -1235,13 +1237,13 @@
         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)
             {
                 if (inboundOrderDetail == null) continue;
 
-                inboundOrderDetail.OverOutQuantity += item.OutboundQuantity;
+                inboundOrderDetail.OverOutQuantity += item.OutboundQuantity;    
 
                 if (inboundOrderDetail.OverOutQuantity == inboundOrderDetail.OrderQuantity)
                 {
@@ -1480,15 +1482,15 @@
                         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()
@@ -1664,72 +1666,348 @@
                 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
+            {
+                // 濡傛灉涓婅堪鏂规硶澶辫触锛屽皾璇曚娇鐢╮undll32
+                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}");
+            }
         }
 
 

--
Gitblit v1.9.3