Browse Source

完成出入库管理中的折现、柱状图

lwx_dev
erdanergou 2 years ago
parent
commit
89595d966c
  1. 62
      src/main/java/com/dreamchaser/depository_manage/controller/DepositoryController.java
  2. 15
      src/main/java/com/dreamchaser/depository_manage/controller/PageController.java
  3. 10
      src/main/java/com/dreamchaser/depository_manage/service/DepositoryRecordService.java
  4. 82
      src/main/java/com/dreamchaser/depository_manage/service/impl/DepositoryRecordServiceImpl.java
  5. 10
      src/main/java/com/dreamchaser/depository_manage/utils/DateUtil.java
  6. 215
      src/main/resources/templates/pages/depository/inEchart/BarChart.html
  7. 108
      src/main/resources/templates/pages/depository/inEchart/LineChart.html
  8. 33
      src/main/resources/templates/pages/depository/table-in.html
  9. 35
      src/main/resources/templates/pages/depository/table-out.html
  10. 77
      src/test/java/com/dreamchaser/depository_manage/BarChartTest.java
  11. 175
      src/test/java/com/dreamchaser/depository_manage/LineChartTest.java

62
src/main/java/com/dreamchaser/depository_manage/controller/DepositoryController.java

@ -201,19 +201,22 @@ public class DepositoryController {
* @param request * @param request
* @return * @return
*/ */
@GetMapping("/getWareHouseVisiblePermissionByUser")
public RestResponse getWareHouseVisiblePermissionByUser(HttpServletRequest request) { public RestResponse getWareHouseVisiblePermissionByUser(HttpServletRequest request) {
String token = request.getHeader("user-token"); String token = request.getHeader("user-token");
if (token == null) { if (token == null) {
token = (String) request.getSession().getAttribute("userToken"); token = (String) request.getSession().getAttribute("userToken");
} }
UserByPort userToken = AuthenticationTokenPool.getUserToken(token); UserByPort userToken = AuthenticationTokenPool.getUserToken(token);
Integer maindeparment = userToken.getMaindeparment();
// 获取当前用户是否存在可见仓库 // 获取当前用户是否存在可见仓库
List<Integer> union = roleService.findDepositoryIdForWareHouseVisiblePermissionByUser(userToken); Map<String, Integer> depositoryAllNameAndId = new HashMap<>();
boolean flag = false; if (PublicConfig.roleAdminorgList.contains(maindeparment)) {
if (union.size() > 0) { depositoryAllNameAndId = depositoryService.findAllDepositoryNameAndId();
flag = true; } else {
depositoryAllNameAndId = depositoryService.findDepositoryAllNameAndId(userToken);
} }
return new RestResponse(flag); return new RestResponse(depositoryAllNameAndId);
} }
/** /**
@ -1244,6 +1247,7 @@ public class DepositoryController {
return new RestResponse(list); return new RestResponse(list);
} }
/** /**
* 获取当前条件下用户管理的仓库 * 获取当前条件下用户管理的仓库
* *
@ -1256,6 +1260,54 @@ public class DepositoryController {
return new RestResponse(result); return new RestResponse(result);
} }
/**
* 用于获取出入库的柱状图数据
*
* @param map 查询数据
* @param request
* @return
*/
@PostMapping("/getBarChartDataByDateType")
public RestResponse getBarChartDataByDateType(@RequestBody Map<String, String> map, HttpServletRequest request) {
if (!map.containsKey("echartType")) {
throw new MyException("错误,请指定图表类型");
}
String echartType = map.get("echartType");
// 获取要查询的类型(1入库2出库)
String type = map.get("type");
// 声明要查询时的日期类型 day代表按天,month代表按月
String dateType = "day";
if (map.containsKey("dateType")) {
dateType = map.get("dateType");
}
Integer depositoryId = -1;
if (map.containsKey("depositoryId")) {
// 如果选择了仓库,则使用选中的仓库
depositoryId = ObjectFormatUtil.toInteger(map.get("depositoryId"));
} else {
// 如果没选中仓库
String token = request.getHeader("user-token");
if (token == null) {
token = (String) request.getSession().getAttribute("userToken");
}
UserByPort userToken = AuthenticationTokenPool.getUserToken(token);
// 获取当前用户可见的仓库id
List<Integer> depositoryIdForUser = roleService.findDepositoryIdForUser(userToken);
// 如果存在仓库
if (depositoryIdForUser != null && depositoryIdForUser.size() > 0) {
// 默认为第一个
depositoryId = depositoryIdForUser.get(0);
}
}
Map<Object, Object> data = new HashMap<>();
if (depositoryId != -1) {
data = depositoryRecordService.getLineOrBarChartData(depositoryId, type, dateType, echartType);
}
return new RestResponse(data);
}
/** /**
* 用于获取当前仓库的所有子类 * 用于获取当前仓库的所有子类
* *

15
src/main/java/com/dreamchaser/depository_manage/controller/PageController.java

@ -3943,4 +3943,19 @@ public class PageController {
return mv; return mv;
} }
@GetMapping("/echartForTable")
public ModelAndView echartForTable(String type, String echartType) {
ModelAndView mv = new ModelAndView();
mv.addObject("type", type);
if ("line".equals(echartType)) {
mv.setViewName("pages/depository/inEchart/LineChart");
} else if ("bar".equals(echartType)) {
mv.setViewName("pages/depository/inEchart/BarChart");
}
return mv;
}
} }

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

@ -500,6 +500,16 @@ public interface DepositoryRecordService {
Map<String, Object> createStackedAreaChartSeriesItem(String name, List<Object> data); Map<String, Object> createStackedAreaChartSeriesItem(String name, List<Object> data);
/**
* 获取折线图数据
* @param depositoryId 待获取仓库Id
* @param type 查看类型1入库2出库
* @param dateType 日期类型day按天month按月
* @return
*/
Map<Object, Object> getLineOrBarChartData(Integer depositoryId, String type, String dateType, String echartType);
} }

