Przeglądaj źródła

月度库存报表

yzc 1 rok temu
rodzic
commit
1d073cd94a

+ 80 - 79
hx-wms/src/main/java/com/fjhx/wms/service/monthly/impl/MonthlyInventoryReportServiceImpl.java

@@ -94,91 +94,92 @@ public class MonthlyInventoryReportServiceImpl extends ServiceImpl<MonthlyInvent
     @DSTransactional
     public void generateReport() {
         DynamicDataSourceContextHolder.push(SourceConstant.WMS);
-
-        List<MonthlyInventoryReport> monthlyInventoryReportList = new ArrayList<>();
-
-        //获取上上月的报表的 结存数量为上月报表的初期数量 并根据租户id分组
-        List<MonthlyInventoryReportVo> olDmonthlyInventoryReportLists = baseMapper.getLastMonthMonthlyInventoryReport();
-        Map<String, List<MonthlyInventoryReportVo>> olDmonthlyInventoryReportMap = new HashMap<>();
-        if (ObjectUtil.isNotEmpty(olDmonthlyInventoryReportLists)) {
-            olDmonthlyInventoryReportMap = olDmonthlyInventoryReportLists.stream().collect(Collectors.groupingBy(MonthlyInventoryReportVo::getTenantId));
-        }
-        //获取上月流水 并根据租户id分组
-        List<StockJournalDetailsVo> lastMonthStockJournals = baseMapper.getLastMonthStockJournal();
-        Map<String, List<StockJournalDetailsVo>> lastMonthStockJournalMap = new HashMap<>();
-        if (ObjectUtil.isNotEmpty(lastMonthStockJournals)) {
-            lastMonthStockJournalMap = lastMonthStockJournals.stream().collect(Collectors.groupingBy(StockJournalDetailsVo::getTenantId));
-        }
-
-        //获取库存 并根据租户id分组
-        List<Stock> stockLists = stockService.list();
-        if (ObjectUtil.isEmpty(stockLists)) {
-            //如果查询不到一条库存信息直接跳过
-            return;
-        }
-        Map<String, List<Stock>> stockListMap = stockLists.stream().collect(Collectors.groupingBy(Stock::getTenantId));
-
-        //遍历每个租户
-        for (Map.Entry<String, List<Stock>> entry : stockListMap.entrySet()) {
-            String tenantId = entry.getKey();
-            List<Stock> stockList = entry.getValue();
-            if (ObjectUtil.isEmpty(stockList)) {
-                //如果租户库存的信息为空直接跳过这个租户
-                continue;
+        try {
+            List<MonthlyInventoryReport> monthlyInventoryReportList = new ArrayList<>();
+
+            //获取上上月的报表的 结存数量为上月报表的初期数量 并根据租户id分组
+            List<MonthlyInventoryReportVo> olDmonthlyInventoryReportLists = baseMapper.getLastMonthMonthlyInventoryReport();
+            Map<String, List<MonthlyInventoryReportVo>> olDmonthlyInventoryReportMap = new HashMap<>();
+            if (ObjectUtil.isNotEmpty(olDmonthlyInventoryReportLists)) {
+                olDmonthlyInventoryReportMap = olDmonthlyInventoryReportLists.stream().collect(Collectors.groupingBy(MonthlyInventoryReportVo::getTenantId));
+            }
+            //获取上月流水 并根据租户id分组
+            List<StockJournalDetailsVo> lastMonthStockJournals = baseMapper.getLastMonthStockJournal();
+            Map<String, List<StockJournalDetailsVo>> lastMonthStockJournalMap = new HashMap<>();
+            if (ObjectUtil.isNotEmpty(lastMonthStockJournals)) {
+                lastMonthStockJournalMap = lastMonthStockJournals.stream().collect(Collectors.groupingBy(StockJournalDetailsVo::getTenantId));
             }
 
-            List<MonthlyInventoryReportVo> olDmonthlyInventoryReportList = olDmonthlyInventoryReportMap.getOrDefault(tenantId, new ArrayList<>());
-            List<StockJournalDetailsVo> lastMonthStockJournal = lastMonthStockJournalMap.getOrDefault(tenantId, new ArrayList<>());
-
-            //生成上月报表
-            for (Stock stock : stockList) {
-                //计算初期数量
-                List<MonthlyInventoryReport> initialQuantityList = olDmonthlyInventoryReportList.stream().filter(it -> stock.getWarehouseId().equals(it.getWarehouseId())).filter(it -> stock.getProductId().equals(it.getProductId())).collect(Collectors.toList());
-                //计算入库数量
-                List<StockJournalDetailsVo> receiptQuantityList = lastMonthStockJournal.stream().filter(it -> stock.getWarehouseId().equals(it.getToWarehouseId())).filter(it -> stock.getProductId().equals(it.getProductId())).filter(it -> it.getOpType() == 1).collect(Collectors.toList());
-                BigDecimal receiptQuantity = receiptQuantityList.stream().map(StockJournalDetailsVo::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
-                //计算出库数量
-                List<StockJournalDetailsVo> outboundQuantityList = lastMonthStockJournal.stream().filter(it -> stock.getWarehouseId().equals(it.getToWarehouseId())).filter(it -> stock.getProductId().equals(it.getProductId())).filter(it -> it.getOpType() == 2).collect(Collectors.toList());
-                BigDecimal outboundQuantity = outboundQuantityList.stream().map(StockJournalDetailsVo::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
-
-                MonthlyInventoryReport monthlyInventoryReport = new MonthlyInventoryReport();
-                monthlyInventoryReport.setWarehouseId(stock.getWarehouseId());
-                monthlyInventoryReport.setProductId(stock.getProductId());
-                monthlyInventoryReport.setInitialQuantity(BigDecimal.ZERO);//初期数量
-                if (ObjectUtil.isNotEmpty(initialQuantityList)) {
-                    //初期数量=上上月报表的结存数量
-                    monthlyInventoryReport.setInitialQuantity(initialQuantityList.get(0).getBalanceQuantity());
-                }
-                monthlyInventoryReport.setReceiptQuantity(receiptQuantity);//入库数量
-                monthlyInventoryReport.setOutboundQuantity(outboundQuantity);//出库数量
-                monthlyInventoryReport.setBalanceQuantity(BigDecimal.ZERO);//结存数量
-                if (ObjectUtil.isNotEmpty(stock.getQuantity())) {
-                    monthlyInventoryReport.setBalanceQuantity(stock.getQuantity());//结存数量
+            //获取库存 并根据租户id分组
+            List<Stock> stockLists = stockService.list();
+            if (ObjectUtil.isEmpty(stockLists)) {
+                //如果查询不到一条库存信息直接跳过
+                return;
+            }
+            Map<String, List<Stock>> stockListMap = stockLists.stream().collect(Collectors.groupingBy(Stock::getTenantId));
+
+            //遍历每个租户
+            for (Map.Entry<String, List<Stock>> entry : stockListMap.entrySet()) {
+                String tenantId = entry.getKey();
+                List<Stock> stockList = entry.getValue();
+                if (ObjectUtil.isEmpty(stockList)) {
+                    //如果租户库存的信息为空直接跳过这个租户
+                    continue;
                 }
-                monthlyInventoryReport.setBalanceUnitPrice(BigDecimal.ZERO);//结存单价
-                BigDecimal unitPriceOfBalance = stock.getUnitPrice();
-                if (ObjectUtil.isNotEmpty(unitPriceOfBalance)) {
-                    monthlyInventoryReport.setBalanceUnitPrice(unitPriceOfBalance);//结存单价
+
+                List<MonthlyInventoryReportVo> olDmonthlyInventoryReportList = olDmonthlyInventoryReportMap.getOrDefault(tenantId, new ArrayList<>());
+                List<StockJournalDetailsVo> lastMonthStockJournal = lastMonthStockJournalMap.getOrDefault(tenantId, new ArrayList<>());
+
+                //生成上月报表
+                for (Stock stock : stockList) {
+                    //计算初期数量
+                    List<MonthlyInventoryReport> initialQuantityList = olDmonthlyInventoryReportList.stream().filter(it -> stock.getWarehouseId().equals(it.getWarehouseId())).filter(it -> stock.getProductId().equals(it.getProductId())).collect(Collectors.toList());
+                    //计算入库数量
+                    List<StockJournalDetailsVo> receiptQuantityList = lastMonthStockJournal.stream().filter(it -> stock.getWarehouseId().equals(it.getToWarehouseId())).filter(it -> stock.getProductId().equals(it.getProductId())).filter(it -> it.getOpType() == 1).collect(Collectors.toList());
+                    BigDecimal receiptQuantity = receiptQuantityList.stream().map(StockJournalDetailsVo::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
+                    //计算出库数量
+                    List<StockJournalDetailsVo> outboundQuantityList = lastMonthStockJournal.stream().filter(it -> stock.getWarehouseId().equals(it.getToWarehouseId())).filter(it -> stock.getProductId().equals(it.getProductId())).filter(it -> it.getOpType() == 2).collect(Collectors.toList());
+                    BigDecimal outboundQuantity = outboundQuantityList.stream().map(StockJournalDetailsVo::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
+
+                    MonthlyInventoryReport monthlyInventoryReport = new MonthlyInventoryReport();
+                    monthlyInventoryReport.setWarehouseId(stock.getWarehouseId());
+                    monthlyInventoryReport.setProductId(stock.getProductId());
+                    monthlyInventoryReport.setInitialQuantity(BigDecimal.ZERO);//初期数量
+                    if (ObjectUtil.isNotEmpty(initialQuantityList)) {
+                        //初期数量=上上月报表的结存数量
+                        monthlyInventoryReport.setInitialQuantity(initialQuantityList.get(0).getBalanceQuantity());
+                    }
+                    monthlyInventoryReport.setReceiptQuantity(receiptQuantity);//入库数量
+                    monthlyInventoryReport.setOutboundQuantity(outboundQuantity);//出库数量
+                    monthlyInventoryReport.setBalanceQuantity(BigDecimal.ZERO);//结存数量
+                    if (ObjectUtil.isNotEmpty(stock.getQuantity())) {
+                        monthlyInventoryReport.setBalanceQuantity(stock.getQuantity());//结存数量
+                    }
+                    monthlyInventoryReport.setBalanceUnitPrice(BigDecimal.ZERO);//结存单价
+                    BigDecimal unitPriceOfBalance = stock.getUnitPrice();
+                    if (ObjectUtil.isNotEmpty(unitPriceOfBalance)) {
+                        monthlyInventoryReport.setBalanceUnitPrice(unitPriceOfBalance);//结存单价
+                    }
+                    BigDecimal multiply = monthlyInventoryReport.getBalanceQuantity().multiply(monthlyInventoryReport.getBalanceUnitPrice());
+                    monthlyInventoryReport.setBalanceAmount(multiply);//结存金额
+
+                    //赋值上月第一天
+                    Calendar cal = Calendar.getInstance();
+                    cal.add(Calendar.MONTH, -1);
+                    cal.set(Calendar.DAY_OF_MONTH, 1);
+                    Date startTime = cal.getTime();//上个月第一天
+                    monthlyInventoryReport.setDailyReportDate(startTime);
+
+                    //赋值租户id
+                    monthlyInventoryReport.setTenantId(tenantId);
+                    monthlyInventoryReportList.add(monthlyInventoryReport);
                 }
-                BigDecimal multiply = monthlyInventoryReport.getBalanceQuantity().multiply(monthlyInventoryReport.getBalanceUnitPrice());
-                monthlyInventoryReport.setBalanceAmount(multiply);//结存金额
-
-                //赋值上月第一天
-                Calendar cal = Calendar.getInstance();
-                cal.add(Calendar.MONTH, -1);
-                cal.set(Calendar.DAY_OF_MONTH, 1);
-                Date startTime = cal.getTime();//上个月第一天
-                monthlyInventoryReport.setDailyReportDate(startTime);
-
-                //赋值租户id
-                monthlyInventoryReport.setTenantId(tenantId);
-                monthlyInventoryReportList.add(monthlyInventoryReport);
             }
-        }
-
-        this.saveBatch(monthlyInventoryReportList);
 
-        DynamicDataSourceContextHolder.clear();
+            this.saveBatch(monthlyInventoryReportList);
+        } finally {
+            DynamicDataSourceContextHolder.clear();
+        }
     }
 
     @Override