From 62c1c41aab86ad3bf8967149769f9c2a40ac4313 Mon Sep 17 00:00:00 2001
From: chenhaozhe <cgz@hz-kingdee.com>
Date: 星期四, 07 五月 2026 16:08:19 +0800
Subject: [PATCH] 锦隆 打印机 自动判断是否超出打印区间 超出换行功能 制作完成

---
 WebAPI/Controllers/WebAPIController.cs |   80 ++++++++++++++++++++++++++++++----------
 1 files changed, 60 insertions(+), 20 deletions(-)

diff --git a/WebAPI/Controllers/WebAPIController.cs b/WebAPI/Controllers/WebAPIController.cs
index 6c68f9e..0d2b7ca 100644
--- a/WebAPI/Controllers/WebAPIController.cs
+++ b/WebAPI/Controllers/WebAPIController.cs
@@ -24127,12 +24127,12 @@
             string suffix = ".txt";
             string folder = "ptTemplate";
             // 鍖归厤 {{ 瀛楁鍚� }} 鎵�闇�姝e垯琛ㄨ揪寮� 鏀寔涓枃
-            var regex = new Regex(@"{{\s*([\u4e00-\u9fa5a-zA-Z0-9_\s]+?)\s*}}", RegexOptions.Compiled);
+            var regex = new Regex(@"{{\s*([\u4e00-\u9fa5a-zA-Z0-9_\s]+?)\s*}}", RegexOptions.Singleline);
 
             // 鍖归厤 鐭╁舰妗� 浣嶇疆 瀹介珮 鎵�闇�姝e垯琛ㄨ揪寮�
-            var RectRegex = new Regex(@"^\^FO(\d+),(\d+)\^GB(\d+),(\d+)", RegexOptions.Compiled);
+            var RectRegex = new Regex(@"^\^FO(\d+),(\d+)\^GB(\d+),(\d+)", RegexOptions.Singleline);
             // 鍖归厤 妯℃澘瀛楃涓� 鎵�鍦ㄨ 浣嶇疆 瀹介珮 鎵�闇�姝e垯琛ㄨ揪寮� 鏀寔涓枃
-            var fieldRegex = new Regex(@"\^FO(\d+),(\d+)\^A[A-Z]+,(\d+),(\d+).*\{\{\s*([\u4e00-\u9fa5a-zA-Z0-9_\s]+?)\s*}\}", RegexOptions.Compiled);
+            var fieldRegex = new Regex(@"\^FO(\d+),(\d+)\^A[A-Z]+,(\d+),(\d+).*?\{\{\s*([\u4e00-\u9fa5a-zA-Z0-9_\s]+?)\s*\}\}", RegexOptions.Singleline);
 
             if (ptMode == "ZPL")
             {
@@ -24221,7 +24221,9 @@
 
                             // 鍒ゆ柇鐭╁舰妗嗭紝濡傛灉鍓嶄竴琛屾槸鐭╁舰妗嗭紝鍒欓渶鍒ゆ柇璇ヨ瀛楃涓查暱搴︽槸鍚﹁秴闀�
                             if(!getConcatStr(rectRegex: RectRegex, fieldRegex: fieldRegex,
-                                PreLine: preLine, CurrLine: currentLine, fieldObject: item, ref concatStr))
+                                PreLine: preLine, CurrLine: currentLine, fieldObject: item,
+                                templateRegex: regex,
+                                ref concatStr))
                             {
                                 // 姝e垯琛ㄨ揪寮忓尮閰� {{ 瀛楁鍚� }} 鎵�鍦ㄨ鐨勫瓧娈碉紝濡傛灉鏈夛紝鍒欒繘琛屾浛鎹紝娌℃湁锛屽垯榛樿涓虹┖瀛楃涓�
                                 concatStr = regex.Replace(currentLine, match =>
@@ -24235,6 +24237,10 @@
                                 });
                             }
 
+                            if(string.IsNullOrWhiteSpace(concatStr))
+                            {
+                                concatStr = currentLine;
+                            }
 
                             
                             // 灏嗘墍鏈� \n 鐨勬崲琛岀 鏇挎崲涓� \r\n
@@ -24257,7 +24263,7 @@
             {
                 objJsonResult.code = "0";
                 objJsonResult.count = 0;
-                objJsonResult.Message = $"妯℃澘娓叉煋澶辫触锛歿ex.Message}";
+                objJsonResult.Message = $"妯℃澘娓叉煋澶辫触锛歿ex}";
                 objJsonResult.data = null;
                 return objJsonResult;
             }