82
src/main/java/com/dreamchaser/depository_manage/service/impl/DepositoryRecordServiceImpl.java

@ -5236,7 +5236,6 @@ public class DepositoryRecordServiceImpl implements DepositoryRecordService {
quantity = (double) depositoryRecordMapper.findApplicationInRecordByDate(map) / 100; quantity = (double) depositoryRecordMapper.findApplicationInRecordByDate(map) / 100;
return quantity; return quantity;
} else { } else {
map.put("state", "已出库");
quantity = (double) depositoryRecordMapper.findApplicationOutRecordByDate(map) / 100; quantity = (double) depositoryRecordMapper.findApplicationOutRecordByDate(map) / 100;
return quantity; return quantity;
} }
@ -5833,6 +5832,9 @@ public class DepositoryRecordServiceImpl implements DepositoryRecordService {
return result; return result;
} }
/** /**
* 用于计算当前主记录下的总额与数量 * 用于计算当前主记录下的总额与数量
*/ */
@ -5886,4 +5888,82 @@ public class DepositoryRecordServiceImpl implements DepositoryRecordService {
} }
} }
/**
* 获取折线图数据
* @param depositoryId 待获取仓库Id
* @param type 查看类型1入库2出库
* @param dateType 日期类型day按天month按月
* @return
*/
public Map<Object, Object> getLineOrBarChartData(Integer depositoryId, String type, String dateType, String echartType) {
// 获取至今的日期名称
List<String> dayNames = new ArrayList<>();
List<Long> dayTimeSpaces = new ArrayList<>();
if ("month".equals(dateType)) {
//获取获取系统的当前日历对象
Map<String, Object> monthBeginToNow = DateUtil.getPreviousMonth();
// 获取至今的日期名称
dayNames = ObjectFormatUtil.objToList(monthBeginToNow.get("monthNames"), String.class);
Collections.reverse(dayNames);
// 获取至今的日期时间戳
dayTimeSpaces = ObjectFormatUtil.objToList(monthBeginToNow.get("months"), Long.class);
Collections.reverse(dayTimeSpaces);
} else if ("day".equals(dateType)) {
//获取获取系统的当前日历对象
Map<String, Object> monthBeginToNow = DateUtil.getMonthBeginToNow();
// 获取至今的日期名称
dayNames = ObjectFormatUtil.objToList(monthBeginToNow.get("dayName"), String.class);
// 获取至今的日期时间戳
dayTimeSpaces = ObjectFormatUtil.objToList(monthBeginToNow.get("dayTimeSpace"), Long.class);
dayTimeSpaces.add(Calendar.getInstance().getTimeInMillis());
}
// 每天仓库出入库数目
Map<Object, Object> show_data = new HashMap<>();
Map<String, Object> applicationRecordByDate = getLineOrBarChartDataForApplicationRecord(type, dayTimeSpaces, depositoryId,echartType);
show_data.put("data",applicationRecordByDate);
show_data.put("dayNames",dayNames);
return show_data;
}
/**
* 获取当前折线图的具体数据
* @param type 查看类型1入库2出库
* @param days 查询日期
* @param depositoryId 仓库id
* @return
*/
public Map<String, Object> getLineOrBarChartDataForApplicationRecord(String type, List<Long> days, Integer depositoryId, String echartType) {
Map<String, Object> map = new HashMap<>();
if("line".equals(echartType)){
map.put("type", "line");
Map<String, Object> areaStyleItem = new HashMap<>();
map.put("areaStyle", areaStyleItem);
}else if("bar".equals(echartType)){
map.put("type", "bar");
Map<String, Object> emphasisItem = new HashMap<>();
emphasisItem.put("focus", "series");
map.put("emphasis", emphasisItem);
Map<String, Object> labelItem = new HashMap<>();
labelItem.put("show", true);
labelItem.put("position", "inside");
map.put("label", labelItem);
}
int i;
List<Double> drCountbyDrName = new ArrayList<>();
for (i = 0; i < days.size() - 1; i++) {
// 遍历 Map并计算各仓库的入库数
// 获取一段时间内的库存额度
Double depositoryRecordByDate = findApplicationRecordByDate(days.get(i + 1), days.get(i), ObjectFormatUtil.toInteger(type), depositoryId);
drCountbyDrName.add(depositoryRecordByDate);
}
map.put("data", drCountbyDrName);
return map;
}
} }

10
src/main/java/com/dreamchaser/depository_manage/utils/DateUtil.java

