Просмотр исходного кода

月度库存报表问题处理

yzc 1 год назад
Родитель
Сommit
5376565273

+ 5 - 0
hx-wms/src/main/java/com/fjhx/wms/entity/monthly/po/MonthlyInventoryReport.java

@@ -66,4 +66,9 @@ public class MonthlyInventoryReport extends BasePo {
      */
     private Date dailyReportDate;
 
+    /**
+     * 租户id
+     */
+    private String tenantId;
+
 }

+ 5 - 0
hx-wms/src/main/java/com/fjhx/wms/entity/monthly/vo/MonthlyInventoryReportVo.java

@@ -34,4 +34,9 @@ public class MonthlyInventoryReportVo extends MonthlyInventoryReport {
      */
     private String productUnit;
 
+    /**
+     * 租户id
+     */
+    private String tenantId;
+
 }

+ 5 - 0
hx-wms/src/main/java/com/fjhx/wms/entity/stock/po/Stock.java

@@ -45,4 +45,9 @@ public class Stock extends BasePo {
      */
     private BigDecimal unitPrice;
 
+    /**
+     * 租户id
+     */
+    private String tenantId;
+
 }

+ 5 - 0
hx-wms/src/main/java/com/fjhx/wms/entity/stock/vo/StockJournalDetailsVo.java

@@ -106,4 +106,9 @@ public class StockJournalDetailsVo extends StockJournalDetails {
      * 金额
      */
     private BigDecimal amount;
+
+    /**
+     * 租户id
+     */
+    private String tenantId;
 }

+ 84 - 46
hx-wms/src/main/java/com/fjhx/wms/service/monthly/impl/MonthlyInventoryReportServiceImpl.java

@@ -2,6 +2,7 @@ package com.fjhx.wms.service.monthly.impl;
 
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.util.ObjectUtil;
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
 import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -22,11 +23,11 @@ import com.fjhx.wms.mapper.monthly.MonthlyInventoryReportMapper;
 import com.fjhx.wms.service.monthly.MonthlyInventoryReportService;
 import com.fjhx.wms.service.stock.StockService;
 import com.fjhx.wms.service.warehouse.WarehouseService;
+import com.ruoyi.common.annotation.TenantIgnore;
 import com.ruoyi.common.utils.wrapper.IWrapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
 
 import javax.servlet.http.HttpServletResponse;
 import java.math.BigDecimal;
@@ -89,58 +90,95 @@ public class MonthlyInventoryReportServiceImpl extends ServiceImpl<MonthlyInvent
      * 每月1号的0:10:00执行自动生成上月报表
      */
     @Scheduled(cron = "0 10 0 1 * ?")
-    @Transactional
+    @TenantIgnore
+    @DSTransactional
     public void generateReport() {
+        DynamicDataSourceContextHolder.push(SourceConstant.WMS);
+
         List<MonthlyInventoryReport> monthlyInventoryReportList = new ArrayList<>();
 
-        //获取上上月的报表的 结存数量为上月报表的初期数量
-        List<MonthlyInventoryReportVo> olDmonthlyInventoryReportList = baseMapper.getLastMonthMonthlyInventoryReport();
-
-        List<StockJournalDetailsVo> lastMonthStockJournal = baseMapper.getLastMonthStockJournal();
-
-        List<Stock> stockList = stockService.list();
-        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);//结存金额
+        //获取上上月的报表的 结存数量为上月报表的初期数量 并根据租户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));
+        }
 
-            //赋值上月第一天
-            Calendar cal = Calendar.getInstance();
-            cal.add(Calendar.MONTH, -1);
-            cal.set(Calendar.DAY_OF_MONTH, 1);
-            Date startTime = cal.getTime();//上个月第一天
-            monthlyInventoryReport.setDailyReportDate(startTime);
+        //获取库存 并根据租户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)) {
+                //如果当前租户的信息为空直接跳过
+                return;
+            }
 
-            monthlyInventoryReportList.add(monthlyInventoryReport);
+            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);
+            }
         }
+
         this.saveBatch(monthlyInventoryReportList);
+
+        DynamicDataSourceContextHolder.clear();
     }
 
     @Override

+ 4 - 2
hx-wms/src/main/resources/mapper/monthly/MonthlyInventoryReportMapper.xml

@@ -23,7 +23,8 @@
         SELECT sj.warehouse_id toWarehouseId,
                sjd.product_id,
                sjd.quantity,
-               sj.op_type
+               sj.op_type,
+               sj.tenant_id
         FROM stock_journal_details sjd
                  LEFT JOIN stock_journal sj ON sjd.stock_journal_id = sj.id
         WHERE DATE_FORMAT(sjd.create_time, '%Y-%m') = DATE_FORMAT(CURRENT_DATE - INTERVAL 1 MONTH, '%Y-%m')
@@ -42,7 +43,8 @@
                mir.create_user,
                mir.create_time,
                mir.update_user,
-               mir.update_time
+               mir.update_time,
+               mir.tenant_id
         from monthly_inventory_report mir
         where DATE_FORMAT(mir.daily_report_date, '%Y-%m') = DATE_FORMAT(CURRENT_DATE - INTERVAL 2 MONTH, '%Y-%m')
     </select>