Commit d995a3204ae1c6ee60e978e1227f8d39eafd6057

Authored by 谭毅彬
1 parent 9959dff8

库存概况性能优化

Signed-off-by: TanYibin <5491541@qq.com>
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/controller/HomePageViewController.java
... ... @@ -42,8 +42,11 @@ import javax.annotation.Resource;
42 42 import javax.servlet.http.HttpServletRequest;
43 43  
44 44 import java.math.BigDecimal;
  45 +import java.text.ParseException;
45 46 import java.text.SimpleDateFormat;
46 47 import java.util.*;
  48 +import java.util.concurrent.ExecutionException;
  49 +import java.util.concurrent.TimeoutException;
47 50 import java.util.stream.Collectors;
48 51  
49 52 /**
... ... @@ -62,9 +65,13 @@ public class HomePageViewController {
62 65 /**
63 66 * 库存概况
64 67 * @return
  68 + * @throws TimeoutException
  69 + * @throws ExecutionException
  70 + * @throws InterruptedException
  71 + * @throws ParseException
65 72 */
66 73 @RequestMapping(value = "/deliveringAmount", method = {RequestMethod.PUT, RequestMethod.POST})
67   - public Result<String> deliveringAmount(HttpServletRequest request) {
  74 + public Result<String> deliveringAmount(HttpServletRequest request) throws ParseException, InterruptedException, ExecutionException, TimeoutException {
68 75 return homePageViewService.deliveringAmount(request);
69 76 }
70 77  
... ...
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;
6 6 import org.jeecg.modules.wms.task.taskHeader.entity.TaskHeader;
7 7  
8 8 import java.io.Serializable;
  9 +import java.text.ParseException;
9 10 import java.util.Collection;
  11 +import java.util.concurrent.ExecutionException;
  12 +import java.util.concurrent.TimeoutException;
10 13  
11 14 import javax.servlet.http.HttpServletRequest;
12 15  
... ... @@ -18,7 +21,7 @@ import javax.servlet.http.HttpServletRequest;
18 21 */
19 22 public interface HomePageViewService {
20 23  
21   - Result<String> deliveringAmount(HttpServletRequest request);
  24 + Result<String> deliveringAmount(HttpServletRequest request) throws ParseException, InterruptedException, ExecutionException, TimeoutException;
22 25  
23 26 Result<String> inventoryUtilization(HttpServletRequest request);
24 27  
... ...
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/home/service/impl/HomePageViewServiceImpl.java
1 1 package org.jeecg.modules.wms.home.service.impl;
2 2  
3   -import cn.hutool.core.util.ArrayUtil;
4   -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
5   -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
6   -import com.baomidou.mybatisplus.core.toolkit.Wrappers;
7   -import com.baomidou.mybatisplus.extension.service.IService;
8   -import com.github.abel533.echarts.Option;
9   -import com.github.abel533.echarts.axis.Axis;
10   -import com.github.abel533.echarts.axis.CategoryAxis;
11   -import com.github.abel533.echarts.axis.ValueAxis;
12   -import com.github.abel533.echarts.code.*;
13   -import com.github.abel533.echarts.json.GsonUtil;
14   -import com.github.abel533.echarts.series.Line;
15   -import com.github.abel533.echarts.series.Pie;
16   -import com.github.abel533.echarts.style.itemstyle.Normal;
  3 +import java.math.BigDecimal;
  4 +import java.text.ParseException;
  5 +import java.text.SimpleDateFormat;
  6 +import java.util.ArrayList;
  7 +import java.util.Arrays;
  8 +import java.util.Date;
  9 +import java.util.HashMap;
  10 +import java.util.LinkedHashMap;
  11 +import java.util.List;
  12 +import java.util.Map;
  13 +import java.util.concurrent.ExecutionException;
  14 +import java.util.concurrent.Future;
  15 +import java.util.concurrent.TimeUnit;
  16 +import java.util.concurrent.TimeoutException;
  17 +import java.util.stream.Collectors;
  18 +
  19 +import javax.annotation.Resource;
  20 +import javax.servlet.http.HttpServletRequest;
  21 +
17 22 import org.jeecg.common.api.vo.Result;
18 23 import org.jeecg.modules.system.entity.SysDictItem;
19 24 import org.jeecg.modules.system.service.impl.SysDictItemServiceImpl;
20 25 import org.jeecg.modules.wms.config.location.entity.Location;
21 26 import org.jeecg.modules.wms.config.location.service.impl.LocationServiceImpl;
22 27 import org.jeecg.modules.wms.home.service.HomePageViewService;
23   -
24 28 import org.jeecg.modules.wms.home.support.ChartData;
25 29 import org.jeecg.modules.wms.inventory.inventoryHeader.entity.InventoryDetail;
26 30 import org.jeecg.modules.wms.inventory.inventoryHeader.entity.InventoryHeader;
27 31 import org.jeecg.modules.wms.inventory.inventoryHeader.service.impl.InventoryDetailServiceImpl;
28 32 import org.jeecg.modules.wms.inventory.inventoryHeader.service.impl.InventoryHeaderServiceImpl;
29 33 import org.jeecg.modules.wms.inventory.inventoryTransaction.entity.InventoryTransaction;
30   -import org.jeecg.modules.wms.inventory.inventoryTransaction.service.impl.InventoryTransactionServiceImpl;
31   -import org.jeecg.modules.wms.receipt.receiptHeader.entity.ReceiptHeader;
  34 +import org.jeecg.modules.wms.inventory.inventoryTransaction.service.IInventoryTransactionService;
32 35 import org.jeecg.modules.wms.receipt.receiptHeader.service.impl.ReceiptHeaderServiceImpl;
33   -import org.jeecg.modules.wms.shipment.shipmentHeader.entity.ShipmentHeader;
34 36 import org.jeecg.modules.wms.shipment.shipmentHeader.service.impl.ShipmentHeaderServiceImpl;
35   -import org.jeecg.modules.wms.task.taskHeader.entity.TaskDetail;
36 37 import org.jeecg.modules.wms.task.taskHeader.entity.TaskHeader;
37 38 import org.jeecg.modules.wms.task.taskHeader.service.impl.TaskDetailServiceImpl;
38 39 import org.jeecg.modules.wms.task.taskHeader.service.impl.TaskHeaderServiceImpl;
39 40 import org.jeecg.utils.HuahengJwtUtil;
40   -import org.jeecg.utils.StringUtils;
41 41 import org.jeecg.utils.constant.QuantityConstant;
42 42 import org.jeecg.utils.support.Convert;
43 43 import org.springframework.stereotype.Service;
44 44  
45   -import javax.annotation.Resource;
46   -import javax.servlet.http.HttpServletRequest;
  45 +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  46 +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  47 +import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  48 +import com.github.abel533.echarts.Option;
  49 +import com.github.abel533.echarts.axis.Axis;
  50 +import com.github.abel533.echarts.axis.CategoryAxis;
  51 +import com.github.abel533.echarts.axis.ValueAxis;
  52 +import com.github.abel533.echarts.code.LegendType;
  53 +import com.github.abel533.echarts.code.Orient;
  54 +import com.github.abel533.echarts.code.PointerType;
  55 +import com.github.abel533.echarts.code.SeriesType;
  56 +import com.github.abel533.echarts.code.Trigger;
  57 +import com.github.abel533.echarts.json.GsonUtil;
  58 +import com.github.abel533.echarts.series.Line;
  59 +import com.github.abel533.echarts.series.Pie;
  60 +import com.github.abel533.echarts.style.itemstyle.Normal;
47 61  
48   -import java.math.BigDecimal;
49   -import java.text.SimpleDateFormat;
50   -import java.util.*;
51   -import java.util.stream.Collectors;
  62 +import cn.hutool.core.date.DateUtil;
  63 +import cn.hutool.core.util.ArrayUtil;
52 64  
53 65 /**
54 66 * @Description: 任务表
... ... @@ -87,60 +99,23 @@ public class HomePageViewServiceImpl implements HomePageViewService {
87 99 HomePageViewService homePageViewService;
88 100  
89 101 @Resource
90   - InventoryTransactionServiceImpl inventoryTransactionServiceImpl;
  102 + IInventoryTransactionService inventoryTransactionService;
91 103  
92 104 @Override
93   - public Result<String> deliveringAmount(HttpServletRequest request) {
94   - // 通过库存交易记录来查询
95   - LambdaQueryWrapper<InventoryTransaction> inventoryTransactionLambdaQueryWrapper = Wrappers.lambdaQuery();
96   - HuahengJwtUtil.setWarehouseCode(inventoryTransactionLambdaQueryWrapper, InventoryTransaction.class, request);
97   - inventoryTransactionLambdaQueryWrapper.select(InventoryTransaction::getCreateTime, InventoryTransaction::getQty, InventoryTransaction::getType);
98   - List<InventoryTransaction> inventoryTransactions = inventoryTransactionServiceImpl.list(inventoryTransactionLambdaQueryWrapper);
99   -
100   - // 查询出库单的7天出库量
101   - List<InventoryTransaction> shipmentHeaders = new ArrayList<>();
102   - shipmentHeaders.addAll(inventoryTransactions);
103   - shipmentHeaders.removeIf(Inventory -> Inventory.getType() != 20);
104   - LinkedHashMap<String, String> list = new LinkedHashMap<>();
105   -
106   - for (int i = 6; i >= 0; i--) {
107   - String today = new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis() - (1000 * 60 * 60 * 24) * i).toString();
108   - List<InventoryTransaction> collect = shipmentHeaders.stream()
109   - .filter(shipmentHeader -> new SimpleDateFormat("yyyy-MM-dd").format(shipmentHeader.getCreateTime()).toString().contains(today))
110   - .collect(Collectors.toList());
111   - if (collect.size() != 0) {
112   - BigDecimal sum = new BigDecimal(0);
113   - for (InventoryTransaction item : collect) {
114   - sum = sum.add(item.getQty());
115   - }
116   - list.put(today, String.valueOf(sum));
117   - } else {
118   - list.put(today, "0");
119   - }
120   - }
121   -
122   - // 查询入库单的7天入库量
123   - List<InventoryTransaction> receiptHeaders = new ArrayList<>();
124   - receiptHeaders.addAll(inventoryTransactions);
125   - receiptHeaders.removeIf(Inventory -> Inventory.getType() != 10);
126   - LinkedHashMap<String, String> list2 = new LinkedHashMap<>();
  105 + public Result<String> deliveringAmount(HttpServletRequest request) throws ParseException, InterruptedException, ExecutionException, TimeoutException {
  106 + Long dateTime = new Date().getTime();
  107 + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  108 + LinkedHashMap<String, Future<BigDecimal>> shipmentQuantityMap = new LinkedHashMap<String, Future<BigDecimal>>();
  109 + LinkedHashMap<String, Future<BigDecimal>> receiptQuantityMap = new LinkedHashMap<String, Future<BigDecimal>>();
  110 + // 查询出库单的7天出库量/入库量
127 111 for (int i = 6; i >= 0; i--) {
128   - List<InventoryTransaction> receiptHeadersCop = receiptHeaders;
129   - String today = new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis() - (1000 * 60 * 60 * 24) * i).toString();
130   - List<InventoryTransaction> collect = receiptHeadersCop.stream()
131   - .filter(ReceiptHeader -> new SimpleDateFormat("yyyy-MM-dd").format(ReceiptHeader.getCreateTime()).toString().contains(today))
132   - .collect(Collectors.toList());
133   - if (collect.size() != 0) {
134   - BigDecimal sum = new BigDecimal(0);
135   - for (InventoryTransaction item : collect) {
136   - sum = sum.add(item.getQty());
137   - }
138   - list2.put(today, String.valueOf(sum));
139   - } else {
140   - list2.put(today, "0");
141   - }
  112 + String startTime = simpleDateFormat.format(dateTime - (1000 * 60 * 60 * 24) * i);
  113 + String endTime = simpleDateFormat.format(DateUtil.offsetDay(simpleDateFormat.parse(startTime), 1));
  114 + shipmentQuantityMap.put(startTime,
  115 + inventoryTransactionService.asyncCalculateInventory(startTime, endTime, QuantityConstant.INVENTORY_TRANSACTION_SHIPMENT));
  116 + receiptQuantityMap.put(startTime,
  117 + inventoryTransactionService.asyncCalculateInventory(startTime, endTime, QuantityConstant.INVENTORY_TRANSACTION_RECEIPT));
142 118 }
143   -
144 119 Option option = new Option();
145 120 option.tooltip().trigger(Trigger.axis).axisPointer().type(PointerType.cross);
146 121 option.grid().setLeft("3%");
... ... @@ -158,17 +133,17 @@ public class HomePageViewServiceImpl implements HomePageViewService {
158 133 axis.boundaryGap(false);
159 134 Line line = new Line();
160 135 line.setName("发货量");
161   - for (String key : list.keySet()) {
  136 + for (String key : shipmentQuantityMap.keySet()) {
162 137 Object o1 = key;
163   - Object o2 = list.get(key);
  138 + Object o2 = shipmentQuantityMap.get(key).get(10000, TimeUnit.MILLISECONDS);
164 139 axis.data().add(o1);
165 140 line.data().add(o2);
166 141 }
167 142 Line line2 = new Line();
168 143 line2.setName("收货量");
169   - for (String key : list2.keySet()) {
  144 + for (String key : receiptQuantityMap.keySet()) {
170 145 Object o1 = key;
171   - Object o2 = list2.get(key);
  146 + Object o2 = receiptQuantityMap.get(key).get(10000, TimeUnit.MILLISECONDS);
172 147 line2.data().add(o2);
173 148 }
174 149 option.setxAxis(Arrays.asList(axis));
... ... @@ -346,7 +321,7 @@ public class HomePageViewServiceImpl implements HomePageViewService {
346 321 LambdaQueryWrapper<InventoryTransaction> inventoryTransactionLambdaQueryWrapper = Wrappers.lambdaQuery();
347 322 HuahengJwtUtil.setWarehouseCode(inventoryTransactionLambdaQueryWrapper, InventoryTransaction.class, request);
348 323 inventoryTransactionLambdaQueryWrapper.select(InventoryTransaction::getType, InventoryTransaction::getQty).gt(InventoryTransaction::getCreateTime, today);
349   - List<InventoryTransaction> inventoryTransactions = inventoryTransactionServiceImpl.list(inventoryTransactionLambdaQueryWrapper);
  324 + List<InventoryTransaction> inventoryTransactions = inventoryTransactionService.list(inventoryTransactionLambdaQueryWrapper);
350 325 BigDecimal receiptQty = BigDecimal.ZERO;
351 326 BigDecimal shipmentQty = BigDecimal.ZERO;
352 327  
... ...
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;
3 3 import com.baomidou.mybatisplus.extension.service.IService;
4 4 import org.jeecg.modules.wms.inventory.inventoryTransaction.entity.InventoryTransaction;
5 5  
  6 +import java.math.BigDecimal;
  7 +import java.text.ParseException;
6 8 import java.util.List;
  9 +import java.util.concurrent.Future;
7 10  
8 11 /**
9 12 * @Description: 库存交易记录
... ... @@ -14,5 +17,7 @@ import java.util.List;
14 17 public interface IInventoryTransactionService extends IService<InventoryTransaction> {
15 18  
16 19 boolean batchSave(List<InventoryTransaction> inventoryTransactionList);
  20 +
  21 + Future<BigDecimal> asyncCalculateInventory(String startTime, String endTime, Integer type) throws ParseException;
17 22  
18 23 }
... ...
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;
3 3 import org.jeecg.modules.wms.inventory.inventoryTransaction.entity.InventoryTransaction;
4 4 import org.jeecg.modules.wms.inventory.inventoryTransaction.mapper.InventoryTransactionMapper;
5 5 import org.jeecg.modules.wms.inventory.inventoryTransaction.service.IInventoryTransactionService;
  6 +import org.springframework.scheduling.annotation.Async;
  7 +import org.springframework.scheduling.annotation.AsyncResult;
6 8 import org.springframework.stereotype.Service;
7 9  
  10 +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  11 +import com.baomidou.mybatisplus.core.toolkit.Wrappers;
8 12 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
9 13  
  14 +import java.math.BigDecimal;
  15 +import java.text.ParseException;
10 16 import java.util.List;
  17 +import java.util.concurrent.Future;
11 18  
12 19 /**
13 20 * @Description: 库存交易记录
... ... @@ -22,4 +29,15 @@ public class InventoryTransactionServiceImpl extends ServiceImpl&lt;InventoryTransa
22 29 public boolean batchSave(List<InventoryTransaction> inventoryTransactionList) {
23 30 return this.saveBatch(inventoryTransactionList);
24 31 }
  32 +
  33 + @Async
  34 + @Override
  35 + public Future<BigDecimal> asyncCalculateInventory(String startTime, String endTime, Integer type) {
  36 + LambdaQueryWrapper<InventoryTransaction> inventoryTransactionLambdaQueryWrapper = Wrappers.lambdaQuery();
  37 + inventoryTransactionLambdaQueryWrapper.select(InventoryTransaction::getCreateTime, InventoryTransaction::getQty, InventoryTransaction::getType)
  38 + .ge(InventoryTransaction::getCreateTime, startTime).lt(InventoryTransaction::getCreateTime, endTime).eq(InventoryTransaction::getType, type);
  39 + List<InventoryTransaction> inventoryTransactions = list(inventoryTransactionLambdaQueryWrapper);
  40 + BigDecimal shipmentResult = inventoryTransactions.stream().map(InventoryTransaction::getQty).reduce(BigDecimal.ZERO, BigDecimal::add);
  41 + return new AsyncResult<>(shipmentResult);
  42 + }
25 43 }
... ...