@ -240,8 +240,9 @@ public class DateUtil {
public static Map<String, Object> getPreviousMonth() { public static Map<String, Object> getPreviousMonth() {
Calendar instance = Calendar.getInstance(); Calendar instance = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM"); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
Map<String, Object> source = new HashMap<>(); HashMap<String, String> source = new HashMap<>();
List<Object> sourceList = new ArrayList<>(); List<Object> sourceList = new ArrayList<>();
List<String> monthNames = new ArrayList<>();
int month = instance.get(Calendar.MONTH) + 1; int month = instance.get(Calendar.MONTH) + 1;
// 获取下个月 // 获取下个月
instance.add(Calendar.MONTH, 1); instance.add(Calendar.MONTH, 1);
@ -253,16 +254,19 @@ public class DateUtil {
while (month > 0) { while (month > 0) {
instance.set(Calendar.MONTH, month); instance.set(Calendar.MONTH, month);
instance.set(Calendar.DAY_OF_MONTH, -1); instance.set(Calendar.DAY_OF_MONTH, -1);
source.put("month", month + "月"); String monthName = month + "月";
source.put("month", monthName);
monthNames.add(monthName);
months.add(DateTimeByMonthToTimeStamp(formatter.format(instance.getTime()))); months.add(DateTimeByMonthToTimeStamp(formatter.format(instance.getTime())));
month--; month--;
sourceList.add(((HashMap<String, Object>) source).clone()); sourceList.add(source.clone());
} }
instance.set(Calendar.MONTH, month); instance.set(Calendar.MONTH, month);
instance.add(Calendar.MONTH, 1); instance.add(Calendar.MONTH, 1);
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("months", months); map.put("months", months);
map.put("sourceList", sourceList); map.put("sourceList", sourceList);
map.put("monthNames", monthNames);
return map; return map;
} }

215
src/main/resources/templates/pages/depository/inEchart/BarChart.html

@ -0,0 +1,215 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>柱状图</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="/static/lib/layui-v2.8.6/css/layui.css" media="all">
<link rel="stylesheet" href="/static/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
<link rel="stylesheet" href="/static/lib/bootstrap-3.4.1-dist/css/bootstrap.min.css" media="all">
<link rel="stylesheet" href="/static/css/public.css" media="all">
<style>
.top-panel {
border: 1px solid #eceff9;
border-radius: 5px;
text-align: center;
}
.top-panel > .layui-card-body {
height: 60px;
}
.top-panel-number {
line-height: 60px;
font-size: 30px;
border-right: 1px solid #eceff9;
}
.top-panel-tips {
line-height: 30px;
font-size: 12px
}
</style>
</head>
<body>
<div class="layuimini-main">
<div class="layui-colla-item">
<h2 class="layui-colla-title">柱状图</h2>
<div class="layui-colla-content layui-show">
<input id="type" th:value="${type}" style="display:none;">
<button class="layui-btn" lay-submit lay-filter="perMensem">
&emsp;按月&emsp;
</button>
<button class="layui-btn" lay-submit lay-filter="byTheDay">
&emsp;按日&emsp;
</button>
<select id="selectDepositoryForChart" class="form-control">
</select>
<div id="echarts-bar" style="background-color:#ffffff;padding: 10px;height: 400px">
</div>
</div>
</div>
</div>
<!--</div>-->
<script src="/static/lib/layui-v2.8.6/layui.js" charset="utf-8"></script>
<script src="/static/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/static/lib/echarts/echarts.js" charset="utf-8"></script>
<script>
function initBarChartByTheDateType() {
}
function getThisUserDepository() {
}
layui.use(['layer', 'element'], function () {
var $ = layui.jquery,
layer = layui.layer,
element = layui.element,
form = layui.form;
let type = $("#type").val();
// 折线图
let BarChart = echarts.init(document.getElementById('echarts-bar'));
let BarChartOption = {
xAxis: {
type: 'category',
data: []
},
yAxis: {
type: 'value'
},
series: [
{
data: [],
type: 'bar'
}
],
toolbox: {
feature: {
myFull: {
show: true,
title: '全屏查看',
icon: 'path://M432.45,595.444c0,2.177-4.661,6.82-11.305,6.82c-6.475,0-11.306-4.567-11.306-6.82s4.852-6.812,11.306-6.812C427.841,588.632,432.452,593.191,432.45,595.444L432.45,595.444z M421.155,589.876c-3.009,0-5.448,2.495-5.448,5.572s2.439,5.572,5.448,5.572c3.01,0,5.449-2.495,5.449-5.572C426.604,592.371,424.165,589.876,421.155,589.876L421.155,589.876z M421.146,591.891c-1.916,0-3.47,1.589-3.47,3.549c0,1.959,1.554,3.548,3.47,3.548s3.469-1.589,3.469-3.548C424.614,593.479,423.062,591.891,421.146,591.891L421.146,591.891zM421.146,591.891',
onclick: function (e){
var opts = e.getOption();
opts.toolbox[0].feature.myFull.show=false;
//window.top表示最顶层iframe 如果在当页面全屏打开 删去window.top即可
window.top.layer.open({
title:false,
type:1,
area :["100%","100%"],
content:'<div id="fullChart" style="width: 100%;height: 100%;padding:30px 0px"></div>',
success:function(){
var fullchart = echarts.init(window.top.document.getElementById('fullChart'));
fullchart.setOption(BarChartOption)
}
})
}
},
saveAsImage:{},
dataZoom: {
yAxisIndex: 'none'
},
}
}
};
//动态加载相关数据
$(function () {
let req = {};
req.type = type;
req.echartType = "bar";
getThisUserDepository();
initBarChartByDateType(req);
});
// 按月加载
form.on('submit(perMensem)', function () {
let req = {};
req.type = type;
req.depositoryId = $("#selectDepositoryForChart").val();
req.dateType = "month";
req.echartType = "bar";
initBarChartByDateType(req);
});
// 按天加载
form.on('submit(byTheDay)', function () {
let req = {};
req.type = type;
req.depositoryId = $("#selectDepositoryForChart").val();
req.dateType = "day";
req.echartType = "bar";
initBarChartByDateType(req);
});
$('#selectDepositoryForChart').on('change', function (obj) {
let value = obj.target.value;
let req = {};
req.depositoryId = value;
req.type = type;
req.echartType = "bar";
initBarChartByDateType(req);
});
initBarChartByDateType = function (req) {
$.ajax({
url: '/repository/getBarChartDataByDateType',
type: 'post',
dataType: "json",
data: JSON.stringify(req),
contentType: "application/json;charset=utf-8",
beforeSend: function () {
this.layerIndex = layer.load(0, {shade: [0.5, '#393D49']});
},
success: function (result) {
layer.close(this.layerIndex);
let data = result.data;
// 日期名称
let dayNames = data.dayNames;
let series = data.data;
BarChartOption.xAxis.data = dayNames;
BarChartOption.series = series;
BarChart.setOption(BarChartOption);
}
})
};
getThisUserDepository = function () {
$.ajax({
url: '/repository/getWareHouseVisiblePermissionByUser',
type: 'get',
dataType: "json",
beforeSend: function () {
this.layerIndex = layer.load(0, {shade: [0.5, '#393D49']});
},
success: function (result) {
layer.close(this.layerIndex);
let data = result.data;
let selectItem = $("#selectDepositoryForChart");
for(let item in data){
let optionItem = '<option value="'+data[item]+'">'+item+'</option>';
selectItem.append(optionItem)
}
}
})
}
});
</script>
</body>
</html>

108
src/main/resources/templates/pages/depository/inEchart/LineChart.html

@ -2,7 +2,7 @@
<html lang="en" xmlns:th="http://www.thymeleaf.org"> <html lang="en" xmlns:th="http://www.thymeleaf.org">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>折线</title> <title>柱状</title>
<meta name="renderer" content="webkit"> <meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
@ -41,6 +41,7 @@
<div class="layui-colla-item"> <div class="layui-colla-item">
<h2 class="layui-colla-title">折线图</h2> <h2 class="layui-colla-title">折线图</h2>
<div class="layui-colla-content layui-show"> <div class="layui-colla-content layui-show">
<input id="type" th:value="${type}" style="display:none;">
<button class="layui-btn" lay-submit lay-filter="perMensem"> <button class="layui-btn" lay-submit lay-filter="perMensem">
&emsp;按月&emsp; &emsp;按月&emsp;
</button> </button>
@ -62,9 +63,14 @@
<script src="/static/lib/echarts/echarts.js" charset="utf-8"></script> <script src="/static/lib/echarts/echarts.js" charset="utf-8"></script>
<script> <script>
function initLineChartByTheDay(){ function initBarChartByTheDateType() {
} }
function getThisUserDepository() {
}
layui.use(['layer', 'element'], function () { layui.use(['layer', 'element'], function () {
var $ = layui.jquery, var $ = layui.jquery,
layer = layui.layer, layer = layui.layer,
@ -72,6 +78,7 @@
form = layui.form; form = layui.form;
let type = $("#type").val();
// 折线图 // 折线图
let LineChart = echarts.init(document.getElementById('echarts-line')); let LineChart = echarts.init(document.getElementById('echarts-line'));
let LineChartOption = { let LineChartOption = {
@ -87,53 +94,120 @@
data: [], data: [],
type: 'line' type: 'line'
} }
] ],
toolbox: {
feature: {
myFull: {
show: true,
title: '全屏查看',
icon: 'path://M432.45,595.444c0,2.177-4.661,6.82-11.305,6.82c-6.475,0-11.306-4.567-11.306-6.82s4.852-6.812,11.306-6.812C427.841,588.632,432.452,593.191,432.45,595.444L432.45,595.444z M421.155,589.876c-3.009,0-5.448,2.495-5.448,5.572s2.439,5.572,5.448,5.572c3.01,0,5.449-2.495,5.449-5.572C426.604,592.371,424.165,589.876,421.155,589.876L421.155,589.876z M421.146,591.891c-1.916,0-3.47,1.589-3.47,3.549c0,1.959,1.554,3.548,3.47,3.548s3.469-1.589,3.469-3.548C424.614,593.479,423.062,591.891,421.146,591.891L421.146,591.891zM421.146,591.891',
onclick: function (e){
var opts = e.getOption();
opts.toolbox[0].feature.myFull.show=false;
//window.top表示最顶层iframe 如果在当页面全屏打开 删去window.top即可
window.top.layer.open({
title:false,
type:1,
area :["100%","100%"],
content:'<div id="fullChart" style="width: 100%;height: 100%;padding:30px 0px"></div>',
success:function(){
var fullchart = echarts.init(window.top.document.getElementById('fullChart'));
fullchart.setOption(LineChartOption)
}
})
}
},
saveAsImage:{},
dataZoom: {
yAxisIndex: 'none'
},
}
}
}; };
//动态加载相关数据 //动态加载相关数据
$(function () { $(function () {
let req = {};
req.type = type;
req.echartType = "line";
getThisUserDepository();
initBarChartByDateType(req);
}); });
// 按月加载 // 按月加载
form.on('submit(perMensem)', function () { form.on('submit(perMensem)', function () {
let req = {};
req.type = type;
req.depositoryId = $("#selectDepositoryForChart").val();
req.dateType = "month";
req.echartType = "line";
initBarChartByDateType(req);
}); });
// 按天加载 // 按天加载
form.on('submit(byTheDay)', function () { form.on('submit(byTheDay)', function () {
let req = {};
req.type = type;
req.depositoryId = $("#selectDepositoryForChart").val();
req.dateType = "day";
req.echartType = "line";
initBarChartByDateType(req);
}); });
$('#selectDepositoryForChart').on('change', function (obj) { $('#selectDepositoryForChart').on('change', function (obj) {
let value = obj.target.value; let value = obj.target.value;
let req = {};
req.depositoryId = value;
req.type = type;
req.echartType = "line";
initBarChartByDateType(req);
}); });
initLineChartByTheDay = function(req) { initBarChartByDateType = function (req) {
$.ajax({ $.ajax({
url: '/repository/layui/echart_back?type=1', url: '/repository/getBarChartDataByDateType',
type: 'get', type: 'post',
async: true,
dataType: "json", dataType: "json",
complete: function (XHR, TS) { data: JSON.stringify(req),
if (XHR.status !== 200) { contentType: "application/json;charset=utf-8",
layer.alert("系统繁忙,稍后重试"); beforeSend: function () {
} this.layerIndex = layer.load(0, {shade: [0.5, '#393D49']});
}, },
success: function (result) {
layer.close(this.layerIndex);
let data = result.data;
// 日期名称
let dayNames = data.dayNames;
let series = data.data;
LineChartOption.xAxis.data = dayNames;
LineChartOption.series = series;
LineChart.setOption(LineChartOption);
}
})
};
getThisUserDepository = function () {
$.ajax({
url: '/repository/getWareHouseVisiblePermissionByUser',
type: 'get',
dataType: "json",
beforeSend: function () { beforeSend: function () {
this.layerIndex = layer.load(0, {shade: [0.5, '#393D49']}); this.layerIndex = layer.load(0, {shade: [0.5, '#393D49']});
}, },
success: function (result) { success: function (result) {
layer.close(this.layerIndex); layer.close(this.layerIndex);
let data = result.data;
let selectItem = $("#selectDepositoryForChart");
for(let item in data){
let optionItem = '<option value="'+data[item]+'">'+item+'</option>';
selectItem.append(optionItem)
}
} }
}) })
} }
}); });
</script> </script>

