Browse Source

为库存查询页面添加图表

lwx_dev
erdanergou 2 years ago
parent
commit
7ca305d04c
  1. 56
      src/main/java/com/dreamchaser/depository_manage/controller/DepositoryRecordController.java
  2. 8
      src/main/java/com/dreamchaser/depository_manage/service/DepositoryRecordService.java
  3. 7
      src/main/java/com/dreamchaser/depository_manage/utils/ObjectFormatUtil.java
  4. 27
      src/main/resources/templates/pages/depository/Inventory-view_back.html
  5. 61
      src/test/java/com/dreamchaser/depository_manage/LineChartForInventoryTest.java

56
src/main/java/com/dreamchaser/depository_manage/controller/DepositoryRecordController.java

@ -2309,6 +2309,8 @@ public class DepositoryRecordController {
@PostMapping("/getApplicationForMaterial") @PostMapping("/getApplicationForMaterial")
public RestResponse getInventoryApplication(@RequestBody JSONObject jsonObject) { public RestResponse getInventoryApplication(@RequestBody JSONObject jsonObject) {
Integer id = jsonObject.getInteger("id"); Integer id = jsonObject.getInteger("id");
// 获取当前物料的库存数据
Inventory inventoryById = materialService.findInventoryById(id);
// 获取本月至今的日期 // 获取本月至今的日期
Map<String, Object> monthBeginToNow = DateUtil.getMonthBeginToNow(); Map<String, Object> monthBeginToNow = DateUtil.getMonthBeginToNow();
// 获取至今的日期名称 // 获取至今的日期名称
@ -2317,27 +2319,61 @@ public class DepositoryRecordController {
List<Long> dayTimeSpaces = ObjectFormatUtil.objToList(monthBeginToNow.get("dayTimeSpace"), Long.class); List<Long> dayTimeSpaces = ObjectFormatUtil.objToList(monthBeginToNow.get("dayTimeSpace"), Long.class);
// 获取当前物料的入库总额与数量 // 获取当前物料的入库总额与数量
Map<String, Object> seriesForApplicationIn = depositoryRecordService.getApplicationByMaterial(id, dayTimeSpaces, 1); Map<String, Object> seriesForApplicationIn = depositoryRecordService.getApplicationByMaterial(id, dayTimeSpaces, 1);
Object amountItemForIn = seriesForApplicationIn.get("amountItem"); Object amountItemForIn = seriesForApplicationIn.get("amountItem");
Object countItemForIn = seriesForApplicationIn.get("countItem"); Object countItemForIn = seriesForApplicationIn.get("countItem");
// 将入库总额项目转为map类型
Map<String, Object> countItemForInMapString = ObjectFormatUtil.objToMap(countItemForIn, String.class, Object.class);
// 获取入库总额data并转为list类型
List<Double> amountListForIn = ObjectFormatUtil.objToList(countItemForInMapString.get("data"), Double.class);
// 获取当前物料的出库总额与数量 // 获取当前物料的出库总额与数量
Map<String, Object> seriesForApplicationOut = depositoryRecordService.getApplicationByMaterial(id, dayTimeSpaces, 2); Map<String, Object> seriesForApplicationOut = depositoryRecordService.getApplicationByMaterial(id, dayTimeSpaces, 2);
Object amountItemForOut = seriesForApplicationOut.get("amountItem"); Object amountItemForOut = seriesForApplicationOut.get("amountItem");
Object countItemForOut = seriesForApplicationOut.get("countItem"); Object countItemForOut = seriesForApplicationOut.get("countItem");
// 将出库总额项目转为map类型
Map<String, Object> countItemForOutMapString = ObjectFormatUtil.objToMap(countItemForOut, String.class, Object.class);
// 获取出库总额data并转为list类型
List<Double> amountListForOut = ObjectFormatUtil.objToList(countItemForOutMapString.get("data"), Double.class);
// 定义库存数量列表
List<Object> inventoryCountList = new ArrayList<>();
// 定义库存总额列表
List<Object> 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<String, Object> countItemForInventory = depositoryRecordService.createStackedAreaChartSeriesItem("count", inventoryCountList);
Map<String, Object> amountItemForInventory = depositoryRecordService.createStackedAreaChartSeriesItem("amount", inventoryAmountList);
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
result.put("amountItemForIn",amountItemForIn); result.put("amountItemForIn", amountItemForIn);
result.put("countItemForIn",countItemForIn); result.put("countItemForIn", countItemForIn);
result.put("amountItemForOut",amountItemForOut); result.put("amountItemForOut", amountItemForOut);
result.put("countItemForOut",countItemForOut); result.put("countItemForOut", countItemForOut);
Map<String,List<String>> legendItem = new HashMap<>(); Map<String, List<String>> legendItem = new HashMap<>();
List<String> legends = new ArrayList<>(); List<String> legends = new ArrayList<>();
legends.add("count"); legends.add("count");
legends.add("amount"); legends.add("amount");
legendItem.put("data",legends); legendItem.put("data", legends);
result.put("legend",legendItem); result.put("legend", legendItem);
result.put("dayNames",dayNames); result.put("dayNames", dayNames);
result.put("countItemForInventory",countItemForInventory);
result.put("amountItemForInventory",amountItemForInventory);
return new RestResponse(result); return new RestResponse(result);
} }

8
src/main/java/com/dreamchaser/depository_manage/service/DepositoryRecordService.java

@ -489,6 +489,14 @@ public interface DepositoryRecordService {
*/ */
Map<String,Object> getApplicationByMaterial(Integer id, List<Long> days,Integer type); Map<String,Object> getApplicationByMaterial(Integer id, List<Long> days,Integer type);
/**
* 用于构造堆叠面积图的series项目
* @param name 当前项目名称
* @param data 数据
* @return
*/
Map<String, Object> createStackedAreaChartSeriesItem(String name, List<Object> data);

7
src/main/java/com/dreamchaser/depository_manage/utils/ObjectFormatUtil.java

@ -59,6 +59,13 @@ public class ObjectFormatUtil {
return bd1.add(bd2).doubleValue(); 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 类型相乘 // double 类型相乘
public static double multiply(double d1, double d2) { public static double multiply(double d1, double d2) {
BigDecimal bd1 = new BigDecimal(Double.toString(d1)); BigDecimal bd1 = new BigDecimal(Double.toString(d1));

27
src/main/resources/templates/pages/depository/Inventory-view_back.html

@ -98,6 +98,8 @@
<div class="panel panel-warning"> <div class="panel panel-warning">
<div class="panel-heading">物料库存明细</div> <div class="panel-heading">物料库存明细</div>
<div class="panel-body"> <div class="panel-body">
<div id="echarts-line-inventory" style="height: 500px;width: 500px">
</div>
</div> </div>
</div> </div>
</div> </div>
@ -365,6 +367,7 @@
// 柱状图 // 柱状图
var echartLineChartIn = echarts.init(document.getElementById('echarts-line-in')); var echartLineChartIn = echarts.init(document.getElementById('echarts-line-in'));
var echartLineChartOut = echarts.init(document.getElementById('echarts-line-out')); var echartLineChartOut = echarts.init(document.getElementById('echarts-line-out'));
var echartLineChartInventory = echarts.init(document.getElementById('echarts-line-inventory'));
var optionLineChartIn = { var optionLineChartIn = {
xAxis: { xAxis: {
type: 'category', type: 'category',
@ -394,6 +397,21 @@
}, },
series: [] series: []
};
var optionLineChartInventory = {
xAxis: {
type: 'category',
boundaryGap: false,
data: []
},
yAxis: {
type: 'value'
},
tooltip: {
trigger: "axis",
},
series: []
}; };
$.ajax({ $.ajax({
@ -419,22 +437,31 @@
let countItemForIn = data["countItemForIn"]; let countItemForIn = data["countItemForIn"];
let amountItemForOut = data["amountItemForOut"]; let amountItemForOut = data["amountItemForOut"];
let countItemForOut = data["countItemForOut"]; let countItemForOut = data["countItemForOut"];
let countItemForInventory = data["countItemForInventory"];
let amountItemForInventory = data["amountItemForInventory"];
let legendItem = data["legend"]; let legendItem = data["legend"];
let seriesInItem = []; let seriesInItem = [];
let seriesOutItem = []; let seriesOutItem = [];
let seriesInventoryItem = [];
seriesInItem.push(amountItemForIn); seriesInItem.push(amountItemForIn);
seriesInItem.push(countItemForIn); seriesInItem.push(countItemForIn);
seriesOutItem.push(amountItemForOut); seriesOutItem.push(amountItemForOut);
seriesOutItem.push(countItemForOut); seriesOutItem.push(countItemForOut);
seriesInventoryItem.push(countItemForInventory);
seriesInventoryItem.push(amountItemForInventory);
optionLineChartIn.series = seriesInItem; optionLineChartIn.series = seriesInItem;
optionLineChartOut.series = seriesOutItem; optionLineChartOut.series = seriesOutItem;
optionLineChartInventory.series = seriesInventoryItem;
optionLineChartIn.xAxis.data = dayNames; optionLineChartIn.xAxis.data = dayNames;
optionLineChartIn.legend = legendItem; optionLineChartIn.legend = legendItem;
optionLineChartOut.legend = legendItem; optionLineChartOut.legend = legendItem;
optionLineChartOut.xAxis.data = dayNames; optionLineChartOut.xAxis.data = dayNames;
optionLineChartInventory.legend = legendItem;
optionLineChartInventory.xAxis.data = dayNames;
echartLineChartIn.setOption(optionLineChartIn); echartLineChartIn.setOption(optionLineChartIn);
echartLineChartOut.setOption(optionLineChartOut); echartLineChartOut.setOption(optionLineChartOut);
echartLineChartInventory.setOption(optionLineChartInventory);
} }
}) })

61
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.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*; import java.util.concurrent.*;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ -35,6 +32,8 @@ public class LineChartForInventoryTest {
* @param id 带查询库存id * @param id 带查询库存id
*/ */
public RestResponse getInventoryApplication(Integer id) { public RestResponse getInventoryApplication(Integer id) {
// 获取当前物料的库存数据
Inventory inventoryById = materialService.findInventoryById(id);
// 获取本月至今的日期 // 获取本月至今的日期
Map<String, Object> monthBeginToNow = DateUtil.getMonthBeginToNow(); Map<String, Object> monthBeginToNow = DateUtil.getMonthBeginToNow();
// 获取至今的日期名称 // 获取至今的日期名称
@ -43,21 +42,61 @@ public class LineChartForInventoryTest {
List<Long> dayTimeSpaces = ObjectFormatUtil.objToList(monthBeginToNow.get("dayTimeSpace"), Long.class); List<Long> dayTimeSpaces = ObjectFormatUtil.objToList(monthBeginToNow.get("dayTimeSpace"), Long.class);
// 获取当前物料的入库总额与数量 // 获取当前物料的入库总额与数量
Map<String, Object> seriesForApplicationIn = depositoryRecordService.getApplicationByMaterial(id, dayTimeSpaces, 1); Map<String, Object> seriesForApplicationIn = depositoryRecordService.getApplicationByMaterial(id, dayTimeSpaces, 1);
Object amountItemForIn = seriesForApplicationIn.get("amountItem"); Object amountItemForIn = seriesForApplicationIn.get("amountItem");
Object countItemForIn = seriesForApplicationIn.get("countItem"); Object countItemForIn = seriesForApplicationIn.get("countItem");
// 将入库总额项目转为map类型
Map<String, Object> countItemForInMapString = ObjectFormatUtil.objToMap(countItemForIn, String.class, Object.class);
// 获取入库总额data并转为list类型
List<Double> amountListForIn = ObjectFormatUtil.objToList(countItemForInMapString.get("data"), Double.class);
// 获取当前物料的出库总额与数量 // 获取当前物料的出库总额与数量
Map<String, Object> seriesForApplicationOut = depositoryRecordService.getApplicationByMaterial(id, dayTimeSpaces, 2); Map<String, Object> seriesForApplicationOut = depositoryRecordService.getApplicationByMaterial(id, dayTimeSpaces, 2);
Object amountItemForOut = seriesForApplicationOut.get("amountItem"); Object amountItemForOut = seriesForApplicationOut.get("amountItem");
Object countItemForOut = seriesForApplicationOut.get("countItem"); Object countItemForOut = seriesForApplicationOut.get("countItem");
// 将出库总额项目转为map类型
Map<String, Object> countItemForOutMapString = ObjectFormatUtil.objToMap(countItemForOut, String.class, Object.class);
// 获取出库总额data并转为list类型
List<Double> amountListForOut = ObjectFormatUtil.objToList(countItemForOutMapString.get("data"), Double.class);
// 定义库存数量列表
List<Object> inventoryCountList = new ArrayList<>();
// 定义库存总额列表
List<Object> 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<String, Object> countItemForInventory = depositoryRecordService.createStackedAreaChartSeriesItem("count", inventoryCountList);
Map<String, Object> amountItemForInventory = depositoryRecordService.createStackedAreaChartSeriesItem("count", inventoryAmountList);
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
result.put("amountItemForIn",amountItemForIn); result.put("amountItemForIn", amountItemForIn);
result.put("countItemForIn",countItemForIn); result.put("countItemForIn", countItemForIn);
result.put("amountItemForOut",amountItemForOut); result.put("amountItemForOut", amountItemForOut);
result.put("countItemForOut",countItemForOut); result.put("countItemForOut", countItemForOut);
result.put("dayNames",dayNames); Map<String, List<String>> legendItem = new HashMap<>();
List<String> 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); return new RestResponse(result);
} }

Loading…
Cancel
Save