From dccc001554c6c5376482df77f929c0d49b258844 Mon Sep 17 00:00:00 2001
From: yangpeixing <yangpeixing@hnkhzn.com>
Date: 星期三, 04 二月 2026 08:58:00 +0800
Subject: [PATCH] 1

---
 WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs |  358 +++++++++++++++++++++++++++-------------------------------
 1 files changed, 167 insertions(+), 191 deletions(-)

diff --git a/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs b/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
index 94ef2cb..9eccfaa 100644
--- a/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
+++ b/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
@@ -84,6 +84,20 @@
 using static WIDESEA_Common.NewHouseInboundPassBack.Parame.Syncretism;
 using static WIDESEA_ITaskInfoService.ITaskService;
 using Parameter = WIDESEA_Common.Parameter;
+using WIDESEA_DTO.ERP;
+using WIDESEA_OutboundRepository;
+using System.ComponentModel.DataAnnotations;
+using System;
+using System.IO;
+using System.Net.Http;
+using System.Threading.Tasks;
+using Spire.Pdf;
+using WIDESEA_InboundRepository;
+using System.Drawing.Printing;
+using System;
+using WIDESEA_Common.Log;
+using static WIDESEA_Common.HouseInboundPassBack.data.data1;
+using System.Diagnostics;
 
 namespace WIDESEA_TaskInfoService
 {
@@ -3496,65 +3510,172 @@
             }
         }
 
-        public virtual void PrintTestDirect(string fullPath)
+        public void PrintTestDirect(string fullPath)
         {
-            const int maxRetryCount = 3;
-            const int retryDelayMs = 1000;
-
             if (!File.Exists(fullPath))
             {
-                Console.WriteLine($"鎵撳嵃澶辫触锛氭枃浠朵笉瀛樺湪 {fullPath}");
+                Console.WriteLine($"鉁� 鎵撳嵃澶辫触锛氭枃浠朵笉瀛樺湪 {fullPath}");
                 return;
             }
 
-            // 鑾峰彇閰嶇疆鐨勬墦鍗版満鍚嶇О
-            string printerName = AppSettings.app("PrinterName");
+            // 鑾峰彇鎵撳嵃鏈哄悕绉伴厤缃�
+            string printerName = AppSettings.app("PrinterName")?.Trim();
 
             if (string.IsNullOrEmpty(printerName))
             {
-                Console.WriteLine("鉁� 鎵撳嵃澶辫触锛氭湭閰嶇疆鎵撳嵃鏈哄悕绉�");
-                return;
+                // 濡傛灉娌℃湁閰嶇疆锛屼娇鐢ㄧ郴缁熼粯璁ゆ墦鍗版満
+                printerName = GetDefaultPrinterUbuntu();
             }
 
-            // 鐩存帴浣跨敤閰嶇疆鐨勬墦鍗版満鍚嶇О锛屼笉杩涜浠讳綍杞崲
-            PrintUsingConfiguredPrinter(fullPath, printerName.Trim());
+            PrintUsingUbuntuPrinter(fullPath, printerName);
+        }
+        /// <summary>
+        /// 鑾峰彇 Ubuntu 榛樿鎵撳嵃鏈�
+        /// </summary>
+        private string GetDefaultPrinterUbuntu()
+        {
+            try
+            {
+                ProcessStartInfo psi = new ProcessStartInfo
+                {
+                    FileName = "lpstat",
+                    Arguments = "-d",
+                    UseShellExecute = false,
+                    CreateNoWindow = true,
+                    RedirectStandardOutput = true
+                };
+
+                using (Process process = new Process { StartInfo = psi })
+                {
+                    process.Start();
+                    string output = process.StandardOutput.ReadToEnd();
+                    process.WaitForExit();
+
+                    // 瑙f瀽榛樿鎵撳嵃鏈哄悕绉帮紝渚嬪: "system default destination: LenovoPrinter"
+                    if (output.Contains(":"))
+                    {
+                        return output.Split(':')[1].Trim();
+                    }
+                    return "LenovoPrinter"; // 榛樿鍥為��
+                }
+            }
+            catch
+            {
+                return "LenovoPrinter";
+            }
+        }
+
+        /// <summary>
+        /// Ubuntu 鎵撳嵃瀹炵幇
+        /// </summary>
+        private void PrintUsingUbuntuPrinter(string filePath, string printerName)
+        {
+            try
+            {
+                PrintWithLpr(filePath, printerName);
+
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine($"鉁� 鎵撳嵃澶辫触: {ex.Message}");
+                throw;
+            }
+        }
+
+        /// <summary>
+        /// 浣跨敤 lpr 鍛戒护鎵撳嵃
+        /// </summary>
+        private void PrintWithLpr(string filePath, string printerName)
+        {
+            Console.WriteLine($"馃搫 姝e湪鎵撳嵃鍒� {printerName}: {Path.GetFileName(filePath)}");
+
+            string command = $"lpr -P {printerName} \"{filePath}\"";
+
+            ExecuteShellCommand(command);
+        }
+
+
+        /// <summary>
+        /// 鎵ц Shell 鍛戒护
+        /// </summary>
+        private void ExecuteShellCommand(string command)
+        {
+            ProcessStartInfo psi = new ProcessStartInfo
+            {
+                FileName = "/bin/bash",
+                Arguments = $"-c \"{command.Replace("\"", "\\\"")}\"",
+                UseShellExecute = false,
+                CreateNoWindow = true,
+                RedirectStandardOutput = true,
+                RedirectStandardError = true
+            };
+
+            using (Process process = new Process { StartInfo = psi })
+            {
+                process.Start();
+                string output = process.StandardOutput.ReadToEnd();
+                string error = process.StandardError.ReadToEnd();
+                process.WaitForExit();
+
+                if (process.ExitCode == 0)
+                {
+                    Console.WriteLine($"鉁� 鎵撳嵃浠诲姟鎻愪氦鎴愬姛");
+                    if (!string.IsNullOrEmpty(output))
+                        Console.WriteLine($"杈撳嚭: {output}");
+                }
+                else
+                {
+                    throw new InvalidOperationException($"鍛戒护鎵ц澶辫触: {error}");
+                }
+            }
         }
 
 
         /// <summary>
         /// 鍙娇鐢ㄩ厤缃殑鎵撳嵃鏈鸿繘琛屾墦鍗�
         /// </summary>
