From 69a25cd577d5639f2869bcf80f498b373e80137e Mon Sep 17 00:00:00 2001
From: 于杰 <1210123631@qq.com>
Date: 星期三, 17 十二月 2025 10:43:44 +0800
Subject: [PATCH] 修改旋转逻辑,增加辅助旋转功能

---
 north-glass-erp/northglass-erp/src/views/pp/glassOptimize/page/OptimizationRect.vue      |  141 +++++++++++++++++++++++++++++++++++++++++++----
 north-glass-erp/src/main/java/com/example/erp/controller/pp/GlassOptimizeController.java |    4 
 2 files changed, 131 insertions(+), 14 deletions(-)

diff --git a/north-glass-erp/northglass-erp/src/views/pp/glassOptimize/page/OptimizationRect.vue b/north-glass-erp/northglass-erp/src/views/pp/glassOptimize/page/OptimizationRect.vue
index 5a9490e..0612cf5 100644
--- a/north-glass-erp/northglass-erp/src/views/pp/glassOptimize/page/OptimizationRect.vue
+++ b/north-glass-erp/northglass-erp/src/views/pp/glassOptimize/page/OptimizationRect.vue
@@ -989,36 +989,153 @@
     }
     adjustGrayRectangles(layoutIndex);
   } else {
+    // 鏃嬭浆澶辫触锛屾仮澶嶅師濮嬪昂瀵�
     glassDetail.width = originalState.width;
     glassDetail.height = originalState.height;
-    ElMessage.warning('鏃犳硶鏃嬭浆锛屽瓨鍦ㄩ噸鍙犳垨瓒呭嚭杈圭晫');
+
+    // 灏濊瘯鍚戜笂绉诲姩骞舵棆杞�
+    const upResult = tryMoveAndRotate(layoutIndex, rectIndex, 'up');
+    if (!upResult) {
+      ElMessage.warning('鏃犳硶鏃嬭浆锛屽嵆浣跨Щ鍔ㄥ悗浠嶅瓨鍦ㄩ噸鍙犳垨瓒呭嚭杈圭晫');
+    }
   }
