From 5232d8b5939cb832c3d17c2aa1d6eaf66e5acb74 Mon Sep 17 00:00:00 2001
From: chen.llin <1442464845@qq.comm>
Date: 星期四, 25 十二月 2025 14:11:05 +0800
Subject: [PATCH] 出入库历史 +月结管理 按钮权限+sql

---
 src/main/java/com/zy/asrs/service/impl/MonthlySettleServiceImpl.java |  136 +++++++++++++++++++++++++++++++++++++--------
 1 files changed, 112 insertions(+), 24 deletions(-)

diff --git a/src/main/java/com/zy/asrs/service/impl/MonthlySettleServiceImpl.java b/src/main/java/com/zy/asrs/service/impl/MonthlySettleServiceImpl.java
index 79921c6..f615603 100644
--- a/src/main/java/com/zy/asrs/service/impl/MonthlySettleServiceImpl.java
+++ b/src/main/java/com/zy/asrs/service/impl/MonthlySettleServiceImpl.java
@@ -3,6 +3,7 @@
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.plugins.Page;
 import com.baomidou.mybatisplus.service.impl.ServiceImpl;
+import com.core.common.Cools;
 import com.core.common.SnowflakeIdWorker;
 import com.core.exception.CoolException;
 import com.zy.asrs.entity.MonthlySettle;
@@ -91,12 +92,21 @@
         cal.set(Calendar.MILLISECOND, 999);
         endDate = cal.getTime();
         
-        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
+        // 娉ㄦ剰锛歰rder_time 鏄� varchar 绫诲瀷锛岄渶瑕佹牸寮忓寲涓哄畬鏁寸殑鏃ユ湡鏃堕棿瀛楃涓茶繘琛屾瘮杈�
         SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-        String startDateStr = dateFormat.format(startDate);
+        // 璧峰鏃ユ湡璁剧疆涓哄綋澶╃殑 00:00:00
+        Calendar startCal = Calendar.getInstance();
+        startCal.setTime(startDate);
+        startCal.set(Calendar.HOUR_OF_DAY, 0);
+        startCal.set(Calendar.MINUTE, 0);
+        startCal.set(Calendar.SECOND, 0);
+        startCal.set(Calendar.MILLISECOND, 0);
+        String startDateStr = dateTimeFormat.format(startCal.getTime());
         String endDateStr = dateTimeFormat.format(endDate);
-        int count = this.baseMapper.countUnfinishedOrdersInRange(startDateStr, endDateStr);
-        return count > 0;
+        // 鍒嗕袱娆℃煡璇細鍏ュ簱鍜屽嚭搴�
+        int pakinCount = this.baseMapper.countUnfinishedOrdersInRangePakin(startDateStr, endDateStr);
+        int pakoutCount = this.baseMapper.countUnfinishedOrdersInRangePakout(startDateStr, endDateStr);
+        return (pakinCount + pakoutCount) > 0;
     }
 
     @Override
@@ -141,9 +151,16 @@
         }
 
         // 缁熻鐗╂枡鍑哄叆搴撴暟閲忥紙鍒嗗埆鏌ヨ涓や釜琛紝鍦↗ava浠g爜涓悎骞讹級
-        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
+        // 娉ㄦ剰锛歰rder_time 鏄� varchar 绫诲瀷锛岄渶瑕佹牸寮忓寲涓哄畬鏁寸殑鏃ユ湡鏃堕棿瀛楃涓茶繘琛屾瘮杈�
         SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-        String startDateStr = dateFormat.format(startDate);
+        // 璧峰鏃ユ湡璁剧疆涓哄綋澶╃殑 00:00:00
+        Calendar startCal = Calendar.getInstance();
+        startCal.setTime(startDate);
+        startCal.set(Calendar.HOUR_OF_DAY, 0);
+        startCal.set(Calendar.MINUTE, 0);
+        startCal.set(Calendar.SECOND, 0);
+        startCal.set(Calendar.MILLISECOND, 0);
+        String startDateStr = dateTimeFormat.format(startCal.getTime());
         String endDateStr = dateTimeFormat.format(endDate);
         
         // 鍒嗗埆鏌ヨ鍏ュ簱琛ㄥ拰鍑哄簱琛�
@@ -259,6 +276,9 @@
             currentMaterialMap.put(key, stat);
         }
 
