From d995a3204ae1c6ee60e978e1227f8d39eafd6057 Mon Sep 17 00:00:00 2001
From: TanYibin <5491541@qq.com>
Date: Fri, 5 May 2023 08:41:22 +0800
Subject: [PATCH] 库存概况性能优化

---
 huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/controller/HomePageViewController.java                                      |   9 ++++++++-
 huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/service/HomePageViewService.java                                            |   5 ++++-
 huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/service/impl/HomePageViewServiceImpl.java                                   | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------
 huaheng-wms-core/src/main/java/org/jeecg/modules/wms/inventory/inventoryTransaction/service/IInventoryTransactionService.java         |   5 +++++
 huaheng-wms-core/src/main/java/org/jeecg/modules/wms/inventory/inventoryTransaction/service/impl/InventoryTransactionServiceImpl.java |  18 ++++++++++++++++++
 5 files changed, 91 insertions(+), 83 deletions(-)

diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/controller/HomePageViewController.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/controller/HomePageViewController.java
index 351e641..7dd1660 100644
--- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/controller/HomePageViewController.java
+++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/controller/HomePageViewController.java
@@ -42,8 +42,11 @@ import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 
 import java.math.BigDecimal;
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeoutException;
 import java.util.stream.Collectors;
 
 /**
@@ -62,9 +65,13 @@ public class HomePageViewController {
     /**
      * 库存概况
      * @return
+     * @throws TimeoutException 
+     * @throws ExecutionException 
+     * @throws InterruptedException 
+     * @throws ParseException 
      */
     @RequestMapping(value = "/deliveringAmount", method = {RequestMethod.PUT, RequestMethod.POST})