33
src/main/resources/templates/pages/depository/table-in.html

@ -273,7 +273,8 @@
} }
}); });
} else if (obj.event === 'delete') { // 监听删除操作 }
else if (obj.event === 'delete') { // 监听删除操作
var checkStatus = table.checkStatus('currentTableId') var checkStatus = table.checkStatus('currentTableId')
, data = checkStatus.data; , data = checkStatus.data;
var req = {}; var req = {};
@ -323,13 +324,39 @@
let listItem = `<ul class="layui-table-tool-panel" style="max-height: 685px;"> let listItem = `<ul class="layui-table-tool-panel" style="max-height: 685px;">
<li lay-event="showLineEchart">查看面积图</li> <li lay-event="showLineEchart">查看面积图</li>
<li lay-event="showSunburstEchart">查看旭日图</li> <li lay-event="showSunburstEchart">查看旭日图</li>
<li lay-event="showLineEchart">查看折线图</li>
<li lay-event="showBarEchart">查看柱状图</li>
</ul>`; </ul>`;
$("#showEchart").append(listItem) $("#showEchart").append(listItem)
}else if(obj.event === 'showLineEchart'){ }else if(obj.event === 'showAreaEchart'){
console.log("showLineEchart") console.log("showAreaEchart")
}else if(obj.event === "showSunburstEchart"){ }else if(obj.event === "showSunburstEchart"){
console.log("showSunburstEchart") console.log("showSunburstEchart")
}else if(obj.event === "showLineEchart"){
layer.open({
type: 2,
offset: 'l',
anim: 'slideRight', // 从右往左
area: ['40%', '100%'],
shade: 0.1,
title: "折线图",
shadeClose: true,
id: 'applicationOutInfoForLeft',
content: '/echartForTable?type=1&echartType=line',
});
}else if(obj.event === "showBarEchart"){
layer.open({
type: 2,
offset: 'l',
anim: 'slideRight', // 从右往左
area: ['40%', '100%'],
shade: 0.1,
title: "柱状图",
shadeClose: true,
id: 'applicationOutInfoForLeft',
content: '/echartForTable?type=1&echartType=bar',
});
} }
}); });