+        /// <summary>
+        /// Ubuntu 鎵撳嵃鏂规硶 - 浣跨敤 lpr 鍛戒护
+        /// </summary>
         private void PrintUsingConfiguredPrinter(string filePath, string printerName)
         {
             try
             {
-                // 1. 棣栧厛楠岃瘉鎵撳嵃鏈烘槸鍚﹀瓨鍦�
-                if (!IsPrinterInstalled(printerName))
+                // 1. 妫�鏌ユ墦鍗版満鏄惁鍦ㄧ嚎
+                if (!IsPrinterAvailableUbuntu(printerName))
                 {
-                    Console.WriteLine($"鉁� 鎵撳嵃鏈� '{printerName}' 鏈畨瑁呮垨涓嶅瓨鍦�");
-                    Console.WriteLine("\n绯荤粺宸插畨瑁呯殑鎵撳嵃鏈哄垪琛�:");
-                    foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
-                    {
-                        Console.WriteLine($"  - {printer}");
-                    }
-                    throw new InvalidOperationException($"鎵撳嵃鏈� '{printerName}' 鏈畨瑁�");
+                    throw new InvalidOperationException($"鎵撳嵃鏈� '{printerName}' 涓嶅彲鐢ㄦ垨鏈壘鍒�");
                 }
 
-                // 3. 浣跨敤 Spire.PDF 鎵撳嵃
-                using (Spire.Pdf.PdfDocument pdf = new Spire.Pdf.PdfDocument())
+                Console.WriteLine($"鉁� 浣跨敤鎵撳嵃鏈�: {printerName}");
+                Console.WriteLine($"鉁� 姝e湪鎵撳嵃: {Path.GetFileName(filePath)}");
+
+                // 2. 浣跨敤 lpr 鍛戒护鎵撳嵃
+                ProcessStartInfo psi = new ProcessStartInfo
                 {
-                    // 鍔犺浇鏂囦欢
-                    pdf.LoadFromFile(filePath);
+                    FileName = "lpr",
+                    Arguments = $"-P {printerName} \"{filePath}\"",
+                    UseShellExecute = false,
+                    CreateNoWindow = true,
+                    RedirectStandardOutput = true,
+                    RedirectStandardError = true
+                };
 
-                    // 璁剧疆鎵撳嵃鏈哄悕绉帮紙鍙娇鐢ㄩ厤缃殑鍚嶇О锛�
-                    pdf.PrintSettings.PrinterName = printerName;
+                using (Process process = new Process { StartInfo = psi })
+                {
+                    process.Start();
+                    string output = process.StandardOutput.ReadToEnd();
+                    string error = process.StandardError.ReadToEnd();
+                    process.WaitForExit();
 
-                    Console.WriteLine($"鉁� 浣跨敤閰嶇疆鐨勬墦鍗版満: {printerName}");
-                    Console.WriteLine($"鉁� 姝e湪鎵撳嵃: {Path.GetFileName(filePath)}");
-
-                    // 鐩存帴鎵撳嵃
-                    pdf.Print();
-                    Console.WriteLine("鉁� 鎵撳嵃浠诲姟宸叉彁浜�");
+                    if (process.ExitCode == 0)
+                    {
+                        Console.WriteLine($"鉁� 鎵撳嵃浠诲姟宸叉彁浜ゅ埌 {printerName}");
+                    }
+                    else
+                    {
+                        throw new InvalidOperationException($"鎵撳嵃澶辫触: {error}");
+                    }
                 }
             }
             catch (Exception ex)
@@ -3565,155 +3686,29 @@
         }
 
         /// <summary>