+        // 鏀堕泦鎵�鏈夋槑缁嗚褰曪紝鐢ㄤ簬鎵归噺鎻掑叆
+        List<MonthlySettleDetail> detailList = new java.util.ArrayList<>();
+
         // 1. 澶勭悊鏈湡鏈夊嚭鍏ュ簱鐨勭墿鏂�
         for (MaterialInOutStatDTO stat : materialStats) {
             // 1. 鎻愬彇鍩虹淇℃伅
@@ -274,16 +294,16 @@
             // 3. 璁$畻搴撳瓨鐩稿叧鏁伴噺
             BigDecimal beginningQty = getBeginningQty(matnr, batch, brand, previousEndingQtyMap);
             BigDecimal endingQty = calculateEndingQty(beginningQty, inQty, outQty);
-            BigDecimal stockQtyDecimal = getCurrentStockQty(matnr, batch);
-            BigDecimal diffQty = calculateDiffQty(stockQtyDecimal, endingQty);
+            // 宸紓鏁伴噺 = 鏈熸湯搴撳瓨 - 鏈熷垵搴撳瓨锛堟湡鏈ぇ浜庢湡鍒濇椂涓烘鏁帮級
+            BigDecimal diffQty = calculateDiffQty(beginningQty, endingQty);
             
-            // 4. 鍒涘缓骞朵繚瀛樻槑缁嗚褰�
+            // 4. 鍒涘缓鏄庣粏璁板綍锛堟殏涓嶆彃鍏ワ級
             MonthlySettleDetail detail = buildMonthlySettleDetail(
                 monthlySettle.getId(), settleNo, matnr, batch, maktx, brand,
-                beginningQty, inQty, outQty, endingQty, stockQtyDecimal, diffQty
+                beginningQty, inQty, outQty, endingQty, diffQty
             );
             detail.setIsDeleted(0); // 鏈垹闄�
-            monthlySettleDetailMapper.insert(detail);
+            detailList.add(detail);
 
             // 5. 绱缁熻
             totalInQty = totalInQty.add(inQty);
@@ -315,21 +335,33 @@
                     BigDecimal beginningQty = previousEndingQty;
                     // 鏈熸湯搴撳瓨 = 鏈熷垵 + 鍏ュ簱 - 鍑哄簱 = 鏈熷垵锛堝洜涓烘湰鏈熸病鏈夊嚭鍏ュ簱锛�
                     BigDecimal endingQty = beginningQty;
-                    // 鑾峰彇褰撳墠瀹為檯搴撳瓨
-                    BigDecimal stockQtyDecimal = getCurrentStockQty(matnr, batch);
-                    // 璁$畻宸紓
-                    BigDecimal diffQty = calculateDiffQty(stockQtyDecimal, endingQty);
+                    // 宸紓鏁伴噺 = 鏈熷垵搴撳瓨 - 鏈熸湯搴撳瓨锛堟湡鍒濆ぇ浜庢湡鏈椂涓烘鏁帮級
+                    BigDecimal diffQty = calculateDiffQty(beginningQty, endingQty);
                     
-                    // 鍒涘缓骞朵繚瀛樻槑缁嗚褰�
+                    // 鍒涘缓鏄庣粏璁板綍锛堟殏涓嶆彃鍏ワ級
                     MonthlySettleDetail detail = buildMonthlySettleDetail(
                         monthlySettle.getId(), settleNo, matnr, batch, maktx, brand,
-                        beginningQty, inQty, outQty, endingQty, stockQtyDecimal, diffQty
+                        beginningQty, inQty, outQty, endingQty, diffQty
                     );
                     detail.setIsDeleted(0); // 鏈垹闄�
-                    monthlySettleDetailMapper.insert(detail);
+                    detailList.add(detail);
                     
                     // 绱缁熻锛堣櫧鐒跺叆搴撳拰鍑哄簱涓�0锛屼絾涔熻璁″叆鐗╂枡绉嶇被鏁帮級
                     materialCount++;
+                }
+            }
+        }
+
+        // 鍒嗘壒鎻掑叆锛屾瘡鎵�1000鏉★紝閬垮厤涓�娆℃�ф彃鍏ヨ繃澶氭暟鎹�
+        if (!detailList.isEmpty()) {
+            int batchSize = 1000;
+            for (int i = 0; i < detailList.size(); i += batchSize) {
+                int end = Math.min(i + batchSize, detailList.size());
+                List<MonthlySettleDetail> batch = detailList.subList(i, end);
+                for (MonthlySettleDetail detail : batch) {
+                    if (monthlySettleDetailMapper.insert(detail) <= 0) {
+                        throw new CoolException("鎻掑叆鏈堢粨鏄庣粏澶辫触");
+                    }
                 }
             }
         }
@@ -429,18 +461,21 @@
     }
 
     /**
-     * 鑾峰彇褰撳墠瀹為檯搴撳瓨鏁伴噺
+     * 鑾峰彇褰撳墠瀹為檯搴撳瓨鏁伴噺锛堝凡搴熷純锛屾敼鐢ㄦ壒閲忔煡璇級
+     * @deprecated 浣跨敤鎵归噺鏌ヨ鏂规硶鏇夸唬锛岄伩鍏峃+1鏌ヨ闂
      */
