From a38d4a8619b886e2544cdefe421f171765ad2229 Mon Sep 17 00:00:00 2001
From: chen.lin <1442464845@qq.com>
Date: 星期二, 03 二月 2026 10:11:51 +0800
Subject: [PATCH] 库存汇总

---
 rsf-server/src/main/java/com/vincent/rsf/server/api/service/impl/ReceiveMsgServiceImpl.java |  182 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 182 insertions(+), 0 deletions(-)

diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/api/service/impl/ReceiveMsgServiceImpl.java b/rsf-server/src/main/java/com/vincent/rsf/server/api/service/impl/ReceiveMsgServiceImpl.java
index f771b8f..cd0ab43 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/api/service/impl/ReceiveMsgServiceImpl.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/api/service/impl/ReceiveMsgServiceImpl.java
@@ -1200,4 +1200,186 @@
 
         return details;
     }
+
+    /**
+     * 搴撳瓨鏌ヨ姹囨�伙紙渚沷pen-api璋冪敤锛�
+     *
+     * @param condition 鏌ヨ鏉′欢瀹炰綋绫�
+     * @return 搴撳瓨姹囨�诲垪琛�
+     */
+    @Override
+    public R erpQueryInventorySummary(InventoryQueryConditionParam condition) {
+        try {
+            // 鍙傛暟楠岃瘉
+            if (condition == null) {
+                return R.error("鏌ヨ鏉′欢涓嶈兘涓虹┖");
+            }
+
+            // 灏咵RP鍙傛暟鏄犲皠涓烘暟鎹簱瀛楁鍚嶏紙涓嬪垝绾挎牸寮忥級
+            Map<String, Object> queryMap = new HashMap<>();
+            
+            // 浠庡疄浣撶被涓彁鍙栨煡璇㈡潯浠讹紝鏄犲皠涓虹湡瀹炵殑鏁版嵁搴撳瓧娈靛悕
+            if (StringUtils.isNotBlank(condition.getMatNr())) {
+                queryMap.put("matnr_code", condition.getMatNr());
+            }
+            if (StringUtils.isNotBlank(condition.getPlanNo())) {
+                queryMap.put("track_code", condition.getPlanNo());
+            }
+            if (StringUtils.isNotBlank(condition.getBatch())) {
+                queryMap.put("batch", condition.getBatch());
+            }
+
+            BaseParam baseParam = new BaseParam();
+            baseParam.syncMap(queryMap);
+            PageParam<LocItem, BaseParam> pageParam = new PageParam<>(baseParam, LocItem.class);
+
+            QueryWrapper<LocItem> wrapper = pageParam.buildWrapper(false);
+
+            // 鐗╂枡缁勶紙闇�瑕侀�氳繃鐗╂枡琛ㄥ叧鑱旀煡璇級
+            if (StringUtils.isNotBlank(condition.getMatGroup())) {
+                // 璋冪敤鐗╂枡Service鏌ヨ鐗╂枡缁勫搴旂殑鐗╂枡ID鍒楄〃锛堝鐢ㄥ凡鏈夋柟娉曪級
+                LambdaQueryWrapper<Matnr> matnrWrapper = new LambdaQueryWrapper<>();
+                matnrWrapper.eq(Matnr::getGroupId, condition.getMatGroup());
+                List<Matnr> matnrs = matnrService.list(matnrWrapper);
+                if (!matnrs.isEmpty()) {
+                    List<Long> matnrIds = matnrs.stream().map(Matnr::getId).collect(Collectors.toList());
+                    wrapper.in("matnr_id", matnrIds);
+                } else {
+                    // 濡傛灉娌℃湁鎵惧埌鐗╂枡锛岃繑鍥炵┖缁撴灉
+                    return R.ok().add(new ArrayList<>());
+                }
+            }
+
+            // 鍙煡璇㈡甯哥姸鎬佺殑搴撳瓨锛坰tatus=1琛ㄧず姝e父锛�
+            wrapper.eq("status", 1);
+
+            // 璁剧疆鍒嗛〉鍙傛暟锛屾煡璇㈡墍鏈夋暟鎹敤浜庢眹鎬�
+            pageParam.setCurrent(1);
+            pageParam.setSize(Integer.MAX_VALUE);
+            PageParam<LocItem, BaseParam> pageResult = locItemService.page(pageParam, wrapper);
+            List<LocItem> locItems = pageResult.getRecords();
+
+            if (locItems.isEmpty()) {
+                return R.ok().add(new ArrayList<>());
+            }
+
+            // 鑾峰彇鎵�鏈夐渶瑕佸叧鑱旂殑ID
+            List<Long> locIds = locItems.stream()
+                    .map(LocItem::getLocId)
+                    .filter(Objects::nonNull)
+                    .distinct()
+                    .collect(Collectors.toList());
+
+            List<Long> warehouseIds = new ArrayList<>();
+
+            // 璋冪敤LocService鏌ヨ搴撲綅淇℃伅锛堝鐢⊿ervice灞傛柟娉曪級
+            Map<Long, Loc> locMap = new HashMap<>();
+            if (!locIds.isEmpty()) {
+                List<Loc> locs = locService.listByIds(locIds);
+                locMap = locs.stream().collect(Collectors.toMap(Loc::getId, loc -> loc));
+
+                // 鏀堕泦浠撳簱ID
+                warehouseIds = locs.stream()
+                        .map(Loc::getWarehouseId)
+                        .filter(Objects::nonNull)
+                        .distinct()
+                        .collect(Collectors.toList());
+            }
+
+            // 浠撳簱缂栫爜杩囨护
+            if (StringUtils.isNotBlank(condition.getWareHouseId())) {
+                String wareHouseId = condition.getWareHouseId();
+                LambdaQueryWrapper<Warehouse> whWrapper = new LambdaQueryWrapper<>();
+                whWrapper.eq(Warehouse::getCode, wareHouseId);
+                // 璋冪敤WarehouseService鏌ヨ浠撳簱淇℃伅锛堝鐢⊿ervice灞傛柟娉曪級
+                List<Warehouse> warehouses = warehouseService.list(whWrapper);
+                if (!warehouses.isEmpty()) {
+                    Long targetWarehouseId = warehouses.get(0).getId();
+                    // 杩囨护搴撲綅锛屽彧淇濈暀鐩爣浠撳簱鐨勫簱浣�
+                    locMap = locMap.entrySet().stream()
+                            .filter(entry -> Objects.equals(entry.getValue().getWarehouseId(), targetWarehouseId))
+                            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+
+                    // 杩囨护locItems锛屽彧淇濈暀鐩爣浠撳簱鐨�
+                    Set<Long> validLocIds = locMap.keySet();
+                    locItems = locItems.stream()
+                            .filter(item -> item.getLocId() != null && validLocIds.contains(item.getLocId()))
+                            .collect(Collectors.toList());
+
+                    warehouseIds = Collections.singletonList(targetWarehouseId);
+                } else {
+                    return R.ok().add(new ArrayList<>());
+                }
+            }
+
+            // 璋冪敤WarehouseService鏌ヨ浠撳簱淇℃伅锛堝鐢⊿ervice灞傛柟娉曪級
+            Map<Long, Warehouse> warehouseMap = new HashMap<>();
+            if (!warehouseIds.isEmpty()) {
+                List<Warehouse> warehouses = warehouseService.listByIds(warehouseIds);
+                warehouseMap = warehouses.stream().collect(Collectors.toMap(Warehouse::getId, wh -> wh));
+            }
+
+            // 鎸変粨搴撱�佺墿鏂欍�佹壒鍙枫�佸簱瀛樼粍缁囧垎缁勬眹鎬�
+            // 浣跨敤Map鐨刱ey浣滀负鍒嗙粍閿細wareHouseId_matNr_batch_stockOrgId
+            Map<String, Map<String, Object>> summaryMap = new HashMap<>();
+            
+            for (LocItem locItem : locItems) {
+                // 鑾峰彇浠撳簱淇℃伅
+                Loc loc = null;
+                if (locItem.getLocId() != null) {
+                    loc = locMap.get(locItem.getLocId());
+                }
+                
+                if (loc == null) {
+                    continue;
+                }
+                
+                Warehouse warehouse = null;
+                if (loc.getWarehouseId() != null && warehouseMap.containsKey(loc.getWarehouseId())) {
+                    warehouse = warehouseMap.get(loc.getWarehouseId());
+                }
+                
+                if (warehouse == null) {
+                    continue;
+                }
+                
+                // 鏋勫缓鍒嗙粍閿細wareHouseId_matNr_batch_stockOrgId
+                String wareHouseId = warehouse.getCode();
+                String matNr = locItem.getMatnrCode();
+                String batch = locItem.getBatch() != null ? locItem.getBatch() : "";
+                String stockOrgId = locItem.getUseOrgId() != null ? locItem.getUseOrgId() : "";
+                String groupKey = wareHouseId + "_" + matNr + "_" + batch + "_" + stockOrgId;
+                
+                // 濡傛灉璇ュ垎缁勫凡瀛樺湪锛岀疮鍔犳暟閲�
+                if (summaryMap.containsKey(groupKey)) {
+                    Map<String, Object> summary = summaryMap.get(groupKey);
+                    Double currentAnfme = (Double) summary.get("anfme");
+                    Double newAnfme = currentAnfme + (locItem.getAnfme() != null ? locItem.getAnfme() : 0.0);
+                    summary.put("anfme", newAnfme);
+                } else {
+                    // 鍒涘缓鏂扮殑姹囨�昏褰�
+                    Map<String, Object> summary = new HashMap<>();
+                    summary.put("wareHouseId", wareHouseId);
+                    summary.put("wareHouseName", warehouse.getName());
+                    summary.put("matNr", matNr);
+                    summary.put("matTx", locItem.getMaktx());
+                    summary.put("spec", locItem.getSpec());
+                    summary.put("unit", locItem.getUnit());
+                    summary.put("anfme", locItem.getAnfme() != null ? locItem.getAnfme() : 0.0);
+                    summary.put("batch", locItem.getBatch());
+                    summary.put("stockOrgId", locItem.getUseOrgId());
+                    summary.put("planNo", locItem.getTrackCode());
+                    summaryMap.put(groupKey, summary);
+                }
+            }
+
+            // 杞崲涓哄垪琛�
+            List<Map<String, Object>> result = new ArrayList<>(summaryMap.values());
+
+            return R.ok().add(result);
+        } catch (Exception e) {
+            log.error("搴撳瓨鏌ヨ姹囨�诲け璐�", e);
+            return R.error("鏌ヨ澶辫触锛�" + e.getMessage());
+        }
+    }
 }

--
Gitblit v1.9.1