35
src/main/resources/templates/pages/depository/table-out.html

@ -248,6 +248,7 @@
defaultToolbar: ['filter', 'exports', 'print',{ defaultToolbar: ['filter', 'exports', 'print',{
title: '图表展示' title: '图表展示'
,layEvent: 'showEchart' ,layEvent: 'showEchart'
,id:"showEchart"
,icon: 'layui-icon-chart-screen' ,icon: 'layui-icon-chart-screen'
}], }],
//这里layui和thymeleaf冲突了,要加个空格 //这里layui和thymeleaf冲突了,要加个空格
@ -482,14 +483,41 @@
let listItem = `<ul class="layui-table-tool-panel" style="max-height: 685px;"> let listItem = `<ul class="layui-table-tool-panel" style="max-height: 685px;">
<li lay-event="showLineEchart">查看面积图</li> <li lay-event="showLineEchart">查看面积图</li>
<li lay-event="showSunburstEchart">查看旭日图</li> <li lay-event="showSunburstEchart">查看旭日图</li>
<li lay-event="showLineEchart">查看折线图</li>
<li lay-event="showBarEchart">查看柱状图</li>
</ul>`; </ul>`;
$("#showEchart").append(listItem) $("#showEchart").append(listItem)
}else if(obj.event === 'showLineEchart'){ }else if(obj.event === 'showAreaEchart'){
console.log("showLineEchart") console.log("showAreaEchart")
}else if(obj.event === "showSunburstEchart"){ }else if(obj.event === "showSunburstEchart"){
console.log("showSunburstEchart") console.log("showSunburstEchart")
}else if(obj.event === "showLineEchart"){
layer.open({
type: 2,
offset: 'l',
anim: 'slideRight', // 从右往左
area: ['40%', '100%'],
shade: 0.1,
title: "折线图",
shadeClose: true,
id: 'applicationOutInfoForLeft',
content: '/echartForTable?type=2&echartType=line',
});
}else if(obj.event === "showBarEchart"){
layer.open({
type: 2,
offset: 'l',
anim: 'slideRight', // 从右往左
area: ['40%', '100%'],
shade: 0.1,
title: "柱状图",
shadeClose: true,
id: 'applicationOutInfoForLeft',
content: '/echartForTable?type=2&echartType=bar',
});
} }
}); });
table.on('rowDouble(currentTableFilter)', function (obj) { table.on('rowDouble(currentTableFilter)', function (obj) {
@ -572,9 +600,6 @@
} }
}); });
$('body').on('click', '[data-refresh]', function () {
location.reload();
})
}); });
</script> </script>