+    @Deprecated
     private BigDecimal getCurrentStockQty(String matnr, String batch) {
         Double stockQty = manLocDetlService.queryStockAnfme(matnr, batch);
         return stockQty != null ? BigDecimal.valueOf(stockQty) : BigDecimal.ZERO;
     }
 
     /**
-     * 璁$畻宸紓鏁伴噺锛堝疄闄呭簱瀛�-鏈熸湯搴撳瓨锛�
+     * 璁$畻宸紓鏁伴噺锛堟湡鏈簱瀛�-鏈熷垵搴撳瓨锛�
+     * 鏈熸湯澶т簬鏈熷垵鏃朵负姝f暟锛岃〃绀哄簱瀛樺鍔�
      */
-    private BigDecimal calculateDiffQty(BigDecimal stockQty, BigDecimal endingQty) {
-        return stockQty.subtract(endingQty);
+    private BigDecimal calculateDiffQty(BigDecimal beginningQty, BigDecimal endingQty) {
+        return endingQty.subtract(beginningQty);
     }
 
     /**
@@ -449,7 +484,7 @@
     private MonthlySettleDetail buildMonthlySettleDetail(
             Long settleId, String settleNo, String matnr, String batch, String maktx, String brand,
             BigDecimal beginningQty, BigDecimal inQty, BigDecimal outQty, BigDecimal endingQty,
-            BigDecimal stockQty, BigDecimal diffQty) {
+            BigDecimal diffQty) {
         MonthlySettleDetail detail = new MonthlySettleDetail();
         // 鍩烘湰淇℃伅
         detail.setSettleId(settleId);
@@ -463,7 +498,6 @@
         detail.setInQty(inQty);
         detail.setOutQty(outQty);
         detail.setEndingQty(endingQty);
-        detail.setStockQty(stockQty);
         detail.setDiffQty(diffQty);
         // 鏃堕棿淇℃伅
         detail.setCreateTime(new Date());
@@ -499,5 +533,59 @@
         settle.setUpdateTime(new Date());
         this.updateById(settle);
     }
+
+    @Override
+    public boolean isOrderTimeInSettledRange(String orderTime) {
+        if (Cools.isEmpty(orderTime)) {
+            return false;
+        }
+        try {
+            // 瑙f瀽璁㈠崟涓氬姟鏃堕棿
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+            Date orderDate = sdf.parse(orderTime);
+            
+            // 鏌ヨ鎵�鏈夊凡鏈堢粨鐨勮褰曪紙status=1涓旀湭鍒犻櫎锛�
+            EntityWrapper<MonthlySettle> wrapper = new EntityWrapper<>();
+            wrapper.eq("status", 1);
+            wrapper.eq("is_deleted", 0);
+            List<MonthlySettle> settledList = this.selectList(wrapper);
+            
+            if (settledList == null || settledList.isEmpty()) {
+                return false;
+            }
+            
+            // 妫�鏌ヨ鍗曟椂闂存槸鍚﹀湪浠讳綍宸叉湀缁撶殑鍖洪棿鍐�
+            for (MonthlySettle settle : settledList) {
+                Date startDate = settle.getStartDate();
+                Date endDate = settle.getEndDate();
+                
+                // 纭繚startDate鏄綋澶╃殑00:00:00
+                Calendar startCal = Calendar.getInstance();
+                startCal.setTime(startDate);
+                startCal.set(Calendar.HOUR_OF_DAY, 0);
+                startCal.set(Calendar.MINUTE, 0);
+                startCal.set(Calendar.SECOND, 0);
+                startCal.set(Calendar.MILLISECOND, 0);
+                
+                // 纭繚endDate鏄綋澶╃殑23:59:59.999
+                Calendar endCal = Calendar.getInstance();
+                endCal.setTime(endDate);
+                endCal.set(Calendar.HOUR_OF_DAY, 23);
+                endCal.set(Calendar.MINUTE, 59);
+                endCal.set(Calendar.SECOND, 59);
+                endCal.set(Calendar.MILLISECOND, 999);
+                
+                // 鍒ゆ柇璁㈠崟鏃堕棿鏄惁鍦ㄥ尯闂村唴锛歔startDate鐨�00:00:00, endDate鐨�23:59:59.999]
+                if (!orderDate.before(startCal.getTime()) && !orderDate.after(endCal.getTime())) {
+                    return true;
+                }
+            }
+            
+            return false;
+        } catch (Exception e) {
+            log.error("妫�鏌ヨ鍗曟椂闂存槸鍚﹀湪宸叉湀缁撳尯闂村唴澶辫触", e);
+            return false;
+        }
+    }
 }
 

--
Gitblit v1.9.1