-        /// 浠庨厤缃殑鍙嬪ソ鍚嶇О鑾峰彇瀹為檯鐨勬墦鍗版満鍚嶇О
+        /// Ubuntu 妫�鏌ユ墦鍗版満鍙敤鎬�
         /// </summary>
-        private string GetActualPrinterName(string displayName)
-        {
-            if (string.IsNullOrEmpty(displayName))
-                return string.Empty;
-
-            // 濡傛灉閰嶇疆鐨勬槸 "192.168.99.3 涓婄殑 Lenovo M7605D"
-            if (displayName.Contains("192.168.99.3 涓婄殑 Lenovo M7605D"))
-            {
-                // 鍦ㄧ郴缁熶腑鏌ユ壘瀹為檯鐨勬墦鍗版満鍚嶇О
-                return FindActualPrinterForIP("192.168.99.3");
-            }
-
-            // 濡傛灉鐩存帴閰嶇疆鐨勬槸IP鍦板潃
-            if (displayName.Contains("192.168.99.3"))
-            {
-                return FindActualPrinterForIP("192.168.99.3");
-            }
-
-            // 濡傛灉宸茬粡鏄疄闄呯殑鎵撳嵃鏈哄悕绉帮紝鐩存帴杩斿洖
-            if (IsPrinterInstalled(displayName))
-            {
-                return displayName;
-            }
-
-            // 榛樿杩斿洖鍘熷悕绉�
-            return displayName;
-        }
-
-        /// <summary>
-        /// 鏌ユ壘IP鍦板潃瀵瑰簲鐨勫疄闄呮墦鍗版満鍚嶇О
-        /// </summary>
-        private string FindActualPrinterForIP(string ipAddress)
+        private bool IsPrinterAvailableUbuntu(string printerName)
         {
             try
             {
-                // 鑾峰彇鎵�鏈夋墦鍗版満
-                var allPrinters = new List<string>();
-                foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
+                ProcessStartInfo psi = new ProcessStartInfo
                 {
-                    allPrinters.Add(printer);
-                }
-
-                Console.WriteLine($"鏌ユ壘IP鍦板潃 {ipAddress} 瀵瑰簲鐨勬墦鍗版満...");
-                Console.WriteLine("绯荤粺鎵撳嵃鏈哄垪琛�:");
-                foreach (var printer in allPrinters)
-                {
-                    Console.WriteLine($"  - {printer}");
-                }
-
-                // 浼樺厛鏌ユ壘鍖呭惈IP鍦板潃鐨勬墦鍗版満
-                foreach (string printer in allPrinters)
-                {
-                    if (printer.Contains(ipAddress))
-                    {
-                        Console.WriteLine($"鉁� 鎵惧埌鍖呭惈IP鐨勬墦鍗版満: {printer}");
-                        return printer;
-                    }
-                }
-
-                // 鏌ユ壘鑱旀兂鎵撳嵃鏈�
-                foreach (string printer in allPrinters)
-                {
-                    if (printer.Contains("M7605D", StringComparison.OrdinalIgnoreCase))
-                    {
-                        Console.WriteLine($"鉁� 鎵惧埌鑱旀兂鎵撳嵃鏈�: {printer}");
-                        return printer;
-                    }
-                }
-
-
-                // 杩斿洖绗竴涓彲鐢ㄧ殑鎵撳嵃鏈�
-                if (allPrinters.Any())
-                {
-                    Console.WriteLine($"鈿狅笍 鏈壘鍒扮簿纭尮閰嶏紝浣跨敤绗竴涓墦鍗版満: {allPrinters.First()}");
-                    return allPrinters.First();
-                }
-
-                return string.Empty;
-            }
-            catch (Exception ex)
-            {
-                Console.WriteLine($"鏌ユ壘鎵撳嵃鏈哄け璐�: {ex.Message}");
-                return string.Empty;
-            }
-        }
-
-        /// <summary>
-        /// 妫�鏌ユ墦鍗版満鏄惁宸插畨瑁�
-        /// </summary>
-        private bool IsPrinterInstalled(string printerName)
-        {
-            try
-            {
-                foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
-                {
-                    if (printer.Equals(printerName, StringComparison.OrdinalIgnoreCase))
-                    {
-                        return true;
-                    }
-                }
-                return false;
-            }
-            catch
-            {
-                return false;
-            }
-        }
-        /// <summary>
-        /// 浣跨敤鍘熷鎵撳嵃鍛戒护
-        /// </summary>
-        private void PrintUsingRawCommand(string filePath, string printerName)
-        {
-            try
-            {
-                var processStartInfo = new System.Diagnostics.ProcessStartInfo
-                {
-                    FileName = filePath,
-                    Verb = "print",  // 浣跨敤"print"鍔ㄨ瘝
-                    UseShellExecute = true,  // 鍏抽敭锛氬惎鐢⊿hell鎵ц
+                    FileName = "lpstat",
+                    Arguments = $"-p {printerName}",
+                    UseShellExecute = false,
                     CreateNoWindow = true,
-                    WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
+                    RedirectStandardOutput = true
                 };
 
-                // 璁剧疆鎵撳嵃鏈猴紙鍙�夛紝绯荤粺閫氬父浼氫娇鐢ㄩ粯璁ゆ墦鍗版満锛�
-                // processStartInfo.Arguments = $"/t \"{filePath}\" \"{printerName}\"";
-
-                System.Diagnostics.Process.Start(processStartInfo);
-            }
-            catch (Exception ex)
-            {
-                Console.WriteLine($"鎵撳嵃澶辫触: {ex.Message}");
-                // 鍙互灏濊瘯鍏朵粬鏂规硶
-                //TryAlternativePrintMethod(filePath, printerName);
-            }
-        }
-
-
-        /// <summary>
-        /// 妫�鏌ユ枃浠舵槸鍚﹀彲璁块棶
-        /// </summary>
-        private bool IsFileAccessible(string filePath)
-        {
-            try
-            {
-                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
+                using (Process process = new Process { StartInfo = psi })
                 {
-                    return fs.Length > 0;
+                    process.Start();
+                    string output = process.StandardOutput.ReadToEnd();
+                    process.WaitForExit();
+
+                    return output.Contains($"{printerName} is idle") ||
+                           output.Contains($"{printerName} is ready");
                 }
             }
             catch
@@ -3721,25 +3716,6 @@
                 return false;
             }
         }
-
-        /// <summary>
-        /// 灏濊瘯寮哄埗鍨冨溇鍥炴敹锛岄噴鏀惧彲鑳藉瓨鍦ㄧ殑鏂囦欢鍙ユ焺
-        /// </summary>
-        private void TryForceGarbageCollection()
-        {
-            try
-            {
-                GC.Collect();
-                GC.WaitForPendingFinalizers();
-                Console.WriteLine("宸叉墽琛屽瀮鍦惧洖鏀�");
-            }
-            catch (Exception ex)
-            {
-                Console.WriteLine($"鍨冨溇鍥炴敹澶辫触: {ex.Message}");
-            }
-        }
-
-
         /// <summary>
         /// 鐩樼偣鍑哄簱瀹屾垚
         /// </summary>

--
Gitblit v1.9.3