+};
+
+// 杈呭姪鏂规硶锛氬皾璇曠Щ鍔ㄥ苟鏃嬭浆
+const tryMoveAndRotate = (layoutIndex, rectIndex, direction) => {
+  // 淇濆瓨鍘熷鐘舵��
+  const layout = layouts.value[layoutIndex];
+  const glassDetail = layout.glassDetails[rectIndex];
+  const originalPosition = {
+    x: glassDetail.x,
+    y: glassDetail.y,
+    width: glassDetail.width,
+    height: glassDetail.height
+  };
+
+  let success = false;
+
+  if (direction === 'up') {
+    // 灏濊瘯鍚戜笂绉诲姩骞舵棆杞�
+    moveRectAndRotate(layoutIndex, rectIndex, 'down');
+
+    // 妫�鏌ユ槸鍚︽垚鍔�
+    const otherRects = layout.glassDetails.filter(r => !r.isRemain && r !== glassDetail);
+    let hasOverlap = false;
+
+    otherRects.forEach(otherRect => {
+      if (checkOverlap(glassDetail, otherRect)) {
+        hasOverlap = true;
+      }
+    });
+
+    if (!hasOverlap &&
+        glassDetail.x + glassDetail.width <= layout.width &&
+        glassDetail.y + glassDetail.height <= layout.height &&
+        glassDetail.x >= 0 && glassDetail.y >= 0) {
+      success = true;
+      // 濡傛灉鍚戜笂绉诲姩骞舵棆杞垚鍔燂紝鍒欏啀鍚戜笅绉诲姩锛岃鐭╁舰闈犺繎搴曢儴
+      moveRect(layoutIndex, rectIndex, 'up');
+    } else {
+      // 鍚戜笂绉诲姩骞舵棆杞け璐ワ紝鎭㈠鍘熷鐘舵��
+      glassDetail.x = originalPosition.x;
+      glassDetail.y = originalPosition.y;
+      glassDetail.width = originalPosition.width;
+      glassDetail.height = originalPosition.height;
+    }
+  } else if (direction === 'down') {
+    // 灏濊瘯鍚戜笅绉诲姩骞舵棆杞�
+    moveRectAndRotate(layoutIndex, rectIndex, 'up');
+
+    // 妫�鏌ユ槸鍚︽垚鍔�
+    const otherRects = layout.glassDetails.filter(r => !r.isRemain && r !== glassDetail);
+    let hasOverlap = false;
+
+    otherRects.forEach(otherRect => {
+      if (checkOverlap(glassDetail, otherRect)) {
+        hasOverlap = true;
+      }
+    });
+
+    if (!hasOverlap &&
+        glassDetail.x + glassDetail.width <= layout.width &&
+        glassDetail.y + glassDetail.height <= layout.height &&
+        glassDetail.x >= 0 && glassDetail.y >= 0) {
+      success = true;
+      // 濡傛灉鍚戜笅绉诲姩骞舵棆杞垚鍔燂紝鍒欏啀鍚戜笂绉诲姩锛岃鐭╁舰闈犺繎椤堕儴
+      moveRect(layoutIndex, rectIndex, 'down');
+    } else {
+      // 鍚戜笅绉诲姩骞舵棆杞け璐ワ紝鎭㈠鍘熷鐘舵��
+      glassDetail.x = originalPosition.x;
+      glassDetail.y = originalPosition.y;
+      glassDetail.width = originalPosition.width;
+      glassDetail.height = originalPosition.height;
+    }
+  }
+
+  return success;
 };
 
 //绉诲姩鏃嬭浆鏂规硶
 const moveRectAndRotate = (layoutIndex, rectIndex, direction) => {
   const layout = layouts.value[layoutIndex];
   const glassDetail = layout.glassDetails[rectIndex];
-  const grayRects = layout.glassDetails.filter(r => r.isRemain);
 
+  // 鍏堢Щ鍔�
+  moveRect(layoutIndex, rectIndex, direction);
+
+  // 鍐嶆棆杞�
+  const originalState = { ...glassDetail };
   const temp = glassDetail.width;
   glassDetail.width = glassDetail.height;
   glassDetail.height = temp;
 
-  const canPlace = grayRects.some(grayRect => {
-    return grayRect.width >= glassDetail.width && grayRect.height >= glassDetail.height;
+  const otherRects = layout.glassDetails.filter(r => !r.isRemain && r !== glassDetail);
+  let isValidRotation = true;
+
+  otherRects.forEach(otherRect => {
+    if (checkOverlap(glassDetail, otherRect)) {
+      isValidRotation = false;
+    }
   });
 
-  if (!canPlace) {
-    const temp = glassDetail.width;
-    glassDetail.width = glassDetail.height;
-    glassDetail.height = temp;
-    ElMessage.warning('鏃犳硶鏃嬭浆锛屾病鏈夎冻澶熺殑绌洪棿');
-    return;
+  if (glassDetail.x + glassDetail.width > layout.width || glassDetail.y + glassDetail.height > layout.height) {
+    isValidRotation = false;
   }
 
-  adjustGrayRectangles(layoutIndex);
-  moveRect(layoutIndex, rectIndex, direction);
+  if (isValidRotation) {
+    // 鏇存柊glassPoint鍧愭爣锛堝鏋滃瓨鍦級
+    if (glassDetail.glassPoint && Array.isArray(glassDetail.glassPoint)) {
+      const originalPoints = JSON.parse(JSON.stringify(glassDetail.glassPoint));
+      glassDetail.glassPoint.forEach((point, index) => {
+        const relX = originalPoints[index].X - originalState.x;
+        const relY = originalPoints[index].Y - originalState.y;
+        point.X = originalState.x + relY;
+        point.Y = originalState.y + (originalState.width - relX);
+        point.X = parseFloat(point.X.toFixed(2));
+        point.Y = parseFloat(point.Y.toFixed(2));
+      });
+    }
+    adjustGrayRectangles(layoutIndex);
+  } else {
+    // 鏃嬭浆澶辫触锛屾仮澶嶅師濮嬪昂瀵稿拰浣嶇疆
+    glassDetail.width = originalState.width;
+    glassDetail.height = originalState.height;
+    // 鎭㈠绉诲姩鍓嶇殑浣嶇疆
+    moveRect(layoutIndex, rectIndex, getReverseDirection(direction));
+    ElMessage.warning('鏃犳硶鏃嬭浆锛屽瓨鍦ㄩ噸鍙犳垨瓒呭嚭杈圭晫');
+  }
+};
+
+// 鑾峰彇鍙嶅悜鏂瑰悜鐨勮緟鍔╁嚱鏁�
+const getReverseDirection = (direction) => {
+  switch (direction) {
+    case 'up': return 'down';
+    case 'down': return 'up';
+    case 'left': return 'right';
+    case 'right': return 'left';
+    default: return direction;
+  }
 };
 
 //绉诲姩鏂规硶
diff --git a/north-glass-erp/src/main/java/com/example/erp/controller/pp/GlassOptimizeController.java b/north-glass-erp/src/main/java/com/example/erp/controller/pp/GlassOptimizeController.java
index 0ff4649..3ab6258 100644
--- a/north-glass-erp/src/main/java/com/example/erp/controller/pp/GlassOptimizeController.java
+++ b/north-glass-erp/src/main/java/com/example/erp/controller/pp/GlassOptimizeController.java
@@ -228,7 +228,7 @@
     }
 
 
-    @PostMapping("/calculate")
+    @PostMapping("/mesCalculate")
     public ResponseEntity<Map<String, Object>> receiveOptimizeRequest(
             @RequestBody Map<String, Object> requestData) {
 
@@ -240,7 +240,7 @@
             response.put("msg", "success");
             response.put("data", "");
 
-            // 寮傛澶勭悊璁$畻浠诲姟
+            // todo 寮傛澶勭悊璁$畻浠诲姟
 //            glassOptimizeService.processExternalOptimizeRequest(requestData);
             System.out.println(requestData);
 

--
Gitblit v1.8.0