From 7ca305d04cb4ad26dc9ea024e7eddf8bf9e668f8 Mon Sep 17 00:00:00 2001 From: erdanergou Date: Fri, 7 Jul 2023 17:00:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E5=BA=93=E5=AD=98=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E6=B7=BB=E5=8A=A0=E5=9B=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DepositoryRecordController.java | 56 ++++++++++++++--- .../service/DepositoryRecordService.java | 8 +++ .../utils/ObjectFormatUtil.java | 7 +++ .../pages/depository/Inventory-view_back.html | 27 ++++++++ .../LineChartForInventoryTest.java | 61 +++++++++++++++---- 5 files changed, 138 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/dreamchaser/depository_manage/controller/DepositoryRecordController.java b/src/main/java/com/dreamchaser/depository_manage/controller/DepositoryRecordController.java index d627dfa0..f5f01e6e 100644 --- a/src/main/java/com/dreamchaser/depository_manage/controller/DepositoryRecordController.java +++ b/src/main/java/com/dreamchaser/depository_manage/controller/DepositoryRecordController.java @@ -2309,6 +2309,8 @@ public class DepositoryRecordController { @PostMapping("/getApplicationForMaterial") public RestResponse getInventoryApplication(@RequestBody JSONObject jsonObject) { Integer id = jsonObject.getInteger("id"); + // 获取当前物料的库存数据 + Inventory inventoryById = materialService.findInventoryById(id); // 获取本月至今的日期 Map monthBeginToNow = DateUtil.getMonthBeginToNow(); // 获取至今的日期名称 @@ -2317,27 +2319,61 @@ public class DepositoryRecordController { List dayTimeSpaces = ObjectFormatUtil.objToList(monthBeginToNow.get("dayTimeSpace"), Long.class); // 获取当前物料的入库总额与数量 Map seriesForApplicationIn = depositoryRecordService.getApplicationByMaterial(id, dayTimeSpaces, 1); + Object amountItemForIn = seriesForApplicationIn.get("amountItem"); Object countItemForIn = seriesForApplicationIn.get("countItem"); + // 将入库总额项目转为map类型 + Map countItemForInMapString = ObjectFormatUtil.objToMap(countItemForIn, String.class, Object.class); + // 获取入库总额data并转为list类型 + List amountListForIn = ObjectFormatUtil.objToList(countItemForInMapString.get("data"), Double.class); // 获取当前物料的出库总额与数量 Map seriesForApplicationOut = depositoryRecordService.getApplicationByMaterial(id, dayTimeSpaces, 2); + Object amountItemForOut = seriesForApplicationOut.get("amountItem"); Object countItemForOut = seriesForApplicationOut.get("countItem"); - + // 将出库总额项目转为map类型 + Map countItemForOutMapString = ObjectFormatUtil.objToMap(countItemForOut, String.class, Object.class); + // 获取出库总额data并转为list类型 + List amountListForOut = ObjectFormatUtil.objToList(countItemForOutMapString.get("data"), Double.class); + // 定义库存数量列表 + List inventoryCountList = new ArrayList<>(); + // 定义库存总额列表 + List inventoryAmountList = new ArrayList<>(); + // 获取当前库存 + double inventory = inventoryById.getQuantity() / 100.0; + // 获取当前物料单价 + Double price = inventoryById.getPrice(); + // 获取当前物料总额 + double amount = ObjectFormatUtil.multiply(inventory, price); + // 添加 + inventoryAmountList.add(amount); + inventoryCountList.add(inventory); + for (int i = amountListForOut.size() - 1; i > 0; i--) { + // 获取当前日期下的库存 库存 = 库存 + 出库 - 入库 + inventory = ObjectFormatUtil.subtract(ObjectFormatUtil.sum(inventory, amountListForOut.get(i)), amountListForIn.get(i)); + inventoryCountList.add(inventory); + inventoryAmountList.add(ObjectFormatUtil.multiply(inventory, price)); + } + // 反转 + Collections.reverse(inventoryAmountList); + Collections.reverse(inventoryCountList); + Map countItemForInventory = depositoryRecordService.createStackedAreaChartSeriesItem("count", inventoryCountList); + Map amountItemForInventory = depositoryRecordService.createStackedAreaChartSeriesItem("amount", inventoryAmountList); Map result = new HashMap<>(); - result.put("amountItemForIn",amountItemForIn); - result.put("countItemForIn",countItemForIn); - result.put("amountItemForOut",amountItemForOut); - result.put("countItemForOut",countItemForOut); - Map> legendItem = new HashMap<>(); + result.put("amountItemForIn", amountItemForIn); + result.put("countItemForIn", countItemForIn); + result.put("amountItemForOut", amountItemForOut); + result.put("countItemForOut", countItemForOut); + Map> legendItem = new HashMap<>(); List legends = new ArrayList<>(); legends.add("count"); legends.add("amount"); - legendItem.put("data",legends); - result.put("legend",legendItem); - result.put("dayNames",dayNames); - + legendItem.put("data", legends); + result.put("legend", legendItem); + result.put("dayNames", dayNames); + result.put("countItemForInventory",countItemForInventory); + result.put("amountItemForInventory",amountItemForInventory); return new RestResponse(result); } diff --git a/src/main/java/com/dreamchaser/depository_manage/service/DepositoryRecordService.java b/src/main/java/com/dreamchaser/depository_manage/service/DepositoryRecordService.java index 936be8a6..363d1781 100644 --- a/src/main/java/com/dreamchaser/depository_manage/service/DepositoryRecordService.java +++ b/src/main/java/com/dreamchaser/depository_manage/service/DepositoryRecordService.java @@ -489,6 +489,14 @@ public interface DepositoryRecordService { */ Map getApplicationByMaterial(Integer id, List days,Integer type); + /** + * 用于构造堆叠面积图的series项目 + * @param name 当前项目名称 + * @param data 数据 + * @return + */ + Map createStackedAreaChartSeriesItem(String name, List data); + diff --git a/src/main/java/com/dreamchaser/depository_manage/utils/ObjectFormatUtil.java b/src/main/java/com/dreamchaser/depository_manage/utils/ObjectFormatUtil.java index cad77df3..e875315a 100644 --- a/src/main/java/com/dreamchaser/depository_manage/utils/ObjectFormatUtil.java +++ b/src/main/java/com/dreamchaser/depository_manage/utils/ObjectFormatUtil.java @@ -59,6 +59,13 @@ public class ObjectFormatUtil { return bd1.add(bd2).doubleValue(); } + // double类型相减 + public static double subtract(double d1, double d2) { + BigDecimal bd1 = new BigDecimal(Double.toString(d1)); + BigDecimal bd2 = new BigDecimal(Double.toString(d2)); + return bd1.subtract(bd2).doubleValue(); + } + // double 类型相乘 public static double multiply(double d1, double d2) { BigDecimal bd1 = new BigDecimal(Double.toString(d1)); diff --git a/src/main/resources/templates/pages/depository/Inventory-view_back.html b/src/main/resources/templates/pages/depository/Inventory-view_back.html index ee323606..66eab2b0 100644 --- a/src/main/resources/templates/pages/depository/Inventory-view_back.html +++ b/src/main/resources/templates/pages/depository/Inventory-view_back.html @@ -98,6 +98,8 @@
物料库存明细
+
+
@@ -365,6 +367,7 @@ // 柱状图 var echartLineChartIn = echarts.init(document.getElementById('echarts-line-in')); var echartLineChartOut = echarts.init(document.getElementById('echarts-line-out')); + var echartLineChartInventory = echarts.init(document.getElementById('echarts-line-inventory')); var optionLineChartIn = { xAxis: { type: 'category', @@ -394,6 +397,21 @@ }, series: [] + }; + var optionLineChartInventory = { + xAxis: { + type: 'category', + boundaryGap: false, + data: [] + }, + yAxis: { + type: 'value' + }, + tooltip: { + trigger: "axis", + }, + series: [] + }; $.ajax({ @@ -419,22 +437,31 @@ let countItemForIn = data["countItemForIn"]; let amountItemForOut = data["amountItemForOut"]; let countItemForOut = data["countItemForOut"]; + let countItemForInventory = data["countItemForInventory"]; + let amountItemForInventory = data["amountItemForInventory"]; let legendItem = data["legend"]; let seriesInItem = []; let seriesOutItem = []; + let seriesInventoryItem = []; seriesInItem.push(amountItemForIn); seriesInItem.push(countItemForIn); seriesOutItem.push(amountItemForOut); seriesOutItem.push(countItemForOut); + seriesInventoryItem.push(countItemForInventory); + seriesInventoryItem.push(amountItemForInventory); optionLineChartIn.series = seriesInItem; optionLineChartOut.series = seriesOutItem; + optionLineChartInventory.series = seriesInventoryItem; optionLineChartIn.xAxis.data = dayNames; optionLineChartIn.legend = legendItem; optionLineChartOut.legend = legendItem; optionLineChartOut.xAxis.data = dayNames; + optionLineChartInventory.legend = legendItem; + optionLineChartInventory.xAxis.data = dayNames; echartLineChartIn.setOption(optionLineChartIn); echartLineChartOut.setOption(optionLineChartOut); + echartLineChartInventory.setOption(optionLineChartInventory); } }) diff --git a/src/test/java/com/dreamchaser/depository_manage/LineChartForInventoryTest.java b/src/test/java/com/dreamchaser/depository_manage/LineChartForInventoryTest.java index 1f46e23a..7e12ff4b 100644 --- a/src/test/java/com/dreamchaser/depository_manage/LineChartForInventoryTest.java +++ b/src/test/java/com/dreamchaser/depository_manage/LineChartForInventoryTest.java @@ -12,10 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.*; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @@ -35,6 +32,8 @@ public class LineChartForInventoryTest { * @param id 带查询库存id */ public RestResponse getInventoryApplication(Integer id) { + // 获取当前物料的库存数据 + Inventory inventoryById = materialService.findInventoryById(id); // 获取本月至今的日期 Map monthBeginToNow = DateUtil.getMonthBeginToNow(); // 获取至今的日期名称 @@ -43,21 +42,61 @@ public class LineChartForInventoryTest { List dayTimeSpaces = ObjectFormatUtil.objToList(monthBeginToNow.get("dayTimeSpace"), Long.class); // 获取当前物料的入库总额与数量 Map seriesForApplicationIn = depositoryRecordService.getApplicationByMaterial(id, dayTimeSpaces, 1); + Object amountItemForIn = seriesForApplicationIn.get("amountItem"); Object countItemForIn = seriesForApplicationIn.get("countItem"); + // 将入库总额项目转为map类型 + Map countItemForInMapString = ObjectFormatUtil.objToMap(countItemForIn, String.class, Object.class); + // 获取入库总额data并转为list类型 + List amountListForIn = ObjectFormatUtil.objToList(countItemForInMapString.get("data"), Double.class); // 获取当前物料的出库总额与数量 Map seriesForApplicationOut = depositoryRecordService.getApplicationByMaterial(id, dayTimeSpaces, 2); + Object amountItemForOut = seriesForApplicationOut.get("amountItem"); Object countItemForOut = seriesForApplicationOut.get("countItem"); - + // 将出库总额项目转为map类型 + Map countItemForOutMapString = ObjectFormatUtil.objToMap(countItemForOut, String.class, Object.class); + // 获取出库总额data并转为list类型 + List amountListForOut = ObjectFormatUtil.objToList(countItemForOutMapString.get("data"), Double.class); + // 定义库存数量列表 + List inventoryCountList = new ArrayList<>(); + // 定义库存总额列表 + List inventoryAmountList = new ArrayList<>(); + // 获取当前库存 + double inventory = inventoryById.getQuantity() / 100.0; + // 获取当前物料单价 + Double price = inventoryById.getPrice(); + // 获取当前物料总额 + double amount = ObjectFormatUtil.multiply(inventory, price); + // 添加 + inventoryAmountList.add(amount); + inventoryCountList.add(inventory); + for (int i = amountListForOut.size() - 1; i > 0; i--) { + // 获取当前日期下的库存 库存 = 库存 + 出库 - 入库 + inventory = ObjectFormatUtil.subtract(ObjectFormatUtil.sum(inventory, amountListForOut.get(i)), amountListForIn.get(i)); + inventoryCountList.add(inventory); + inventoryAmountList.add(ObjectFormatUtil.multiply(inventory, price)); + } + // 反转 + Collections.reverse(inventoryAmountList); + Collections.reverse(inventoryCountList); + Map countItemForInventory = depositoryRecordService.createStackedAreaChartSeriesItem("count", inventoryCountList); + Map amountItemForInventory = depositoryRecordService.createStackedAreaChartSeriesItem("count", inventoryAmountList); Map result = new HashMap<>(); - result.put("amountItemForIn",amountItemForIn); - result.put("countItemForIn",countItemForIn); - result.put("amountItemForOut",amountItemForOut); - result.put("countItemForOut",countItemForOut); - result.put("dayNames",dayNames); - + result.put("amountItemForIn", amountItemForIn); + result.put("countItemForIn", countItemForIn); + result.put("amountItemForOut", amountItemForOut); + result.put("countItemForOut", countItemForOut); + Map> legendItem = new HashMap<>(); + List legends = new ArrayList<>(); + legends.add("count"); + legends.add("amount"); + legendItem.put("data", legends); + result.put("legend", legendItem); + result.put("dayNames", dayNames); + result.put("countItemForInventory",countItemForInventory); + result.put("amountItemForInventory",amountItemForInventory); return new RestResponse(result); }