|
@@ -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
|