77
src/test/java/com/dreamchaser/depository_manage/BarChartTest.java

@ -78,23 +78,13 @@ public class BarChartTest {
map.put("password", "123456789"); map.put("password", "123456789");
map.put("captcha", "528765"); map.put("captcha", "528765");
map.put("captchaId", "UksBSXVFy0xVZbXUC4dQ"); map.put("captchaId", "UksBSXVFy0xVZbXUC4dQ");
JSONObject jsonObject = PublicConfig.baseLogin(map); // JSONObject jsonObject = PublicConfig.baseLogin(map);
// UserByPort userByPort = JSONObject.toJavaObject((JSONObject) jsonObject.get("usercont"), UserByPort.class); // UserByPort userByPort = JSONObject.toJavaObject((JSONObject) jsonObject.get("usercont"), UserByPort.class);
Map<Object, Object> barChartData = getBarChartData("2", 41); Map<Object, Object> barChartData = getBarChartDataByDateType(28,"1", "day");
System.out.println(barChartData); System.out.println(barChartData);
} }
/**
* 用于获取出入库对照柱状图
*
* @return
*/
Map<Object, Object> getBarChartData(String type, Integer depositoryId) {
// 定义CompletionService
Map<Object, Object> barChartData = getBarChartData(depositoryId, type);
return barChartData;
}
/** /**
@ -102,50 +92,46 @@ public class BarChartTest {
* *
* @param depositoryId 要查看的仓库id * @param depositoryId 要查看的仓库id
* @param type 类型1入库2出库 * @param type 类型1入库2出库
* @param dateType 日期类型month按月day按天月初至今
* @return * @return
*/ */
public Map<Object, Object> getBarChartData(Integer depositoryId, String type) { public Map<Object, Object> getBarChartDataByDateType(Integer depositoryId, String type, String dateType) {
// 获取至今的日期名称
List<String> dayNames = new ArrayList<>();
List<Long> dayTimeSpaces = new ArrayList<>();
if ("month".equals(dateType)) {
//获取获取系统的当前日历对象 //获取获取系统的当前日历对象
Map<String, Object> monthBeginToNow = DateUtil.getMonthBeginToNow(); Map<String, Object> monthBeginToNow = DateUtil.getPreviousMonth();
// 获取至今的日期名称
dayNames = ObjectFormatUtil.objToList(monthBeginToNow.get("monthNames"), String.class);
Collections.reverse(dayNames);
// 获取至今的日期时间戳
dayTimeSpaces = ObjectFormatUtil.objToList(monthBeginToNow.get("months"), Long.class);
Collections.reverse(dayTimeSpaces);
} else if ("day".equals(dateType)) {
//获取获取系统的当前日历对象
Map<String, Object> monthBeginToNow = DateUtil.getMonthBeginToNow();
// 获取至今的日期名称 // 获取至今的日期名称
List<String> dayNames = ObjectFormatUtil.objToList(monthBeginToNow.get("dayName"), String.class); dayNames = ObjectFormatUtil.objToList(monthBeginToNow.get("dayName"), String.class);
// 获取至今的日期时间戳 // 获取至今的日期时间戳
List<Long> dayTimeSpaces = ObjectFormatUtil.objToList(monthBeginToNow.get("dayTimeSpace"), Long.class); dayTimeSpaces = ObjectFormatUtil.objToList(monthBeginToNow.get("dayTimeSpace"), Long.class);
// 定义线程 dayTimeSpaces.add(Calendar.getInstance().getTimeInMillis());
}
// 每天各仓库入库数目 // 每天各仓库入库数目
Map<Object, Object> show_data = new HashMap<>(); Map<Object, Object> show_data = new HashMap<>();
getBarChartDataForApplicationRecord getApplicationRecordByDate = new getBarChartDataForApplicationRecord(type, depositoryId, dayTimeSpaces); Map<String, Object> data = getBarChartDataForApplicationRecord(type, depositoryId, dayTimeSpaces);
try { show_data.put("data", data);
Object call = getApplicationRecordByDate.call(); show_data.put("dayNames", dayNames);
show_data.put("data", call);
} catch (Exception e) {
e.printStackTrace();
}
show_data.put("dayNames",dayNames);
return show_data; return show_data;
} }
/** /**
* 根据日期获取出入库的数量(用于柱状图) * 根据日期获取出入库的数量(用于柱状图)
*/ */
@Data public Map<String, Object> getBarChartDataForApplicationRecord(String type, Integer depositoryId, List<Long> days) {
class getBarChartDataForApplicationRecord implements Callable<Object> {
Integer depositoryId;
List<Long> days;
String type;
getBarChartDataForApplicationRecord(String type, Integer depositoryId, List<Long> days) {
this.days = days;
this.type = type;
this.depositoryId = depositoryId;
}
@Override
public Object call() throws Exception {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("type", "bar"); map.put("type", "bar");
Map<String, Object> emphasisItem = new HashMap<>(); Map<String, Object> emphasisItem = new HashMap<>();
@ -157,14 +143,15 @@ public class BarChartTest {
map.put("label", labelItem); map.put("label", labelItem);
int i; int i;
List<Double> drCountbyDrName = new ArrayList<>(); List<Double> drCountbyDrName = new ArrayList<>();
for (i = days.size() - 1; i > 0; i--) {
for (i = 0; i < days.size() - 1; i++) {
// 遍历 Map并计算各仓库的入库数 // 遍历 Map并计算各仓库的入库数
// 获取一段时间内的库存额度 // 获取一段时间内的库存额度
Double depositoryRecordByDate1 = depositoryRecordService.findApplicationRecordByDate( days.get(i),days.get(i - 1), ObjectFormatUtil.toInteger(type), depositoryId); Double depositoryRecordByDate = depositoryRecordService.findApplicationRecordByDate(days.get(i + 1), days.get(i), ObjectFormatUtil.toInteger(type), depositoryId);
drCountbyDrName.add(depositoryRecordByDate1); drCountbyDrName.add(depositoryRecordByDate);
} }
map.put("data", drCountbyDrName); map.put("data", drCountbyDrName);
return map; return map;
} }
}
} }