@@ -24265,10 +24271,11 @@
         }
 
         private bool getConcatStr(Regex rectRegex, Regex fieldRegex,
-            string PreLine, string CurrLine, JObject fieldObject, ref string concatStr)
+            string PreLine, string CurrLine, JObject fieldObject, Regex templateRegex, ref string concatStr)
         {
             var rectRegexResult = rectRegex.Match(PreLine);
             var fieldRegexResult = fieldRegex.Match(CurrLine);
+            var templateRegexResult = templateRegex.Match(CurrLine);
             if (!rectRegexResult.Success)
             {
                 return false;
@@ -24279,26 +24286,37 @@
                 return false;
             }
 
+            if(!templateRegexResult.Success)
+            {
+                return false;
+            }
+            
+
             // 鐭╁舰 浣嶇疆 瀹介珮
             int rectLocationX = int.Parse(rectRegexResult.Groups[1].Value);
             int rectLocationY = int.Parse(rectRegexResult.Groups[2].Value);
             int rectWidth = int.Parse(rectRegexResult.Groups[3].Value);
             int rectHeight = int.Parse(rectRegexResult.Groups[4].Value);
 
+            var fiEnum = fieldRegexResult.Groups;
+
             // 妯℃澘瀛楃涓� 浣嶇疆 瀛楃瀹介珮
             int fieldLocationX = int.Parse(fieldRegexResult.Groups[1].Value);
             int fieldLocationY = int.Parse(fieldRegexResult.Groups[2].Value);
             int fieldWidth = int.Parse(fieldRegexResult.Groups[3].Value);
             int fieldHeight = int.Parse(fieldRegexResult.Groups[4].Value);
-            string fieldName = fieldRegexResult.Groups[5].Value;
+            var fieldName = fieldRegexResult.Groups[5].Value;
+            
+            
+            JToken fieldValueToken = "";
+            fieldObject.TryGetValue(fieldName, out fieldValueToken);
 
-            string fieldValue = fieldObject[fieldName].ToString() ?? "";
-            if(string.IsNullOrWhiteSpace(fieldValue))
+            if (string.IsNullOrWhiteSpace(fieldValueToken?.ToString()))
             {
                 return false;
             }
-
-            if(fieldValue.Length * fieldWidth > rectWidth)
+            string fieldValue = fieldValueToken.ToString();
+            if (fieldValue.Length * fieldWidth > rectWidth)
             {
                 // 瀛楃涓查暱搴﹀ぇ浜庣煩褰㈢殑鏈�澶ч暱搴� 鍒欐枃鏈渶瑕佷粠鐭╁舰鐨刌浣嶇疆寮�濮嬫覆鏌�
                 fieldLocationY = rectLocationY;
@@ -24308,21 +24326,43 @@
                 // 璁$畻鎹㈣鍚庣殑鏈�澶ц鏁� * 瀛楃楂樺害 鏄惁澶т簬鐭╁舰鏈�澶ч珮搴�
                 if(lineCount * fieldHeight > rectHeight)
                 {
-                    fieldWidth = int.Parse(Math.Floor((float)fieldHeight / lineCount).ToString());
-                    fieldHeight = int.Parse(Math.Floor((float)fieldHeight / lineCount).ToString());
+                    
+                    fieldHeight = int.Parse(Math.Floor((float)rectHeight / lineCount).ToString());
+                    fieldWidth = fieldHeight;
                 }
 
-                for(uint i=0;i<lineCount; i++)
+                // 璁$畻姣忚搴旇鏀惧灏戝瓧绗︼紙姝g‘绠楁硶锛�
+                int maxCharsPerLine = (int)Math.Ceiling((double)fieldValue.Length / lineCount);
+
+                for (int i = 0; i < lineCount; i++)
                 {
-                    concatStr += fieldRegex.Replace(CurrLine, match =>
-                    {
-                        return "";
-                    });
-                }
+                    // 璧峰浣嶇疆
+                    int start = i * maxCharsPerLine;
 
+                    // 鍓╀綑澶氬皯瀛楃锛堟牳蹇冿細淇濊瘉涓嶈秺鐣岋級
+                    int remaining = fieldValue.Length - start;
+
+                    if (remaining <= 0)
+                        break;
+
+                    // 鏈鍙栧嚑涓瓧绗︼紙缁濆瀹夊叏锛�
+                    int take = Math.Min(maxCharsPerLine, remaining);
+
+                    // 鎴彇褰撳墠琛屾枃鏈�
+                    string lineText = fieldValue.Substring(start, take);
+
+                    // 璁$畻鏂拌鍧愭爣
+                    int newY = fieldLocationY + i * fieldHeight;
+
+                    // 鏇挎崲鎷兼帴
+                    concatStr += fieldRegex.Replace(CurrLine, m =>
+                        $"^FO{fieldLocationX},{newY}^AJN,{fieldWidth},{fieldHeight}^CI28^FD{lineText}^FS"
+                    );
+                }
+                return true;
             }
 
-            return true;
+            return false;
 
         }
         #endregion

--
Gitblit v1.9.1