-    public Result<String> deliveringAmount(HttpServletRequest request) {
+    public Result<String> deliveringAmount(HttpServletRequest request) throws ParseException, InterruptedException, ExecutionException, TimeoutException {
         return homePageViewService.deliveringAmount(request);
     }
 
diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/service/HomePageViewService.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/service/HomePageViewService.java
index 8df8037..0b1abab 100644
--- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/service/HomePageViewService.java
+++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/service/HomePageViewService.java
@@ -6,7 +6,10 @@ import org.jeecg.modules.wms.config.location.entity.Location;
 import org.jeecg.modules.wms.task.taskHeader.entity.TaskHeader;
 
 import java.io.Serializable;
+import java.text.ParseException;
 import java.util.Collection;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeoutException;
 
 import javax.servlet.http.HttpServletRequest;
 
@@ -18,7 +21,7 @@ import javax.servlet.http.HttpServletRequest;
  */
 public interface HomePageViewService {
 
-    Result<String> deliveringAmount(HttpServletRequest request);
+    Result<String> deliveringAmount(HttpServletRequest request) throws ParseException, InterruptedException, ExecutionException, TimeoutException;
 
     Result<String> inventoryUtilization(HttpServletRequest request);
 
diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/service/impl/HomePageViewServiceImpl.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/service/impl/HomePageViewServiceImpl.java
index dd06d04..dd1e6a8 100644
--- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/service/impl/HomePageViewServiceImpl.java
+++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/service/impl/HomePageViewServiceImpl.java
@@ -1,54 +1,66 @@
 package org.jeecg.modules.wms.home.service.impl;
 
-import cn.hutool.core.util.ArrayUtil;
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.core.toolkit.Wrappers;
-import com.baomidou.mybatisplus.extension.service.IService;
-import com.github.abel533.echarts.Option;
-import com.github.abel533.echarts.axis.Axis;
-import com.github.abel533.echarts.axis.CategoryAxis;
-import com.github.abel533.echarts.axis.ValueAxis;
-import com.github.abel533.echarts.code.*;
-import com.github.abel533.echarts.json.GsonUtil;
-import com.github.abel533.echarts.series.Line;
-import com.github.abel533.echarts.series.Pie;
-import com.github.abel533.echarts.style.itemstyle.Normal;
+import java.math.BigDecimal;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.stream.Collectors;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.modules.system.entity.SysDictItem;
 import org.jeecg.modules.system.service.impl.SysDictItemServiceImpl;
 import org.jeecg.modules.wms.config.location.entity.Location;
 import org.jeecg.modules.wms.config.location.service.impl.LocationServiceImpl;
 import org.jeecg.modules.wms.home.service.HomePageViewService;
-
 import org.jeecg.modules.wms.home.support.ChartData;
 import org.jeecg.modules.wms.inventory.inventoryHeader.entity.InventoryDetail;
 import org.jeecg.modules.wms.inventory.inventoryHeader.entity.InventoryHeader;
 import org.jeecg.modules.wms.inventory.inventoryHeader.service.impl.InventoryDetailServiceImpl;
 import org.jeecg.modules.wms.inventory.inventoryHeader.service.impl.InventoryHeaderServiceImpl;
 import org.jeecg.modules.wms.inventory.inventoryTransaction.entity.InventoryTransaction;
-import org.jeecg.modules.wms.inventory.inventoryTransaction.service.impl.InventoryTransactionServiceImpl;
-import org.jeecg.modules.wms.receipt.receiptHeader.entity.ReceiptHeader;
+import org.jeecg.modules.wms.inventory.inventoryTransaction.service.IInventoryTransactionService;
 import org.jeecg.modules.wms.receipt.receiptHeader.service.impl.ReceiptHeaderServiceImpl;
-import org.jeecg.modules.wms.shipment.shipmentHeader.entity.ShipmentHeader;
 import org.jeecg.modules.wms.shipment.shipmentHeader.service.impl.ShipmentHeaderServiceImpl;
-import org.jeecg.modules.wms.task.taskHeader.entity.TaskDetail;
 import org.jeecg.modules.wms.task.taskHeader.entity.TaskHeader;
 import org.jeecg.modules.wms.task.taskHeader.service.impl.TaskDetailServiceImpl;
 import org.jeecg.modules.wms.task.taskHeader.service.impl.TaskHeaderServiceImpl;
 import org.jeecg.utils.HuahengJwtUtil;
-import org.jeecg.utils.StringUtils;
 import org.jeecg.utils.constant.QuantityConstant;
 import org.jeecg.utils.support.Convert;
 import org.springframework.stereotype.Service;
 
-import javax.annotation.Resource;
-import javax.servlet.http.HttpServletRequest;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.github.abel533.echarts.Option;
+import com.github.abel533.echarts.axis.Axis;
+import com.github.abel533.echarts.axis.CategoryAxis;
+import com.github.abel533.echarts.axis.ValueAxis;
+import com.github.abel533.echarts.code.LegendType;
+import com.github.abel533.echarts.code.Orient;
+import com.github.abel533.echarts.code.PointerType;
+import com.github.abel533.echarts.code.SeriesType;
+import com.github.abel533.echarts.code.Trigger;
+import com.github.abel533.echarts.json.GsonUtil;
+import com.github.abel533.echarts.series.Line;
+import com.github.abel533.echarts.series.Pie;
+import com.github.abel533.echarts.style.itemstyle.Normal;
 
-import java.math.BigDecimal;
-import java.text.SimpleDateFormat;
-import java.util.*;
-import java.util.stream.Collectors;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.ArrayUtil;
 
 /**
  * @Description: 任务表
@@ -87,60 +99,23 @@ public class HomePageViewServiceImpl implements HomePageViewService {
     HomePageViewService homePageViewService;
 
     @Resource
-    InventoryTransactionServiceImpl inventoryTransactionServiceImpl;
+    IInventoryTransactionService inventoryTransactionService;
 
     @Override
-    public Result<String> deliveringAmount(HttpServletRequest request) {
-        // 通过库存交易记录来查询
-        LambdaQueryWrapper<InventoryTransaction> inventoryTransactionLambdaQueryWrapper = Wrappers.lambdaQuery();
-        HuahengJwtUtil.setWarehouseCode(inventoryTransactionLambdaQueryWrapper, InventoryTransaction.class, request);
-        inventoryTransactionLambdaQueryWrapper.select(InventoryTransaction::getCreateTime, InventoryTransaction::getQty, InventoryTransaction::getType);
-        List<InventoryTransaction> inventoryTransactions = inventoryTransactionServiceImpl.list(inventoryTransactionLambdaQueryWrapper);
-
-        // 查询出库单的7天出库量
-        List<InventoryTransaction> shipmentHeaders = new ArrayList<>();
-        shipmentHeaders.addAll(inventoryTransactions);
-        shipmentHeaders.removeIf(Inventory -> Inventory.getType() != 20);
-        LinkedHashMap<String, String> list = new LinkedHashMap<>();
-
-        for (int i = 6; i >= 0; i--) {
-            String today = new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis() - (1000 * 60 * 60 * 24) * i).toString();
-            List<InventoryTransaction> collect = shipmentHeaders.stream()
-                .filter(shipmentHeader -> new SimpleDateFormat("yyyy-MM-dd").format(shipmentHeader.getCreateTime()).toString().contains(today))
-                .collect(Collectors.toList());
-            if (collect.size() != 0) {
-                BigDecimal sum = new BigDecimal(0);
-                for (InventoryTransaction item : collect) {
-                    sum = sum.add(item.getQty());
-                }
-                list.put(today, String.valueOf(sum));
-            } else {
-                list.put(today, "0");
-            }
-        }
-
-        // 查询入库单的7天入库量
-        List<InventoryTransaction> receiptHeaders = new ArrayList<>();
-        receiptHeaders.addAll(inventoryTransactions);
-        receiptHeaders.removeIf(Inventory -> Inventory.getType() != 10);
-        LinkedHashMap<String, String> list2 = new LinkedHashMap<>();
+    public Result<String> deliveringAmount(HttpServletRequest request) throws ParseException, InterruptedException, ExecutionException, TimeoutException {
+        Long dateTime = new Date().getTime();
+        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
+        LinkedHashMap<String, Future<BigDecimal>> shipmentQuantityMap = new LinkedHashMap<String, Future<BigDecimal>>();
+        LinkedHashMap<String, Future<BigDecimal>> receiptQuantityMap = new LinkedHashMap<String, Future<BigDecimal>>();
+        // 查询出库单的7天出库量/入库量
         for (int i = 6; i >= 0; i--) {
-            List<InventoryTransaction> receiptHeadersCop = receiptHeaders;
-            String today = new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis() - (1000 * 60 * 60 * 24) * i).toString();
-            List<InventoryTransaction> collect = receiptHeadersCop.stream()
-                .filter(ReceiptHeader -> new SimpleDateFormat("yyyy-MM-dd").format(ReceiptHeader.getCreateTime()).toString().contains(today))
-                .collect(Collectors.toList());
-            if (collect.size() != 0) {
-                BigDecimal sum = new BigDecimal(0);
-                for (InventoryTransaction item : collect) {
-                    sum = sum.add(item.getQty());
-                }
-                list2.put(today, String.valueOf(sum));
-            } else {
-                list2.put(today, "0");
-            }
+            String startTime = simpleDateFormat.format(dateTime - (1000 * 60 * 60 * 24) * i);
+            String endTime = simpleDateFormat.format(DateUtil.offsetDay(simpleDateFormat.parse(startTime), 1));
+            shipmentQuantityMap.put(startTime,
+                inventoryTransactionService.asyncCalculateInventory(startTime, endTime, QuantityConstant.INVENTORY_TRANSACTION_SHIPMENT));
+            receiptQuantityMap.put(startTime,
+                inventoryTransactionService.asyncCalculateInventory(startTime, endTime, QuantityConstant.INVENTORY_TRANSACTION_RECEIPT));
         }
-
         Option option = new Option();
         option.tooltip().trigger(Trigger.axis).axisPointer().type(PointerType.cross);
         option.grid().setLeft("3%");
@@ -158,17 +133,17 @@ public class HomePageViewServiceImpl implements HomePageViewService {
         axis.boundaryGap(false);
         Line line = new Line();
         line.setName("发货量");
-        for (String key : list.keySet()) {
+        for (String key : shipmentQuantityMap.keySet()) {
             Object o1 = key;
-            Object o2 = list.get(key);
+            Object o2 = shipmentQuantityMap.get(key).get(10000, TimeUnit.MILLISECONDS);
             axis.data().add(o1);
             line.data().add(o2);
         }
         Line line2 = new Line();
         line2.setName("收货量");
-        for (String key : list2.keySet()) {
+        for (String key : receiptQuantityMap.keySet()) {
             Object o1 = key;
-            Object o2 = list2.get(key);
+            Object o2 = receiptQuantityMap.get(key).get(10000, TimeUnit.MILLISECONDS);
             line2.data().add(o2);
         }
         option.setxAxis(Arrays.asList(axis));
@@ -346,7 +321,7 @@ public class HomePageViewServiceImpl implements HomePageViewService {
         LambdaQueryWrapper<InventoryTransaction> inventoryTransactionLambdaQueryWrapper = Wrappers.lambdaQuery();
         HuahengJwtUtil.setWarehouseCode(inventoryTransactionLambdaQueryWrapper, InventoryTransaction.class, request);
         inventoryTransactionLambdaQueryWrapper.select(InventoryTransaction::getType, InventoryTransaction::getQty).gt(InventoryTransaction::getCreateTime, today);
-        List<InventoryTransaction> inventoryTransactions = inventoryTransactionServiceImpl.list(inventoryTransactionLambdaQueryWrapper);
+        List<InventoryTransaction> inventoryTransactions = inventoryTransactionService.list(inventoryTransactionLambdaQueryWrapper);
         BigDecimal receiptQty = BigDecimal.ZERO;
         BigDecimal shipmentQty = BigDecimal.ZERO;
 
diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/inventory/inventoryTransaction/service/IInventoryTransactionService.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/inventory/inventoryTransaction/service/IInventoryTransactionService.java
index dfa1175..3803a6e 100644
--- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/inventory/inventoryTransaction/service/IInventoryTransactionService.java
+++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/inventory/inventoryTransaction/service/IInventoryTransactionService.java
@@ -3,7 +3,10 @@ package org.jeecg.modules.wms.inventory.inventoryTransaction.service;
 import com.baomidou.mybatisplus.extension.service.IService;
 import org.jeecg.modules.wms.inventory.inventoryTransaction.entity.InventoryTransaction;
 
+import java.math.BigDecimal;
+import java.text.ParseException;
 import java.util.List;
+import java.util.concurrent.Future;
 
 /**
  * @Description: 库存交易记录
@@ -14,5 +17,7 @@ import java.util.List;
 public interface IInventoryTransactionService extends IService<InventoryTransaction> {
 
     boolean batchSave(List<InventoryTransaction> inventoryTransactionList);
+
+    Future<BigDecimal> asyncCalculateInventory(String startTime, String endTime, Integer type) throws ParseException;
     
 }
diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/inventory/inventoryTransaction/service/impl/InventoryTransactionServiceImpl.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/inventory/inventoryTransaction/service/impl/InventoryTransactionServiceImpl.java
index f4e0f28..172a76d 100644
--- a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/inventory/inventoryTransaction/service/impl/InventoryTransactionServiceImpl.java
+++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/inventory/inventoryTransaction/service/impl/InventoryTransactionServiceImpl.java
@@ -3,11 +3,18 @@ package org.jeecg.modules.wms.inventory.inventoryTransaction.service.impl;
 import org.jeecg.modules.wms.inventory.inventoryTransaction.entity.InventoryTransaction;
 import org.jeecg.modules.wms.inventory.inventoryTransaction.mapper.InventoryTransactionMapper;
 import org.jeecg.modules.wms.inventory.inventoryTransaction.service.IInventoryTransactionService;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.AsyncResult;
 import org.springframework.stereotype.Service;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
+import java.math.BigDecimal;
+import java.text.ParseException;
 import java.util.List;
+import java.util.concurrent.Future;
 
 /**
  * @Description: 库存交易记录
@@ -22,4 +29,15 @@ public class InventoryTransactionServiceImpl extends ServiceImpl<InventoryTransa
     public boolean batchSave(List<InventoryTransaction> inventoryTransactionList) {
         return this.saveBatch(inventoryTransactionList);
     }
+
+    @Async
+    @Override
+    public Future<BigDecimal> asyncCalculateInventory(String startTime, String endTime, Integer type) {
+        LambdaQueryWrapper<InventoryTransaction> inventoryTransactionLambdaQueryWrapper = Wrappers.lambdaQuery();
+        inventoryTransactionLambdaQueryWrapper.select(InventoryTransaction::getCreateTime, InventoryTransaction::getQty, InventoryTransaction::getType)
+            .ge(InventoryTransaction::getCreateTime, startTime).lt(InventoryTransaction::getCreateTime, endTime).eq(InventoryTransaction::getType, type);
+        List<InventoryTransaction> inventoryTransactions = list(inventoryTransactionLambdaQueryWrapper);
+        BigDecimal shipmentResult = inventoryTransactions.stream().map(InventoryTransaction::getQty).reduce(BigDecimal.ZERO, BigDecimal::add);
+        return new AsyncResult<>(shipmentResult);
+    }
 }
--
libgit2 0.22.2