175
src/test/java/com/dreamchaser/depository_manage/LineChartTest.java

@ -1,24 +1,18 @@
package com.dreamchaser.depository_manage; package com.dreamchaser.depository_manage;
import com.alibaba.fastjson.JSONObject;
import com.dreamchaser.depository_manage.config.PublicConfig;
import com.dreamchaser.depository_manage.entity.UserByPort;
import com.dreamchaser.depository_manage.service.DepositoryRecordService; import com.dreamchaser.depository_manage.service.DepositoryRecordService;
import com.dreamchaser.depository_manage.service.DepositoryService; import com.dreamchaser.depository_manage.service.DepositoryService;
import com.dreamchaser.depository_manage.service.MaterialService; import com.dreamchaser.depository_manage.service.MaterialService;
import com.dreamchaser.depository_manage.service.MaterialTypeService; import com.dreamchaser.depository_manage.service.MaterialTypeService;
import com.dreamchaser.depository_manage.utils.DateUtil; import com.dreamchaser.depository_manage.utils.DateUtil;
import com.dreamchaser.depository_manage.utils.ObjectFormatUtil; import com.dreamchaser.depository_manage.utils.ObjectFormatUtil;
import lombok.Data;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; 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.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.concurrent.*;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ -37,140 +31,87 @@ public class LineChartTest {
@Autowired @Autowired
DepositoryService depositoryService; DepositoryService depositoryService;
/**
* 获取本月之前的月份
*
* @return
*/
public static Map<String, Object> getPreviousMonth() {
Calendar instance = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
Map<String, Object> source = new HashMap<>();
List<Object> sourceList = new ArrayList<>();
int month = instance.get(Calendar.MONTH) + 1;
// 获取下个月
instance.add(Calendar.MONTH, 1);
Long nextMonth = DateUtil.DateTimeByMonthToTimeStamp(formatter.format(instance.getTime()));
ArrayList<Object> months = new ArrayList<>();
months.add(nextMonth);
instance.add(Calendar.MONTH, -1);
while (month > 0) {
instance.set(Calendar.MONTH, month);
instance.set(Calendar.DAY_OF_MONTH, -1);
source.put("month", month + "月");
months.add(DateUtil.DateTimeByMonthToTimeStamp(formatter.format(instance.getTime())));
month--;
sourceList.add(((HashMap<String, Object>) source).clone());
}
instance.set(Calendar.MONTH, month);
instance.add(Calendar.MONTH, 1);
Map<String, Object> map = new HashMap<>();
map.put("months", months);
map.put("sourceList", sourceList);
return map;
}
@Test @Test
public void main() { public void main() {
UserByPort userByPort = PublicConfig.FindUserById(87, null,null); Map<Object, Object> day = getLineOrBarChartData(28, "1", "day","bar");
Map<String, Integer> depositoryAllNameAndId = depositoryService.findDepositoryAllNameAndId(userByPort); System.out.println(day);
Map<Object, Object> barChartData = getBarChartData(depositoryAllNameAndId, "1");
System.out.println(JSONObject.toJSONString(barChartData));
} }
// 获取柱状图数据 /**
public Map<Object,Object> getBarChartData(Map<String, Integer> depositoryAllNameAndId, String type) { * 获取折线图数据
* @param depositoryId 待获取仓库Id
* @param type 查看类型1入库2出库
* @param dateType 日期类型day按天month按月
* @return
*/
public Map<Object, Object> getLineOrBarChartData(Integer depositoryId, String type, String dateType, String echartType) {
// 获取遍历器
Iterator it = depositoryAllNameAndId.keySet().iterator(); // 获取至今的日期名称
List<String> dayNames = new ArrayList<>();
List<Long> dayTimeSpaces = new ArrayList<>();
if ("month".equals(dateType)) {
//获取获取系统的当前日历对象 //获取获取系统的当前日历对象
Calendar instance = Calendar.getInstance();
// 获取日期
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
int now = instance.get(Calendar.DAY_OF_WEEK) - 1 == 0 ? 7 : instance.get(Calendar.DAY_OF_WEEK) - 1;
List<Long> days = new ArrayList<>(); // 周一至今的每天
instance.add(Calendar.DATE, 1);
days.add(DateUtil.DateTimeByDayToTimeStamp(formatter.format(instance.getTime())));
instance.add(Calendar.DATE, -1);
days.add(DateUtil.DateTimeByDayToTimeStamp(formatter.format(instance.getTime())));
while (now - 1 > 0) {
now--;
instance.add(Calendar.DATE, -1);
Long format = DateUtil.DateTimeByDayToTimeStamp(formatter.format(instance.getTime()));
days.add(format);
}
// 定义线程
int threadSize = depositoryAllNameAndId.size();
ExecutorService exs = new ThreadPoolExecutor(threadSize, threadSize, 100, TimeUnit.SECONDS, new LinkedBlockingQueue<>(threadSize));
// 结果集
List<Future<Object>> futureList = new ArrayList<Future<Object>>();
// 1.定义CompletionService
CompletionService<Object> completionService = new ExecutorCompletionService<Object>(exs);
// 每天各仓库入库数目
Map<Object,Object> show_data = new HashMap<>();
while (it.hasNext()) {
Object next = it.next();
getApplicationRecordByDate getApplicationRecordByDate = new getApplicationRecordByDate(type, next.toString(), days, depositoryAllNameAndId);
Future<Object> future = completionService.submit(getApplicationRecordByDate);
futureList.add(future);
}
for (int i = 0; i < depositoryAllNameAndId.size(); i++) { Map<String, Object> monthBeginToNow = DateUtil.getPreviousMonth();
Object result = null;
try {
result = completionService.take().get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
show_data.putAll((Map<?, ?>) result);
}
// 获取至今的日期名称
dayNames = ObjectFormatUtil.objToList(monthBeginToNow.get("monthNames"), String.class);
Collections.reverse(dayNames);
// 获取至今的日期时间戳
dayTimeSpaces = ObjectFormatUtil.objToList(monthBeginToNow.get("months"), Long.class);
Collections.reverse(dayTimeSpaces);
} else if ("day".equals(dateType)) {
//获取获取系统的当前日历对象
Map<String, Object> monthBeginToNow = DateUtil.getMonthBeginToNow();
// 获取至今的日期名称
dayNames = ObjectFormatUtil.objToList(monthBeginToNow.get("dayName"), String.class);
// 获取至今的日期时间戳
dayTimeSpaces = ObjectFormatUtil.objToList(monthBeginToNow.get("dayTimeSpace"), Long.class);
dayTimeSpaces.add(Calendar.getInstance().getTimeInMillis());
}
// 每天仓库出入库数目
Map<Object, Object> show_data = new HashMap<>();
Map<String, Object> applicationRecordByDate = getLineOrBarChartDataForApplicationRecord(type, dayTimeSpaces, depositoryId,echartType);
show_data.put("data",applicationRecordByDate);
show_data.put("dayNames",dayNames);
return show_data; return show_data;
} }
/** /**
* 根据日期获取出入库的数量 * 获取当前折线图的具体数据
* @param type 查看类型1入库2出库
* @param days 查询日期
* @param depositoryId 仓库id
* @return
*/ */
@Data public Map<String, Object> getLineOrBarChartDataForApplicationRecord(String type, List<Long> days, Integer depositoryId, String echartType) {
class getApplicationRecordByDate implements Callable<Object> {
String key;
List<Long> days;
Map<String, Integer> depositoryAllNameAndId;
String type;
getApplicationRecordByDate(String type, String key, List<Long> days, Map<String, Integer> depositoryAllNameAndId) {
this.key = key;
this.depositoryAllNameAndId = depositoryAllNameAndId;
this.days = days;
this.type = type;
}
@Override
public Object call() throws Exception {
Map<Integer, Object> result = new HashMap<>();
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
if("line".equals(echartType)){
map.put("type", "line"); map.put("type", "line");
Map<String, Object> areaStyleItem = new HashMap<>(); Map<String, Object> areaStyleItem = new HashMap<>();
map.put("areaStyle", areaStyleItem); map.put("areaStyle", areaStyleItem);
}else if("bar".equals(echartType)){
map.put("type", "bar");
Map<String, Object> emphasisItem = new HashMap<>();
emphasisItem.put("focus", "series");
map.put("emphasis", emphasisItem);
Map<String, Object> labelItem = new HashMap<>();
labelItem.put("show", true);
labelItem.put("position", "inside");
map.put("label", labelItem);
}
int i; int i;
List<Double> drCountbyDrName = new ArrayList<>(); List<Double> drCountbyDrName = new ArrayList<>();
for (i = 0; i < days.size() - 1; i++) {
Integer val = (Integer) depositoryAllNameAndId.get(key);
for (i = days.size() - 1; i > 0; i--) {
// 遍历 Map并计算各仓库的入库数 // 遍历 Map并计算各仓库的入库数
// 获取一段时间内的库存额度 // 获取一段时间内的库存额度
Double depositoryRecordByDate1 = depositoryRecordService.findApplicationRecordByDate(days.get(i - 1), days.get(i), ObjectFormatUtil.toInteger(type), val); Double depositoryRecordByDate = depositoryRecordService.findApplicationRecordByDate(days.get(i + 1), days.get(i), ObjectFormatUtil.toInteger(type), depositoryId);
drCountbyDrName.add(depositoryRecordByDate1); drCountbyDrName.add(depositoryRecordByDate);
} }
map.put("data", drCountbyDrName); map.put("data", drCountbyDrName);
result.put(val,map); return map;
return result;
}
} }
} }

Loading…
Cancel
Save